import type { CredentialFieldSchema } from "@codemation/core"; import { inject } from "@codemation/core"; import { ApplicationTokens } from "../../applicationTokens"; import type { AppConfig } from "../../presentation/config/AppConfig"; import { QueryHandler } from "../bus/QueryHandler"; import { HandlesQuery } from "../../infrastructure/di/HandlesQueryRegistry"; import { CredentialTypeRegistryImpl } from "../../domain/credentials/CredentialServices"; import { GetCredentialFieldEnvStatusQuery, type CredentialFieldEnvStatusDto } from "./GetCredentialFieldEnvStatusQuery"; @HandlesQuery.for(GetCredentialFieldEnvStatusQuery) export class GetCredentialFieldEnvStatusQueryHandler extends QueryHandler< GetCredentialFieldEnvStatusQuery, CredentialFieldEnvStatusDto > { constructor( @inject(CredentialTypeRegistryImpl) private readonly credentialTypeRegistry: CredentialTypeRegistryImpl, @inject(ApplicationTokens.AppConfig) private readonly appConfig: AppConfig, ) { super(); } async execute(): Promise { const names = new Set(); for (const type of this.credentialTypeRegistry.listTypes()) { for (const n of this.collectEnvVarNames(type.publicFields)) { names.add(n); } for (const n of this.collectEnvVarNames(type.secretFields)) { names.add(n); } } const out: Record = {}; for (const name of names) { const v = this.appConfig.env[name]; out[name] = typeof v === "string" && v.length > 0; } return Object.freeze(out); } private collectEnvVarNames(fields: ReadonlyArray | undefined): ReadonlyArray { if (!fields) { return []; } return fields .map((f) => f.envVarName) .filter((name): name is string => typeof name === "string" && name.trim().length > 0); } }