setup restuarant
This commit is contained in:
@@ -77,8 +77,7 @@ export class RestaurantsController {
|
|||||||
@ApiOperation({ summary: 'Create a new restaurant' })
|
@ApiOperation({ summary: 'Create a new restaurant' })
|
||||||
@Post('super-admin/restaurants')
|
@Post('super-admin/restaurants')
|
||||||
createRestaurant(@Body() createRestaurantDto: CreateRestaurantDto) {
|
createRestaurant(@Body() createRestaurantDto: CreateRestaurantDto) {
|
||||||
console.log('create rest')
|
return this.restaurantsService.setupRestuarant(createRestaurantDto);
|
||||||
return this.restaurantsService.create(createRestaurantDto);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(SuperAdminAuthGuard)
|
@UseGuards(SuperAdminAuthGuard)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
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 { Type } from 'class-transformer';
|
||||||
|
import { PlanEnum } from '../interface/plan.interface';
|
||||||
|
|
||||||
export class CreateRestaurantDto {
|
export class CreateRestaurantDto {
|
||||||
@ApiProperty({ example: 'کافه ژیوان', description: 'نام رستوران' })
|
@ApiProperty({ example: 'کافه ژیوان', description: 'نام رستوران' })
|
||||||
@@ -21,6 +22,10 @@ export class CreateRestaurantDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
phone: string;
|
phone: string;
|
||||||
|
|
||||||
|
@ApiProperty({ example: 'base', enum: PlanEnum, description: 'پلن رستوران' })
|
||||||
|
@IsEnum(PlanEnum)
|
||||||
|
plan!: PlanEnum;
|
||||||
|
|
||||||
@ApiProperty({ example: 'sub_1234567890', description: 'شناسه اشتراک' })
|
@ApiProperty({ example: 'sub_1234567890', description: 'شناسه اشتراک' })
|
||||||
@IsString()
|
@IsString()
|
||||||
subscriptionId!: string;
|
subscriptionId!: string;
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ import { RestMessage } from 'src/common/enums/message.enum';
|
|||||||
import { FindRestaurantsDto } from '../dto/find-restaurants.dto';
|
import { FindRestaurantsDto } from '../dto/find-restaurants.dto';
|
||||||
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
||||||
import { PlanEnum } from '../interface/plan.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()
|
@Injectable()
|
||||||
export class RestaurantsService {
|
export class RestaurantsService {
|
||||||
@@ -16,30 +19,63 @@ export class RestaurantsService {
|
|||||||
private readonly restRepository: RestRepository,
|
private readonly restRepository: RestRepository,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async create(dto: CreateRestaurantDto): Promise<Restaurant> {
|
async setupRestuarant(dto: CreateRestaurantDto): Promise<Restaurant> {
|
||||||
const validateSlug = await this.em.findOne(Restaurant, { slug: dto.slug })
|
return await this.em.transactional(async (em) => {
|
||||||
if (validateSlug) {
|
// Validate unique constraints
|
||||||
throw new BadRequestException("Duplicate slug: " + dto.slug)
|
const validateSlug = await em.findOne(Restaurant, { slug: dto.slug })
|
||||||
}
|
if (validateSlug) {
|
||||||
const validateSubscriptionId = await this.em.findOne(Restaurant, { subscriptionId: dto.subscriptionId })
|
throw new BadRequestException("Duplicate slug: " + dto.slug)
|
||||||
if (validateSubscriptionId) {
|
}
|
||||||
throw new BadRequestException("Duplicate subscriptionId: " + dto.subscriptionId)
|
const validateSubscriptionId = await em.findOne(Restaurant, { subscriptionId: dto.subscriptionId })
|
||||||
}
|
if (validateSubscriptionId) {
|
||||||
const restaurant = this.em.create(Restaurant, {
|
throw new BadRequestException("Duplicate subscriptionId: " + dto.subscriptionId)
|
||||||
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,
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.em.persistAndFlush(restaurant);
|
// Create restaurant
|
||||||
return 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>> {
|
async findAll(dto: FindRestaurantsDto): Promise<PaginatedResult<Restaurant>> {
|
||||||
|
|||||||
@@ -11,14 +11,14 @@ export const adminsData: AdminData[] = [
|
|||||||
phone: '09362532122',
|
phone: '09362532122',
|
||||||
firstName: 'مرتضی',
|
firstName: 'مرتضی',
|
||||||
lastName: 'مرتضایی',
|
lastName: 'مرتضایی',
|
||||||
roleName: 'restaurant',
|
roleName: 'مدیر ( پلن ویژه)',
|
||||||
restaurantSlug: 'zhivan',
|
restaurantSlug: 'zhivan',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
phone: '09185290775',
|
phone: '09185290775',
|
||||||
firstName: 'حمید',
|
firstName: 'حمید',
|
||||||
lastName: 'ضرقامی',
|
lastName: 'ضرقامی',
|
||||||
roleName: 'restaurant',
|
roleName: 'مدیر (پلن پایه)',
|
||||||
restaurantSlug: 'boote',
|
restaurantSlug: 'boote',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -32,7 +32,7 @@ export const adminsData: AdminData[] = [
|
|||||||
phone: '09184317567',
|
phone: '09184317567',
|
||||||
firstName: 'ادمین',
|
firstName: 'ادمین',
|
||||||
lastName: 'هنر',
|
lastName: 'هنر',
|
||||||
roleName: 'restaurant',
|
roleName: 'مدیر (پلن پایه)',
|
||||||
restaurantSlug: 'honar',
|
restaurantSlug: 'honar',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export interface RestaurantData {
|
|||||||
marriageDateScore: string;
|
marriageDateScore: string;
|
||||||
referrerScore: string;
|
referrerScore: string;
|
||||||
};
|
};
|
||||||
plan: PlanEnum.Premium;
|
plan: PlanEnum;
|
||||||
subscriptionId: string;
|
subscriptionId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@ export const restaurantsData: RestaurantData[] = [
|
|||||||
marriageDateScore: '10000',
|
marriageDateScore: '10000',
|
||||||
referrerScore: '10000',
|
referrerScore: '10000',
|
||||||
},
|
},
|
||||||
plan: PlanEnum.Premium,
|
plan: PlanEnum.Base,
|
||||||
subscriptionId: 'sub_seed_boote_001',
|
subscriptionId: 'sub_seed_boote_001',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -115,7 +115,7 @@ export const restaurantsData: RestaurantData[] = [
|
|||||||
marriageDateScore: '10000',
|
marriageDateScore: '10000',
|
||||||
referrerScore: '10000',
|
referrerScore: '10000',
|
||||||
},
|
},
|
||||||
plan: PlanEnum.Premium,
|
plan: PlanEnum.Base,
|
||||||
subscriptionId: 'sub_seed_honar_001',
|
subscriptionId: 'sub_seed_honar_001',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -8,12 +8,21 @@ export interface RoleConfig {
|
|||||||
|
|
||||||
export const rolesData: RoleConfig[] = [
|
export const rolesData: RoleConfig[] = [
|
||||||
{
|
{
|
||||||
name: 'restaurant',
|
name: 'مدیر (پلن پایه)',
|
||||||
|
isSystem: false,
|
||||||
|
permissionFilter: (name: Permission) =>
|
||||||
|
name === Permission.MANAGE_FOODS || name === Permission.MANAGE_CATEGORIES ||
|
||||||
|
name === Permission.MANAGE_SCHEDULES ||
|
||||||
|
name === Permission.MANAGE_PAGER ||
|
||||||
|
name === Permission.MANAGE_CONTACTS,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'مدیر ( پلن ویژه)',
|
||||||
isSystem: false,
|
isSystem: false,
|
||||||
permissionFilter: () => true, // All permissions for restaurant role
|
permissionFilter: () => true, // All permissions for restaurant role
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'accountant',
|
name: 'حسابدار ',
|
||||||
isSystem: false,
|
isSystem: false,
|
||||||
permissionFilter: (name: Permission) =>
|
permissionFilter: (name: Permission) =>
|
||||||
name === Permission.VIEW_REPORTS || name === Permission.MANAGE_PAYMENTS || name === Permission.MANAGE_ORDERS,
|
name === Permission.VIEW_REPORTS || name === Permission.MANAGE_PAYMENTS || name === Permission.MANAGE_ORDERS,
|
||||||
|
|||||||
Reference in New Issue
Block a user