fix bug in admin

This commit is contained in:
2025-11-24 11:34:54 +03:30
parent 72c93e637c
commit e79040d81e
2 changed files with 63 additions and 53 deletions
@@ -31,21 +31,21 @@ export class AdminController {
return admin; return admin;
} }
@Post() // @Post()
@HttpCode(HttpStatus.CREATED) // @HttpCode(HttpStatus.CREATED)
@ApiOperation({ summary: 'Create a new admin' }) // @ApiOperation({ summary: 'Create a new admin' })
@ApiBody({ type: CreateAdminDto }) // @ApiBody({ type: CreateAdminDto })
@ApiResponse({ status: 201, description: 'Admin created' }) // @ApiResponse({ status: 201, description: 'Admin created' })
async create(@Body() dto: CreateAdminDto, @RestId() restId: string) { // async create(@Body() dto: CreateAdminDto, @RestId() restId: string) {
const admin = await this.adminService.createRestaurantBySuperAdmin({ // const admin = await this.adminService.createRestaurantBySuperAdmin({
phone: dto.phone, // phone: dto.phone,
firstName: dto.firstName, // firstName: dto.firstName,
lastName: dto.lastName, // lastName: dto.lastName,
roleId: dto.roleId, // roleId: dto.roleId,
restId, // restId,
}); // });
return admin; // return admin;
} // }
@Patch(':id') @Patch(':id')
@ApiOperation({ summary: 'Update an admin' }) @ApiOperation({ summary: 'Update an admin' })
+48 -38
View File
@@ -28,7 +28,7 @@ export class AdminService {
async findById(adminId: string, restId: string): Promise<Admin | null> { async findById(adminId: string, restId: string): Promise<Admin | null> {
const admin = await this.adminRepository.findOne( const admin = await this.adminRepository.findOne(
{ id: adminId, roles: { restaurant: { id: restId } } }, { id: adminId, roles: { restaurant: { id: restId } } },
{ populate: ['roles', 'roles.role'] }, { populate: ['roles', 'roles.role'], distinct: true },
); );
return admin; return admin;
} }
@@ -39,54 +39,69 @@ export class AdminService {
async createAdminForMyRestaurant(restId: string, dto: CreateMyRestaurantAdminDto) { async createAdminForMyRestaurant(restId: string, dto: CreateMyRestaurantAdminDto) {
const { phone, firstName, lastName, roleId } = dto; const { phone, firstName, lastName, roleId } = dto;
let currentAdmin: Admin | null = null; let admin: Admin | null = null;
currentAdmin = await this.adminRepository.findOne({ admin = await this.adminRepository.findOne({
phone, phone,
}); });
if (!currentAdmin) { if (!admin) {
currentAdmin = await this.adminRepository.findOne({ admin = this.adminRepository.create({
phone, phone,
firstName, firstName,
lastName, lastName,
}); });
await this.em.persistAndFlush(admin);
} }
const role = await this.em.findOne(Role, { id: roleId }); const role = await this.em.findOne(Role, { id: roleId });
if (!role) throw new Error('Role not found'); if (!role) throw new NotFoundException('Role not found');
const restaurant = await this.em.findOne(Restaurant, { id: restId }); const restaurant = await this.em.findOne(Restaurant, { id: restId });
if (!restaurant) throw new Error('Restaurant not found'); if (!restaurant) throw new NotFoundException('Restaurant not found');
const admin = this.adminRepository.create({ phone, firstName, lastName, roles: [{ role, restaurant }] });
await this.em.persistAndFlush(admin); let adminRole = await this.adminRoleRepository.findOne({
admin: admin,
restaurant: restaurant,
});
if (!adminRole) {
adminRole = this.adminRoleRepository.create({
admin: admin,
role,
restaurant,
});
} else {
adminRole.role = role;
}
await this.em.persistAndFlush(adminRole);
return admin; return admin;
} }
async createRestaurantBySuperAdmin(data: { // async createRestaurantBySuperAdmin(data: {
phone: string; // phone: string;
firstName?: string; // firstName?: string;
lastName?: string; // lastName?: string;
roleId: string; // roleId: string;
restId: string; // restId: string;
}) { // }) {
const { phone, firstName, lastName, roleId, restId } = data; // const { phone, firstName, lastName, roleId, restId } = data;
// check existing // // check existing
const existing = await this.adminRepository.findOne({ phone, roles: { restaurant: { id: restId } } }); // const existing = await this.adminRepository.findOne({ phone, roles: { restaurant: { id: restId } } });
if (existing) { // if (existing) {
return existing; // return existing;
} // }
// load Role and Restaurant using the EntityManager // // load Role and Restaurant using the EntityManager
const role = await this.em.findOne(Role, { id: roleId }); // const role = await this.em.findOne(Role, { id: roleId });
if (!role) throw new Error('Role not found'); // if (!role) throw new Error('Role not found');
const restaurant = await this.em.findOne(Restaurant, { id: restId }); // const restaurant = await this.em.findOne(Restaurant, { id: restId });
if (!restaurant) throw new Error('Restaurant not found'); // if (!restaurant) throw new Error('Restaurant not found');
const admin = this.adminRepository.create({ phone, firstName, lastName, roles: [{ role, restaurant }] }); // const admin = this.adminRepository.create({ phone, firstName, lastName, roles: [{ role, restaurant }] });
await this.em.persistAndFlush(admin); // await this.em.persistAndFlush(admin);
return admin; // return admin;
} // }
async update(adminId: string, restId: string, dto: UpdateAdminDto): Promise<Admin> { async update(adminId: string, restId: string, dto: UpdateAdminDto): Promise<Admin> {
const admin = await this.adminRepository.findOne( const admin = await this.adminRepository.findOne(
@@ -110,7 +125,7 @@ export class AdminService {
if (rest.phone !== undefined && rest.phone !== admin.phone) { if (rest.phone !== undefined && rest.phone !== admin.phone) {
const exists = await this.adminRepository.findOne({ phone: rest.phone }); const exists = await this.adminRepository.findOne({ phone: rest.phone });
if (exists) { if (exists) {
throw new ConflictException('This Phone Number is already Admin for this restaurant'); throw new ConflictException('This Phone Number is already taken');
} }
admin.phone = rest.phone; admin.phone = rest.phone;
} }
@@ -122,11 +137,6 @@ export class AdminService {
throw new NotFoundException('Role not found'); throw new NotFoundException('Role not found');
} }
const restaurant = await this.em.findOne(Restaurant, { id: restId });
if (!restaurant) {
throw new NotFoundException('Restaurant not found');
}
// Find existing AdminRole for this admin and restaurant // Find existing AdminRole for this admin and restaurant
const existingAdminRole = await this.em.findOne(AdminRole, { const existingAdminRole = await this.em.findOne(AdminRole, {
admin: { id: adminId }, admin: { id: adminId },