69 lines
2.4 KiB
TypeScript
69 lines
2.4 KiB
TypeScript
import { Inject, Injectable } from "@nestjs/common";
|
|
import { Account } from "../contracts/models/account";
|
|
import { Registration } from "../contracts/models/registration";
|
|
import * as mailVerificationService from "../contracts/services/mail.verification.service";
|
|
import { AccountService } from "./account.service";
|
|
import { AccountFactoryService } from "./account.factory.service";
|
|
|
|
@Injectable()
|
|
export class RegistrationService {
|
|
constructor(
|
|
@Inject(AccountService)
|
|
private readonly accountService: AccountService,
|
|
@Inject(AccountFactoryService)
|
|
private readonly accountFactory: AccountFactoryService,
|
|
@Inject("@apihub24/mail_verification_service")
|
|
private readonly mailVerificationService: mailVerificationService.MailVerificationService
|
|
) {}
|
|
|
|
async registerAccount(registration: Registration): Promise<Account | null> {
|
|
const newAccount = await this.accountFactory.createFromRegistration(
|
|
registration
|
|
);
|
|
return this.accountService.save(newAccount);
|
|
}
|
|
|
|
async verify(email: string, code: string): Promise<boolean> {
|
|
const verified = await this.mailVerificationService.verify(email, code);
|
|
if (!verified) {
|
|
return false;
|
|
}
|
|
const unverifiedAccounts = await this.accountService.getBy(
|
|
(x) => x.email === email && !x.emailVerified
|
|
);
|
|
for (const account of unverifiedAccounts) {
|
|
account.emailVerified = true;
|
|
await this.accountService.save(account);
|
|
}
|
|
return verified;
|
|
}
|
|
|
|
async activateAccount(accountId: string): Promise<boolean> {
|
|
const accounts = await this.accountService.getBy((x) => x.id === accountId);
|
|
if (!accounts?.length) {
|
|
return false;
|
|
}
|
|
accounts[0].active = true;
|
|
const savedAccount = await this.accountService.save(accounts[0]);
|
|
return savedAccount?.active === true;
|
|
}
|
|
|
|
async deactivateAccount(accountId: string): Promise<boolean> {
|
|
const accounts = await this.accountService.getBy((x) => x.id === accountId);
|
|
if (!accounts?.length) {
|
|
return true;
|
|
}
|
|
accounts[0].active = false;
|
|
const savedAccount = await this.accountService.save(accounts[0]);
|
|
return savedAccount?.active === false;
|
|
}
|
|
|
|
async unregisterAccount(accountId: string): Promise<void> {
|
|
const accounts = await this.accountService.getBy((x) => x.id === accountId);
|
|
if (!accounts?.length) {
|
|
return;
|
|
}
|
|
void this.accountService.delete((x) => x.id === accountId);
|
|
}
|
|
}
|