import { omit } from 'lodash' ; import { LoggerPort } from '@core/domain/common/port' ; import ResponseRepositoryPort from '@core/domain/response/port/ResponseRepositoryPort' ; import Response from '@core/domain/response/Response' ; import ResponseModel from './ResponseModel' ; class MongoDbResponseRepositoryAdapter implements ResponseRepositoryPort { constructor ( private readonly logger ) { this . logger = logger ; } public async save ( response ) { const mongoResponse = new ResponseModel ( { _id : response . requestId , fetchedData : response . fetchedData , selectedData : response . selectedData , state : response . state . name , } ) ; const upsertDocument = omit ( mongoResponse . toObject ( ) , [ '_id' ] ) ; return ResponseModel . findOneAndUpdate ( { _id : mongoResponse . _id } , upsertDocument , { upsert : true , new : true } ) . then ( result => this . logger . info ( `Response saved into database [response=${ JSON . stringify ( result ) }]` ) ) ; } public async get ( responseId ) { const responseModel = await ResponseModel . findById ( responseId ) ; const response = new Response ( responseModel . _id ) ; response . addFetchedData ( response . fetchedData ) ; response . addSelectedData ( response . selectedData ) ; return response ; } } export default MongoDbResponseRepositoryAdapter ;