diff --git a/src/modules/business/business.controller.ts b/src/modules/business/business.controller.ts index 9932dd8..ea5b019 100644 --- a/src/modules/business/business.controller.ts +++ b/src/modules/business/business.controller.ts @@ -3,7 +3,7 @@ import { BusinessService } from './business.service'; import { SetupBusinessDto } from './dto/setup-business.dto'; import { SuperAdminAuthGuard } from '../auth/guards/superAdminAuth.guard'; import { FindBusinessesDto } from './dto/find-businesses.dto'; -// import { UpdateBusinessDto } from './dto/update-business.dto'; +import { UpdateBusinessDto } from './dto/update-business.dto'; @Controller('super-admin/business') @UseGuards(SuperAdminAuthGuard) @@ -15,15 +15,15 @@ export class BusinessController { return this.businessService.findAll(dto); } - // @Get(':id') - // findOne(@Param('id') id: string) { - // return this.businessService.fin(id); - // } + @Get(':id') + findOne(@Param('id') id: string) { + return this.businessService.findByIdOrFail(id); + } - // @Patch(':id') - // update(@Param('id') id: string, @Body() updateBusinessDto: UpdateBusinessDto) { - // return this.businessService.update(+id, updateBusinessDto); - // } + @Patch(':id') + update(@Param('id') id: string, @Body() updateBusinessDto: UpdateBusinessDto) { + return this.businessService.update(id, updateBusinessDto); + } @Post('setup') create(@Body() dto: SetupBusinessDto) { diff --git a/src/modules/business/business.module.ts b/src/modules/business/business.module.ts index f4e0887..c857a0c 100644 --- a/src/modules/business/business.module.ts +++ b/src/modules/business/business.module.ts @@ -6,7 +6,7 @@ import { Business } from './entities/business.entity'; import { JwtModule } from '@nestjs/jwt'; @Module({ - imports: [MikroOrmModule.forFeature([Business]), JwtModule.register({})], + imports: [MikroOrmModule.forFeature([Business]), JwtModule], controllers: [BusinessController], providers: [BusinessService], exports: [BusinessService], diff --git a/src/modules/business/business.service.ts b/src/modules/business/business.service.ts index 2c3eeeb..f0e035e 100644 --- a/src/modules/business/business.service.ts +++ b/src/modules/business/business.service.ts @@ -19,10 +19,23 @@ export class BusinessService { async setupAccount(dto: SetupBusinessDto) { const { danakSubscriptionId, name, phone, slug, firstName, lastName, maxCataloguesCount } = dto - this.logger.log('recived request from danak payload', { + + this.logger.log('recived request payload from danak ', { danakSubscriptionId, name, phone, slug, firstName, lastName, maxCataloguesCount }) + + const existingSlug = await this.businessRepository.findOne({ slug: dto.slug }) + + if (existingSlug) { + throw new NotFoundException("this Slug is already taken, Please select another one.") + } + + const existingSubscriptionId = await this.businessRepository.findOne({ danakSubscriptionId: dto.danakSubscriptionId }) + if (existingSubscriptionId) { + throw new NotFoundException("this danakSubscriptionId is already used") + } + const business = await this.em.transactional(async (em) => { const business = em.create(Business, { danakSubscriptionId, @@ -74,13 +87,33 @@ export class BusinessService { return business } - update(id: number, updateBusinessDto: UpdateBusinessDto) { - return `This action updates a #${id} business`; + async update(id: string, dto: UpdateBusinessDto) { + const business = await this.findByIdOrFail(id) + + if (dto.slug) { + const existing = await this.businessRepository.findOne({ slug: dto.slug, id: { $not: id } }) + if (existing) { + throw new NotFoundException("this Slug is already taken, Please select another one.") + } + } + + if (dto.danakSubscriptionId) { + const existing = await this.businessRepository.findOne({ danakSubscriptionId: dto.danakSubscriptionId, id: { $not: id } }) + if (existing) { + throw new NotFoundException("this danakSubscriptionId is already used") + } + } + + this.em.assign(business, dto) + + await this.em.flush() + + return business } async hardDelete(businessId: string) { return await this.em.transactional(async (em) => { - // Find the restaurant first + const business = await em.findOne(Business, { id: businessId }); if (!business) { diff --git a/src/modules/business/dto/update-business.dto.ts b/src/modules/business/dto/update-business.dto.ts index 0d4380b..8df40e9 100644 --- a/src/modules/business/dto/update-business.dto.ts +++ b/src/modules/business/dto/update-business.dto.ts @@ -1,4 +1,21 @@ -import { PartialType } from '@nestjs/swagger'; -import { SetupBusinessDto } from './setup-business.dto'; +import { IsInt, IsString, } from 'class-validator'; +import { ApiProperty } from '@nestjs/swagger'; -export class UpdateBusinessDto extends PartialType(SetupBusinessDto) { } +export class UpdateBusinessDto { + @IsString() + @ApiProperty({ example: '01619684-aebc-47d4-acc4-2e912a3d9dce' }) + danakSubscriptionId: string; + + @IsString() + @ApiProperty() + name: string; + + @IsString() + @ApiProperty() + slug: string; + + @IsInt() + @ApiProperty() + maxCataloguesCount: number; + +}