/** * SyncService file */ import { IDynamoDbEvent, IDynamoRecord, KushkiError } from "@kushki/core"; import { IDENTIFIERS } from "constant/Identifiers"; import { TABLE_RESOURCES } from "constant/Resources"; import { ERRORS } from "infrastructure/ErrorEnum"; import { inject, injectable } from "inversify"; import "reflect-metadata"; import { IDynamoGateway } from "repository/IDynamoGateway"; import { ISyncService } from "repository/ISyncService"; import { from, iif, Observable, of } from "rxjs"; import { tag } from "rxjs-spy/operators"; import { map, mergeMap, switchMap } from "rxjs/operators"; import tsLogClass from "ts-log-class"; import { MerchantFetch } from "types/remote/merchant_fetch"; import { ProcessorFetch } from "types/remote/processor_fetch"; /** * Implementation */ @injectable() @tsLogClass() export class SyncService implements ISyncService { public static readonly sPseProcessor: string = "Pse Processor"; private readonly _storage: IDynamoGateway; constructor(@inject(IDENTIFIERS.DynamoGateway) storage: IDynamoGateway) { this._storage = storage; } public syncProcessors( event: IDynamoDbEvent ): Observable { return from(event.Records).pipe( mergeMap((record: IDynamoRecord) => iif( () => record.dynamodb.NewImage.StoreInformation.CreditInfo .processorName === SyncService.sPseProcessor, this._storage .getItem(TABLE_RESOURCES.UsrvMerchantsTable, { publicMerchantId: record.dynamodb.NewImage.merchantId, }) .pipe( map((merchant: object | undefined) => { if (merchant === undefined) throw new KushkiError(ERRORS.E007); return merchant; }), switchMap((currentMerchant: MerchantFetch) => this._storage.put( { publicMerchantId: record.dynamodb.NewImage.merchantId, privateMerchantId: currentMerchant.privateMerchantId, // TODO this is the processor privateMid [@pmoreanoj] processorPublicMerchantId: record.dynamodb.NewImage.publicMerchantId, entityCode: record.dynamodb.NewImage.StoreInformation.CreditInfo .entityCode, serviceCode: record.dynamodb.NewImage.StoreInformation.CreditInfo .serviceCode, webhookSignature: currentMerchant.privateMerchantId, }, TABLE_RESOURCES.MerchantsTable ) ) ), of(true) ) ), tag("Sync Service | syncProcessors") ); } }