setup restuarant

This commit is contained in:
2025-12-27 10:40:15 +03:30
parent 21da5d52f8
commit c8e00f9e1a
6 changed files with 83 additions and 34 deletions
@@ -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>> {