2026-10-05 11:13:51 +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 { ResourceUseCases } from "./application/resource.use-cases";
|
|
|
|
|
import { ResourceCatalogUseCases } from "./application/resource-catalog.use-cases";
|
|
|
|
|
import { seedResourceCatalog } from "./domain/resource-catalog-seed";
|
|
|
|
|
import { registerResourceRoutes } from "./infrastructure/http/resource.routes";
|
|
|
|
|
import { registerResourceCatalogRoutes } from "./infrastructure/http/resource-catalog.routes";
|
2026-10-05 15:01:32 +00:00
|
|
|
import { migrateLegacyResources } from "./infrastructure/persistence/migrate-legacy-resources";
|
2026-10-05 11:13:51 +00:00
|
|
|
import { migrateInventory } from "./infrastructure/persistence/resource.model";
|
|
|
|
|
import {
|
|
|
|
|
ResourceCatalogModel,
|
|
|
|
|
migrateResourceCatalog,
|
|
|
|
|
} from "./infrastructure/persistence/resource-catalog.model";
|
|
|
|
|
import { ResourceCatalogPersistenceMapper } from "./infrastructure/persistence/resource-catalog-persistence.mapper";
|
|
|
|
|
import { SequelizeResourceRepository } from "./infrastructure/persistence/sequelize-resource.repository";
|
|
|
|
|
import { SequelizeResourceCatalogRepository } from "./infrastructure/persistence/sequelize-resource-catalog.repository";
|
|
|
|
|
|
|
|
|
|
export async function createResourceModule(database: Sequelize, authenticate: RequestHandler) {
|
|
|
|
|
await migrateResourceCatalog(database);
|
|
|
|
|
await ResourceCatalogModel.findOrCreate({
|
|
|
|
|
where: { catalogKey: "global" },
|
|
|
|
|
defaults: { id: UniqueID.generateNewID().toPrimitive(), catalogKey: "global" },
|
|
|
|
|
});
|
|
|
|
|
const repository = new SequelizeResourceCatalogRepository(
|
|
|
|
|
database,
|
|
|
|
|
new ResourceCatalogPersistenceMapper()
|
|
|
|
|
);
|
|
|
|
|
const seeded = await repository.update(Maybe.none(), (catalog) =>
|
|
|
|
|
Result.ok(seedResourceCatalog(catalog))
|
|
|
|
|
);
|
|
|
|
|
if (!seeded.isSuccess) {
|
|
|
|
|
throw new Error("No se pudo inicializar el catálogo de recursos.");
|
|
|
|
|
}
|
|
|
|
|
await migrateInventory(database);
|
|
|
|
|
const catalog = new ResourceCatalogUseCases(repository);
|
|
|
|
|
const inventory = new ResourceUseCases(new SequelizeResourceRepository(database));
|
|
|
|
|
return {
|
2026-10-05 15:01:32 +00:00
|
|
|
migrateLegacy: () => migrateLegacyResources(database),
|
2026-10-05 11:13:51 +00:00
|
|
|
register: (app: Parameters<typeof registerResourceCatalogRoutes>[0]) => {
|
|
|
|
|
registerResourceCatalogRoutes(app, catalog, authenticate);
|
|
|
|
|
registerResourceRoutes(app, inventory, authenticate);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|