first
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
export { WarrantyService };
|
||||
Reference in New Issue
Block a user