From 57da2605b46b8f2f2e56a17701546be69247d84b Mon Sep 17 00:00:00 2001 From: dhzdhd Date: Fri, 3 Nov 2023 19:43:48 +0530 Subject: [PATCH] Tweak makeBid to upsert instead of create --- backend/src/api/bid/services/bid.ts | 34 +++++++++++++++------ backend/src/api/product/services/product.ts | 1 + 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/backend/src/api/bid/services/bid.ts b/backend/src/api/bid/services/bid.ts index d82c415..fda75e4 100644 --- a/backend/src/api/bid/services/bid.ts +++ b/backend/src/api/bid/services/bid.ts @@ -5,15 +5,31 @@ import { factories } from "@strapi/strapi"; export default factories.createCoreService("api::bid.bid", ({ strapi }) => ({ - makeBid(params) { - return strapi.service("api::bid.bid").create({ - data: { - value: params.bidValue, - user: params.user, - product: params.product, - publishedAt: new Date(), - }, - }); + async makeBid(params) { + const currentBids: any[] = await strapi.entityService.findMany( + "api::bid.bid", + { + filters: { product: params.product }, + } + ); + + if (currentBids.length === 0) { + return strapi.service("api::bid.bid").create({ + data: { + value: params.bidValue, + user: params.user, + product: params.product, + publishedAt: new Date(), + }, + }); + } else { + return strapi.entityService.update("api::bid.bid", currentBids[0].id, { + data: { + value: params.bidValue, + user: params.user, + }, + }); + } }, getWinner(params) { diff --git a/backend/src/api/product/services/product.ts b/backend/src/api/product/services/product.ts index b88d837..2dbbf1a 100644 --- a/backend/src/api/product/services/product.ts +++ b/backend/src/api/product/services/product.ts @@ -30,6 +30,7 @@ export default factories.createCoreService( }, }); }, + async findAndUpdateBidPrice(found, price) { return strapi.entityService.update("api::product.product", found.id, { data: {