From 3fc57e772db1486d03cd883a4bdf44cc664f75fb Mon Sep 17 00:00:00 2001 From: Jannik Zinkl Date: Mon, 7 Oct 2024 18:14:06 +0200 Subject: [PATCH] fix: handling connect or create logic for existing pricelist entries --- pkg/integration-kencove-api/src/pricelists.ts | 72 +++++++++++-------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/pkg/integration-kencove-api/src/pricelists.ts b/pkg/integration-kencove-api/src/pricelists.ts index c150d7c8..d4154bd1 100644 --- a/pkg/integration-kencove-api/src/pricelists.ts +++ b/pkg/integration-kencove-api/src/pricelists.ts @@ -339,6 +339,16 @@ export class KencoveApiAppPricelistSyncService { endDate: endDate ?? (undefined as any), }, }); + this.logger.info( + `Creating new price entry for SKU ${productVariant.sku} and ` + + `${pricelistEntry.pricelist_name}.`, + { + productVariantId: productVariant.id, + salesChannelId: salesChannel.id, + pricelistEntryId: + pricelistEntry.pricelist_item_id, + }, + ); await this.db.kencoveApiPricelistItem.create({ data: { id: pricelistEntry.pricelist_item_id.toString(), @@ -350,35 +360,39 @@ export class KencoveApiAppPricelistSyncService { }, }, salesChannelPriceEntry: { - connectOrCreate: { - where: { - id: existingSalesChannelPriceEntry?.id, - }, - create: { - id: id.id("salesChannelPriceEntry"), - tenant: { - connect: { - id: this.kencoveApiApp - .tenantId, - }, - }, - salesChannel: { - connect: { - id: salesChannel.id, - }, - }, - productVariant: { - connect: { - id: productVariant.id, - }, - }, - startDate, - endDate, - price: pricelistEntry.price, - minQuantity: - pricelistEntry.min_quantity, - }, - }, + connect: existingSalesChannelPriceEntry?.id + ? { + id: existingSalesChannelPriceEntry.id, + } + : undefined, + create: !existingSalesChannelPriceEntry?.id + ? { + id: id.id( + "salesChannelPriceEntry", + ), + tenant: { + connect: { + id: this.kencoveApiApp + .tenantId, + }, + }, + salesChannel: { + connect: { + id: salesChannel.id, + }, + }, + productVariant: { + connect: { + id: productVariant.id, + }, + }, + startDate, + endDate, + price: pricelistEntry.price, + minQuantity: + pricelistEntry.min_quantity, + } + : undefined, }, }, });