setup restuarant
This commit is contained in:
@@ -77,8 +77,7 @@ export class RestaurantsController {
|
||||
@ApiOperation({ summary: 'Create a new restaurant' })
|
||||
@Post('super-admin/restaurants')
|
||||
createRestaurant(@Body() createRestaurantDto: CreateRestaurantDto) {
|
||||
console.log('create rest')
|
||||
return this.restaurantsService.create(createRestaurantDto);
|
||||
return this.restaurantsService.setupRestuarant(createRestaurantDto);
|
||||
}
|
||||
|
||||
@UseGuards(SuperAdminAuthGuard)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsString, IsOptional, IsNumber, IsDate } from 'class-validator';
|
||||
import { IsString, IsOptional, IsNumber, IsDate, IsEnum } from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
import { PlanEnum } from '../interface/plan.interface';
|
||||
|
||||
export class CreateRestaurantDto {
|
||||
@ApiProperty({ example: 'کافه ژیوان', description: 'نام رستوران' })
|
||||
@@ -21,6 +22,10 @@ export class CreateRestaurantDto {
|
||||
@IsString()
|
||||
phone: string;
|
||||
|
||||
@ApiProperty({ example: 'base', enum: PlanEnum, description: 'پلن رستوران' })
|
||||
@IsEnum(PlanEnum)
|
||||
plan!: PlanEnum;
|
||||
|
||||
@ApiProperty({ example: 'sub_1234567890', description: 'شناسه اشتراک' })
|
||||
@IsString()
|
||||
subscriptionId!: string;
|
||||
|
||||
@@ -8,6 +8,9 @@ import { RestMessage } from 'src/common/enums/message.enum';
|
||||
import { FindRestaurantsDto } from '../dto/find-restaurants.dto';
|
||||
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
||||
import { PlanEnum } from '../interface/plan.interface';
|
||||
import { Admin } from '../../admin/entities/admin.entity';
|
||||
import { AdminRole } from '../../admin/entities/adminRole.entity';
|
||||
import { Role } from '../../roles/entities/role.entity';
|
||||
|
||||
@Injectable()
|
||||
export class RestaurantsService {
|
||||
@@ -16,30 +19,63 @@ export class RestaurantsService {
|
||||
private readonly restRepository: RestRepository,
|
||||
) { }
|
||||
|
||||
async create(dto: CreateRestaurantDto): Promise<Restaurant> {
|
||||
const validateSlug = await this.em.findOne(Restaurant, { slug: dto.slug })
|
||||
if (validateSlug) {
|
||||
throw new BadRequestException("Duplicate slug: " + dto.slug)
|
||||
}
|
||||
const validateSubscriptionId = await this.em.findOne(Restaurant, { subscriptionId: dto.subscriptionId })
|
||||
if (validateSubscriptionId) {
|
||||
throw new BadRequestException("Duplicate subscriptionId: " + dto.subscriptionId)
|
||||
}
|
||||
const restaurant = this.em.create(Restaurant, {
|
||||
name: dto.name,
|
||||
slug: dto.slug,
|
||||
establishedYear: dto.establishedYear,
|
||||
phone: dto.phone,
|
||||
isActive: true,
|
||||
plan: PlanEnum.Base,
|
||||
domain: `https://dmenu-plus-front.dev.danakcorp.com/${dto.slug}`,
|
||||
subscriptionId: dto.subscriptionId,
|
||||
subscriptionEndDate: dto.subscriptionEndDate,
|
||||
subscriptionStartDate: dto.subscriptionStartDate,
|
||||
});
|
||||
async setupRestuarant(dto: CreateRestaurantDto): Promise<Restaurant> {
|
||||
return await this.em.transactional(async (em) => {
|
||||
// Validate unique constraints
|
||||
const validateSlug = await em.findOne(Restaurant, { slug: dto.slug })
|
||||
if (validateSlug) {
|
||||
throw new BadRequestException("Duplicate slug: " + dto.slug)
|
||||
}
|
||||
const validateSubscriptionId = await em.findOne(Restaurant, { subscriptionId: dto.subscriptionId })
|
||||
if (validateSubscriptionId) {
|
||||
throw new BadRequestException("Duplicate subscriptionId: " + dto.subscriptionId)
|
||||
}
|
||||
|
||||
await this.em.persistAndFlush(restaurant);
|
||||
return restaurant;
|
||||
// Create restaurant
|
||||
const restaurant = em.create(Restaurant, {
|
||||
name: dto.name,
|
||||
slug: dto.slug,
|
||||
establishedYear: dto.establishedYear,
|
||||
phone: dto.phone,
|
||||
isActive: true,
|
||||
plan: dto.plan,
|
||||
domain: `https://dmenu-plus-front.dev.danakcorp.com/${dto.slug}`,
|
||||
subscriptionId: dto.subscriptionId,
|
||||
subscriptionEndDate: dto.subscriptionEndDate,
|
||||
subscriptionStartDate: dto.subscriptionStartDate,
|
||||
});
|
||||
|
||||
// Find the appropriate role based on plan
|
||||
const roleName = dto.plan === PlanEnum.Base ? 'مدیر (پلن پایه)' : 'مدیر ( پلن ویژه)';
|
||||
const role = await em.findOne(Role, { name: roleName });
|
||||
if (!role) {
|
||||
throw new BadRequestException(`Role not found for plan: ${dto.plan}`);
|
||||
}
|
||||
|
||||
// Create admin (using a default phone number for now - this could be made configurable)
|
||||
const adminPhone = `admin_${dto.slug}@system.local`; // Temporary phone format for system-generated admins
|
||||
let admin = await em.findOne(Admin, { phone: adminPhone });
|
||||
if (!admin) {
|
||||
admin = em.create(Admin, {
|
||||
phone: adminPhone,
|
||||
firstName: 'مدیر',
|
||||
lastName: dto.name,
|
||||
});
|
||||
}
|
||||
|
||||
// Create admin role relationship
|
||||
const adminRole = em.create(AdminRole, {
|
||||
admin: admin,
|
||||
role: role,
|
||||
restaurant: restaurant,
|
||||
});
|
||||
|
||||
// Persist all entities
|
||||
em.persist([restaurant, admin, adminRole]);
|
||||
await em.flush();
|
||||
|
||||
return restaurant;
|
||||
});
|
||||
}
|
||||
|
||||
async findAll(dto: FindRestaurantsDto): Promise<PaginatedResult<Restaurant>> {
|
||||
|
||||
Reference in New Issue
Block a user