From ef3c5df49495a0cf007bc7fc273096b8907acbfc Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Wed, 7 Jan 2026 00:03:51 +0330 Subject: [PATCH] fix create contact --- .../contact/controllers/contact.controller.ts | 7 +++++-- src/modules/contact/dto/create-contact.dto.ts | 8 +------- src/modules/contact/entities/contact.entity.ts | 1 + src/modules/contact/providers/contact.service.ts | 16 ++++++++++------ 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/modules/contact/controllers/contact.controller.ts b/src/modules/contact/controllers/contact.controller.ts index d3e3344..83736fa 100644 --- a/src/modules/contact/controllers/contact.controller.ts +++ b/src/modules/contact/controllers/contact.controller.ts @@ -16,21 +16,24 @@ import { UpdateContactStatusDto } from '../dto/update-contact-status.dto'; import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam, ApiQuery, ApiHeader } from '@nestjs/swagger'; import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard'; import { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.guard'; +import { OptionalAuthGuard } from 'src/modules/auth/guards/optinalAuth.guard'; import { API_HEADER_SLUG } from 'src/common/constants'; import { Permission } from 'src/common/enums/permission.enum'; import { Permissions } from 'src/common/decorators/permissions.decorator'; import { RestId } from 'src/common/decorators/rest-id.decorator'; +import { RestSlug } from 'src/common/decorators/rest-slug.decorator'; @ApiTags('contact') @Controller() export class ContactController { constructor(private readonly contactService: ContactService) { } + @UseGuards(OptionalAuthGuard) @Post('public/contact') @ApiOperation({ summary: 'Create a new contact (public endpoint)' }) @ApiHeader(API_HEADER_SLUG) - async create(@Body() createContactDto: CreateContactDto) { - return this.contactService.create(createContactDto); + async create(@Body() createContactDto: CreateContactDto, @RestSlug() slug: string, @RestId() restId?: string) { + return this.contactService.create(createContactDto, restId, slug); } diff --git a/src/modules/contact/dto/create-contact.dto.ts b/src/modules/contact/dto/create-contact.dto.ts index 940c711..134bd54 100644 --- a/src/modules/contact/dto/create-contact.dto.ts +++ b/src/modules/contact/dto/create-contact.dto.ts @@ -1,4 +1,4 @@ -import { IsNotEmpty, IsString, IsOptional, IsMobilePhone, IsEnum, IsUUID, ValidateIf } from 'class-validator'; +import { IsNotEmpty, IsString, IsOptional, IsMobilePhone, IsEnum } from 'class-validator'; import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; import { ContactScope } from '../interface/interface'; @@ -18,12 +18,6 @@ export class CreateContactDto { @ApiProperty({ description: 'Contact Scope' }) scope!: ContactScope; - @ValidateIf((o) => o.scope === ContactScope.RESTAURANT) - @IsNotEmpty() - @IsUUID() - @ApiPropertyOptional({ example: '550e8400-e29b-41d4-a716-446655440000', description: 'Restaurant ID (required when scope is restaurant)' }) - restaurantId?: string; - @IsOptional() @IsString() @IsMobilePhone('fa-IR') diff --git a/src/modules/contact/entities/contact.entity.ts b/src/modules/contact/entities/contact.entity.ts index db1e291..281855c 100644 --- a/src/modules/contact/entities/contact.entity.ts +++ b/src/modules/contact/entities/contact.entity.ts @@ -19,6 +19,7 @@ export class Contact extends BaseEntity { @ManyToOne(() => Restaurant, { nullable: true }) restaurant?: Restaurant | null = null; + @Property({ default: ContactStatusEnum.New }) status: ContactStatusEnum = ContactStatusEnum.New; diff --git a/src/modules/contact/providers/contact.service.ts b/src/modules/contact/providers/contact.service.ts index 2b04004..48fad03 100644 --- a/src/modules/contact/providers/contact.service.ts +++ b/src/modules/contact/providers/contact.service.ts @@ -17,19 +17,23 @@ export class ContactService { private readonly em: EntityManager, ) { } - async create(createContactDto: CreateContactDto): Promise { - const { restaurantId, ...contactData } = createContactDto; + async create(dto: CreateContactDto, restId?: string, slug?: string): Promise { + let restaurant: Restaurant | null = null; - if (restaurantId && createContactDto.scope === ContactScope.RESTAURANT) { - restaurant = await this.em.findOne(Restaurant, { id: restaurantId }); + + if ((restId || slug) && dto.scope === ContactScope.RESTAURANT) { + restaurant = await this.em.findOne(Restaurant, { + ...(restId ? { id: restId } : {}) + , ...(slug ? { slug: slug } : {}) + }); if (!restaurant) { - throw new NotFoundException(`Restaurant with ID ${restaurantId} not found`); + throw new NotFoundException(`Restaurant with ID ${restId} not found`); } } const contact = this.contactRepository.create({ - ...contactData, + ...dto, status: ContactStatusEnum.New, restaurant, });