42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import Promotion from './promotions.entity';
|
|
import { HttpService } from '@nestjs/axios';
|
|
import { Utility } from 'src/common/Utility';
|
|
|
|
@Injectable()
|
|
export class PromotionService {
|
|
constructor(private readonly httpService: HttpService) { }
|
|
|
|
async findAll(): Promise<{ rows: Promotion[], count: number }> {
|
|
return Promotion.findAndCountAll();
|
|
}
|
|
|
|
async findByPk(id: number): Promise<Promotion> {
|
|
return Promotion.findByPk(id);
|
|
}
|
|
|
|
findOne(promotion: Promotion): Promise<Promotion> {
|
|
return Promotion.findOne({ where: promotion as any });
|
|
}
|
|
|
|
filter(promotion: Promotion): Promise<Promotion[]> {
|
|
return Promotion.findAll({ where: promotion as any });
|
|
}
|
|
|
|
async remove(id: number): Promise<number> {
|
|
return Promotion.destroy({ where: { id: id } });
|
|
}
|
|
|
|
async upsert(promotion: any, insertIfNotFound: boolean): Promise<Promotion | [affectedCount: number]> {
|
|
if (promotion.id) {
|
|
const existingPromotion = await this.findByPk(promotion.id);
|
|
if (existingPromotion) {
|
|
return Promotion.update(promotion, { where: { id: promotion.id } });
|
|
}
|
|
}
|
|
if (insertIfNotFound) {
|
|
return Promotion.create(promotion as any);
|
|
}
|
|
}
|
|
}
|