66 lines
2.3 KiB
TypeScript
66 lines
2.3 KiB
TypeScript
import { EntityManager } from "@mikro-orm/postgresql";
|
|
import { BadRequestException, Injectable } from "@nestjs/common";
|
|
|
|
import { BillListQueryDto } from "./dto/bill-list-query.dto";
|
|
import { CreateBarnameDto } from "./dto/create-barname.dto";
|
|
import { Barname } from "./entities/barname.entity";
|
|
import { BarnameRepository } from "./repositories/barname.repository";
|
|
import { CompanyMessage } from "../../common/enums/message.enum";
|
|
import { Business } from "../businesses/entities/business.entity";
|
|
import { Company } from "../companies/entities/company.entity";
|
|
// import { CompaniesService } from "../companies/services/companies.service";
|
|
|
|
|
|
@Injectable()
|
|
export class BarnameService {
|
|
readonly SEWAGE_RATE = 0.3;
|
|
readonly TAX_RATE = 0.1;
|
|
|
|
constructor(
|
|
private readonly em: EntityManager,
|
|
// private readonly companiesService: CompaniesService,
|
|
private readonly billRepository: BarnameRepository,
|
|
) { }
|
|
|
|
async createBarname(dto: CreateBarnameDto, business: Business, userId: string) {
|
|
const { description, driverName, driverPhone, carType, exitAt, origin, plaque, weight,attachments } = dto;
|
|
|
|
const company = await this.em.findOne(Company, { business, user: { id: userId }, deletedAt: null });
|
|
if (!company) throw new BadRequestException(CompanyMessage.COMPANY_NOT_FOUND);
|
|
|
|
const bill = this.em.create(Barname,
|
|
{ carType, exitAt, origin, plaque, weight, business, description, driverName, driverPhone, company,attachments });
|
|
|
|
await this.em.persistAndFlush(bill);
|
|
|
|
return bill
|
|
}
|
|
|
|
|
|
async findAll(queryDto: BillListQueryDto, businessId: string) {
|
|
const [bills, count] = await this.billRepository.getBillsList(queryDto, businessId);
|
|
return {
|
|
bills,
|
|
count,
|
|
paginate: true,
|
|
};
|
|
}
|
|
|
|
async findOne(id: string, businessId: string) {
|
|
const bill = await this.billRepository.findOne({ id, business: { id: businessId } }, { populate: [] });
|
|
if (!bill) {
|
|
throw new BadRequestException("Bill not found");
|
|
}
|
|
return { bill };
|
|
}
|
|
|
|
async remove(id: string, businessId: string) {
|
|
const bill = await this.billRepository.findOne({ id, business: { id: businessId } }, { populate: [] });
|
|
if (!bill) {
|
|
throw new BadRequestException("Bill not found");
|
|
}
|
|
await this.em.removeAndFlush(bill);
|
|
return { deleted: true };
|
|
}
|
|
}
|