2026-10-06 16:44:21 +00:00
|
|
|
import { Result } from "@repo/rdx-utils";
|
|
|
|
|
|
|
|
|
|
import type { RemoteConnection } from "../domain/remote-connection.aggregate";
|
2026-10-08 14:43:34 +00:00
|
|
|
import type { RemoteConnectionInput } from "../domain/remote-connection-values";
|
|
|
|
|
import { RemoteConnectionError } from "../domain/remote-connection-values";
|
2026-10-09 08:38:19 +00:00
|
|
|
import { RemoteConnectionValues } from "../domain/value-objects/remote-connection-values.vo";
|
2026-10-06 16:44:21 +00:00
|
|
|
|
|
|
|
|
import type { RemoteContext } from "./remote-connection.repository";
|
|
|
|
|
import { remoteReferences } from "./remote-connection-policy";
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-06 16:44:21 +00:00
|
|
|
export function mapRemoteInput(
|
|
|
|
|
input: RemoteConnectionInput,
|
|
|
|
|
context: RemoteContext,
|
|
|
|
|
previous?: RemoteConnection
|
|
|
|
|
) {
|
|
|
|
|
const mapped = RemoteConnectionValues.create(input);
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-06 16:44:21 +00:00
|
|
|
if (!mapped.isSuccess) {
|
|
|
|
|
return mapped;
|
|
|
|
|
}
|
|
|
|
|
const values = mapped.data;
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-06 16:44:21 +00:00
|
|
|
if (previous && !previous.values.sameTarget(values)) {
|
|
|
|
|
return Result.fail(new RemoteConnectionError("REMOTE_TARGET_IMMUTABLE"));
|
|
|
|
|
}
|
|
|
|
|
if (!remoteReferences(values, context)) {
|
|
|
|
|
return Result.fail(new RemoteConnectionError("REMOTE_REFERENCE_NOT_FOUND"));
|
|
|
|
|
}
|
2026-10-16 09:09:56 +00:00
|
|
|
if (
|
|
|
|
|
!previous &&
|
|
|
|
|
input.siteId &&
|
|
|
|
|
!context.sites.some((site) => site.id.toPrimitive() === input.siteId && site.active)
|
|
|
|
|
) {
|
|
|
|
|
return Result.fail(new RemoteConnectionError("REMOTE_REFERENCE_NOT_FOUND"));
|
|
|
|
|
}
|
2026-10-06 16:44:21 +00:00
|
|
|
// Se documentan conexiones existentes aunque su autorización ya no sea vigente; se señalan para revisión.
|
|
|
|
|
if (
|
|
|
|
|
!values.closed &&
|
|
|
|
|
context.connections.some(
|
|
|
|
|
(item) =>
|
|
|
|
|
!(item.id.equals(previous?.id) || item.values.closed) &&
|
|
|
|
|
item.values.connectionKey === values.connectionKey
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
return Result.fail(new RemoteConnectionError("REMOTE_ALREADY_OPEN"));
|
|
|
|
|
}
|
2026-10-10 10:01:58 +00:00
|
|
|
|
2026-10-06 16:44:21 +00:00
|
|
|
return mapped;
|
|
|
|
|
}
|