feat: Add support plans module with entities and repositories
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
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 userSupportPlanRepo: UserSupportPlanRepository,
|
||||
private readonly supportPlanFeatureRepo: SupportPlanFeatureRepository,
|
||||
private readonly dataSource: DataSource,
|
||||
) {}
|
||||
|
||||
async createSupportPlan(createSupportPlanDto: CreateSupportPlanDto) {
|
||||
@@ -50,13 +53,41 @@ export class SupportPlansService {
|
||||
}
|
||||
//***************************************** */
|
||||
async updateSupportPlanById(id: string, updateSupportPlanDto: UpdateSupportPlanDto) {
|
||||
const supportPlan = await this.supportPlanRepo.getSupportPlanById(id);
|
||||
if (!supportPlan) throw new BadRequestException(SupportPlanMessage.SUPPORT_PLAN_NOT_FOUND);
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
|
||||
await this.supportPlanRepo.save({ ...supportPlan, ...updateSupportPlanDto });
|
||||
try {
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
return {
|
||||
message: SupportPlanMessage.SUPPORT_PLAN_UPDATED,
|
||||
};
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user