2026-10-07 08:16:39 +00:00
|
|
|
import { UniqueID } from "@repo/rdx-ddd";
|
|
|
|
|
import { Result } from "@repo/rdx-utils";
|
|
|
|
|
|
|
|
|
|
import { SupportConnection } from "../domain/support-connection.aggregate";
|
|
|
|
|
import { SupportTool } from "../domain/support-tool.aggregate";
|
2026-10-08 14:43:34 +00:00
|
|
|
import type { SupportInput, SupportToolInput } from "../domain/support-values";
|
|
|
|
|
import { SupportError } from "../domain/support-values";
|
2026-10-07 08:16:39 +00:00
|
|
|
|
|
|
|
|
import type { ISupportRepository } from "./support.repository";
|
|
|
|
|
import { mapSupportInput } from "./support-input.mapper";
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
export class SupportUseCases {
|
|
|
|
|
constructor(private readonly repository: ISupportRepository) {}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
get(companyId: string, assetId: string) {
|
|
|
|
|
const company = UniqueID.create(companyId);
|
|
|
|
|
const asset = UniqueID.create(assetId);
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
if (!(company.isSuccess && asset.isSuccess)) {
|
|
|
|
|
return Promise.resolve(Result.fail(new SupportError("INVALID_REQUEST")));
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
return this.repository.get(company.data, asset.data);
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-09 22:19:35 +00:00
|
|
|
remove(companyId: string, assetId: string, id: string) {
|
|
|
|
|
const company = UniqueID.create(companyId);
|
|
|
|
|
const asset = UniqueID.create(assetId);
|
|
|
|
|
const identity = UniqueID.create(id);
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-09 22:19:35 +00:00
|
|
|
if (!(company.isSuccess && asset.isSuccess && identity.isSuccess)) {
|
|
|
|
|
return Promise.resolve(Result.fail(new SupportError("INVALID_REQUEST")));
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-09 22:19:35 +00:00
|
|
|
return this.repository.remove(company.data, asset.data, identity.data);
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
tools() {
|
|
|
|
|
return this.repository.tools();
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
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;
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
if (
|
|
|
|
|
!(company.isSuccess && asset.isSuccess && actor.isSuccess) ||
|
|
|
|
|
(identity && !identity.isSuccess)
|
|
|
|
|
) {
|
|
|
|
|
return Promise.resolve(Result.fail(new SupportError("INVALID_REQUEST")));
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
return this.repository.save(company.data, asset.data, (context) => {
|
|
|
|
|
const previous = identity?.isSuccess
|
|
|
|
|
? context.connections.find((item) => item.id.equals(identity.data))
|
|
|
|
|
: undefined;
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
if (id && !previous) {
|
|
|
|
|
return Result.fail(new SupportError("CONNECTION_NOT_FOUND"));
|
|
|
|
|
}
|
|
|
|
|
const values = mapSupportInput(input, context, previous);
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
if (!values.isSuccess) {
|
|
|
|
|
return Result.fail(values.error);
|
|
|
|
|
}
|
|
|
|
|
const now = new Date().toISOString();
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
return previous
|
|
|
|
|
? previous.update(values.data, actor.data, now)
|
|
|
|
|
: SupportConnection.create(company.data, asset.data, values.data, actor.data, now);
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
saveTool(id: string | undefined, input: SupportToolInput) {
|
|
|
|
|
const identity = id ? UniqueID.create(id) : undefined;
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
if (identity && !identity.isSuccess) {
|
|
|
|
|
return Promise.resolve(Result.fail(new SupportError("INVALID_REQUEST")));
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
return this.repository.saveTool((tools) => {
|
|
|
|
|
const previous = identity?.isSuccess
|
|
|
|
|
? tools.find((item) => item.id.equals(identity.data))
|
|
|
|
|
: undefined;
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
if (id && !previous) {
|
|
|
|
|
return Result.fail(new SupportError("TOOL_NOT_FOUND"));
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
tools.some(
|
|
|
|
|
(item) =>
|
|
|
|
|
item.id.toPrimitive() !== previous?.id.toPrimitive() &&
|
|
|
|
|
item.name
|
|
|
|
|
.toPrimitive()
|
|
|
|
|
.localeCompare(input.name.trim(), "en", { sensitivity: "base" }) === 0
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
return Result.fail(new SupportError("TOOL_EXISTS"));
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-07 08:16:39 +00:00
|
|
|
return previous ? previous.update(input) : SupportTool.create(input);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|