feat: add new route for user me and quota

This commit is contained in:
mahyargdz
2025-07-12 12:42:01 +03:30
parent 97383d25f6
commit 7177a13e94
12 changed files with 91 additions and 36 deletions
+20 -5
View File
@@ -57,9 +57,24 @@ export class UsersService {
/*******************************/
async getMe(userId: string) {
const user = await this.userRepository.findOne({ id: userId }, { populate: ["role"] });
const user = await this.userRepository.findOne({ id: userId }, { exclude: ["password"], populate: ["role"] });
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
return { user };
return {
user,
quota: {
total: Number(user.emailQuota),
used: Number(user.emailQuotaUsed),
remaining: Number(user.emailQuota - user.emailQuotaUsed),
totalInMB: Math.round(Number(user.emailQuota) / (1024 * 1024)),
totalInGB: Math.round(Number(user.emailQuota) / (1024 * 1024 * 1024)),
usedInMB: Math.round(Number(user.emailQuotaUsed) / (1024 * 1024)),
usedInGB: Math.round(Number(user.emailQuotaUsed) / (1024 * 1024 * 1024)),
remainingInMB: Math.round(Number(user.emailQuota - user.emailQuotaUsed) / (1024 * 1024)),
remainingInGB: Math.round(Number(user.emailQuota - user.emailQuotaUsed) / (1024 * 1024 * 1024)),
usagePercentage: Math.round((Number(user.emailQuotaUsed) / Number(user.emailQuota)) * 100),
},
};
}
/*******************************/
@@ -77,7 +92,7 @@ export class UsersService {
if (!business) throw new BadRequestException(BusinessMessage.NOT_FOUND);
// Check if business has enough quota
const remainingQuota = business.remainingQuota || 0;
const remainingQuota = Number(business.remainingQuota || 0);
if (remainingQuota < QUOTA_CONSTANTS.USER_QUOTA_DEDUCTION) {
throw new BadRequestException("Insufficient quota to create user. Please upgrade your plan.");
}
@@ -129,8 +144,8 @@ export class UsersService {
});
// Update business quota after successful user creation
business.usedQuota = business.usedQuota + QUOTA_CONSTANTS.USER_QUOTA_DEDUCTION;
business.remainingQuota = business.quota - business.usedQuota;
business.usedQuota = Number(business.usedQuota) + QUOTA_CONSTANTS.USER_QUOTA_DEDUCTION;
business.remainingQuota = Number(business.quota) - Number(business.usedQuota);
await this.em.persistAndFlush([user, business]);