fix: client update set win when is not rechard close time

This commit is contained in:
Admin 2025-06-23 08:46:21 +07:00
parent 5af2f6e548
commit 52167a360b
3 changed files with 80 additions and 21 deletions

View File

@ -8,7 +8,6 @@ import {
import { Timestamp } from './timestamp'; import { Timestamp } from './timestamp';
import { Bid } from './bid.entity'; import { Bid } from './bid.entity';
import { Exclude } from 'class-transformer'; import { Exclude } from 'class-transformer';
import { ScrapItem } from '@/modules/scraps/entities/scrap-item.entity';
import { ScrapConfig } from '@/modules/scraps/entities/scrap-config.entity'; import { ScrapConfig } from '@/modules/scraps/entities/scrap-config.entity';
@Entity('web_bids') @Entity('web_bids')

View File

@ -224,6 +224,18 @@ export class BidsService {
return AppResponse.toResponse(true); 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']) { async toggle(id: Bid['id']) {
const bid = await this.bidsRepo.findOne({ const bid = await this.bidsRepo.findOne({
where: { id }, where: { id },
@ -278,20 +290,49 @@ export class BidsService {
return AppResponse.toResponse(true); return AppResponse.toResponse(true);
} }
/**
* Workflow
* START
* |
* |--> Tìm bid theo id --> Không ? --> Throw 404
* |
* |--> Nếu chưa 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ập nhật
* |--> Nếu model mới chưa model --> gán
* |
* |--> Gọi `save(...)` đ lưu lại DB
* |--> Nếu metadata --> gọi `upsert`
* |
* |--> Gửi sự kiện emitAllBidEvent
* |--> Nếu status out-bid hoặc win-bid --> gửi notification
* |
* * |--> Trả response
* END
*/
async clientUpdate( async clientUpdate(
id: Bid['id'], id: Bid['id'],
{ close_time, model, metadata, ...data }: ClientUpdateBidDto, // Nhận dữ liệu cập nhật { 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 // // Tìm kiếm phiên đấu giá trong database theo id
const bid = await this.bidsRepo.findOne({ // let bid = await this.bidsRepo.findOne({
where: { id }, // where: { id },
relations: { histories: true, web_bid: true, metadata: true }, // relations: { histories: true, web_bid: true, metadata: true },
order: { // order: {
histories: { // histories: {
price: 'DESC', // 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 // Nếu không tìm thấy phiên đấu giá, trả về lỗi 404
if (!bid) 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) // 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)) { 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) 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ũ // // 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 ( // if (
close_time && // close_time &&
new Date(close_time).getTime() > new Date(bid.close_time).getTime() // new Date(close_time).getTime() > new Date(bid.close_time).getTime()
) { // ) {
bid.close_time = close_time; // bid.close_time = close_time;
bid.close_time_ts = new Date(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 // 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) { if (model && !bid.model) {

View File

@ -1,9 +1,18 @@
{ {
"moduleFileExtensions": ["js", "json", "ts"], "moduleFileExtensions": ["ts", "js", "json"],
"rootDir": ".", "rootDir": ".",
"testEnvironment": "node", "testEnvironment": "node",
"testRegex": ".e2e-spec.ts$", "preset": "ts-jest",
"transform": { "transform": {
"^.+\\.(t|j)s$": "ts-jest" "^.+\\.(t|j)s$": "ts-jest"
},
"testMatch": ["**/?(*.)+(spec|test).ts"],
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1"
},
"globals": {
"ts-jest": {
"tsconfig": "<rootDir>/tsconfig.json"
}
} }
} }