Add AdminId decorator for extracting adminId from requests; refactor AdminController to utilize new decorator for fetching admin details and restaurant-specific admins; update AdminService to handle admin creation and retrieval by restaurant; remove unused role and permission entities for cleaner architecture.

This commit is contained in:
2025-11-18 16:45:44 +03:30
parent 9abab789fc
commit 8a217f6034
18 changed files with 254 additions and 206 deletions
@@ -17,17 +17,60 @@ export interface AdminLoginResponse {
export class AdminLoginTransformer {
static async transform(admin: Admin, restaurant?: Restaurant): Promise<AdminLoginResponse> {
// Extract role name
const role = admin.role?.name || '';
// Find the AdminRole that matches the restaurant (or get the first one)
let adminRole = admin.roles.getItems().find(r => (restaurant ? r.restaurant?.id === restaurant.id : true));
// If no match found, get the first one
if (!adminRole && admin.roles.getItems().length > 0) {
adminRole = admin.roles.getItems()[0];
}
if (!adminRole) {
return {
id: admin.id,
firstName: admin.firstName,
lastName: admin.lastName,
phone: admin.phone,
role: '',
permissions: [],
restaurant: restaurant
? {
id: restaurant.id,
name: restaurant.name,
slug: restaurant.slug,
}
: undefined,
};
}
// Get the role from AdminRole
const role = adminRole.role;
if (!role) {
return {
id: admin.id,
firstName: admin.firstName,
lastName: admin.lastName,
phone: admin.phone,
role: '',
permissions: [],
restaurant: restaurant
? {
id: restaurant.id,
name: restaurant.name,
slug: restaurant.slug,
}
: undefined,
};
}
// Extract permissions - ensure collection is loaded if needed
let permissions: string[] = [];
if (admin.role?.permissions) {
if (admin.role.permissions.isInitialized()) {
permissions = admin.role.permissions.getItems().map(p => p.name);
if (role.permissions) {
if (role.permissions.isInitialized()) {
permissions = role.permissions.getItems().map(p => p.name);
} else {
await admin.role.permissions.loadItems();
permissions = admin.role.permissions.getItems().map(p => p.name);
await role.permissions.loadItems();
permissions = role.permissions.getItems().map(p => p.name);
}
}
@@ -36,7 +79,7 @@ export class AdminLoginTransformer {
firstName: admin.firstName,
lastName: admin.lastName,
phone: admin.phone,
role,
role: role.name,
permissions,
restaurant: restaurant
? {