61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { EventEmitter2 } from '@nestjs/event-emitter';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { ScrapConfig } from '../entities/scrap-config.entity';
|
|
import AppResponse from '@/response/app-response';
|
|
import { CreateScrapConfigDto } from '../dto/scrap-config/create-scrap-config';
|
|
import { UpdateScrapConfigDto } from '../dto/scrap-config/update-scrap-config';
|
|
import axios from 'axios';
|
|
import { WebBid } from '@/modules/bids/entities/wed-bid.entity';
|
|
import { GraysScrapModel } from '../models/https:/www.grays.com/grays-scrap-model';
|
|
|
|
@Injectable()
|
|
export class ScrapConfigsService {
|
|
constructor(
|
|
@InjectRepository(ScrapConfig)
|
|
readonly scrapConfigRepo: Repository<ScrapConfig>,
|
|
) {}
|
|
|
|
async create(data: CreateScrapConfigDto) {
|
|
const result = await this.scrapConfigRepo.save({
|
|
search_url: data.search_url,
|
|
keywords: data.keywords,
|
|
web_bid: { id: data.web_id },
|
|
});
|
|
|
|
if (!result) return AppResponse.toResponse(false);
|
|
|
|
return AppResponse.toResponse(true);
|
|
}
|
|
|
|
async update(
|
|
id: ScrapConfig['id'],
|
|
{ web_id, ...data }: UpdateScrapConfigDto,
|
|
) {
|
|
const result = await this.scrapConfigRepo.update(id, { ...data });
|
|
|
|
if (!result.affected) return AppResponse.toResponse(false);
|
|
|
|
return AppResponse.toResponse(true);
|
|
}
|
|
|
|
scrapModel(scrapConfig: ScrapConfig) {
|
|
switch (scrapConfig.web_bid.origin_url) {
|
|
case 'https://www.grays.com': {
|
|
return new GraysScrapModel({
|
|
...scrapConfig,
|
|
scrap_config_id: scrapConfig.id,
|
|
});
|
|
}
|
|
default: {
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
scrapModels(data: ScrapConfig[]) {
|
|
return data.map((item) => this.scrapModel(item)).filter((item) => !!item);
|
|
}
|
|
}
|