fix create contact
This commit is contained in:
@@ -16,21 +16,24 @@ import { UpdateContactStatusDto } from '../dto/update-contact-status.dto';
|
|||||||
import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam, ApiQuery, ApiHeader } from '@nestjs/swagger';
|
import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam, ApiQuery, ApiHeader } from '@nestjs/swagger';
|
||||||
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||||
import { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.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 { API_HEADER_SLUG } from 'src/common/constants';
|
||||||
import { Permission } from 'src/common/enums/permission.enum';
|
import { Permission } from 'src/common/enums/permission.enum';
|
||||||
import { Permissions } from 'src/common/decorators/permissions.decorator';
|
import { Permissions } from 'src/common/decorators/permissions.decorator';
|
||||||
import { RestId } from 'src/common/decorators/rest-id.decorator';
|
import { RestId } from 'src/common/decorators/rest-id.decorator';
|
||||||
|
import { RestSlug } from 'src/common/decorators/rest-slug.decorator';
|
||||||
|
|
||||||
@ApiTags('contact')
|
@ApiTags('contact')
|
||||||
@Controller()
|
@Controller()
|
||||||
export class ContactController {
|
export class ContactController {
|
||||||
constructor(private readonly contactService: ContactService) { }
|
constructor(private readonly contactService: ContactService) { }
|
||||||
|
|
||||||
|
@UseGuards(OptionalAuthGuard)
|
||||||
@Post('public/contact')
|
@Post('public/contact')
|
||||||
@ApiOperation({ summary: 'Create a new contact (public endpoint)' })
|
@ApiOperation({ summary: 'Create a new contact (public endpoint)' })
|
||||||
@ApiHeader(API_HEADER_SLUG)
|
@ApiHeader(API_HEADER_SLUG)
|
||||||
async create(@Body() createContactDto: CreateContactDto) {
|
async create(@Body() createContactDto: CreateContactDto, @RestSlug() slug: string, @RestId() restId?: string) {
|
||||||
return this.contactService.create(createContactDto);
|
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 { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||||
import { ContactScope } from '../interface/interface';
|
import { ContactScope } from '../interface/interface';
|
||||||
|
|
||||||
@@ -18,12 +18,6 @@ export class CreateContactDto {
|
|||||||
@ApiProperty({ description: 'Contact Scope' })
|
@ApiProperty({ description: 'Contact Scope' })
|
||||||
scope!: ContactScope;
|
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()
|
@IsOptional()
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsMobilePhone('fa-IR')
|
@IsMobilePhone('fa-IR')
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ export class Contact extends BaseEntity {
|
|||||||
@ManyToOne(() => Restaurant, { nullable: true })
|
@ManyToOne(() => Restaurant, { nullable: true })
|
||||||
restaurant?: Restaurant | null = null;
|
restaurant?: Restaurant | null = null;
|
||||||
|
|
||||||
|
|
||||||
@Property({ default: ContactStatusEnum.New })
|
@Property({ default: ContactStatusEnum.New })
|
||||||
status: ContactStatusEnum = ContactStatusEnum.New;
|
status: ContactStatusEnum = ContactStatusEnum.New;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,19 +17,23 @@ export class ContactService {
|
|||||||
private readonly em: EntityManager,
|
private readonly em: EntityManager,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async create(createContactDto: CreateContactDto): Promise<Contact> {
|
async create(dto: CreateContactDto, restId?: string, slug?: string): Promise<Contact> {
|
||||||
const { restaurantId, ...contactData } = createContactDto;
|
|
||||||
|
|
||||||
let restaurant: Restaurant | null = null;
|
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) {
|
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({
|
const contact = this.contactRepository.create({
|
||||||
...contactData,
|
...dto,
|
||||||
status: ContactStatusEnum.New,
|
status: ContactStatusEnum.New,
|
||||||
restaurant,
|
restaurant,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user