2026-10-06 15:15:42 +00:00
|
|
|
import type { UniqueID } from "@repo/rdx-ddd";
|
|
|
|
|
|
|
|
|
|
import type { IResourceRepository } from "./resource.repository";
|
|
|
|
|
export interface IResourceInventoryService {
|
|
|
|
|
list(company: UniqueID): Promise<
|
|
|
|
|
readonly {
|
|
|
|
|
id: UniqueID;
|
2026-10-07 07:34:18 +00:00
|
|
|
name: string;
|
2026-10-06 15:15:42 +00:00
|
|
|
active: boolean;
|
|
|
|
|
usage: "application" | "certificate" | null;
|
|
|
|
|
expiresOn: string | null;
|
|
|
|
|
}[]
|
|
|
|
|
>;
|
|
|
|
|
}
|
|
|
|
|
export class ResourceInventoryService implements IResourceInventoryService {
|
|
|
|
|
constructor(private readonly repository: IResourceRepository) {}
|
|
|
|
|
async list(company: UniqueID) {
|
|
|
|
|
const result = await this.repository.get(company);
|
|
|
|
|
if (!result.isSuccess) {
|
|
|
|
|
throw new Error("No se pudo consultar el inventario de recursos.");
|
|
|
|
|
}
|
|
|
|
|
const expiryFieldId = result.data.catalog.definitions
|
|
|
|
|
.find((item) => item.stockKey.getOrUndefined() === "field:expires")
|
|
|
|
|
?.id.toPrimitive();
|
|
|
|
|
return result.data.resources.map((resource) => {
|
|
|
|
|
const template = result.data.catalog.definitions.find(
|
|
|
|
|
(item) => item.id.toPrimitive() === resource.values.toPrimitive().templateId
|
|
|
|
|
);
|
|
|
|
|
const data = template?.values.data;
|
|
|
|
|
const key = template?.stockKey.getOrUndefined();
|
|
|
|
|
const usage: "application" | "certificate" | null =
|
|
|
|
|
key === "template:application"
|
|
|
|
|
? "application"
|
|
|
|
|
: key === "template:certificate"
|
|
|
|
|
? "certificate"
|
|
|
|
|
: data?.kind === "template"
|
|
|
|
|
? (data.usage ?? null)
|
|
|
|
|
: null;
|
|
|
|
|
const expiresOn = expiryFieldId
|
|
|
|
|
? resource.values.toPrimitive().characteristics[expiryFieldId]
|
|
|
|
|
: null;
|
|
|
|
|
return {
|
|
|
|
|
id: resource.id,
|
2026-10-07 07:34:18 +00:00
|
|
|
name: resource.values.toPrimitive().name,
|
2026-10-06 15:15:42 +00:00
|
|
|
active: resource.values.toPrimitive().active,
|
|
|
|
|
usage,
|
|
|
|
|
expiresOn: typeof expiresOn === "string" ? expiresOn : null,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|