feat: add new route for user me and quota
This commit is contained in:
@@ -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]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user