76 lines
2.5 KiB
TypeScript
76 lines
2.5 KiB
TypeScript
import { inject, injectable } from "inversify";
|
|
|
|
import { CreateWarrantyDTO } from "./DTO/createWarranty.dto";
|
|
import { UpdateWarrantyDTO } from "./DTO/UpdateWarranty.dto";
|
|
import { WarrantyRepository } from "./warranty.repository";
|
|
import { CommonMessage, WarrantyMessage } from "../../common/enums/message.enum";
|
|
import { StatusEnum } from "../../common/enums/status.enum";
|
|
import { BadRequestError } from "../../core/app/app.errors";
|
|
import { IOCTYPES } from "../../IOC/ioc.types";
|
|
|
|
@injectable()
|
|
class WarrantyService {
|
|
@inject(IOCTYPES.WarrantyRepository) warrantyRepo: WarrantyRepository;
|
|
|
|
async createWarrantyS(createDto: CreateWarrantyDTO) {
|
|
const existWarr = await this.warrantyRepo.model.exists({ name: createDto.name });
|
|
if (existWarr) throw new BadRequestError(WarrantyMessage.DuplicateName);
|
|
const newWarranty = await this.warrantyRepo.model.create(createDto);
|
|
return {
|
|
message: WarrantyMessage.Created,
|
|
newWarranty,
|
|
};
|
|
}
|
|
|
|
async getAllS(queries: { limit: number; page: number; status: string }) {
|
|
const { count, docs } = await this.warrantyRepo.findAllWarranties(queries);
|
|
|
|
return { warranties: docs, count };
|
|
}
|
|
|
|
async deleteById(id: number) {
|
|
const deletedWarranty = await this.warrantyRepo.model.findByIdAndUpdate(id, { deleted: true }, { new: true });
|
|
if (!deletedWarranty) throw new BadRequestError(CommonMessage.NotValidId);
|
|
|
|
return {
|
|
message: CommonMessage.Deleted,
|
|
deletedWarranty,
|
|
};
|
|
}
|
|
|
|
async updateById(id: number, updateDto: UpdateWarrantyDTO) {
|
|
const updatedWarranty = await this.warrantyRepo.model.findByIdAndUpdate(id, updateDto, { new: true });
|
|
if (!updatedWarranty) throw new BadRequestError(CommonMessage.NotValidId);
|
|
|
|
return {
|
|
message: CommonMessage.Updated,
|
|
updatedWarranty,
|
|
};
|
|
}
|
|
|
|
async approve(id: number) {
|
|
const updatedWarranty = await this.warrantyRepo.model.findByIdAndUpdate(id, { status: StatusEnum.Approved }, { new: true });
|
|
if (!updatedWarranty) throw new BadRequestError(CommonMessage.NotValidId);
|
|
|
|
return {
|
|
message: CommonMessage.Updated,
|
|
updatedWarranty,
|
|
};
|
|
}
|
|
|
|
async getPendingWarranties() {
|
|
return await this.warrantyRepo.pendingWarranties();
|
|
}
|
|
|
|
async getWarranty(id: string) {
|
|
const warranty = await this.warrantyRepo.model.findOne({ _id: id, deleted: false });
|
|
if (!warranty) throw new BadRequestError(CommonMessage.NotValidId);
|
|
|
|
return {
|
|
warranty,
|
|
};
|
|
}
|
|
}
|
|
|
|
export { WarrantyService };
|