admin refactor
This commit is contained in:
@@ -10,12 +10,12 @@ import { CreateMyShopAdminDto } from '../dto/create-shop-admin.dto';
|
|||||||
import { UpdateAdminDto } from '../dto/update-admin.dto';
|
import { UpdateAdminDto } from '../dto/update-admin.dto';
|
||||||
import { AdminRole } from '../entities/adminRole.entity';
|
import { AdminRole } from '../entities/adminRole.entity';
|
||||||
import { normalizePhone } from '../../utils/phone.util';
|
import { normalizePhone } from '../../utils/phone.util';
|
||||||
|
import { AdminRepository } from '../repositories/admin.repository';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AdminService {
|
export class AdminService {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(Admin)
|
private readonly adminRepository:AdminRepository,
|
||||||
private readonly adminRepository: EntityRepository<Admin>,
|
|
||||||
@InjectRepository(AdminRole)
|
@InjectRepository(AdminRole)
|
||||||
private readonly adminRoleRepository: EntityRepository<AdminRole>,
|
private readonly adminRoleRepository: EntityRepository<AdminRole>,
|
||||||
private readonly em: EntityManager,
|
private readonly em: EntityManager,
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ export class AuthService {
|
|||||||
throw new BadRequestException(RestMessage.NOT_FOUND);
|
throw new BadRequestException(RestMessage.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
const tokens = await this.tokensService.generateAccessAndRefreshToken(user.id, shop.id, false, slug);
|
const tokens = await this.tokensService.generateTokens(user.id, shop.id, false, slug);
|
||||||
|
|
||||||
const userResponse = UserLoginTransformer.transform(user, shop);
|
const userResponse = UserLoginTransformer.transform(user, shop);
|
||||||
|
|
||||||
@@ -93,7 +93,7 @@ export class AuthService {
|
|||||||
throw new BadRequestException(RestMessage.NOT_FOUND);
|
throw new BadRequestException(RestMessage.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
const tokens = await this.tokensService.generateAccessAndRefreshToken(admin.id, shop.id, true, slug);
|
const tokens = await this.tokensService.generateTokens(admin.id, shop.id, true, slug);
|
||||||
|
|
||||||
const adminResponse = await AdminLoginTransformer.transform(admin, shop);
|
const adminResponse = await AdminLoginTransformer.transform(admin, shop);
|
||||||
|
|
||||||
@@ -117,7 +117,7 @@ export class AuthService {
|
|||||||
throw new BadRequestException(RestMessage.NOT_FOUND);
|
throw new BadRequestException(RestMessage.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
const tokens = await this.tokensService.generateAccessAndRefreshToken(admin.id, shop.id, true, slug);
|
const tokens = await this.tokensService.generateTokens(admin.id, shop.id, true, slug);
|
||||||
|
|
||||||
const adminResponse = await AdminLoginTransformer.transform(admin, shop);
|
const adminResponse = await AdminLoginTransformer.transform(admin, shop);
|
||||||
|
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ export class TokensService {
|
|||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
private readonly jwtService: JwtService,
|
private readonly jwtService: JwtService,
|
||||||
private readonly em: EntityManager,
|
private readonly em: EntityManager,
|
||||||
) {}
|
) { }
|
||||||
|
|
||||||
async generateAccessAndRefreshToken(
|
async generateTokens(
|
||||||
ownerId: string,
|
ownerId: string,
|
||||||
shopId: string,
|
shopId: string,
|
||||||
isAdmin: boolean,
|
isAdmin: boolean,
|
||||||
@@ -118,7 +118,7 @@ export class TokensService {
|
|||||||
try {
|
try {
|
||||||
// Generate new tokens first (before removing old token)
|
// Generate new tokens first (before removing old token)
|
||||||
// Pass the transaction EntityManager to ensure operations are within the transaction
|
// Pass the transaction EntityManager to ensure operations are within the transaction
|
||||||
const tokens = await this.generateAccessAndRefreshToken(ownerId, shop.id, isAdmin, shop.slug, em);
|
const tokens = await this.generateTokens(ownerId, shop.id, isAdmin, shop.slug, em);
|
||||||
|
|
||||||
// Remove old token only after new token is created
|
// Remove old token only after new token is created
|
||||||
em.remove(token);
|
em.remove(token);
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ export interface AdminLoginResponse {
|
|||||||
lastName?: string;
|
lastName?: string;
|
||||||
phone: string;
|
phone: string;
|
||||||
role: string;
|
role: string;
|
||||||
permissions: string[];
|
|
||||||
shop?: {
|
shop?: {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
@@ -32,13 +31,12 @@ export class AdminLoginTransformer {
|
|||||||
lastName: admin.lastName,
|
lastName: admin.lastName,
|
||||||
phone: admin.phone,
|
phone: admin.phone,
|
||||||
role: '',
|
role: '',
|
||||||
permissions: [],
|
|
||||||
shop: shop
|
shop: shop
|
||||||
? {
|
? {
|
||||||
id: shop.id,
|
id: shop.id,
|
||||||
name: shop.name,
|
name: shop.name,
|
||||||
slug: shop.slug,
|
slug: shop.slug,
|
||||||
}
|
}
|
||||||
: undefined,
|
: undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -52,27 +50,16 @@ export class AdminLoginTransformer {
|
|||||||
lastName: admin.lastName,
|
lastName: admin.lastName,
|
||||||
phone: admin.phone,
|
phone: admin.phone,
|
||||||
role: '',
|
role: '',
|
||||||
permissions: [],
|
|
||||||
shop: shop
|
shop: shop
|
||||||
? {
|
? {
|
||||||
id: shop.id,
|
id: shop.id,
|
||||||
name: shop.name,
|
name: shop.name,
|
||||||
slug: shop.slug,
|
slug: shop.slug,
|
||||||
}
|
}
|
||||||
: undefined,
|
: undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract permissions - ensure collection is loaded if needed
|
|
||||||
let permissions: string[] = [];
|
|
||||||
if (role.permissions) {
|
|
||||||
if (role.permissions.isInitialized()) {
|
|
||||||
permissions = role.permissions.getItems().map(p => p.name);
|
|
||||||
} else {
|
|
||||||
await role.permissions.loadItems();
|
|
||||||
permissions = role.permissions.getItems().map(p => p.name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: admin.id,
|
id: admin.id,
|
||||||
@@ -80,13 +67,12 @@ export class AdminLoginTransformer {
|
|||||||
lastName: admin.lastName,
|
lastName: admin.lastName,
|
||||||
phone: admin.phone,
|
phone: admin.phone,
|
||||||
role: role.name,
|
role: role.name,
|
||||||
permissions,
|
|
||||||
shop: shop
|
shop: shop
|
||||||
? {
|
? {
|
||||||
id: shop.id,
|
id: shop.id,
|
||||||
name: shop.name,
|
name: shop.name,
|
||||||
slug: shop.slug,
|
slug: shop.slug,
|
||||||
}
|
}
|
||||||
: undefined,
|
: undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user