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;
}
@Post()
@HttpCode(HttpStatus.CREATED)
@ApiOperation({ summary: 'Create a new admin' })
@ApiBody({ type: CreateAdminDto })
@ApiResponse({ status: 201, description: 'Admin created' })
async create(@Body() dto: CreateAdminDto, @RestId() restId: string) {
const admin = await this.adminService.createRestaurantBySuperAdmin({
phone: dto.phone,
firstName: dto.firstName,
lastName: dto.lastName,
roleId: dto.roleId,
restId,
});
return admin;
}
// @Post()
// @HttpCode(HttpStatus.CREATED)
// @ApiOperation({ summary: 'Create a new admin' })
// @ApiBody({ type: CreateAdminDto })
// @ApiResponse({ status: 201, description: 'Admin created' })
// async create(@Body() dto: CreateAdminDto, @RestId() restId: string) {
// const admin = await this.adminService.createRestaurantBySuperAdmin({
// phone: dto.phone,
// firstName: dto.firstName,
// lastName: dto.lastName,
// roleId: dto.roleId,
// restId,
// });
// return admin;
// }
@Patch(':id')
@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> {
const admin = await this.adminRepository.findOne(
{ id: adminId, roles: { restaurant: { id: restId } } },
{ populate: ['roles', 'roles.role'] },
{ populate: ['roles', 'roles.role'], distinct: true },
);
return admin;
}
@@ -39,54 +39,69 @@ export class AdminService {
async createAdminForMyRestaurant(restId: string, dto: CreateMyRestaurantAdminDto) {
const { phone, firstName, lastName, roleId } = dto;
let currentAdmin: Admin | null = null;
currentAdmin = await this.adminRepository.findOne({
let admin: Admin | null = null;
admin = await this.adminRepository.findOne({
phone,
});
if (!currentAdmin) {
currentAdmin = await this.adminRepository.findOne({
});
if (!admin) {
admin = this.adminRepository.create({
phone,
firstName,
lastName,
});
await this.em.persistAndFlush(admin);
}
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 });
if (!restaurant) throw new Error('Restaurant not found');
const admin = this.adminRepository.create({ phone, firstName, lastName, roles: [{ role, restaurant }] });
if (!restaurant) throw new NotFoundException('Restaurant not found');
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;
}
async createRestaurantBySuperAdmin(data: {
phone: string;
firstName?: string;
lastName?: string;
roleId: string;
restId: string;
}) {
const { phone, firstName, lastName, roleId, restId } = data;
// async createRestaurantBySuperAdmin(data: {
// phone: string;
// firstName?: string;
// lastName?: string;
// roleId: string;
// restId: string;
// }) {
// const { phone, firstName, lastName, roleId, restId } = data;
// check existing
const existing = await this.adminRepository.findOne({ phone, roles: { restaurant: { id: restId } } });
if (existing) {
return existing;
}
// // check existing
// const existing = await this.adminRepository.findOne({ phone, roles: { restaurant: { id: restId } } });
// if (existing) {
// return existing;
// }
// load Role and Restaurant using the EntityManager
const role = await this.em.findOne(Role, { id: roleId });
if (!role) throw new Error('Role 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');
const restaurant = await this.em.findOne(Restaurant, { id: restId });
if (!restaurant) throw new Error('Restaurant not found');
// const restaurant = await this.em.findOne(Restaurant, { id: restId });
// if (!restaurant) throw new Error('Restaurant not found');
const admin = this.adminRepository.create({ phone, firstName, lastName, roles: [{ role, restaurant }] });
await this.em.persistAndFlush(admin);
return admin;
}
// const admin = this.adminRepository.create({ phone, firstName, lastName, roles: [{ role, restaurant }] });
// await this.em.persistAndFlush(admin);
// return admin;
// }
async update(adminId: string, restId: string, dto: UpdateAdminDto): Promise<Admin> {
const admin = await this.adminRepository.findOne(
@@ -110,7 +125,7 @@ export class AdminService {
if (rest.phone !== undefined && rest.phone !== admin.phone) {
const exists = await this.adminRepository.findOne({ phone: rest.phone });
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;
}
@@ -122,11 +137,6 @@ export class AdminService {
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
const existingAdminRole = await this.em.findOne(AdminRole, {
admin: { id: adminId },