Files
backend/src/categories/categories.controller.ts
T
Alireza HassaniandCursor d14f91a606 Add catalog EN/AR translations with AI translate-all by locale.
Store product/category translations in entity_translations, expose CMS APIs, and gate the MultiLanguage Data module for businesses.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 09:51:11 +03:30

260 lines
7.6 KiB
TypeScript

import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
Put,
Query,
UseGuards,
} from '@nestjs/common';
import { AuthUser } from '../auth/auth.types';
import { CurrentUser } from '../auth/decorators/current-user.decorator';
import { RequireBusinessPermission } from '../auth/decorators/require-business-permission.decorator';
import { BusinessPermissionGuard } from '../auth/guards/business-permission.guard';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { CategoriesService } from './categories.service';
import { CategoryTechnicalFormService } from './category-technical-form.service';
import { CategoryTechnicalFormAiService } from './category-technical-form-ai.service';
import { CategoryAiService } from './category-ai.service';
import { CategoryVariationsService } from './category-variations.service';
import { CreateCategoryDto, ListCategoriesDto, UpdateCategoryDto } from './dto/category.dto';
import {
ReplaceCategoryTechnicalFormDto,
SuggestCategoryTechnicalFormDto,
} from './dto/category-technical-form.dto';
import { ReplaceCategoryVariationsDto } from './dto/category-variation.dto';
import { TranslationEntityType } from '@prisma/client';
import { UpsertTranslationDto, TranslateAllDto } from '../translations/dto/translation.dto';
import { TranslationsService } from '../translations/translations.service';
import { GenerateCategoriesDto } from './dto/category-ai.dto';
@Controller('businesses/:businessId/categories')
@UseGuards(JwtAuthGuard, BusinessPermissionGuard)
export class CategoriesController {
constructor(
private readonly service: CategoriesService,
private readonly variationsService: CategoryVariationsService,
private readonly technicalFormService: CategoryTechnicalFormService,
private readonly technicalFormAiService: CategoryTechnicalFormAiService,
private readonly categoryAiService: CategoryAiService,
private readonly translations: TranslationsService,
) {}
@Get('color-presets')
@RequireBusinessPermission('categories.read')
listColorPresets() {
return this.variationsService.listColorPresets();
}
@Get()
@RequireBusinessPermission('categories.read')
list(
@Param('businessId') businessId: string,
@Query() query: ListCategoriesDto,
@CurrentUser() user: AuthUser,
) {
return this.service.list(businessId, query, user);
}
@Post('ai-generate')
@RequireBusinessPermission('categories.create')
generateWithAi(
@Param('businessId') businessId: string,
@Body() dto: GenerateCategoriesDto,
@CurrentUser() user: AuthUser,
) {
return this.categoryAiService.generateProductCategories(businessId, dto, user);
}
@Post('translations/ai-all')
@RequireBusinessPermission('categories.update')
translateAllWithAi(
@Param('businessId') businessId: string,
@Body() dto: TranslateAllDto,
@CurrentUser() user: AuthUser,
) {
return this.translations.translateAllCategories(businessId, user, dto.locale);
}
@Post()
@RequireBusinessPermission('categories.create')
create(
@Param('businessId') businessId: string,
@Body() dto: CreateCategoryDto,
@CurrentUser() user: AuthUser,
) {
return this.service.create(businessId, dto, user);
}
@Patch(':categoryId')
@RequireBusinessPermission('categories.update')
update(
@Param('businessId') businessId: string,
@Param('categoryId') categoryId: string,
@Body() dto: UpdateCategoryDto,
@CurrentUser() user: AuthUser,
) {
return this.service.update(businessId, categoryId, dto, user);
}
@Get(':categoryId/translations')
@RequireBusinessPermission('categories.read')
listTranslations(
@Param('businessId') businessId: string,
@Param('categoryId') categoryId: string,
@CurrentUser() user: AuthUser,
) {
return this.translations.list(
businessId,
TranslationEntityType.category,
categoryId,
user,
);
}
@Post(':categoryId/translations/:locale/ai')
@RequireBusinessPermission('categories.update')
translateWithAi(
@Param('businessId') businessId: string,
@Param('categoryId') categoryId: string,
@Param('locale') locale: string,
@CurrentUser() user: AuthUser,
) {
return this.translations.translateWithAi(
businessId,
TranslationEntityType.category,
categoryId,
locale,
user,
);
}
@Put(':categoryId/translations/:locale')
@RequireBusinessPermission('categories.update')
saveTranslation(
@Param('businessId') businessId: string,
@Param('categoryId') categoryId: string,
@Param('locale') locale: string,
@Body() dto: UpsertTranslationDto,
@CurrentUser() user: AuthUser,
) {
return this.translations.save(
businessId,
TranslationEntityType.category,
categoryId,
locale,
dto,
user,
);
}
@Get(':categoryId/variations')
@RequireBusinessPermission('categories.read')
listVariations(
@Param('businessId') businessId: string,
@Param('categoryId') categoryId: string,
@CurrentUser() user: AuthUser,
) {
return this.variationsService.listForCategory(businessId, categoryId, user);
}
@Put(':categoryId/variations')
@RequireBusinessPermission('categories.update')
replaceVariations(
@Param('businessId') businessId: string,
@Param('categoryId') categoryId: string,
@Body() dto: ReplaceCategoryVariationsDto,
@CurrentUser() user: AuthUser,
) {
return this.variationsService.replaceForCategory(
businessId,
categoryId,
dto,
user,
);
}
@Get(':categoryId/technical-form')
@RequireBusinessPermission('categories.read')
getTechnicalForm(
@Param('businessId') businessId: string,
@Param('categoryId') categoryId: string,
@CurrentUser() user: AuthUser,
) {
return this.technicalFormService.getForCategory(businessId, categoryId, user);
}
@Put(':categoryId/technical-form')
@RequireBusinessPermission('categories.update')
replaceTechnicalForm(
@Param('businessId') businessId: string,
@Param('categoryId') categoryId: string,
@Body() dto: ReplaceCategoryTechnicalFormDto,
@CurrentUser() user: AuthUser,
) {
return this.technicalFormService.replaceForCategory(
businessId,
categoryId,
dto,
user,
);
}
@Post(':categoryId/technical-form/ai-suggest')
@RequireBusinessPermission('categories.update')
suggestTechnicalForm(
@Param('businessId') businessId: string,
@Param('categoryId') categoryId: string,
@Body() dto: SuggestCategoryTechnicalFormDto,
@CurrentUser() user: AuthUser,
) {
return this.technicalFormAiService.suggestForCategory(
businessId,
categoryId,
dto,
user,
);
}
@Delete(':categoryId')
@RequireBusinessPermission('categories.delete')
remove(
@Param('businessId') businessId: string,
@Param('categoryId') categoryId: string,
@CurrentUser() user: AuthUser,
) {
return this.service.remove(businessId, categoryId, user);
}
}
@Controller('tenants/:host/categories')
export class PublicCategoriesController {
constructor(private readonly service: CategoriesService) {}
@Get()
list(@Param('host') host: string, @Query() query: ListCategoriesDto) {
return this.service.listPublic(host, query);
}
@Get('by-id/:categoryId')
getById(
@Param('host') host: string,
@Param('categoryId') categoryId: string,
@Query() query: ListCategoriesDto,
) {
return this.service.getPublicById(host, categoryId, query.entityType);
}
@Get('by-slug/:slug')
getBySlug(
@Param('host') host: string,
@Param('slug') slug: string,
@Query() query: ListCategoriesDto,
) {
return this.service.getPublicBySlug(host, slug, query.entityType);
}
}