From 52167a360bd5f4ec54b5af0c57cfd7eeca98b002 Mon Sep 17 00:00:00 2001 From: Admin Date: Mon, 23 Jun 2025 08:46:21 +0700 Subject: [PATCH] fix: client update set win when is not rechard close time --- .../modules/bids/entities/wed-bid.entity.ts | 1 - .../src/modules/bids/services/bids.service.ts | 87 +++++++++++++++---- auto-bid-server/test/jest-e2e.json | 13 ++- 3 files changed, 80 insertions(+), 21 deletions(-) diff --git a/auto-bid-server/src/modules/bids/entities/wed-bid.entity.ts b/auto-bid-server/src/modules/bids/entities/wed-bid.entity.ts index 26a4fbd..1827a87 100644 --- a/auto-bid-server/src/modules/bids/entities/wed-bid.entity.ts +++ b/auto-bid-server/src/modules/bids/entities/wed-bid.entity.ts @@ -8,7 +8,6 @@ import { import { Timestamp } from './timestamp'; import { Bid } from './bid.entity'; import { Exclude } from 'class-transformer'; -import { ScrapItem } from '@/modules/scraps/entities/scrap-item.entity'; import { ScrapConfig } from '@/modules/scraps/entities/scrap-config.entity'; @Entity('web_bids') diff --git a/auto-bid-server/src/modules/bids/services/bids.service.ts b/auto-bid-server/src/modules/bids/services/bids.service.ts index 66ed16a..662ad68 100644 --- a/auto-bid-server/src/modules/bids/services/bids.service.ts +++ b/auto-bid-server/src/modules/bids/services/bids.service.ts @@ -224,6 +224,18 @@ export class BidsService { return AppResponse.toResponse(true); } + async getBidForClientUpdate(id: Bid['id']) { + return await this.bidsRepo.findOne({ + where: { id }, + relations: { histories: true, web_bid: true, metadata: true }, + order: { + histories: { + price: 'DESC', + }, + }, + }); + } + async toggle(id: Bid['id']) { const bid = await this.bidsRepo.findOne({ where: { id }, @@ -278,20 +290,49 @@ export class BidsService { return AppResponse.toResponse(true); } + /** + * Workflow + * START + * | + * |--> Tìm bid theo id --> Không có? --> Throw 404 + * | + * |--> Nếu chưa có thời gian bắt đầu --> Tính offset --> Gán start_time + * | + * |--> Nếu đã hết giờ: + * | |--> Nếu outbid --> Gán status 'out-bid' + * | |--> Else --> Gán status 'win-bid' + * | + * |--> Nếu chưa hết giờ: + * | |--> Nếu vượt giới hạn --> Gán status 'out-bid' + * | + * |--> Nếu close_time mới > cũ --> cập nhật + * |--> Nếu có model mới và chưa có model --> gán + * | + * |--> Gọi `save(...)` để lưu lại DB + * |--> Nếu có metadata --> gọi `upsert` + * | + * |--> Gửi sự kiện emitAllBidEvent + * |--> Nếu status là out-bid hoặc win-bid --> gửi notification + * | + * * |--> Trả response + * END + */ async clientUpdate( id: Bid['id'], { close_time, model, metadata, ...data }: ClientUpdateBidDto, // Nhận dữ liệu cập nhật ) { - // Tìm kiếm phiên đấu giá trong database theo id - const bid = await this.bidsRepo.findOne({ - where: { id }, - relations: { histories: true, web_bid: true, metadata: true }, - order: { - histories: { - price: 'DESC', - }, - }, - }); + // // Tìm kiếm phiên đấu giá trong database theo id + // let bid = await this.bidsRepo.findOne({ + // where: { id }, + // relations: { histories: true, web_bid: true, metadata: true }, + // order: { + // histories: { + // price: 'DESC', + // }, + // }, + // }); + + let bid = await this.getBidForClientUpdate(id); // Nếu không tìm thấy phiên đấu giá, trả về lỗi 404 if (!bid) @@ -317,6 +358,16 @@ export class BidsService { ); } + // Cập nhật thời gian kết thúc đấu giá nếu `close_time` mới lớn hơn `close_time` cũ + if ( + close_time && + new Date(close_time).getTime() > new Date(bid.close_time).getTime() + ) { + bid.close_time = close_time; + bid.close_time_ts = new Date(close_time); + bid = await this.getBidForClientUpdate(id); + } + // Kiểm tra nếu thời gian đóng bid đã đạt tới (tức phiên đấu giá đã kết thúc) if (bid.close_time && isTimeReached(bid.close_time)) { const bidHistoriesItem = bid.histories[0]; // Lấy lịch sử bid gần nhất (mới nhất) @@ -343,14 +394,14 @@ export class BidsService { } } - // Cập nhật thời gian kết thúc đấu giá nếu `close_time` mới lớn hơn `close_time` cũ - if ( - close_time && - new Date(close_time).getTime() > new Date(bid.close_time).getTime() - ) { - bid.close_time = close_time; - bid.close_time_ts = new Date(close_time); - } + // // Cập nhật thời gian kết thúc đấu giá nếu `close_time` mới lớn hơn `close_time` cũ + // if ( + // close_time && + // new Date(close_time).getTime() > new Date(bid.close_time).getTime() + // ) { + // bid.close_time = close_time; + // bid.close_time_ts = new Date(close_time); + // } // Nếu chưa có `model` nhưng dữ liệu mới có model, thì cập nhật model if (model && !bid.model) { diff --git a/auto-bid-server/test/jest-e2e.json b/auto-bid-server/test/jest-e2e.json index e9d912f..baaae17 100644 --- a/auto-bid-server/test/jest-e2e.json +++ b/auto-bid-server/test/jest-e2e.json @@ -1,9 +1,18 @@ { - "moduleFileExtensions": ["js", "json", "ts"], + "moduleFileExtensions": ["ts", "js", "json"], "rootDir": ".", "testEnvironment": "node", - "testRegex": ".e2e-spec.ts$", + "preset": "ts-jest", "transform": { "^.+\\.(t|j)s$": "ts-jest" + }, + "testMatch": ["**/?(*.)+(spec|test).ts"], + "moduleNameMapper": { + "^@/(.*)$": "/src/$1" + }, + "globals": { + "ts-jest": { + "tsconfig": "/tsconfig.json" + } } }