80 lines
2.7 KiB
TypeScript
80 lines
2.7 KiB
TypeScript
|
|
import { UniqueID } from "@repo/rdx-ddd";
|
||
|
|
import { Result } from "@repo/rdx-utils";
|
||
|
|
|
||
|
|
import { Mailbox } from "../domain/mailbox.aggregate";
|
||
|
|
import { MailboxUse } from "../domain/mailbox-use.aggregate";
|
||
|
|
import { MailboxError, type MailboxInput, type MailboxUseInput } from "../domain/mailbox-values";
|
||
|
|
|
||
|
|
import type { IMailboxRepository, MailboxContext } from "./mailbox.repository";
|
||
|
|
import { mapMailboxInput, mapMailboxUseInput } from "./mailbox-input.mapper";
|
||
|
|
|
||
|
|
function saveMailbox(
|
||
|
|
company: UniqueID,
|
||
|
|
actor: UniqueID,
|
||
|
|
id: UniqueID | undefined,
|
||
|
|
input: MailboxInput,
|
||
|
|
context: MailboxContext,
|
||
|
|
now: string
|
||
|
|
) {
|
||
|
|
const previous = context.mailboxes.find((item) => item.id.equals(id));
|
||
|
|
if (id && !previous) {
|
||
|
|
return Result.fail(new MailboxError("MAILBOX_NOT_FOUND"));
|
||
|
|
}
|
||
|
|
const values = mapMailboxInput(input, context, previous);
|
||
|
|
if (!values.isSuccess) {
|
||
|
|
return values;
|
||
|
|
}
|
||
|
|
return previous
|
||
|
|
? previous.update(values.data, actor, now)
|
||
|
|
: Mailbox.create(company, values.data, actor, now);
|
||
|
|
}
|
||
|
|
function saveUse(
|
||
|
|
company: UniqueID,
|
||
|
|
actor: UniqueID,
|
||
|
|
id: UniqueID | undefined,
|
||
|
|
input: MailboxUseInput,
|
||
|
|
context: MailboxContext,
|
||
|
|
now: string
|
||
|
|
) {
|
||
|
|
const previous = context.uses.find((item) => item.id.equals(id));
|
||
|
|
if (id && !previous) {
|
||
|
|
return Result.fail(new MailboxError("MAILBOX_USE_NOT_FOUND"));
|
||
|
|
}
|
||
|
|
const values = mapMailboxUseInput(input, context, previous);
|
||
|
|
if (!values.isSuccess) {
|
||
|
|
return values;
|
||
|
|
}
|
||
|
|
return previous
|
||
|
|
? previous.update(values.data, actor, now)
|
||
|
|
: MailboxUse.create(company, values.data, actor, now);
|
||
|
|
}
|
||
|
|
export class MailboxUseCases {
|
||
|
|
constructor(private readonly repository: IMailboxRepository) {}
|
||
|
|
get(companyId: string) {
|
||
|
|
const company = UniqueID.create(companyId);
|
||
|
|
return company.isSuccess
|
||
|
|
? this.repository.get(company.data)
|
||
|
|
: Promise.resolve(Result.fail(new MailboxError("INVALID_REQUEST")));
|
||
|
|
}
|
||
|
|
save(
|
||
|
|
companyId: string,
|
||
|
|
id: string | undefined,
|
||
|
|
input: MailboxInput | MailboxUseInput,
|
||
|
|
actorId: string
|
||
|
|
) {
|
||
|
|
const company = UniqueID.create(companyId);
|
||
|
|
const actor = UniqueID.create(actorId);
|
||
|
|
const identity = id ? UniqueID.create(id) : undefined;
|
||
|
|
if (!(company.isSuccess && actor.isSuccess) || (identity && !identity.isSuccess)) {
|
||
|
|
return Promise.resolve(Result.fail(new MailboxError("INVALID_REQUEST")));
|
||
|
|
}
|
||
|
|
const target = identity?.isSuccess ? identity.data : undefined;
|
||
|
|
return this.repository.save(company.data, (context) => {
|
||
|
|
const now = new Date().toISOString();
|
||
|
|
return "address" in input
|
||
|
|
? saveMailbox(company.data, actor.data, target, input, context, now)
|
||
|
|
: saveUse(company.data, actor.data, target, input, context, now);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|