This commit is contained in:
2026-03-10 11:54:16 +03:30
parent ee6af69eee
commit 4ead1cdaf6
6 changed files with 48 additions and 27 deletions
+1 -4
View File
@@ -17,8 +17,5 @@ export class CreateAdminDto {
@IsString() @IsString()
lastName?: string; lastName?: string;
@ApiProperty({ description: 'Role id for the admin' })
@IsNotEmpty()
@IsString()
roleId!: string;
} }
+4 -4
View File
@@ -1,15 +1,15 @@
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { BusinessService } from './business.service'; import { BusinessService } from './business.service';
import { CreateBusinessDto } from './dto/create-business.dto'; import { SetupBusinessDto } from './dto/create-business.dto';
import { UpdateBusinessDto } from './dto/update-business.dto'; import { UpdateBusinessDto } from './dto/update-business.dto';
@Controller('business') @Controller('business')
export class BusinessController { export class BusinessController {
constructor(private readonly businessService: BusinessService) {} constructor(private readonly businessService: BusinessService) { }
@Post() @Post()
create(@Body() createBusinessDto: CreateBusinessDto) { create(@Body() createBusinessDto: SetupBusinessDto) {
return this.businessService.create(createBusinessDto); return this.businessService.setupAccount(createBusinessDto);
} }
@Get() @Get()
+23 -8
View File
@@ -1,5 +1,5 @@
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { CreateBusinessDto } from './dto/create-business.dto'; import { SetupBusinessDto } from './dto/create-business.dto';
import { UpdateBusinessDto } from './dto/update-business.dto'; import { UpdateBusinessDto } from './dto/update-business.dto';
import { EntityManager } from '@mikro-orm/postgresql'; import { EntityManager } from '@mikro-orm/postgresql';
import { Business } from './entities/business.entity'; import { Business } from './entities/business.entity';
@@ -13,13 +13,28 @@ export class BusinessService {
private readonly businessRepository: BusinessRepository private readonly businessRepository: BusinessRepository
) { } ) { }
async create(dto: CreateBusinessDto) { async setupAccount(dto: SetupBusinessDto) {
const admin = await this.em.findOne(Admin, {}) const { danakSubscriptionId, name, phone, slug, firstName, lastName } = dto
if (!admin) {
throw new BadRequestException() const business = await this.em.transactional(async (em) => {
} const business = em.create(Business, {
const business = this.em.create(Business, { ...dto, admin }) danakSubscriptionId,
await this.em.persistAndFlush(business) name,
slug
})
const admin = em.create(Admin, {
firstName,
lastName,
phone,
business
})
await em.persistAndFlush([business, admin])
return business
})
return business return business
} }
@@ -1,15 +1,11 @@
import { IsNotEmpty, IsOptional, IsString, } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsOptional, IsBoolean, IsInt, Min, IsEnum } from 'class-validator'; export class SetupBusinessDto {
import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';
export class CreateBusinessDto {
@IsString() @IsString()
@ApiProperty({ example: '01619684-aebc-47d4-acc4-2e912a3d9dce' }) @ApiProperty({ example: '01619684-aebc-47d4-acc4-2e912a3d9dce' })
danakSubscriptionId: string; danakSubscriptionId: string;
@IsString() @IsString()
@ApiProperty() @ApiProperty()
name: string; name: string;
@@ -18,6 +14,19 @@ export class CreateBusinessDto {
@ApiProperty() @ApiProperty()
slug: string; slug: string;
@ApiProperty({ description: 'Mobile phone number' })
@IsNotEmpty()
@IsString()
phone!: string;
@ApiProperty({ description: 'First name', required: false })
@IsOptional()
@IsString()
firstName?: string;
@ApiProperty({ description: 'Last name', required: false })
@IsOptional()
@IsString()
lastName?: string;
} }
@@ -1,4 +1,4 @@
import { PartialType } from '@nestjs/swagger'; import { PartialType } from '@nestjs/swagger';
import { CreateBusinessDto } from './create-business.dto'; import { SetupBusinessDto } from './create-business.dto';
export class UpdateBusinessDto extends PartialType(CreateBusinessDto) {} export class UpdateBusinessDto extends PartialType(SetupBusinessDto) { }
@@ -40,7 +40,7 @@ export class Business extends BaseEntity {
catalogues = new Collection<Catalogue>(this); catalogues = new Collection<Catalogue>(this);
@OneToOne(() => Admin, (admin) => admin.business, { deleteRule: 'restrict' }) @OneToOne(() => Admin, (admin) => admin.business, { deleteRule: 'restrict' })
admin: Admin admin: Admin & Opt
[EntityRepositoryType]?: BusinessRepository; [EntityRepositoryType]?: BusinessRepository;