import { featureKeys } from "@hivegrid/entitlements"; import { CatalogError, foundationErrors } from "@hivegrid/foundation"; import { loadRequiredWebConfig } from "@hivegrid/config"; import { tenantHasModule } from "~/utils/connector/tenantConnector.server"; import type { IntegrationsRouteContext } from "~/utils/integrations/integrationsAccess.server"; export const KWP_INTEGRATION_MODULE_KEY = featureKeys.kwpIntegration; export async function hasKwpIntegrationModule(context: IntegrationsRouteContext): Promise { const configuredTenantId = loadRequiredWebConfig().kwp?.hiveGridTenantId?.trim(); if (!configuredTenantId || configuredTenantId !== context.tenantId) return false; return tenantHasModule({ tenantId: context.tenantId, userId: context.userId, moduleKey: KWP_INTEGRATION_MODULE_KEY, }); } /** Fail closed with the platform's 404 permission semantics. */ export async function requireKwpIntegrationModule( context: IntegrationsRouteContext, ): Promise { if (!(await hasKwpIntegrationModule(context))) { throw new CatalogError(foundationErrors.notFound, { module: KWP_INTEGRATION_MODULE_KEY }); } return context; }