|
| 1 | +import { |
| 2 | + ConflictException, |
| 3 | + Injectable, |
| 4 | + Logger, |
| 5 | + NotFoundException, |
| 6 | + UnprocessableEntityException, |
| 7 | +} from '@nestjs/common'; |
| 8 | +import { AccountProvider } from './account-provider.port.js'; |
| 9 | +import { MailAccount } from './domain/mail-account.domain.js'; |
| 10 | +import { AccountRepository } from './repositories/account.repository.js'; |
| 11 | +import { AddressRepository } from './repositories/address.repository.js'; |
| 12 | +import { DomainRepository } from './repositories/domain.repository.js'; |
| 13 | + |
| 14 | +@Injectable() |
| 15 | +export class AccountService { |
| 16 | + private readonly logger = new Logger(AccountService.name); |
| 17 | + |
| 18 | + constructor( |
| 19 | + private readonly provider: AccountProvider, |
| 20 | + private readonly accounts: AccountRepository, |
| 21 | + private readonly addresses: AddressRepository, |
| 22 | + private readonly domains: DomainRepository, |
| 23 | + ) {} |
| 24 | + |
| 25 | + async getAccount(driveUserUuid: string): Promise<MailAccount> { |
| 26 | + return this.getAccountOrFail(driveUserUuid); |
| 27 | + } |
| 28 | + |
| 29 | + async deleteAccount(driveUserUuid: string): Promise<void> { |
| 30 | + const account = await this.getAccountOrFail(driveUserUuid); |
| 31 | + |
| 32 | + if (account.principalName) { |
| 33 | + await this.provider.deleteAccount(account.principalName); |
| 34 | + } |
| 35 | + |
| 36 | + await this.accounts.delete(account.id); |
| 37 | + this.logger.log(`Deleted account for drive user '${driveUserUuid}'`); |
| 38 | + } |
| 39 | + |
| 40 | + async addAddress( |
| 41 | + driveUserUuid: string, |
| 42 | + address: string, |
| 43 | + domainName: string, |
| 44 | + ): Promise<void> { |
| 45 | + const [account, domain, existing] = await Promise.all([ |
| 46 | + this.accounts.findByDriveUserUuid(driveUserUuid), |
| 47 | + this.domains.findByDomain(domainName), |
| 48 | + this.addresses.findByAddress(address), |
| 49 | + ]); |
| 50 | + |
| 51 | + if (!account) { |
| 52 | + throw new NotFoundException( |
| 53 | + `No mail account for drive user '${driveUserUuid}'`, |
| 54 | + ); |
| 55 | + } |
| 56 | + const principalName = this.requirePrincipalName(account); |
| 57 | + |
| 58 | + if (!domain) { |
| 59 | + throw new NotFoundException(`Domain '${domainName}' not found`); |
| 60 | + } |
| 61 | + if (existing) { |
| 62 | + throw new ConflictException(`Address '${address}' already exists`); |
| 63 | + } |
| 64 | + |
| 65 | + const newAddress = await this.addresses.create({ |
| 66 | + mailAccountId: account.id, |
| 67 | + address, |
| 68 | + domainId: domain.id, |
| 69 | + isDefault: false, |
| 70 | + }); |
| 71 | + |
| 72 | + try { |
| 73 | + await this.provider.addAddress(principalName, address); |
| 74 | + } catch (error) { |
| 75 | + await this.addresses.delete(newAddress.id); |
| 76 | + throw error; |
| 77 | + } |
| 78 | + |
| 79 | + await this.addresses.createProviderLink({ |
| 80 | + mailAddressId: newAddress.id, |
| 81 | + provider: 'stalwart', |
| 82 | + externalId: principalName, |
| 83 | + }); |
| 84 | + |
| 85 | + this.logger.log(`Added address '${address}' to account '${driveUserUuid}'`); |
| 86 | + } |
| 87 | + |
| 88 | + async removeAddress(driveUserUuid: string, address: string): Promise<void> { |
| 89 | + const account = await this.getAccountOrFail(driveUserUuid); |
| 90 | + |
| 91 | + const addressRecord = account.addresses.find((a) => a.address === address); |
| 92 | + if (!addressRecord) { |
| 93 | + throw new NotFoundException( |
| 94 | + `Address '${address}' not found for this account`, |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + if (addressRecord.isDefault) { |
| 99 | + throw new UnprocessableEntityException( |
| 100 | + 'Cannot remove the default address', |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + const principalName = this.requirePrincipalName(account); |
| 105 | + |
| 106 | + await this.provider.removeAddress(principalName, address); |
| 107 | + await Promise.all([ |
| 108 | + this.addresses.deleteProviderLink(addressRecord.id), |
| 109 | + this.addresses.delete(addressRecord.id), |
| 110 | + ]); |
| 111 | + |
| 112 | + this.logger.log( |
| 113 | + `Removed address '${address}' from account '${driveUserUuid}'`, |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + async setPrimaryAddress( |
| 118 | + driveUserUuid: string, |
| 119 | + newAddress: string, |
| 120 | + ): Promise<void> { |
| 121 | + const account = await this.getAccountOrFail(driveUserUuid); |
| 122 | + |
| 123 | + const addressRecord = account.addresses.find( |
| 124 | + (a) => a.address === newAddress, |
| 125 | + ); |
| 126 | + if (!addressRecord) { |
| 127 | + throw new NotFoundException( |
| 128 | + `Address '${newAddress}' not found for this account`, |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + if (addressRecord.isDefault) return; |
| 133 | + |
| 134 | + const oldPrincipalName = this.requirePrincipalName(account); |
| 135 | + |
| 136 | + await this.provider.setPrimaryAddress(oldPrincipalName, newAddress); |
| 137 | + |
| 138 | + await Promise.all([ |
| 139 | + this.addresses.setDefault(addressRecord.id, account.id), |
| 140 | + this.addresses.updateAllProviderExternalIds(account.id, newAddress), |
| 141 | + ]); |
| 142 | + |
| 143 | + this.logger.log( |
| 144 | + `Set primary address to '${newAddress}' for account '${driveUserUuid}'`, |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + private async getAccountOrFail(driveUserUuid: string): Promise<MailAccount> { |
| 149 | + const account = await this.accounts.findByDriveUserUuid(driveUserUuid); |
| 150 | + if (!account) { |
| 151 | + throw new NotFoundException( |
| 152 | + `No mail account for drive user '${driveUserUuid}'`, |
| 153 | + ); |
| 154 | + } |
| 155 | + return account; |
| 156 | + } |
| 157 | + |
| 158 | + private requirePrincipalName(account: MailAccount): string { |
| 159 | + const name = account.principalName; |
| 160 | + if (!name) { |
| 161 | + throw new UnprocessableEntityException( |
| 162 | + 'Account has no primary address with a provider link', |
| 163 | + ); |
| 164 | + } |
| 165 | + return name; |
| 166 | + } |
| 167 | +} |
0 commit comments