fix create contact

This commit is contained in:
2026-01-07 00:03:51 +03:30
parent 9fe2321c9b
commit ef3c5df494
4 changed files with 17 additions and 15 deletions
@@ -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);
}
@@ -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')
@@ -20,6 +20,7 @@ export class Contact extends BaseEntity {
@ManyToOne(() => Restaurant, { nullable: true })
restaurant?: Restaurant | null = null;
@Property({ default: ContactStatusEnum.New })
status: ContactStatusEnum = ContactStatusEnum.New;
}
@@ -17,19 +17,23 @@ export class ContactService {
private readonly em: EntityManager,
) { }
async create(createContactDto: CreateContactDto): Promise<Contact> {
const { restaurantId, ...contactData } = createContactDto;
async create(dto: CreateContactDto, restId?: string, slug?: string): Promise<Contact> {
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,
});