-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-vault.usecase.ts
More file actions
40 lines (38 loc) · 1.64 KB
/
create-vault.usecase.ts
File metadata and controls
40 lines (38 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { inject, injectable } from 'tsyringe';
import type { VaultModelDto } from '@shared/dto/models/vault.model.dto';
import { IUseCaseWithInput } from '@api/usecases/abstract/usecase.with-input.interface';
import { Vault } from '@prisma/generated';
import { VaultAdapter } from '@api/adapters/vault.adapter';
import { VaultsRepository } from '@api/repositories/vaults.repository';
import { CreateVaultRequestDto } from '@shared/dto/input/requests/create-vault.request.dto';
import { VaultAlreadyExistsError } from '@api/errors/business/vaults/vault-already-exists.error';
import { RequestedValueTooLongError } from '@api/errors/http/prisma/requested-value-too-long.error';
import { VaultLabelTooLongError } from '@api/errors/business/vaults/vault-label-too-long.error';
@injectable()
export class CreateVaultUseCase
implements IUseCaseWithInput<CreateVaultRequestDto, VaultModelDto>
{
public constructor(
@inject(VaultsRepository)
private readonly _vaultsRepository: VaultsRepository,
@inject(VaultAdapter)
private readonly _vaultAdapter: VaultAdapter
) {}
public async handle(input: CreateVaultRequestDto): Promise<VaultModelDto> {
const vaultsFound: number = await this._vaultsRepository.countByLabel(
input.label
);
if (vaultsFound > 0) {
throw new VaultAlreadyExistsError(input.label);
}
let vaultCreated: Vault;
try {
vaultCreated = await this._vaultsRepository.create(input);
} catch (error: unknown) {
if (error instanceof RequestedValueTooLongError)
throw new VaultLabelTooLongError();
throw error;
}
return this._vaultAdapter.getDtoFromEntity(vaultCreated);
}
}