import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository, LessThan } from 'typeorm'; import { BidItem } from '../entities/bid-item.entity'; @Injectable() export class BidsService { constructor( @InjectRepository(BidItem) private bidRepository: Repository, ) {} async findAll(query?: any) { const { page = 1, limit = 10, source, keyword } = query || {}; const qb = this.bidRepository.createQueryBuilder('bid'); if (source) { qb.andWhere('bid.source = :source', { source }); } if (keyword) { qb.andWhere('bid.title LIKE :keyword', { keyword: `%${keyword}%` }); } qb.orderBy('bid.publishDate', 'DESC') .skip((page - 1) * limit) .take(limit); const [items, total] = await qb.getManyAndCount(); return { items, total }; } getHighPriority() { return this.bidRepository.find({ where: { priority: LessThan(0) }, // This is just a placeholder logic, priority should be > 0 order: { priority: 'DESC', publishDate: 'DESC' }, take: 10, }); } // Update logic for priority async getHighPriorityCorrected() { return this.bidRepository.createQueryBuilder('bid') .where('bid.priority > 0') .orderBy('bid.priority', 'DESC') .addOrderBy('bid.publishDate', 'DESC') .limit(10) .getMany(); } async createOrUpdate(data: Partial) { // Use URL or a hash of URL to check for duplicates let item = await this.bidRepository.findOne({ where: { url: data.url } }); if (item) { Object.assign(item, data); return this.bidRepository.save(item); } return this.bidRepository.save(data); } async cleanOldData() { const thirtyDaysAgo = new Date(); thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30); return this.bidRepository.delete({ createdAt: LessThan(thirtyDaysAgo), }); } }