chore: add new logic for the quota
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import { EntityManager } from "@mikro-orm/core";
|
||||
import { Injectable, Logger } from "@nestjs/common";
|
||||
import { BadRequestException, Injectable, Logger } from "@nestjs/common";
|
||||
import { firstValueFrom } from "rxjs";
|
||||
|
||||
import { BusinessMessage, MailServerMessage, RoleMessage } from "../../../common/enums/message.enum";
|
||||
import { QUOTA_CONSTANTS } from "../../businesses/constant";
|
||||
import { MailServerService } from "../../mail-server/services/mail-server.service";
|
||||
import { MailboxEnum } from "../../mailbox/enums/mailbox.enum";
|
||||
import { Role } from "../../users/entities/role.entity";
|
||||
@@ -114,9 +116,7 @@ export class DomainAutomationService {
|
||||
|
||||
// Get user role
|
||||
const userRole = await this.em.findOne(Role, { name: RoleEnum.USER });
|
||||
if (!userRole) {
|
||||
throw new Error("User role not found in database");
|
||||
}
|
||||
if (!userRole) throw new BadRequestException(RoleMessage.NOT_FOUND);
|
||||
|
||||
// Create user in mail server (WildDuck)
|
||||
const mailServerUser = await firstValueFrom(
|
||||
@@ -125,14 +125,15 @@ export class DomainAutomationService {
|
||||
password,
|
||||
address: infoEmail,
|
||||
name: `Info - ${domain.business.name}`,
|
||||
quota: 1073741824, // 1GB default quota
|
||||
language: "fa",
|
||||
quota: QUOTA_CONSTANTS.DEFAULT_EMAIL_QUOTA_BYTES,
|
||||
}),
|
||||
);
|
||||
|
||||
if (!mailServerUser.success) {
|
||||
throw new Error(`Failed to create mail server user`);
|
||||
}
|
||||
if (!mailServerUser.success) throw new BadRequestException(MailServerMessage.FAILED_TO_CREATE_ACCOUNT);
|
||||
|
||||
// Check if business has enough quota
|
||||
const remainingQuota = domain.business.remainingQuota || 0;
|
||||
if (remainingQuota < QUOTA_CONSTANTS.USER_QUOTA_DEDUCTION) throw new BadRequestException(BusinessMessage.INSUFFICIENT_QUOTA);
|
||||
|
||||
// Create user in local database
|
||||
const user = this.em.create(User, {
|
||||
@@ -141,7 +142,7 @@ export class DomainAutomationService {
|
||||
password: hashedPassword,
|
||||
emailAddress: infoEmail,
|
||||
emailEnabled: true,
|
||||
emailQuota: 1073741824, // 1GB default
|
||||
emailQuota: QUOTA_CONSTANTS.DEFAULT_EMAIL_QUOTA_BYTES,
|
||||
wildduckUserId: mailServerUser.id,
|
||||
displayName: `Info - ${domain.business.name}`,
|
||||
isActive: true,
|
||||
@@ -150,7 +151,11 @@ export class DomainAutomationService {
|
||||
role: userRole,
|
||||
});
|
||||
|
||||
await this.em.persistAndFlush(user);
|
||||
// Update business quota after successful user creation
|
||||
domain.business.usedQuota = (domain.business.usedQuota || 0) + QUOTA_CONSTANTS.USER_QUOTA_DEDUCTION;
|
||||
domain.business.remainingQuota = (domain.business.quota || 0) - domain.business.usedQuota;
|
||||
|
||||
await this.em.persistAndFlush([user, domain.business]);
|
||||
|
||||
// Create default mailboxes (Archive and Favorite)
|
||||
await this.createDefaultMailboxes(mailServerUser.id);
|
||||
|
||||
Reference in New Issue
Block a user