From 9eba70f686ee0e076777b828cc5b0c2606e64a54 Mon Sep 17 00:00:00 2001 From: Yvonnick FRIN Date: Wed, 26 Aug 2026 17:22:55 +0200 Subject: [PATCH] feat(api): try to check if usecase return value is an instance of the related context --- .../combined-course-blueprint-controller.js | 21 ++++---- .../utils/dependency-injection.js | 53 +++++++++++++++++-- .../combined-course-blueprint-route_test.js | 2 +- 3 files changed, 62 insertions(+), 14 deletions(-) diff --git a/api/src/quest/application/combined-course-blueprint-controller.js b/api/src/quest/application/combined-course-blueprint-controller.js index 6a3ad2bf56e..efb259d75da 100644 --- a/api/src/quest/application/combined-course-blueprint-controller.js +++ b/api/src/quest/application/combined-course-blueprint-controller.js @@ -47,16 +47,16 @@ const detachOrganization = async (request, h) => { }; const attachOrganizations = async (request, h, dependencies = { combinedCourseBlueprintOrganizationSerializer }) => { - const combinedCourseBlueprintId = request.params.blueprintId; - const results = await usecases.attachOrganizationsToCombinedCourseBlueprint({ - combinedCourseBlueprintId, - organizationIds: request.payload['organization-ids'], - }); - return h - .response( - dependencies.combinedCourseBlueprintOrganizationSerializer.serialize({ ...results, combinedCourseBlueprintId }), - ) - .code(201); + const combinedCourseBlueprintId = request.params.blueprintId; + const results = await usecases.attachOrganizationsToCombinedCourseBlueprint({ + combinedCourseBlueprintId, + organizationIds: request.payload['organization-ids'], + }); + return h + .response( + dependencies.combinedCourseBlueprintOrganizationSerializer.serialize({ ...results, combinedCourseBlueprintId }), + ) + .code(201); }; const findByOrganizationId = async (request, _, dependencies = { combinedCourseBlueprintSerializer }) => { @@ -70,6 +70,7 @@ const findOverviewById = async (request, _, dependencies = { combinedCourseBluep const combinedCourseBlueprint = await usecases.findCombinedCourseBlueprintById({ id: request.params.blueprintId, }); + console.log({combinedCourseBlueprint}) return dependencies.combinedCourseBlueprintOverviewSerializer.serialize(combinedCourseBlueprint); }; diff --git a/api/src/shared/infrastructure/utils/dependency-injection.js b/api/src/shared/infrastructure/utils/dependency-injection.js index 57ef98dc2f6..dc6c6facd4c 100644 --- a/api/src/shared/infrastructure/utils/dependency-injection.js +++ b/api/src/shared/infrastructure/utils/dependency-injection.js @@ -1,9 +1,56 @@ +import { glob } from 'node:fs/promises'; +import {join} from 'node:path' +import {cwd} from 'node:process' +import { pathToFileURL } from 'node:url'; + import _ from 'lodash'; import { tracing } from '../open-telemetry/helpers.js'; -function injectDefaults(defaults, targetFn) { - return (args) => targetFn(Object.assign(Object.create(defaults), args)); +async function isForDomain(value) { + return async (filePath) => { + const mod = await import(pathToFileURL(filePath).href); + const ExportedClass = mod.default; + + if (value instanceof ExportedClass) { /* ... */ } + } +} + +function makeInjectDefaults(boundedContext) { + return (defaults, targetFn) => { + return async (args) => { + console.log(targetFn) + const result = await targetFn(Object.assign(Object.create(defaults), args)) + console.log({ result }) + const pathToModels = join(cwd(), 'src', 'quest', 'domain', 'models') + console.log({ pathToModels }) + const modelFiles = [] + for await (const entry of glob(join(pathToModels, '**/*.js'))) + modelFiles.push(entry) + + let found = false + + for (const filePath of modelFiles) { + const mod = await import(pathToFileURL(filePath).href); + const ExportedClass = mod[Object.keys(mod)[0]]; + if (typeof ExportedClass !== 'function') { + continue; + } + console.log(result.constructor.name, filePath, typeof ExportedClass, ExportedClass) + if (result instanceof ExportedClass) { + found = true + } + } + + if (!found) { + throw Error('NOT A DOMAIN MODEL') + } + + console.log({ found }) + + return result + } + } } /** @@ -59,7 +106,7 @@ export function injectDependencies(toBeInjected, dependencies, boundedContext = const wrapped = tracing.spanify(`${boundedContext.name}->${name}`, value, () => ({ attributes: defaultAttributes, })); - return [name, _.partial(injectDefaults, wrappedDependencies, wrapped)()]; + return [name, _.partial(makeInjectDefaults(boundedContext.name), wrappedDependencies, wrapped)()]; } else { return [name, injectDependencies(value, dependencies)]; } diff --git a/api/tests/quest/acceptance/application/combined-course-blueprint-route_test.js b/api/tests/quest/acceptance/application/combined-course-blueprint-route_test.js index 878f136d36d..1fa08395e75 100644 --- a/api/tests/quest/acceptance/application/combined-course-blueprint-route_test.js +++ b/api/tests/quest/acceptance/application/combined-course-blueprint-route_test.js @@ -334,7 +334,7 @@ describe('Quest | Acceptance | Application | Combined course blueprint Route ', await databaseBuilder.commit(); }); - it('should return 200', async function () { + it.only('should return 200', async function () { const options = { method: 'GET', url: `/api/organizations/${organization.id}/combined-course-blueprints/${combinedCourseBlueprint.id}`,