94 lines
3.6 KiB
TypeScript
94 lines
3.6 KiB
TypeScript
import { BadRequestException, Injectable } from "@nestjs/common";
|
|
import { DataSource } from "typeorm";
|
|
|
|
import { SupportPlanMessage } from "../../../common/enums/message.enum";
|
|
import { CreateSupportPlanDto } from "../DTO/create-support-plan.dto";
|
|
import { GetSupportPlanListQueryDto } from "../DTO/get-support-plan-list-query.dto";
|
|
import { UpdateSupportPlanDto } from "../DTO/update-support-plan-feature.dto";
|
|
import { SupportPlanFeatureRepository } from "../repositories/support-plan-feature.repository";
|
|
import { SupportPlanRepository } from "../repositories/support-plan.repository";
|
|
@Injectable()
|
|
export class SupportPlansService {
|
|
constructor(
|
|
private readonly supportPlanRepo: SupportPlanRepository,
|
|
private readonly supportPlanFeatureRepo: SupportPlanFeatureRepository,
|
|
private readonly dataSource: DataSource,
|
|
) {}
|
|
|
|
async createSupportPlan(createSupportPlanDto: CreateSupportPlanDto) {
|
|
const supportPlan = this.supportPlanRepo.create(createSupportPlanDto);
|
|
await this.supportPlanRepo.save(supportPlan);
|
|
return {
|
|
message: SupportPlanMessage.SUPPORT_PLAN_CREATED,
|
|
supportPlan,
|
|
};
|
|
}
|
|
//***************************************** */
|
|
async getSupportPlansList(queryDto: GetSupportPlanListQueryDto) {
|
|
const [supportPlans, count] = await this.supportPlanRepo.getSupportPlansListAdmin(queryDto);
|
|
return {
|
|
supportPlans,
|
|
count,
|
|
paginate: true,
|
|
};
|
|
}
|
|
//***************************************** */
|
|
async getSupportPlanById(id: string) {
|
|
const supportPlan = await this.supportPlanRepo.getSupportPlanById(id);
|
|
if (!supportPlan) throw new BadRequestException(SupportPlanMessage.SUPPORT_PLAN_NOT_FOUND);
|
|
|
|
return {
|
|
supportPlan,
|
|
};
|
|
}
|
|
//***************************************** */
|
|
async deleteSupportPlanById(id: string) {
|
|
const supportPlan = await this.supportPlanRepo.getSupportPlanById(id);
|
|
if (!supportPlan) throw new BadRequestException(SupportPlanMessage.SUPPORT_PLAN_NOT_FOUND);
|
|
await this.supportPlanRepo.softDelete(id);
|
|
|
|
return {
|
|
message: SupportPlanMessage.SUPPORT_PLAN_DELETED,
|
|
};
|
|
}
|
|
//***************************************** */
|
|
async updateSupportPlanById(id: string, updateSupportPlanDto: UpdateSupportPlanDto) {
|
|
const queryRunner = this.dataSource.createQueryRunner();
|
|
|
|
try {
|
|
await queryRunner.connect();
|
|
await queryRunner.startTransaction();
|
|
|
|
const supportPlan = await queryRunner.manager.findOne(this.supportPlanRepo.target, { where: { id }, relations: { features: true } });
|
|
if (!supportPlan) throw new BadRequestException(SupportPlanMessage.SUPPORT_PLAN_NOT_FOUND);
|
|
|
|
// If updating features:
|
|
if (updateSupportPlanDto.features) {
|
|
// Remove old features (optional, if replacing)
|
|
await queryRunner.manager.delete(this.supportPlanFeatureRepo.target, { supportPlan: { id } });
|
|
|
|
// Add new features, making sure to set the relation
|
|
const features = updateSupportPlanDto.features.map((f) => ({
|
|
...f,
|
|
supportPlan, // or supportPlan: { id }
|
|
}));
|
|
await queryRunner.manager.save(this.supportPlanFeatureRepo.target, features);
|
|
}
|
|
|
|
// Update the plan itself
|
|
await queryRunner.manager.save(this.supportPlanRepo.target, { ...supportPlan, ...updateSupportPlanDto, features: undefined });
|
|
|
|
await queryRunner.commitTransaction();
|
|
|
|
return {
|
|
message: SupportPlanMessage.SUPPORT_PLAN_UPDATED,
|
|
};
|
|
} catch (error) {
|
|
await queryRunner.rollbackTransaction();
|
|
throw error;
|
|
} finally {
|
|
await queryRunner.release();
|
|
}
|
|
}
|
|
}
|