From 87d8912a1fe6b6108ac46ec7318825f390477270 Mon Sep 17 00:00:00 2001 From: Mahdi Date: Sat, 9 May 2026 16:53:27 +0330 Subject: [PATCH] module service-templates 050219 --- src/app.module.ts | 2 + .../dto/create-service-template.dto.ts | 8 ++ .../dto/update-service-template.dto.ts | 8 ++ .../service-templates.controller.ts | 59 +++++++++ .../service-templates.module.ts | 15 +++ .../service-templates.service.ts | 117 ++++++++++++++++++ 6 files changed, 209 insertions(+) create mode 100644 src/modules/service-templates/dto/create-service-template.dto.ts create mode 100644 src/modules/service-templates/dto/update-service-template.dto.ts create mode 100644 src/modules/service-templates/service-templates.controller.ts create mode 100644 src/modules/service-templates/service-templates.module.ts create mode 100644 src/modules/service-templates/service-templates.service.ts diff --git a/src/app.module.ts b/src/app.module.ts index 417cd6a..da6668b 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -6,6 +6,7 @@ import { AppService } from './app.service'; import { dataSourceOptions } from './config/typeorm.config'; import { BusinessCategoriesModule } from './modules/business-categories/business-categories.module'; import { BusinessesModule } from './modules/businesses/businesses.module'; +import { ServiceTemplatesModule } from './modules/service-templates/service-templates.module'; @Module({ imports: [ @@ -13,6 +14,7 @@ import { BusinessesModule } from './modules/businesses/businesses.module'; TypeOrmModule.forRoot(dataSourceOptions), BusinessCategoriesModule, BusinessesModule, + ServiceTemplatesModule, ], controllers: [AppController], providers: [AppService], diff --git a/src/modules/service-templates/dto/create-service-template.dto.ts b/src/modules/service-templates/dto/create-service-template.dto.ts new file mode 100644 index 0000000..3841f30 --- /dev/null +++ b/src/modules/service-templates/dto/create-service-template.dto.ts @@ -0,0 +1,8 @@ +export class CreateServiceTemplateDto { + title!: string; + slug!: string; + categoryId!: string; + description?: string; + defaultDurationMinutes?: number; + isActive?: boolean; +} diff --git a/src/modules/service-templates/dto/update-service-template.dto.ts b/src/modules/service-templates/dto/update-service-template.dto.ts new file mode 100644 index 0000000..e06b126 --- /dev/null +++ b/src/modules/service-templates/dto/update-service-template.dto.ts @@ -0,0 +1,8 @@ +export class UpdateServiceTemplateDto { + title?: string; + slug?: string; + categoryId?: string; + description?: string | null; + defaultDurationMinutes?: number; + isActive?: boolean; +} diff --git a/src/modules/service-templates/service-templates.controller.ts b/src/modules/service-templates/service-templates.controller.ts new file mode 100644 index 0000000..0e65f1a --- /dev/null +++ b/src/modules/service-templates/service-templates.controller.ts @@ -0,0 +1,59 @@ +import { + Body, + Controller, + Delete, + Get, + Param, + Patch, + Post, + Query, +} from '@nestjs/common'; +import { ServiceTemplateEntity } from '../../entities/service-template.entity'; +import { ServiceTemplatesService } from './service-templates.service'; +import { CreateServiceTemplateDto } from './dto/create-service-template.dto'; +import { UpdateServiceTemplateDto } from './dto/update-service-template.dto'; + +@Controller('service-templates') +export class ServiceTemplatesController { + constructor(private readonly service: ServiceTemplatesService) {} + + @Get() + findAll( + @Query('categoryId') categoryId?: string, + @Query('isActive') isActive?: string, + ): Promise { + const parsedIsActive = + typeof isActive === 'string' ? isActive === 'true' : undefined; + + return this.service.findAll({ + categoryId, + isActive: parsedIsActive, + }); + } + + @Get(':id') + findOne(@Param('id') id: string): Promise { + return this.service.findOne(id); + } + + @Post() + create( + @Body() payload: CreateServiceTemplateDto, + ): Promise { + return this.service.create(payload); + } + + @Patch(':id') + update( + @Param('id') id: string, + @Body() payload: UpdateServiceTemplateDto, + ): Promise { + return this.service.update(id, payload); + } + + @Delete(':id') + async remove(@Param('id') id: string): Promise<{ success: true }> { + await this.service.remove(id); + return { success: true }; + } +} diff --git a/src/modules/service-templates/service-templates.module.ts b/src/modules/service-templates/service-templates.module.ts new file mode 100644 index 0000000..430b18c --- /dev/null +++ b/src/modules/service-templates/service-templates.module.ts @@ -0,0 +1,15 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { BusinessCategoryEntity } from '../../entities/business-category.entity'; +import { ServiceTemplateEntity } from '../../entities/service-template.entity'; +import { ServiceTemplatesController } from './service-templates.controller'; +import { ServiceTemplatesService } from './service-templates.service'; + +@Module({ + imports: [ + TypeOrmModule.forFeature([ServiceTemplateEntity, BusinessCategoryEntity]), + ], + controllers: [ServiceTemplatesController], + providers: [ServiceTemplatesService], +}) +export class ServiceTemplatesModule {} diff --git a/src/modules/service-templates/service-templates.service.ts b/src/modules/service-templates/service-templates.service.ts new file mode 100644 index 0000000..1612478 --- /dev/null +++ b/src/modules/service-templates/service-templates.service.ts @@ -0,0 +1,117 @@ +import { + ConflictException, + Injectable, + NotFoundException, +} from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { BusinessCategoryEntity } from '../../entities/business-category.entity'; +import { ServiceTemplateEntity } from '../../entities/service-template.entity'; +import { CreateServiceTemplateDto } from './dto/create-service-template.dto'; +import { UpdateServiceTemplateDto } from './dto/update-service-template.dto'; + +type FindAllFilters = { + categoryId?: string; + isActive?: boolean; +}; + +@Injectable() +export class ServiceTemplatesService { + constructor( + @InjectRepository(ServiceTemplateEntity) + private readonly repo: Repository, + @InjectRepository(BusinessCategoryEntity) + private readonly categoryRepo: Repository, + ) {} + + async findAll(filters: FindAllFilters): Promise { + const query = this.repo + .createQueryBuilder('template') + .leftJoinAndSelect('template.category', 'category') + .orderBy('template.createdAt', 'DESC'); + + if (filters.categoryId) { + query.andWhere('template.categoryId = :categoryId', { + categoryId: filters.categoryId, + }); + } + + if (typeof filters.isActive === 'boolean') { + query.andWhere('template.isActive = :isActive', { + isActive: filters.isActive, + }); + } + + return query.getMany(); + } + + async findOne(id: string): Promise { + const template = await this.repo.findOne({ + where: { id }, + relations: { category: true }, + }); + if (!template) { + throw new NotFoundException('Service template not found'); + } + return template; + } + + async create(p: CreateServiceTemplateDto): Promise { + const slugTaken = await this.repo.findOne({ where: { slug: p.slug } }); + if (slugTaken) { + throw new ConflictException('Slug already exists'); + } + + await this.ensureCategoryExists(p.categoryId); + + const entity = this.repo.create({ + title: p.title, + slug: p.slug, + categoryId: p.categoryId, + description: p.description ?? null, + defaultDurationMinutes: p.defaultDurationMinutes ?? 30, + isActive: p.isActive ?? true, + }); + + return this.repo.save(entity); + } + + async update( + id: string, + payload: UpdateServiceTemplateDto, + ): Promise { + const template = await this.findOne(id); + + if (payload.slug && payload.slug !== template.slug) { + const slugTaken = await this.repo.findOne({ + where: { slug: payload.slug }, + }); + if (slugTaken) { + throw new ConflictException('Slug already exists'); + } + } + + if (payload.categoryId && payload.categoryId !== template.categoryId) { + await this.ensureCategoryExists(payload.categoryId); + } + + Object.assign(template, payload); + return this.repo.save(template); + } + + async remove(id: string): Promise { + const result = await this.repo.delete(id); + if (!result.affected) { + throw new NotFoundException('Service template not found'); + } + } + + private async ensureCategoryExists(categoryId: string): Promise { + const category = await this.categoryRepo.findOne({ + where: { id: categoryId }, + }); + if (!category) { + throw new NotFoundException('Business category not found'); + } + } +}