module service-templates 050219
This commit is contained in:
@@ -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],
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
export class CreateServiceTemplateDto {
|
||||
title!: string;
|
||||
slug!: string;
|
||||
categoryId!: string;
|
||||
description?: string;
|
||||
defaultDurationMinutes?: number;
|
||||
isActive?: boolean;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
export class UpdateServiceTemplateDto {
|
||||
title?: string;
|
||||
slug?: string;
|
||||
categoryId?: string;
|
||||
description?: string | null;
|
||||
defaultDurationMinutes?: number;
|
||||
isActive?: boolean;
|
||||
}
|
||||
@@ -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<ServiceTemplateEntity[]> {
|
||||
const parsedIsActive =
|
||||
typeof isActive === 'string' ? isActive === 'true' : undefined;
|
||||
|
||||
return this.service.findAll({
|
||||
categoryId,
|
||||
isActive: parsedIsActive,
|
||||
});
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string): Promise<ServiceTemplateEntity> {
|
||||
return this.service.findOne(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(
|
||||
@Body() payload: CreateServiceTemplateDto,
|
||||
): Promise<ServiceTemplateEntity> {
|
||||
return this.service.create(payload);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(
|
||||
@Param('id') id: string,
|
||||
@Body() payload: UpdateServiceTemplateDto,
|
||||
): Promise<ServiceTemplateEntity> {
|
||||
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 };
|
||||
}
|
||||
}
|
||||
@@ -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 {}
|
||||
@@ -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<ServiceTemplateEntity>,
|
||||
@InjectRepository(BusinessCategoryEntity)
|
||||
private readonly categoryRepo: Repository<BusinessCategoryEntity>,
|
||||
) {}
|
||||
|
||||
async findAll(filters: FindAllFilters): Promise<ServiceTemplateEntity[]> {
|
||||
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<ServiceTemplateEntity> {
|
||||
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<ServiceTemplateEntity> {
|
||||
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<ServiceTemplateEntity> {
|
||||
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<void> {
|
||||
const result = await this.repo.delete(id);
|
||||
if (!result.affected) {
|
||||
throw new NotFoundException('Service template not found');
|
||||
}
|
||||
}
|
||||
|
||||
private async ensureCategoryExists(categoryId: string): Promise<void> {
|
||||
const category = await this.categoryRepo.findOne({
|
||||
where: { id: categoryId },
|
||||
});
|
||||
if (!category) {
|
||||
throw new NotFoundException('Business category not found');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user