feat: Add support plans module with entities and repositories

This commit is contained in:
mahyargdz
2025-05-05 15:18:37 +03:30
parent 61513d5059
commit 47c9cd47e6
5 changed files with 61 additions and 17 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ export function databaseConfigs(): TypeOrmModuleAsyncOptions {
username: configService.getOrThrow<string>("DB_USER"), username: configService.getOrThrow<string>("DB_USER"),
password: configService.getOrThrow<string>("DB_PASS"), password: configService.getOrThrow<string>("DB_PASS"),
autoLoadEntities: true, autoLoadEntities: true,
synchronize: configService.getOrThrow<string>("NODE_ENV") == "production" ? false : false, synchronize: configService.getOrThrow<string>("NODE_ENV") == "production" ? false : true,
logging: configService.getOrThrow<string>("NODE_ENV") == "production" ? false : true, logging: configService.getOrThrow<string>("NODE_ENV") == "production" ? false : true,
migrationsTableName: "typeorm_migrations", migrationsTableName: "typeorm_migrations",
migrationsRun: false, migrationsRun: false,
@@ -1,15 +1,18 @@
import { BadRequestException, Injectable } from "@nestjs/common"; import { BadRequestException, Injectable } from "@nestjs/common";
import { DataSource } from "typeorm";
import { SupportPlanMessage } from "../../../common/enums/message.enum"; import { SupportPlanMessage } from "../../../common/enums/message.enum";
import { CreateSupportPlanDto } from "../DTO/create-support-plan.dto"; import { CreateSupportPlanDto } from "../DTO/create-support-plan.dto";
import { GetSupportPlanListQueryDto } from "../DTO/get-support-plan-list-query.dto"; import { GetSupportPlanListQueryDto } from "../DTO/get-support-plan-list-query.dto";
import { UpdateSupportPlanDto } from "../DTO/update-support-plan-feature.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"; import { SupportPlanRepository } from "../repositories/support-plan.repository";
@Injectable() @Injectable()
export class SupportPlansService { export class SupportPlansService {
constructor( constructor(
private readonly supportPlanRepo: SupportPlanRepository, private readonly supportPlanRepo: SupportPlanRepository,
// private readonly userSupportPlanRepo: UserSupportPlanRepository, private readonly supportPlanFeatureRepo: SupportPlanFeatureRepository,
private readonly dataSource: DataSource,
) {} ) {}
async createSupportPlan(createSupportPlanDto: CreateSupportPlanDto) { async createSupportPlan(createSupportPlanDto: CreateSupportPlanDto) {
@@ -50,13 +53,41 @@ export class SupportPlansService {
} }
//***************************************** */ //***************************************** */
async updateSupportPlanById(id: string, updateSupportPlanDto: UpdateSupportPlanDto) { async updateSupportPlanById(id: string, updateSupportPlanDto: UpdateSupportPlanDto) {
const supportPlan = await this.supportPlanRepo.getSupportPlanById(id); 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 (!supportPlan) throw new BadRequestException(SupportPlanMessage.SUPPORT_PLAN_NOT_FOUND);
await this.supportPlanRepo.save({ ...supportPlan, ...updateSupportPlanDto }); // 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 { return {
message: SupportPlanMessage.SUPPORT_PLAN_UPDATED, message: SupportPlanMessage.SUPPORT_PLAN_UPDATED,
}; };
} catch (error) {
await queryRunner.rollbackTransaction();
throw error;
} finally {
await queryRunner.release();
}
} }
} }
@@ -0,0 +1,12 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { SupportPlanFeature } from "../entities/support-plan-feature.entity";
@Injectable()
export class SupportPlanFeatureRepository extends Repository<SupportPlanFeature> {
constructor(@InjectRepository(SupportPlanFeature) repository: Repository<SupportPlanFeature>) {
super(repository.target, repository.manager, repository.queryRunner);
}
}
@@ -1,8 +1,9 @@
import { Body, Controller, Delete, Get, Param, Post, Query } from "@nestjs/common"; import { Body, Controller, Delete, Get, Param, Patch, Post, Query } from "@nestjs/common";
import { ApiOperation } from "@nestjs/swagger"; import { ApiOperation } from "@nestjs/swagger";
import { CreateSupportPlanDto } from "./DTO/create-support-plan.dto"; import { CreateSupportPlanDto } from "./DTO/create-support-plan.dto";
import { GetSupportPlanListQueryDto } from "./DTO/get-support-plan-list-query.dto"; import { GetSupportPlanListQueryDto } from "./DTO/get-support-plan-list-query.dto";
import { UpdateSupportPlanDto } from "./DTO/update-support-plan-feature.dto";
import { SupportPlansService } from "./providers/support-plans.service"; import { SupportPlansService } from "./providers/support-plans.service";
import { AdminRoute } from "../../common/decorators/admin.decorator"; import { AdminRoute } from "../../common/decorators/admin.decorator";
import { AuthGuards } from "../../common/decorators/auth-guard.decorator"; import { AuthGuards } from "../../common/decorators/auth-guard.decorator";
@@ -45,11 +46,11 @@ export class SupportPlansController {
return this.supportPlansService.deleteSupportPlanById(paramDto.id); return this.supportPlansService.deleteSupportPlanById(paramDto.id);
} }
// @AdminRoute() @AdminRoute()
// @PermissionsDec(PermissionEnum.SUPPORT_PLAN) // @PermissionsDec(PermissionEnum.SUPPORT_PLAN)
// @ApiOperation({ summary: "Update a support plan by id (admin)" }) @ApiOperation({ summary: "Update a support plan by id (admin)" })
// @Patch(":id") @Patch(":id")
// updateSupportPlanById(@Param() paramDto: ParamDto, @Body() updateSupportPlanDto: UpdateSupportPlanDto) { updateSupportPlanById(@Param() paramDto: ParamDto, @Body() updateSupportPlanDto: UpdateSupportPlanDto) {
// return this.supportPlansService.updateSupportPlanById(paramDto.id, updateSupportPlanDto); return this.supportPlansService.updateSupportPlanById(paramDto.id, updateSupportPlanDto);
// } }
} }
@@ -5,14 +5,14 @@ import { SupportPlanFeature } from "./entities/support-plan-feature.entity";
import { SupportPlan } from "./entities/support-plan.entity"; import { SupportPlan } from "./entities/support-plan.entity";
import { UserSupportPlan } from "./entities/user-support-plan.entity"; import { UserSupportPlan } from "./entities/user-support-plan.entity";
import { SupportPlansService } from "./providers/support-plans.service"; import { SupportPlansService } from "./providers/support-plans.service";
import { SupportPlanFeatureRepository } from "./repositories/support-plan-feature.repository";
import { SupportPlanRepository } from "./repositories/support-plan.repository"; import { SupportPlanRepository } from "./repositories/support-plan.repository";
import { UserSupportPlanRepository } from "./repositories/user-support-plan.repository"; import { UserSupportPlanRepository } from "./repositories/user-support-plan.repository";
import { SupportPlansController } from "./support-plans.controller"; import { SupportPlansController } from "./support-plans.controller";
@Module({ @Module({
imports: [TypeOrmModule.forFeature([SupportPlan, SupportPlanFeature, UserSupportPlan])], imports: [TypeOrmModule.forFeature([SupportPlan, SupportPlanFeature, UserSupportPlan])],
controllers: [SupportPlansController], controllers: [SupportPlansController],
providers: [SupportPlansService, SupportPlanRepository, UserSupportPlanRepository], providers: [SupportPlansService, SupportPlanRepository, UserSupportPlanRepository, SupportPlanFeatureRepository],
exports: [SupportPlansService], exports: [SupportPlansService],
}) })
export class SupportPlansModule {} export class SupportPlansModule {}