2026-10-04 16:36:09 +00:00
|
|
|
import { UniqueID } from "@repo/rdx-ddd";
|
|
|
|
|
import { Maybe, Result } from "@repo/rdx-utils";
|
|
|
|
|
import type { RequestHandler } from "express";
|
|
|
|
|
import type { Sequelize } from "sequelize";
|
|
|
|
|
|
|
|
|
|
import { AssetUseCases } from "./application/asset.use-cases";
|
|
|
|
|
import { AssetCatalogUseCases } from "./application/asset-catalog.use-cases";
|
2026-10-04 19:04:14 +00:00
|
|
|
import { AssetRelationshipUseCases } from "./application/asset-relationship.use-cases";
|
|
|
|
|
import { WorkstationUseCases } from "./application/workstation.use-cases";
|
2026-10-04 16:36:09 +00:00
|
|
|
import { seedAssetCatalog } from "./domain/asset-catalog-seed";
|
|
|
|
|
import { registerAssetRoutes } from "./infrastructure/http/asset.routes";
|
|
|
|
|
import { registerAssetCatalogRoutes } from "./infrastructure/http/asset-catalog.routes";
|
2026-10-04 19:04:14 +00:00
|
|
|
import { registerRelationshipRoutes } from "./infrastructure/http/asset-relationship.routes";
|
|
|
|
|
import { registerWorkstationRoutes } from "./infrastructure/http/workstation.routes";
|
2026-10-04 16:36:09 +00:00
|
|
|
import { migrateInventory } from "./infrastructure/persistence/asset.model";
|
|
|
|
|
import {
|
|
|
|
|
CatalogModel,
|
|
|
|
|
migrateAssetCatalog,
|
|
|
|
|
} from "./infrastructure/persistence/asset-catalog.model";
|
|
|
|
|
import { AssetCatalogPersistenceMapper } from "./infrastructure/persistence/asset-catalog-persistence.mapper";
|
2026-10-04 19:04:14 +00:00
|
|
|
import { migrateRelationships } from "./infrastructure/persistence/asset-relationship.model";
|
2026-10-04 16:36:09 +00:00
|
|
|
import { SequelizeAssetRepository } from "./infrastructure/persistence/sequelize-asset.repository";
|
|
|
|
|
import { SequelizeAssetCatalogRepository } from "./infrastructure/persistence/sequelize-asset-catalog.repository";
|
2026-10-04 19:04:14 +00:00
|
|
|
import { migrateWorkstations } from "./infrastructure/persistence/workstation.model";
|
2026-10-04 16:36:09 +00:00
|
|
|
export async function createAssetModule(database: Sequelize, authenticate: RequestHandler) {
|
|
|
|
|
await migrateAssetCatalog(database);
|
|
|
|
|
await CatalogModel.findOrCreate({
|
|
|
|
|
where: { catalogKey: "global" },
|
|
|
|
|
defaults: { id: UniqueID.generateNewID().toPrimitive(), catalogKey: "global" },
|
|
|
|
|
});
|
|
|
|
|
const repository = new SequelizeAssetCatalogRepository(
|
|
|
|
|
database,
|
|
|
|
|
new AssetCatalogPersistenceMapper()
|
|
|
|
|
);
|
|
|
|
|
const seeded = await repository.update(Maybe.none(), (catalog) =>
|
|
|
|
|
Result.ok(seedAssetCatalog(catalog))
|
|
|
|
|
);
|
|
|
|
|
if (!seeded.isSuccess) {
|
|
|
|
|
throw new Error("No se pudo inicializar el catálogo de activos.");
|
|
|
|
|
}
|
|
|
|
|
await migrateInventory(database);
|
2026-10-04 19:04:14 +00:00
|
|
|
await migrateRelationships(database);
|
|
|
|
|
await migrateWorkstations(database);
|
|
|
|
|
const assetRepository = new SequelizeAssetRepository(database);
|
|
|
|
|
const inventory = new AssetUseCases(assetRepository);
|
|
|
|
|
const workstations = new WorkstationUseCases(assetRepository);
|
|
|
|
|
const relationships = new AssetRelationshipUseCases(assetRepository);
|
2026-10-04 16:36:09 +00:00
|
|
|
const useCases = new AssetCatalogUseCases(repository);
|
|
|
|
|
return {
|
|
|
|
|
register: (app: Parameters<typeof registerAssetCatalogRoutes>[0]) => {
|
|
|
|
|
registerAssetCatalogRoutes(app, useCases, authenticate);
|
|
|
|
|
registerAssetRoutes(app, inventory, authenticate);
|
2026-10-04 19:04:14 +00:00
|
|
|
registerRelationshipRoutes(app, relationships, authenticate);
|
|
|
|
|
registerWorkstationRoutes(app, workstations, authenticate);
|
2026-10-04 16:36:09 +00:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|