Deploy to staging #75
			
				
			
		
		
		
	| 
						 | 
				
			
			@ -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')
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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": {
 | 
			
		||||
    "^@/(.*)$": "<rootDir>/src/$1"
 | 
			
		||||
  },
 | 
			
		||||
  "globals": {
 | 
			
		||||
    "ts-jest": {
 | 
			
		||||
      "tsconfig": "<rootDir>/tsconfig.json"
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue