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: {