77 lines
2.5 KiB
TypeScript
77 lines
2.5 KiB
TypeScript
import { UniqueID } from "@repo/rdx-ddd";
|
|
import { Result } from "@repo/rdx-utils";
|
|
|
|
import { SupportConnection } from "../domain/support-connection.aggregate";
|
|
import type { SupportInput } from "../domain/support-values";
|
|
import { SupportError } from "../domain/support-values";
|
|
|
|
import type { ISupportRepository } from "./support.repository";
|
|
import { mapSupportInput } from "./support-input.mapper";
|
|
|
|
export class SupportUseCases {
|
|
constructor(private readonly repository: ISupportRepository) {}
|
|
|
|
get(companyId: string, assetId: string) {
|
|
const company = UniqueID.create(companyId);
|
|
const asset = UniqueID.create(assetId);
|
|
|
|
if (!(company.isSuccess && asset.isSuccess)) {
|
|
return Promise.resolve(Result.fail(new SupportError("INVALID_REQUEST")));
|
|
}
|
|
|
|
return this.repository.get(company.data, asset.data);
|
|
}
|
|
|
|
remove(companyId: string, assetId: string, id: string) {
|
|
const company = UniqueID.create(companyId);
|
|
const asset = UniqueID.create(assetId);
|
|
const identity = UniqueID.create(id);
|
|
|
|
if (!(company.isSuccess && asset.isSuccess && identity.isSuccess)) {
|
|
return Promise.resolve(Result.fail(new SupportError("INVALID_REQUEST")));
|
|
}
|
|
|
|
return this.repository.remove(company.data, asset.data, identity.data);
|
|
}
|
|
|
|
save(
|
|
companyId: string,
|
|
assetId: string,
|
|
id: string | undefined,
|
|
input: SupportInput,
|
|
actorId: string
|
|
) {
|
|
const company = UniqueID.create(companyId);
|
|
const asset = UniqueID.create(assetId);
|
|
const actor = UniqueID.create(actorId);
|
|
const identity = id ? UniqueID.create(id) : undefined;
|
|
|
|
if (
|
|
!(company.isSuccess && asset.isSuccess && actor.isSuccess) ||
|
|
(identity && !identity.isSuccess)
|
|
) {
|
|
return Promise.resolve(Result.fail(new SupportError("INVALID_REQUEST")));
|
|
}
|
|
|
|
return this.repository.save(company.data, asset.data, (context) => {
|
|
const previous = identity?.isSuccess
|
|
? context.connections.find((item) => item.id.equals(identity.data))
|
|
: undefined;
|
|
|
|
if (id && !previous) {
|
|
return Result.fail(new SupportError("CONNECTION_NOT_FOUND"));
|
|
}
|
|
const values = mapSupportInput(input, context, previous);
|
|
|
|
if (!values.isSuccess) {
|
|
return Result.fail(values.error);
|
|
}
|
|
const now = new Date().toISOString();
|
|
|
|
return previous
|
|
? previous.update(values.data, actor.data, now)
|
|
: SupportConnection.create(company.data, asset.data, values.data, actor.data, now);
|
|
});
|
|
}
|
|
}
|