get admins, get system roles,

This commit is contained in:
2025-12-28 10:26:24 +03:30
parent 4da1680b29
commit 9b409f3e07
5 changed files with 139 additions and 37 deletions
+36 -23
View File
@@ -20,7 +20,7 @@ export class AdminService {
private readonly adminRoleRepository: EntityRepository<AdminRole>,
private readonly em: EntityManager,
private readonly cacheService: CacheService,
) {}
) { }
async findByPhone(phone: string): Promise<Admin | null> {
const normalizedPhone = normalizePhone(phone);
@@ -79,32 +79,45 @@ export class AdminService {
return admin;
}
// async createRestaurantBySuperAdmin(data: {
// phone: string;
// firstName?: string;
// lastName?: string;
// roleId: string;
// restId: string;
// }) {
// const { phone, firstName, lastName, roleId, restId } = data;
async createAdminForRestaurantBySuperAdmin(restId: string, dto: CreateMyRestaurantAdminDto) {
const { phone, firstName, lastName, roleId } = dto;
const normalizedPhone = normalizePhone(phone);
let admin: Admin | null = null;
admin = await this.adminRepository.findOne({
phone: normalizedPhone,
});
if (!admin) {
admin = this.adminRepository.create({
phone: normalizedPhone,
firstName,
lastName,
});
await this.em.persistAndFlush(admin);
}
const role = await this.em.findOne(Role, { id: roleId, isSystem: false });
if (!role) throw new NotFoundException('Role not found');
// // check existing
// const existing = await this.adminRepository.findOne({ phone, roles: { restaurant: { id: restId } } });
// if (existing) {
// return existing;
// }
const restaurant = await this.em.findOne(Restaurant, { id: restId });
if (!restaurant) throw new NotFoundException('Restaurant not found');
// // load Role and Restaurant using the EntityManager
// const role = await this.em.findOne(Role, { id: roleId });
// if (!role) throw new Error('Role not found');
let adminRole = await this.adminRoleRepository.findOne({
admin: admin,
restaurant: restaurant,
});
// const restaurant = await this.em.findOne(Restaurant, { id: restId });
// if (!restaurant) throw new Error('Restaurant not found');
if (!adminRole) {
adminRole = this.adminRoleRepository.create({
admin: admin,
role,
restaurant,
});
} else {
adminRole.role = role;
}
// const admin = this.adminRepository.create({ phone, firstName, lastName, roles: [{ role, restaurant }] });
// await this.em.persistAndFlush(admin);
// return admin;
// }
await this.em.persistAndFlush(adminRole);
return admin;
}
async update(adminId: string, restId: string, dto: UpdateAdminDto): Promise<Admin> {
const admin = await this.adminRepository.findOne(