@open-mercato/core 0.6.4-develop.4236.1.9fa6806b34 → 0.6.4-develop.4254.1.7a123d970c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/dist/helpers/integration/authFixtures.js +70 -1
- package/dist/helpers/integration/authFixtures.js.map +2 -2
- package/dist/helpers/integration/dbFixtures.js +98 -0
- package/dist/helpers/integration/dbFixtures.js.map +7 -0
- package/dist/modules/business_rules/api/execute/route.js +2 -1
- package/dist/modules/business_rules/api/execute/route.js.map +2 -2
- package/dist/modules/business_rules/api/rules/route.js +10 -0
- package/dist/modules/business_rules/api/rules/route.js.map +2 -2
- package/dist/modules/business_rules/backend/logs/[id]/page.js +24 -5
- package/dist/modules/business_rules/backend/logs/[id]/page.js.map +2 -2
- package/dist/modules/business_rules/cli.js +6 -0
- package/dist/modules/business_rules/cli.js.map +2 -2
- package/dist/modules/business_rules/lib/rule-engine.js +116 -9
- package/dist/modules/business_rules/lib/rule-engine.js.map +2 -2
- package/dist/modules/business_rules/subscribers/crud-rule-trigger.js +3 -2
- package/dist/modules/business_rules/subscribers/crud-rule-trigger.js.map +2 -2
- package/dist/modules/catalog/api/offers/route.js +15 -5
- package/dist/modules/catalog/api/offers/route.js.map +2 -2
- package/dist/modules/catalog/api/products/route.js +21 -4
- package/dist/modules/catalog/api/products/route.js.map +2 -2
- package/dist/modules/catalog/lib/pricing.js +6 -0
- package/dist/modules/catalog/lib/pricing.js.map +2 -2
- package/dist/modules/catalog/services/catalogPricingService.js +5 -1
- package/dist/modules/catalog/services/catalogPricingService.js.map +2 -2
- package/dist/modules/currencies/backend/currencies/[id]/page.js +19 -2
- package/dist/modules/currencies/backend/currencies/[id]/page.js.map +2 -2
- package/dist/modules/customer_accounts/backend/customer_accounts/roles/[id]/page.js +27 -7
- package/dist/modules/customer_accounts/backend/customer_accounts/roles/[id]/page.js.map +2 -2
- package/dist/modules/customer_accounts/backend/customer_accounts/users/[id]/page.js +27 -7
- package/dist/modules/customer_accounts/backend/customer_accounts/users/[id]/page.js.map +2 -2
- package/dist/modules/customers/api/activities/route.js +15 -2
- package/dist/modules/customers/api/activities/route.js.map +2 -2
- package/dist/modules/customers/api/comments/route.js +15 -3
- package/dist/modules/customers/api/comments/route.js.map +2 -2
- package/dist/modules/customers/api/companies/[id]/people/route.js +2 -4
- package/dist/modules/customers/api/companies/[id]/people/route.js.map +2 -2
- package/dist/modules/customers/api/companies/[id]/route.js +2 -4
- package/dist/modules/customers/api/companies/[id]/route.js.map +2 -2
- package/dist/modules/customers/api/deals/[id]/companies/route.js +2 -4
- package/dist/modules/customers/api/deals/[id]/companies/route.js.map +2 -2
- package/dist/modules/customers/api/deals/[id]/people/route.js +2 -4
- package/dist/modules/customers/api/deals/[id]/people/route.js.map +2 -2
- package/dist/modules/customers/api/deals/[id]/route.js +2 -9
- package/dist/modules/customers/api/deals/[id]/route.js.map +2 -2
- package/dist/modules/customers/api/deals/[id]/stats/route.js +2 -9
- package/dist/modules/customers/api/deals/[id]/stats/route.js.map +2 -2
- package/dist/modules/customers/api/entity-roles-factory.js +2 -8
- package/dist/modules/customers/api/entity-roles-factory.js.map +2 -2
- package/dist/modules/customers/api/people/[id]/companies/context.js +2 -4
- package/dist/modules/customers/api/people/[id]/companies/context.js.map +2 -2
- package/dist/modules/customers/api/people/[id]/companies/enriched/route.js +2 -4
- package/dist/modules/customers/api/people/[id]/companies/enriched/route.js.map +2 -2
- package/dist/modules/customers/api/people/[id]/route.js +2 -4
- package/dist/modules/customers/api/people/[id]/route.js.map +2 -2
- package/dist/modules/customers/backend/customers/people/[id]/page.js +29 -8
- package/dist/modules/customers/backend/customers/people/[id]/page.js.map +2 -2
- package/dist/modules/directory/utils/organizationScopeGuard.js +22 -0
- package/dist/modules/directory/utils/organizationScopeGuard.js.map +7 -0
- package/dist/modules/progress/acl.js +8 -4
- package/dist/modules/progress/acl.js.map +2 -2
- package/dist/modules/workflows/backend/events/[id]/page.js +24 -6
- package/dist/modules/workflows/backend/events/[id]/page.js.map +2 -2
- package/dist/modules/workflows/backend/instances/[id]/page.js +27 -5
- package/dist/modules/workflows/backend/instances/[id]/page.js.map +2 -2
- package/dist/modules/workflows/backend/tasks/[id]/page.js +25 -6
- package/dist/modules/workflows/backend/tasks/[id]/page.js.map +2 -2
- package/dist/modules/workflows/cli.js +8 -0
- package/dist/modules/workflows/cli.js.map +2 -2
- package/dist/modules/workflows/lib/seeds.js +8 -4
- package/dist/modules/workflows/lib/seeds.js.map +2 -2
- package/dist/modules/workflows/setup.js +3 -1
- package/dist/modules/workflows/setup.js.map +2 -2
- package/package.json +7 -7
- package/src/helpers/integration/authFixtures.ts +98 -0
- package/src/helpers/integration/dbFixtures.ts +144 -0
- package/src/modules/business_rules/api/execute/route.ts +2 -1
- package/src/modules/business_rules/api/rules/route.ts +10 -0
- package/src/modules/business_rules/backend/logs/[id]/page.tsx +32 -7
- package/src/modules/business_rules/cli.ts +6 -0
- package/src/modules/business_rules/lib/rule-engine.ts +163 -9
- package/src/modules/business_rules/subscribers/crud-rule-trigger.ts +3 -2
- package/src/modules/catalog/api/offers/route.ts +20 -5
- package/src/modules/catalog/api/products/route.ts +23 -4
- package/src/modules/catalog/lib/pricing.ts +9 -0
- package/src/modules/catalog/services/catalogPricingService.ts +6 -0
- package/src/modules/currencies/backend/currencies/[id]/page.tsx +21 -2
- package/src/modules/currencies/i18n/de.json +1 -0
- package/src/modules/currencies/i18n/en.json +1 -0
- package/src/modules/currencies/i18n/es.json +1 -0
- package/src/modules/currencies/i18n/pl.json +1 -0
- package/src/modules/customer_accounts/backend/customer_accounts/roles/[id]/page.tsx +34 -11
- package/src/modules/customer_accounts/backend/customer_accounts/users/[id]/page.tsx +34 -11
- package/src/modules/customers/api/activities/route.ts +16 -5
- package/src/modules/customers/api/comments/route.ts +15 -5
- package/src/modules/customers/api/companies/[id]/people/route.ts +2 -4
- package/src/modules/customers/api/companies/[id]/route.ts +2 -5
- package/src/modules/customers/api/deals/[id]/companies/route.ts +2 -4
- package/src/modules/customers/api/deals/[id]/people/route.ts +2 -4
- package/src/modules/customers/api/deals/[id]/route.ts +2 -9
- package/src/modules/customers/api/deals/[id]/stats/route.ts +2 -9
- package/src/modules/customers/api/entity-roles-factory.ts +2 -12
- package/src/modules/customers/api/people/[id]/companies/context.ts +2 -5
- package/src/modules/customers/api/people/[id]/companies/enriched/route.ts +2 -5
- package/src/modules/customers/api/people/[id]/route.ts +2 -5
- package/src/modules/customers/backend/customers/people/[id]/page.tsx +35 -11
- package/src/modules/directory/utils/organizationScopeGuard.ts +39 -0
- package/src/modules/progress/acl.ts +4 -0
- package/src/modules/workflows/backend/events/[id]/page.tsx +32 -10
- package/src/modules/workflows/backend/instances/[id]/page.tsx +33 -9
- package/src/modules/workflows/backend/tasks/[id]/page.tsx +33 -10
- package/src/modules/workflows/cli.ts +8 -0
- package/src/modules/workflows/i18n/de.json +1 -0
- package/src/modules/workflows/i18n/en.json +1 -0
- package/src/modules/workflows/i18n/es.json +1 -0
- package/src/modules/workflows/i18n/pl.json +1 -0
- package/src/modules/workflows/lib/seeds.ts +13 -3
- package/src/modules/workflows/setup.ts +3 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../../../src/modules/business_rules/backend/logs/%5Bid%5D/page.tsx"],
|
|
4
|
-
"sourcesContent": ["\"use client\"\n\nimport * as React from 'react'\nimport Link from 'next/link'\nimport { useParams, useRouter } from 'next/navigation'\nimport { useQuery } from '@tanstack/react-query'\nimport { apiFetch } from '@open-mercato/ui/backend/utils/api'\nimport { useT } from '@open-mercato/shared/lib/i18n/context'\nimport { Page, PageBody } from '@open-mercato/ui/backend/Page'\nimport { Button } from '@open-mercato/ui/primitives/button'\nimport { Spinner } from '@open-mercato/ui/primitives/spinner'\nimport { JsonDisplay } from '@open-mercato/ui/backend/JsonDisplay'\n\ntype RuleExecutionLog = {\n id: string\n ruleId: string\n rule?: {\n id: string\n ruleId: string\n ruleName: string\n ruleType: string\n } | null\n entityType: string\n entityId: string | null\n eventType: string | null\n executedAt: string\n executionTimeMs: number\n executionResult: 'SUCCESS' | 'FAILURE' | 'ERROR'\n resultValue: any | null\n errorMessage: string | null\n inputContext: any | null\n outputContext: any | null\n executedBy: string | null\n tenantId: string | null\n organizationId: string | null\n}\n\nexport default function ExecutionLogDetailPage() {\n const router = useRouter()\n const params = useParams()\n\n // Handle catch-all route: params.slug = ['logs', 'id']\n let logId: string | undefined\n if (params?.slug && Array.isArray(params.slug)) {\n logId = params.slug[1] // Second element is the ID\n } else if (params?.id) {\n logId = Array.isArray(params.id) ? params.id[0] : params.id\n }\n\n const t = useT()\n\n const { data: log, isLoading, error } = useQuery({\n queryKey: ['business-rules', 'logs', logId],\n queryFn: async () => {\n const response = await apiFetch(`/api/business_rules/logs/${logId}`)\n if (!response.ok) {\n throw new Error(t('business_rules.logs.errors.fetchFailed'))\n }\n const result = await response.json()\n return result as RuleExecutionLog\n },\n enabled: !!logId,\n })\n\n if (isLoading) {\n return (\n <Page>\n <PageBody>\n <div className=\"flex h-[50vh] flex-col items-center justify-center gap-2 text-muted-foreground\">\n <Spinner className=\"h-6 w-6\" />\n <span>{t('business_rules.logs.detail.loading')}</span>\n </div>\n </PageBody>\n </Page>\n )\n }\n\n if (error || !log) {\n return (\n <Page>\n <PageBody>\n <div className=\"flex h-[50vh] flex-col items-center justify-center gap-2 text-muted-foreground\">\n <p>{error ? t('business_rules.logs.errors.loadFailed') : t('business_rules.logs.errors.notFound')}</p>\n <Button asChild variant=\"outline\">\n <Link href=\"/backend/logs\">{t('business_rules.logs.backToList')}</Link>\n </Button>\n </div>\n </PageBody>\n </Page>\n )\n }\n\n const getResultBadgeClass = (result: string) => {\n switch (result) {\n case 'SUCCESS':\n return 'bg-green-100 text-green-800'\n case 'FAILURE':\n return 'bg-yellow-100 text-yellow-800'\n case 'ERROR':\n return 'bg-red-100 text-red-800'\n default:\n return 'bg-muted text-foreground'\n }\n }\n\n return (\n <Page>\n <PageBody>\n <div className=\"space-y-6\">\n {/* Header */}\n <div className=\"flex items-center justify-between\">\n <div>\n <h1 className=\"text-2xl font-bold text-foreground\">\n {t('business_rules.logs.detail.title')}\n </h1>\n <p className=\"text-sm text-muted-foreground mt-1\">\n {t('business_rules.logs.detail.logId')}: {log.id}\n </p>\n </div>\n <Button onClick={() => router.push('/backend/logs')} variant=\"outline\">\n {t('business_rules.logs.backToList')}\n </Button>\n </div>\n\n {/* Execution Summary */}\n <div className=\"rounded-lg border bg-card p-6\">\n <h2 className=\"text-lg font-semibold mb-4\">\n {t('business_rules.logs.detail.summary')}\n </h2>\n <dl className=\"grid grid-cols-1 md:grid-cols-2 gap-4\">\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.executedAt')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground\">\n {new Date(log.executedAt).toLocaleString()}\n </dd>\n </div>\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.result')}\n </dt>\n <dd className=\"mt-1\">\n <span\n className={`inline-flex items-center px-2 py-1 rounded text-xs font-medium ${getResultBadgeClass(\n log.executionResult\n )}`}\n >\n {t(`business_rules.logs.result.${log.executionResult.toLowerCase()}`)}\n </span>\n </dd>\n </div>\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.executionTime')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground\">{log.executionTimeMs}ms</dd>\n </div>\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.executedBy')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground\">\n {log.executedBy || t('common.unknown')}\n </dd>\n </div>\n </dl>\n </div>\n\n {/* Rule Information */}\n <div className=\"rounded-lg border bg-card p-6\">\n <h2 className=\"text-lg font-semibold mb-4\">\n {t('business_rules.logs.detail.ruleInfo')}\n </h2>\n {log.rule ? (\n <dl className=\"grid grid-cols-1 md:grid-cols-2 gap-4\">\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.ruleName')}\n </dt>\n <dd className=\"mt-1\">\n <Link\n href={`/backend/rules/${log.rule.id}`}\n className=\"text-sm text-primary hover:underline\"\n >\n {log.rule.ruleName}\n </Link>\n </dd>\n </div>\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.ruleType')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground\">{log.rule.ruleType}</dd>\n </div>\n <div className=\"md:col-span-2\">\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.ruleId')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground font-mono\">{log.rule.ruleId}</dd>\n </div>\n </dl>\n ) : (\n <p className=\"text-sm text-muted-foreground\">{t('business_rules.logs.ruleDeleted')}</p>\n )}\n </div>\n\n {/* Entity Information */}\n <div className=\"rounded-lg border bg-card p-6\">\n <h2 className=\"text-lg font-semibold mb-4\">\n {t('business_rules.logs.detail.entityInfo')}\n </h2>\n <dl className=\"grid grid-cols-1 md:grid-cols-2 gap-4\">\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.entityType')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground\">{log.entityType}</dd>\n </div>\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.eventType')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground\">\n {log.eventType || t('common.none')}\n </dd>\n </div>\n {log.entityId && (\n <div className=\"md:col-span-2\">\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.entityId')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground font-mono break-all\">\n {log.entityId}\n </dd>\n </div>\n )}\n </dl>\n </div>\n\n {/* Error Message (if present) */}\n {log.errorMessage && (\n <div className=\"rounded-lg border border-destructive bg-destructive/5 p-6\">\n <h2 className=\"text-lg font-semibold mb-4 text-destructive\">\n {t('business_rules.logs.detail.errorMessage')}\n </h2>\n <pre className=\"text-sm text-destructive whitespace-pre-wrap font-mono\">\n {log.errorMessage}\n </pre>\n </div>\n )}\n\n {/* Input Context */}\n {log.inputContext && (\n <JsonDisplay\n data={log.inputContext}\n title={t('business_rules.logs.detail.inputContext')}\n />\n )}\n\n {/* Output Context */}\n {log.outputContext && (\n <JsonDisplay\n data={log.outputContext}\n title={t('business_rules.logs.detail.outputContext')}\n />\n )}\n\n {/* Result Value */}\n {log.resultValue && (\n <JsonDisplay\n data={log.resultValue}\n title={t('business_rules.logs.detail.resultValue')}\n />\n )}\n </div>\n </PageBody>\n </Page>\n )\n}\n"],
|
|
5
|
-
"mappings": ";
|
|
4
|
+
"sourcesContent": ["\"use client\"\n\nimport * as React from 'react'\nimport Link from 'next/link'\nimport { useParams, useRouter } from 'next/navigation'\nimport { useQuery } from '@tanstack/react-query'\nimport { apiFetch } from '@open-mercato/ui/backend/utils/api'\nimport { useT } from '@open-mercato/shared/lib/i18n/context'\nimport { Page, PageBody } from '@open-mercato/ui/backend/Page'\nimport { Button } from '@open-mercato/ui/primitives/button'\nimport { Spinner } from '@open-mercato/ui/primitives/spinner'\nimport { JsonDisplay } from '@open-mercato/ui/backend/JsonDisplay'\nimport { RecordNotFoundState, ErrorMessage } from '@open-mercato/ui/backend/detail'\n\ntype RuleExecutionLog = {\n id: string\n ruleId: string\n rule?: {\n id: string\n ruleId: string\n ruleName: string\n ruleType: string\n } | null\n entityType: string\n entityId: string | null\n eventType: string | null\n executedAt: string\n executionTimeMs: number\n executionResult: 'SUCCESS' | 'FAILURE' | 'ERROR'\n resultValue: any | null\n errorMessage: string | null\n inputContext: any | null\n outputContext: any | null\n executedBy: string | null\n tenantId: string | null\n organizationId: string | null\n}\n\nexport default function ExecutionLogDetailPage() {\n const router = useRouter()\n const params = useParams()\n\n // Handle catch-all route: params.slug = ['logs', 'id']\n let logId: string | undefined\n if (params?.slug && Array.isArray(params.slug)) {\n logId = params.slug[1] // Second element is the ID\n } else if (params?.id) {\n logId = Array.isArray(params.id) ? params.id[0] : params.id\n }\n\n const t = useT()\n\n const { data: log, isLoading, error } = useQuery({\n queryKey: ['business-rules', 'logs', logId],\n queryFn: async () => {\n const response = await apiFetch(`/api/business_rules/logs/${logId}`)\n if (!response.ok) {\n const httpErr = new Error(\n response.status === 404\n ? t('business_rules.logs.errors.notFound', 'Execution log not found.')\n : t('business_rules.logs.errors.fetchFailed')\n ) as Error & { status: number }\n httpErr.status = response.status\n throw httpErr\n }\n const result = await response.json()\n return result as RuleExecutionLog\n },\n enabled: !!logId,\n })\n\n if (isLoading) {\n return (\n <Page>\n <PageBody>\n <div className=\"flex h-[50vh] flex-col items-center justify-center gap-2 text-muted-foreground\">\n <Spinner className=\"h-6 w-6\" />\n <span>{t('business_rules.logs.detail.loading')}</span>\n </div>\n </PageBody>\n </Page>\n )\n }\n\n const isNotFound = !isLoading && (error as (Error & { status?: number }) | null)?.status === 404\n\n if (isNotFound) {\n return (\n <Page>\n <PageBody>\n <RecordNotFoundState\n label={t('business_rules.logs.errors.notFound', 'Execution log not found.')}\n backHref=\"/backend/logs\"\n backLabel={t('business_rules.logs.backToList', 'Back to logs')}\n />\n </PageBody>\n </Page>\n )\n }\n\n if (error || !log) {\n return (\n <Page>\n <PageBody>\n <ErrorMessage\n label={(error as Error | null)?.message ?? t('business_rules.logs.errors.loadFailed')}\n action={\n <Button asChild variant=\"outline\" size=\"sm\">\n <Link href=\"/backend/logs\">{t('business_rules.logs.backToList', 'Back to logs')}</Link>\n </Button>\n }\n />\n </PageBody>\n </Page>\n )\n }\n\n const getResultBadgeClass = (result: string) => {\n switch (result) {\n case 'SUCCESS':\n return 'bg-green-100 text-green-800'\n case 'FAILURE':\n return 'bg-yellow-100 text-yellow-800'\n case 'ERROR':\n return 'bg-red-100 text-red-800'\n default:\n return 'bg-muted text-foreground'\n }\n }\n\n return (\n <Page>\n <PageBody>\n <div className=\"space-y-6\">\n {/* Header */}\n <div className=\"flex items-center justify-between\">\n <div>\n <h1 className=\"text-2xl font-bold text-foreground\">\n {t('business_rules.logs.detail.title')}\n </h1>\n <p className=\"text-sm text-muted-foreground mt-1\">\n {t('business_rules.logs.detail.logId')}: {log.id}\n </p>\n </div>\n <Button onClick={() => router.push('/backend/logs')} variant=\"outline\">\n {t('business_rules.logs.backToList')}\n </Button>\n </div>\n\n {/* Execution Summary */}\n <div className=\"rounded-lg border bg-card p-6\">\n <h2 className=\"text-lg font-semibold mb-4\">\n {t('business_rules.logs.detail.summary')}\n </h2>\n <dl className=\"grid grid-cols-1 md:grid-cols-2 gap-4\">\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.executedAt')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground\">\n {new Date(log.executedAt).toLocaleString()}\n </dd>\n </div>\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.result')}\n </dt>\n <dd className=\"mt-1\">\n <span\n className={`inline-flex items-center px-2 py-1 rounded text-xs font-medium ${getResultBadgeClass(\n log.executionResult\n )}`}\n >\n {t(`business_rules.logs.result.${log.executionResult.toLowerCase()}`)}\n </span>\n </dd>\n </div>\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.executionTime')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground\">{log.executionTimeMs}ms</dd>\n </div>\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.executedBy')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground\">\n {log.executedBy || t('common.unknown')}\n </dd>\n </div>\n </dl>\n </div>\n\n {/* Rule Information */}\n <div className=\"rounded-lg border bg-card p-6\">\n <h2 className=\"text-lg font-semibold mb-4\">\n {t('business_rules.logs.detail.ruleInfo')}\n </h2>\n {log.rule ? (\n <dl className=\"grid grid-cols-1 md:grid-cols-2 gap-4\">\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.ruleName')}\n </dt>\n <dd className=\"mt-1\">\n <Link\n href={`/backend/rules/${log.rule.id}`}\n className=\"text-sm text-primary hover:underline\"\n >\n {log.rule.ruleName}\n </Link>\n </dd>\n </div>\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.ruleType')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground\">{log.rule.ruleType}</dd>\n </div>\n <div className=\"md:col-span-2\">\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.ruleId')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground font-mono\">{log.rule.ruleId}</dd>\n </div>\n </dl>\n ) : (\n <p className=\"text-sm text-muted-foreground\">{t('business_rules.logs.ruleDeleted')}</p>\n )}\n </div>\n\n {/* Entity Information */}\n <div className=\"rounded-lg border bg-card p-6\">\n <h2 className=\"text-lg font-semibold mb-4\">\n {t('business_rules.logs.detail.entityInfo')}\n </h2>\n <dl className=\"grid grid-cols-1 md:grid-cols-2 gap-4\">\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.entityType')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground\">{log.entityType}</dd>\n </div>\n <div>\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.eventType')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground\">\n {log.eventType || t('common.none')}\n </dd>\n </div>\n {log.entityId && (\n <div className=\"md:col-span-2\">\n <dt className=\"text-sm font-medium text-muted-foreground\">\n {t('business_rules.logs.fields.entityId')}\n </dt>\n <dd className=\"mt-1 text-sm text-foreground font-mono break-all\">\n {log.entityId}\n </dd>\n </div>\n )}\n </dl>\n </div>\n\n {/* Error Message (if present) */}\n {log.errorMessage && (\n <div className=\"rounded-lg border border-destructive bg-destructive/5 p-6\">\n <h2 className=\"text-lg font-semibold mb-4 text-destructive\">\n {t('business_rules.logs.detail.errorMessage')}\n </h2>\n <pre className=\"text-sm text-destructive whitespace-pre-wrap font-mono\">\n {log.errorMessage}\n </pre>\n </div>\n )}\n\n {/* Input Context */}\n {log.inputContext && (\n <JsonDisplay\n data={log.inputContext}\n title={t('business_rules.logs.detail.inputContext')}\n />\n )}\n\n {/* Output Context */}\n {log.outputContext && (\n <JsonDisplay\n data={log.outputContext}\n title={t('business_rules.logs.detail.outputContext')}\n />\n )}\n\n {/* Result Value */}\n {log.resultValue && (\n <JsonDisplay\n data={log.resultValue}\n title={t('business_rules.logs.detail.resultValue')}\n />\n )}\n </div>\n </PageBody>\n </Page>\n )\n}\n"],
|
|
5
|
+
"mappings": ";AA2EU,SACE,KADF;AAxEV,OAAO,UAAU;AACjB,SAAS,WAAW,iBAAiB;AACrC,SAAS,gBAAgB;AACzB,SAAS,gBAAgB;AACzB,SAAS,YAAY;AACrB,SAAS,MAAM,gBAAgB;AAC/B,SAAS,cAAc;AACvB,SAAS,eAAe;AACxB,SAAS,mBAAmB;AAC5B,SAAS,qBAAqB,oBAAoB;AA0BnC,SAAR,yBAA0C;AAC/C,QAAM,SAAS,UAAU;AACzB,QAAM,SAAS,UAAU;AAGzB,MAAI;AACJ,MAAI,QAAQ,QAAQ,MAAM,QAAQ,OAAO,IAAI,GAAG;AAC9C,YAAQ,OAAO,KAAK,CAAC;AAAA,EACvB,WAAW,QAAQ,IAAI;AACrB,YAAQ,MAAM,QAAQ,OAAO,EAAE,IAAI,OAAO,GAAG,CAAC,IAAI,OAAO;AAAA,EAC3D;AAEA,QAAM,IAAI,KAAK;AAEf,QAAM,EAAE,MAAM,KAAK,WAAW,MAAM,IAAI,SAAS;AAAA,IAC/C,UAAU,CAAC,kBAAkB,QAAQ,KAAK;AAAA,IAC1C,SAAS,YAAY;AACnB,YAAM,WAAW,MAAM,SAAS,4BAA4B,KAAK,EAAE;AACnE,UAAI,CAAC,SAAS,IAAI;AAChB,cAAM,UAAU,I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G,WAAU,6CACX,YAAE,qCAAqC,GAC1C;AAAA,UACA,oBAAC,QAAG,WAAU,oDACX,cAAI,UACP;AAAA,WACF;AAAA,SAEJ;AAAA,OACF;AAAA,IAGC,IAAI,gBACH,qBAAC,SAAI,WAAU,6DACb;AAAA,0BAAC,QAAG,WAAU,+CACX,YAAE,yCAAyC,GAC9C;AAAA,MACA,oBAAC,SAAI,WAAU,0DACZ,cAAI,cACP;AAAA,OACF;AAAA,IAID,IAAI,gBACH;AAAA,MAAC;AAAA;AAAA,QACC,MAAM,IAAI;AAAA,QACV,OAAO,EAAE,yCAAyC;AAAA;AAAA,IACpD;AAAA,IAID,IAAI,iBACH;AAAA,MAAC;AAAA;AAAA,QACC,MAAM,IAAI;AAAA,QACV,OAAO,EAAE,0CAA0C;AAAA;AAAA,IACrD;AAAA,IAID,IAAI,eACH;AAAA,MAAC;AAAA;AAAA,QACC,MAAM,IAAI;AAAA,QACV,OAAO,EAAE,wCAAwC;AAAA;AAAA,IACnD;AAAA,KAEJ,GACF,GACF;AAEJ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
|
|
2
2
|
import { BusinessRule } from "./data/entities.js";
|
|
3
|
+
import {
|
|
4
|
+
invalidateBusinessRuleDiscoveryCache,
|
|
5
|
+
resolveBusinessRuleDiscoveryCache
|
|
6
|
+
} from "./lib/rule-engine.js";
|
|
3
7
|
import * as fs from "fs";
|
|
4
8
|
import * as path from "path";
|
|
5
9
|
function parseArgs(args) {
|
|
@@ -27,6 +31,7 @@ const seedGuardRules = {
|
|
|
27
31
|
try {
|
|
28
32
|
const { resolve } = await createRequestContainer();
|
|
29
33
|
const em = resolve("em");
|
|
34
|
+
const cache = resolveBusinessRuleDiscoveryCache(resolve);
|
|
30
35
|
const rulesPath = path.join(__dirname, "../workflows/examples", "guard-rules-example.json");
|
|
31
36
|
const rulesData = JSON.parse(fs.readFileSync(rulesPath, "utf8"));
|
|
32
37
|
console.log("\u{1F9E0} Seeding guard rules...");
|
|
@@ -49,6 +54,7 @@ const seedGuardRules = {
|
|
|
49
54
|
organizationId
|
|
50
55
|
});
|
|
51
56
|
await em.persist(rule).flush();
|
|
57
|
+
await invalidateBusinessRuleDiscoveryCache(cache, tenantId, organizationId);
|
|
52
58
|
console.log(` \u2713 Seeded guard rule: ${rule.ruleName}`);
|
|
53
59
|
seededCount++;
|
|
54
60
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/modules/business_rules/cli.ts"],
|
|
4
|
-
"sourcesContent": ["import type { ModuleCli } from '@open-mercato/shared/modules/registry'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { BusinessRule } from './data/entities'\nimport * as fs from 'fs'\nimport * as path from 'path'\n\n/**\n * Parse CLI arguments\n */\nfunction parseArgs(args: string[]) {\n const result: Record<string, string> = {}\n for (let i = 0; i < args.length; i += 2) {\n const key = args[i]?.replace(/^-+/, '')\n const value = args[i + 1]\n if (key && value) {\n result[key] = value\n }\n }\n return result\n}\n\n/**\n * Seed guard rules for workflow checkout demo\n */\nconst seedGuardRules: ModuleCli = {\n command: 'seed-guard-rules',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? '')\n const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? '')\n\n if (!tenantId || !organizationId) {\n console.error('Usage: mercato business_rules seed-guard-rules --tenant <tenantId> --org <organizationId>')\n console.error(' or: mercato business_rules seed-guard-rules -t <tenantId> -o <organizationId>')\n return\n }\n\n try {\n const { resolve } = await createRequestContainer()\n const em = resolve<EntityManager>('em')\n\n // Read guard rules from workflows examples\n const rulesPath = path.join(__dirname, '../workflows/examples', 'guard-rules-example.json')\n const rulesData = JSON.parse(fs.readFileSync(rulesPath, 'utf8'))\n\n console.log('\uD83E\uDDE0 Seeding guard rules...')\n let seededCount = 0\n let skippedCount = 0\n\n for (const ruleData of rulesData) {\n // Check if rule already exists\n const existing = await em.findOne(BusinessRule, {\n ruleId: ruleData.ruleId,\n tenantId,\n organizationId,\n })\n\n if (existing) {\n console.log(` \u2298 Guard rule '${ruleData.ruleId}' already exists`)\n skippedCount++\n continue\n }\n\n // Create the business rule\n const rule = em.create(BusinessRule, {\n ...ruleData,\n tenantId,\n organizationId,\n })\n\n await em.persist(rule).flush()\n console.log(` \u2713 Seeded guard rule: ${rule.ruleName}`)\n seededCount++\n }\n\n console.log(`\\n\u2705 Guard rules seeding complete:`)\n console.log(` - Seeded: ${seededCount}`)\n console.log(` - Skipped (existing): ${skippedCount}`)\n console.log(` - Total: ${rulesData.length}`)\n } catch (error) {\n console.error('Error seeding guard rules:', error)\n throw error\n }\n },\n}\n\nconst businessRulesCliCommands = [\n seedGuardRules,\n]\n\nexport default businessRulesCliCommands\n"],
|
|
5
|
-
"mappings": "AACA,SAAS,8BAA8B;AAEvC,SAAS,oBAAoB;AAC7B,YAAY,QAAQ;AACpB,YAAY,UAAU;AAKtB,SAAS,UAAU,MAAgB;AACjC,QAAM,SAAiC,CAAC;AACxC,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,GAAG;AACvC,UAAM,MAAM,KAAK,CAAC,GAAG,QAAQ,OAAO,EAAE;AACtC,UAAM,QAAQ,KAAK,IAAI,CAAC;AACxB,QAAI,OAAO,OAAO;AAChB,aAAO,GAAG,IAAI;AAAA,IAChB;AAAA,EACF;AACA,SAAO;AACT;AAKA,MAAM,iBAA4B;AAAA,EAChC,SAAS;AAAA,EACT,MAAM,IAAI,MAAgB;AACxB,UAAM,OAAO,UAAU,IAAI;AAC3B,UAAM,WAAW,OAAO,KAAK,YAAY,KAAK,UAAU,KAAK,KAAK,EAAE;AACpE,UAAM,iBAAiB,OAAO,KAAK,kBAAkB,KAAK,SAAS,KAAK,OAAO,KAAK,KAAK,EAAE;AAE3F,QAAI,CAAC,YAAY,CAAC,gBAAgB;AAChC,cAAQ,MAAM,2FAA2F;AACzG,cAAQ,MAAM,kFAAkF;AAChG;AAAA,IACF;AAEA,QAAI;AACF,YAAM,EAAE,QAAQ,IAAI,MAAM,uBAAuB;AACjD,YAAM,KAAK,QAAuB,IAAI;
|
|
4
|
+
"sourcesContent": ["import type { ModuleCli } from '@open-mercato/shared/modules/registry'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { BusinessRule } from './data/entities'\nimport {\n invalidateBusinessRuleDiscoveryCache,\n resolveBusinessRuleDiscoveryCache,\n} from './lib/rule-engine'\nimport * as fs from 'fs'\nimport * as path from 'path'\n\n/**\n * Parse CLI arguments\n */\nfunction parseArgs(args: string[]) {\n const result: Record<string, string> = {}\n for (let i = 0; i < args.length; i += 2) {\n const key = args[i]?.replace(/^-+/, '')\n const value = args[i + 1]\n if (key && value) {\n result[key] = value\n }\n }\n return result\n}\n\n/**\n * Seed guard rules for workflow checkout demo\n */\nconst seedGuardRules: ModuleCli = {\n command: 'seed-guard-rules',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? '')\n const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? '')\n\n if (!tenantId || !organizationId) {\n console.error('Usage: mercato business_rules seed-guard-rules --tenant <tenantId> --org <organizationId>')\n console.error(' or: mercato business_rules seed-guard-rules -t <tenantId> -o <organizationId>')\n return\n }\n\n try {\n const { resolve } = await createRequestContainer()\n const em = resolve<EntityManager>('em')\n const cache = resolveBusinessRuleDiscoveryCache(resolve)\n\n // Read guard rules from workflows examples\n const rulesPath = path.join(__dirname, '../workflows/examples', 'guard-rules-example.json')\n const rulesData = JSON.parse(fs.readFileSync(rulesPath, 'utf8'))\n\n console.log('\uD83E\uDDE0 Seeding guard rules...')\n let seededCount = 0\n let skippedCount = 0\n\n for (const ruleData of rulesData) {\n // Check if rule already exists\n const existing = await em.findOne(BusinessRule, {\n ruleId: ruleData.ruleId,\n tenantId,\n organizationId,\n })\n\n if (existing) {\n console.log(` \u2298 Guard rule '${ruleData.ruleId}' already exists`)\n skippedCount++\n continue\n }\n\n // Create the business rule\n const rule = em.create(BusinessRule, {\n ...ruleData,\n tenantId,\n organizationId,\n })\n\n await em.persist(rule).flush()\n await invalidateBusinessRuleDiscoveryCache(cache, tenantId, organizationId)\n console.log(` \u2713 Seeded guard rule: ${rule.ruleName}`)\n seededCount++\n }\n\n console.log(`\\n\u2705 Guard rules seeding complete:`)\n console.log(` - Seeded: ${seededCount}`)\n console.log(` - Skipped (existing): ${skippedCount}`)\n console.log(` - Total: ${rulesData.length}`)\n } catch (error) {\n console.error('Error seeding guard rules:', error)\n throw error\n }\n },\n}\n\nconst businessRulesCliCommands = [\n seedGuardRules,\n]\n\nexport default businessRulesCliCommands\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,8BAA8B;AAEvC,SAAS,oBAAoB;AAC7B;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP,YAAY,QAAQ;AACpB,YAAY,UAAU;AAKtB,SAAS,UAAU,MAAgB;AACjC,QAAM,SAAiC,CAAC;AACxC,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,GAAG;AACvC,UAAM,MAAM,KAAK,CAAC,GAAG,QAAQ,OAAO,EAAE;AACtC,UAAM,QAAQ,KAAK,IAAI,CAAC;AACxB,QAAI,OAAO,OAAO;AAChB,aAAO,GAAG,IAAI;AAAA,IAChB;AAAA,EACF;AACA,SAAO;AACT;AAKA,MAAM,iBAA4B;AAAA,EAChC,SAAS;AAAA,EACT,MAAM,IAAI,MAAgB;AACxB,UAAM,OAAO,UAAU,IAAI;AAC3B,UAAM,WAAW,OAAO,KAAK,YAAY,KAAK,UAAU,KAAK,KAAK,EAAE;AACpE,UAAM,iBAAiB,OAAO,KAAK,kBAAkB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",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { runWithCacheTenant } from "@open-mercato/cache";
|
|
1
2
|
import { BusinessRule, RuleExecutionLog } from "../data/entities.js";
|
|
2
3
|
import * as ruleEvaluator from "./rule-evaluator.js";
|
|
3
4
|
import * as actionExecutor from "./action-executor.js";
|
|
@@ -10,6 +11,91 @@ const EXECUTION_RESULT_FAILURE = "FAILURE";
|
|
|
10
11
|
const MAX_RULES_PER_EXECUTION = 100;
|
|
11
12
|
const MAX_SINGLE_RULE_TIMEOUT_MS = 3e4;
|
|
12
13
|
const MAX_TOTAL_EXECUTION_TIMEOUT_MS = 6e4;
|
|
14
|
+
const RULE_DISCOVERY_CACHE_TTL = 5 * 60 * 1e3;
|
|
15
|
+
function normalizeCachePart(value) {
|
|
16
|
+
return encodeURIComponent(value?.trim() || "*");
|
|
17
|
+
}
|
|
18
|
+
function getRuleDiscoveryCacheKey(options) {
|
|
19
|
+
return [
|
|
20
|
+
normalizeCachePart(options.tenantId),
|
|
21
|
+
normalizeCachePart(options.organizationId),
|
|
22
|
+
normalizeCachePart(options.entityType),
|
|
23
|
+
normalizeCachePart(options.eventType),
|
|
24
|
+
normalizeCachePart(options.ruleType)
|
|
25
|
+
].join(":");
|
|
26
|
+
}
|
|
27
|
+
function isCachedRuleDiscovery(value) {
|
|
28
|
+
if (!value || typeof value !== "object") return false;
|
|
29
|
+
const ruleIds = value.ruleIds;
|
|
30
|
+
return Array.isArray(ruleIds) && ruleIds.every((entry) => typeof entry === "string");
|
|
31
|
+
}
|
|
32
|
+
function isRuleDiscoveryCache(value) {
|
|
33
|
+
if (!value || typeof value !== "object") return false;
|
|
34
|
+
const candidate = value;
|
|
35
|
+
return typeof candidate.get === "function" && typeof candidate.set === "function" && typeof candidate.deleteByTags === "function";
|
|
36
|
+
}
|
|
37
|
+
function resolveBusinessRuleDiscoveryCache(resolve) {
|
|
38
|
+
try {
|
|
39
|
+
const cache = resolve("cache");
|
|
40
|
+
return isRuleDiscoveryCache(cache) ? cache : null;
|
|
41
|
+
} catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function getRuleDiscoveryCacheTags(options) {
|
|
46
|
+
return [
|
|
47
|
+
"business_rules:discovery",
|
|
48
|
+
`business_rules:discovery:organization:${options.organizationId}`
|
|
49
|
+
];
|
|
50
|
+
}
|
|
51
|
+
async function getCachedRuleDiscovery(cache, options) {
|
|
52
|
+
if (!cache) return null;
|
|
53
|
+
let value;
|
|
54
|
+
try {
|
|
55
|
+
value = await runWithCacheTenant(
|
|
56
|
+
options.tenantId,
|
|
57
|
+
() => cache.get(getRuleDiscoveryCacheKey(options))
|
|
58
|
+
);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.warn("[business_rules] Failed to read rule discovery cache:", error);
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
return isCachedRuleDiscovery(value) ? value : null;
|
|
64
|
+
}
|
|
65
|
+
async function cacheRuleDiscovery(cache, options, rules) {
|
|
66
|
+
if (!cache) return;
|
|
67
|
+
const ruleIds = rules.map((rule) => rule.id).filter((id) => typeof id === "string" && id.length > 0);
|
|
68
|
+
if (ruleIds.length !== rules.length) return;
|
|
69
|
+
try {
|
|
70
|
+
await runWithCacheTenant(
|
|
71
|
+
options.tenantId,
|
|
72
|
+
() => cache.set(
|
|
73
|
+
getRuleDiscoveryCacheKey(options),
|
|
74
|
+
{ ruleIds },
|
|
75
|
+
{
|
|
76
|
+
ttl: RULE_DISCOVERY_CACHE_TTL,
|
|
77
|
+
tags: getRuleDiscoveryCacheTags(options)
|
|
78
|
+
}
|
|
79
|
+
)
|
|
80
|
+
);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
console.warn("[business_rules] Failed to write rule discovery cache:", error);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async function invalidateBusinessRuleDiscoveryCache(cache, tenantId, organizationId) {
|
|
86
|
+
if (!cache) return;
|
|
87
|
+
const normalizedTenantId = tenantId?.trim();
|
|
88
|
+
const normalizedOrganizationId = organizationId?.trim();
|
|
89
|
+
if (!normalizedTenantId) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const tags = normalizedOrganizationId ? [`business_rules:discovery:organization:${normalizedOrganizationId}`] : ["business_rules:discovery"];
|
|
93
|
+
try {
|
|
94
|
+
await runWithCacheTenant(normalizedTenantId, () => cache.deleteByTags(tags));
|
|
95
|
+
} catch (error) {
|
|
96
|
+
console.warn("[business_rules] Failed to invalidate rule discovery cache:", error);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
13
99
|
async function withTimeout(promise, timeoutMs, errorMessage) {
|
|
14
100
|
let timeoutId;
|
|
15
101
|
const timeoutPromise = new Promise((_, reject) => {
|
|
@@ -44,7 +130,7 @@ async function executeRules(em, context, options = {}) {
|
|
|
44
130
|
eventType: context.eventType,
|
|
45
131
|
tenantId: context.tenantId,
|
|
46
132
|
organizationId: context.organizationId
|
|
47
|
-
});
|
|
133
|
+
}, { cache: options.cache });
|
|
48
134
|
if (rules.length > MAX_RULES_PER_EXECUTION) {
|
|
49
135
|
errors.push(
|
|
50
136
|
`Rule count limit exceeded: ${rules.length} rules found, maximum is ${MAX_RULES_PER_EXECUTION}`
|
|
@@ -236,10 +322,10 @@ async function executeSingleRule(em, rule, context, options = {}) {
|
|
|
236
322
|
};
|
|
237
323
|
}
|
|
238
324
|
}
|
|
239
|
-
async function findApplicableRules(em, options) {
|
|
325
|
+
async function findApplicableRules(em, options, cacheOptions = {}) {
|
|
240
326
|
ruleDiscoveryOptionsSchema.parse(options);
|
|
241
327
|
const { entityType, eventType, tenantId, organizationId, ruleType } = options;
|
|
242
|
-
const
|
|
328
|
+
const baseWhere = {
|
|
243
329
|
entityType,
|
|
244
330
|
tenantId,
|
|
245
331
|
organizationId,
|
|
@@ -247,14 +333,32 @@ async function findApplicableRules(em, options) {
|
|
|
247
333
|
deletedAt: null
|
|
248
334
|
};
|
|
249
335
|
if (eventType) {
|
|
250
|
-
|
|
336
|
+
baseWhere.eventType = eventType;
|
|
251
337
|
}
|
|
252
338
|
if (ruleType) {
|
|
253
|
-
|
|
339
|
+
baseWhere.ruleType = ruleType;
|
|
340
|
+
}
|
|
341
|
+
const cached = await getCachedRuleDiscovery(cacheOptions.cache, options);
|
|
342
|
+
let rules;
|
|
343
|
+
if (cached) {
|
|
344
|
+
if (cached.ruleIds.length === 0) {
|
|
345
|
+
rules = [];
|
|
346
|
+
} else {
|
|
347
|
+
const cachedRules = await em.find(BusinessRule, {
|
|
348
|
+
...baseWhere,
|
|
349
|
+
id: { $in: cached.ruleIds }
|
|
350
|
+
}, {
|
|
351
|
+
orderBy: { priority: "DESC", ruleId: "ASC" }
|
|
352
|
+
});
|
|
353
|
+
const byId = new Map(cachedRules.map((rule) => [rule.id, rule]));
|
|
354
|
+
rules = cached.ruleIds.map((id) => byId.get(id)).filter((rule) => Boolean(rule));
|
|
355
|
+
}
|
|
356
|
+
} else {
|
|
357
|
+
rules = await em.find(BusinessRule, baseWhere, {
|
|
358
|
+
orderBy: { priority: "DESC", ruleId: "ASC" }
|
|
359
|
+
});
|
|
360
|
+
await cacheRuleDiscovery(cacheOptions.cache, options, rules);
|
|
254
361
|
}
|
|
255
|
-
const rules = await em.find(BusinessRule, where, {
|
|
256
|
-
orderBy: { priority: "DESC", ruleId: "ASC" }
|
|
257
|
-
});
|
|
258
362
|
const now = /* @__PURE__ */ new Date();
|
|
259
363
|
return rules.filter((rule) => {
|
|
260
364
|
if (rule.effectiveFrom && rule.effectiveFrom > now) {
|
|
@@ -539,6 +643,9 @@ export {
|
|
|
539
643
|
executeRules,
|
|
540
644
|
executeSingleRule,
|
|
541
645
|
findApplicableRules,
|
|
542
|
-
|
|
646
|
+
invalidateBusinessRuleDiscoveryCache,
|
|
647
|
+
isRuleDiscoveryCache,
|
|
648
|
+
logRuleExecution,
|
|
649
|
+
resolveBusinessRuleDiscoveryCache
|
|
543
650
|
};
|
|
544
651
|
//# sourceMappingURL=rule-engine.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/business_rules/lib/rule-engine.ts"],
|
|
4
|
-
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/core'\nimport type { EventBus } from '@open-mercato/events'\nimport { BusinessRule, RuleExecutionLog, type RuleType } from '../data/entities'\nimport * as ruleEvaluator from './rule-evaluator'\nimport * as actionExecutor from './action-executor'\nimport type { RuleEvaluationContext } from './rule-evaluator'\nimport type { ActionContext, ActionExecutionOutcome } from './action-executor'\nimport { ruleEngineContextSchema, ruleDiscoveryOptionsSchema, directRuleExecutionContextSchema, ruleIdExecutionContextSchema } from '../data/validators'\n\n/**\n * Constants\n */\nconst DEFAULT_ENTITY_ID = 'unknown'\nconst RULE_TYPE_GUARD = 'GUARD'\nconst EXECUTION_RESULT_ERROR = 'ERROR'\nconst EXECUTION_RESULT_SUCCESS = 'SUCCESS'\nconst EXECUTION_RESULT_FAILURE = 'FAILURE'\n\n/**\n * Execution limits\n */\nconst MAX_RULES_PER_EXECUTION = 100\nconst MAX_SINGLE_RULE_TIMEOUT_MS = 30000 // 30 seconds\nconst MAX_TOTAL_EXECUTION_TIMEOUT_MS = 60000 // 60 seconds\n\n/**\n * Rule execution context\n */\nexport interface RuleEngineContext {\n entityType: string\n entityId?: string\n eventType?: string\n data: any\n user?: {\n id?: string\n email?: string\n role?: string\n [key: string]: any\n }\n tenant?: {\n id?: string\n [key: string]: any\n }\n organization?: {\n id?: string\n [key: string]: any\n }\n tenantId: string\n organizationId: string\n executedBy?: string\n dryRun?: boolean\n [key: string]: any\n}\n\n/**\n * Single rule execution result\n */\nexport interface RuleExecutionResult {\n rule: BusinessRule\n conditionResult: boolean\n actionsExecuted: ActionExecutionOutcome | null\n executionTime: number\n error?: string\n logId?: string // Database log ID (if logged)\n}\n\n/**\n * Overall rule engine result\n */\nexport interface RuleEngineResult {\n allowed: boolean\n executedRules: RuleExecutionResult[]\n totalExecutionTime: number\n errors?: string[]\n logIds?: string[]\n}\n\n/**\n * Rule discovery options\n */\nexport interface RuleDiscoveryOptions {\n entityType: string\n eventType?: string\n tenantId: string\n organizationId: string\n ruleType?: RuleType\n}\n\nexport type RuleEngineExecutionOptions = {\n eventBus?: Pick<EventBus, 'emitEvent'> | null\n}\n\ntype RuleExecutionFailedPayload = {\n ruleId: string\n ruleName: string\n entityType?: string | null\n errorMessage?: string | null\n tenantId: string\n organizationId?: string | null\n}\n\n/**\n * Direct rule execution context (for executing a specific rule by ID)\n */\nexport interface DirectRuleExecutionContext {\n ruleId: string // Database UUID of the rule\n data: any\n user?: {\n id?: string\n email?: string\n role?: string\n [key: string]: any\n }\n tenantId: string\n organizationId: string\n executedBy?: string\n dryRun?: boolean\n // Optional for logging (falls back to rule's entityType)\n entityType?: string\n entityId?: string\n eventType?: string\n}\n\n/**\n * Direct rule execution result\n */\nexport interface DirectRuleExecutionResult {\n success: boolean\n ruleId: string\n ruleName: string\n conditionResult: boolean\n actionsExecuted: ActionExecutionOutcome | null\n executionTime: number\n error?: string\n logId?: string\n}\n\n/**\n * Context for executing a rule by its string rule_id identifier\n * Unlike DirectRuleExecutionContext which uses database UUID,\n * this uses the string identifier (e.g., \"workflow_checkout_inventory_available\")\n */\nexport interface RuleIdExecutionContext {\n ruleId: string // String identifier (e.g., \"workflow_checkout_inventory_available\")\n data: any\n user?: {\n id?: string\n email?: string\n role?: string\n [key: string]: any\n }\n tenantId: string\n organizationId: string\n executedBy?: string\n dryRun?: boolean\n entityType?: string\n entityId?: string\n eventType?: string\n}\n\n/**\n * Execute a function with a timeout\n */\nasync function withTimeout<T>(\n promise: Promise<T>,\n timeoutMs: number,\n errorMessage: string\n): Promise<T> {\n let timeoutId: NodeJS.Timeout\n\n const timeoutPromise = new Promise<never>((_, reject) => {\n timeoutId = setTimeout(() => {\n reject(new Error(`${errorMessage} (timeout: ${timeoutMs}ms)`))\n }, timeoutMs)\n })\n\n try {\n return await Promise.race([promise, timeoutPromise])\n } finally {\n clearTimeout(timeoutId!)\n }\n}\n\n/**\n * Execute all applicable rules for the given context\n */\nexport async function executeRules(\n em: EntityManager,\n context: RuleEngineContext,\n options: RuleEngineExecutionOptions = {}\n): Promise<RuleEngineResult> {\n // Validate input\n const validation = ruleEngineContextSchema.safeParse(context)\n if (!validation.success) {\n const validationErrors = validation.error.issues.map(e => `${e.path.join('.')}: ${e.message}`)\n return {\n allowed: false,\n executedRules: [],\n totalExecutionTime: 0,\n errors: validationErrors,\n }\n }\n\n const startTime = Date.now()\n const executedRules: RuleExecutionResult[] = []\n const errors: string[] = []\n const logIds: string[] = []\n\n try {\n // Discover applicable rules\n const rules = await findApplicableRules(em, {\n entityType: context.entityType,\n eventType: context.eventType,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n })\n\n // Check rule count limit\n if (rules.length > MAX_RULES_PER_EXECUTION) {\n errors.push(\n `Rule count limit exceeded: ${rules.length} rules found, maximum is ${MAX_RULES_PER_EXECUTION}`\n )\n return {\n allowed: false,\n executedRules: [],\n totalExecutionTime: Date.now() - startTime,\n errors,\n }\n }\n\n // Rules already sorted by database query (priority DESC, ruleId ASC)\n // Execute each rule with total timeout\n const executionPromise = (async () => {\n for (const rule of rules) {\n try {\n const ruleResult = await executeSingleRule(em, rule, context, options)\n executedRules.push(ruleResult)\n\n if (ruleResult.logId) {\n logIds.push(ruleResult.logId)\n }\n\n if (ruleResult.error) {\n errors.push(\n `Rule execution failed [ruleId=${rule.ruleId}, type=${rule.ruleType}, entityType=${context.entityType}]: ${ruleResult.error}`\n )\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error)\n errors.push(\n `Unexpected error in rule execution [ruleId=${rule.ruleId}, type=${rule.ruleType}]: ${errorMessage}`\n )\n\n if (!context.dryRun) {\n await emitRuleExecutionFailed(options.eventBus, {\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n entityType: context.entityType ?? null,\n errorMessage,\n tenantId: context.tenantId,\n organizationId: context.organizationId ?? null,\n })\n }\n\n executedRules.push({\n rule,\n conditionResult: false,\n actionsExecuted: null,\n executionTime: 0,\n error: errorMessage,\n })\n }\n }\n })()\n\n // Execute with timeout\n await withTimeout(\n executionPromise,\n MAX_TOTAL_EXECUTION_TIMEOUT_MS,\n `Total rule execution timeout [entityType=${context.entityType}]`\n )\n\n // Determine overall allowed status\n // For GUARD rules: all must pass for operation to be allowed\n const guardRules = executedRules.filter((r) => r.rule.ruleType === RULE_TYPE_GUARD)\n const allowed = guardRules.length === 0 || guardRules.every((r) => r.conditionResult)\n\n const totalExecutionTime = Date.now() - startTime\n\n return {\n allowed,\n executedRules,\n totalExecutionTime,\n errors: errors.length > 0 ? errors : undefined,\n logIds: logIds.length > 0 ? logIds : undefined,\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error)\n const stack = error instanceof Error ? error.stack : undefined\n errors.push(\n `Critical rule engine error [entityType=${context.entityType}, entityId=${context.entityId || 'unknown'}]: ${errorMessage}${stack ? `\\nStack: ${stack}` : ''}`\n )\n\n const totalExecutionTime = Date.now() - startTime\n\n return {\n allowed: false,\n executedRules,\n totalExecutionTime,\n errors,\n }\n }\n}\n\n/**\n * Execute a single rule\n */\nexport async function executeSingleRule(\n em: EntityManager,\n rule: BusinessRule,\n context: RuleEngineContext,\n options: RuleEngineExecutionOptions = {}\n): Promise<RuleExecutionResult> {\n const startTime = Date.now()\n\n try {\n // Wrap execution in timeout\n const executeWithTimeout = async () => {\n // Build evaluation context\n const evalContext: RuleEvaluationContext = {\n entityType: context.entityType,\n entityId: context.entityId,\n eventType: context.eventType,\n user: context.user,\n tenant: context.tenant,\n organization: context.organization,\n }\n\n // Evaluate rule conditions\n const result = await ruleEvaluator.evaluateSingleRule(rule, context.data, evalContext)\n\n // Check if evaluation completed (not just if conditions passed)\n if (!result.evaluationCompleted) {\n const executionTime = Date.now() - startTime\n\n let logId: string | undefined\n\n // Log failure if not dry run\n if (!context.dryRun) {\n logId = await logRuleExecution(em, {\n rule,\n context,\n conditionResult: false,\n actionsExecuted: null,\n executionTime,\n error: result.error,\n })\n\n await emitRuleExecutionFailed(options.eventBus, {\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n entityType: context.entityType ?? null,\n errorMessage: result.error ?? null,\n tenantId: context.tenantId,\n organizationId: context.organizationId ?? null,\n })\n }\n\n return {\n rule,\n conditionResult: false,\n actionsExecuted: null,\n executionTime,\n error: result.error,\n logId,\n }\n }\n\n // Evaluation completed successfully - determine which actions to execute\n const actions = result.conditionsPassed ? rule.successActions : rule.failureActions\n\n let actionsExecuted: ActionExecutionOutcome | null = null\n\n if (actions && Array.isArray(actions) && actions.length > 0) {\n // Build action context\n const actionContext: ActionContext = {\n ...evalContext,\n data: context.data,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n }\n\n // Execute actions\n actionsExecuted = await actionExecutor.executeActions(actions, actionContext)\n }\n\n const executionTime = Date.now() - startTime\n\n let logId: string | undefined\n\n // Log execution if not dry run\n if (!context.dryRun) {\n logId = await logRuleExecution(em, {\n rule,\n context,\n conditionResult: result.conditionsPassed,\n actionsExecuted,\n executionTime,\n })\n }\n\n return {\n rule,\n conditionResult: result.conditionsPassed,\n actionsExecuted,\n executionTime,\n logId,\n }\n }\n\n // Execute with single rule timeout\n return await withTimeout(\n executeWithTimeout(),\n MAX_SINGLE_RULE_TIMEOUT_MS,\n `Single rule execution timeout [ruleId=${rule.ruleId}]`\n )\n } catch (error) {\n const executionTime = Date.now() - startTime\n const errorMessage = error instanceof Error ? error.message : String(error)\n const enhancedError = `Failed to execute rule [ruleId=${rule.ruleId}, entityType=${context.entityType}]: ${errorMessage}`\n\n let logId: string | undefined\n\n // Log error if not dry run\n if (!context.dryRun) {\n logId = await logRuleExecution(em, {\n rule,\n context,\n conditionResult: false,\n actionsExecuted: null,\n executionTime,\n error: enhancedError,\n })\n\n await emitRuleExecutionFailed(options.eventBus, {\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n entityType: context.entityType ?? null,\n errorMessage: enhancedError,\n tenantId: context.tenantId,\n organizationId: context.organizationId ?? null,\n })\n }\n\n return {\n rule,\n conditionResult: false,\n actionsExecuted: null,\n executionTime,\n error: enhancedError,\n logId,\n }\n }\n}\n\n/**\n * Find all applicable rules for the given criteria\n */\nexport async function findApplicableRules(\n em: EntityManager,\n options: RuleDiscoveryOptions\n): Promise<BusinessRule[]> {\n // Validate input\n ruleDiscoveryOptionsSchema.parse(options)\n\n const { entityType, eventType, tenantId, organizationId, ruleType } = options\n\n const where: Partial<BusinessRule> = {\n entityType,\n tenantId,\n organizationId,\n enabled: true,\n deletedAt: null,\n }\n\n if (eventType) {\n where.eventType = eventType\n }\n\n if (ruleType) {\n where.ruleType = ruleType\n }\n\n const rules = await em.find(BusinessRule, where, {\n orderBy: { priority: 'DESC', ruleId: 'ASC' },\n })\n\n // Filter by effective date range\n const now = new Date()\n return rules.filter((rule) => {\n if (rule.effectiveFrom && rule.effectiveFrom > now) {\n return false\n }\n if (rule.effectiveTo && rule.effectiveTo < now) {\n return false\n }\n return true\n })\n}\n\n/**\n * Execute a specific rule by its database UUID\n * This bypasses the entityType/eventType discovery mechanism and directly executes the rule\n */\nexport async function executeRuleById(\n em: EntityManager,\n context: DirectRuleExecutionContext\n): Promise<DirectRuleExecutionResult> {\n const startTime = Date.now()\n\n // Validate input\n const validation = directRuleExecutionContextSchema.safeParse(context)\n if (!validation.success) {\n const validationErrors = validation.error.issues.map(e => `${e.path.join('.')}: ${e.message}`)\n return {\n success: false,\n ruleId: context.ruleId,\n ruleName: 'Unknown',\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: `Validation failed: ${validationErrors.join(', ')}`,\n }\n }\n\n // Fetch rule by ID with tenant/org validation\n const rule = await em.findOne(BusinessRule, {\n id: context.ruleId,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n deletedAt: null,\n })\n\n if (!rule) {\n return {\n success: false,\n ruleId: context.ruleId,\n ruleName: 'Unknown',\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: 'Rule not found',\n }\n }\n\n if (!rule.enabled) {\n return {\n success: false,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: 'Rule is disabled',\n }\n }\n\n // Check effective date range\n const now = new Date()\n if (rule.effectiveFrom && rule.effectiveFrom > now) {\n return {\n success: false,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: `Rule is not yet effective (starts ${rule.effectiveFrom.toISOString()})`,\n }\n }\n if (rule.effectiveTo && rule.effectiveTo < now) {\n return {\n success: false,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: `Rule has expired (ended ${rule.effectiveTo.toISOString()})`,\n }\n }\n\n // Build RuleEngineContext (use provided entityType or fall back to rule's)\n const engineContext: RuleEngineContext = {\n entityType: context.entityType || rule.entityType,\n entityId: context.entityId,\n eventType: context.eventType || rule.eventType || undefined,\n data: context.data,\n user: context.user,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n executedBy: context.executedBy,\n dryRun: context.dryRun,\n }\n\n // Execute via existing executeSingleRule\n const result = await executeSingleRule(em, rule, engineContext)\n\n return {\n success: !result.error,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: result.conditionResult,\n actionsExecuted: result.actionsExecuted,\n executionTime: result.executionTime,\n error: result.error,\n logId: result.logId,\n }\n}\n\n/**\n * Execute a rule by its string rule_id identifier\n * Looks up rule by rule_id (string column) + tenant_id (unique constraint)\n * This is useful for workflow conditions that reference rules by their string identifiers\n */\nexport async function executeRuleByRuleId(\n em: EntityManager,\n context: RuleIdExecutionContext\n): Promise<DirectRuleExecutionResult> {\n const startTime = Date.now()\n\n // Validate input\n const validation = ruleIdExecutionContextSchema.safeParse(context)\n if (!validation.success) {\n const validationErrors = validation.error.issues.map(e => `${e.path.join('.')}: ${e.message}`)\n return {\n success: false,\n ruleId: context.ruleId || 'unknown',\n ruleName: 'Unknown',\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: `Validation failed: ${validationErrors.join(', ')}`,\n }\n }\n\n // Fetch rule by rule_id (string identifier) + tenant/org\n const rule = await em.findOne(BusinessRule, {\n ruleId: context.ruleId, // String identifier column\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n deletedAt: null,\n })\n\n if (!rule) {\n return {\n success: false,\n ruleId: context.ruleId,\n ruleName: 'Unknown',\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: 'Rule not found',\n }\n }\n\n if (!rule.enabled) {\n return {\n success: false,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: 'Rule is disabled',\n }\n }\n\n // Check effective date range\n const now = new Date()\n if (rule.effectiveFrom && rule.effectiveFrom > now) {\n return {\n success: false,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: `Rule is not yet effective (starts ${rule.effectiveFrom.toISOString()})`,\n }\n }\n if (rule.effectiveTo && rule.effectiveTo < now) {\n return {\n success: false,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: `Rule has expired (ended ${rule.effectiveTo.toISOString()})`,\n }\n }\n\n // Build RuleEngineContext (use provided entityType or fall back to rule's)\n const engineContext: RuleEngineContext = {\n entityType: context.entityType || rule.entityType,\n entityId: context.entityId,\n eventType: context.eventType || rule.eventType || undefined,\n data: context.data,\n user: context.user,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n executedBy: context.executedBy,\n dryRun: context.dryRun,\n }\n\n // Execute via existing executeSingleRule\n const result = await executeSingleRule(em, rule, engineContext)\n\n return {\n success: !result.error,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: result.conditionResult,\n actionsExecuted: result.actionsExecuted,\n executionTime: result.executionTime,\n error: result.error,\n logId: result.logId,\n }\n}\n\n/**\n * Sensitive field patterns to exclude from logs\n */\nconst SENSITIVE_FIELD_PATTERNS = [\n /password/i,\n /passwd/i,\n /pwd/i,\n /secret/i,\n /token/i,\n /api[_-]?key/i,\n /auth/i,\n /credit[_-]?card/i,\n /card[_-]?number/i,\n /cvv/i,\n /ssn/i,\n /social[_-]?security/i,\n /tax[_-]?id/i,\n /driver[_-]?license/i,\n /passport/i,\n]\n\n/**\n * Maximum depth for nested object sanitization\n */\nconst MAX_SANITIZATION_DEPTH = 5\n\n/**\n * Sanitize data for logging by removing sensitive fields\n */\nfunction sanitizeForLogging(data: any, depth: number = 0): any {\n // Prevent infinite recursion\n if (depth > MAX_SANITIZATION_DEPTH) {\n return '[Max depth exceeded]'\n }\n\n // Handle null/undefined\n if (data === null || data === undefined) {\n return data\n }\n\n // Handle primitives\n if (typeof data !== 'object') {\n return data\n }\n\n // Handle arrays\n if (Array.isArray(data)) {\n return data.map(item => sanitizeForLogging(item, depth + 1))\n }\n\n // Handle objects\n const sanitized: Record<string, any> = {}\n\n for (const [key, value] of Object.entries(data)) {\n // Check if field name matches sensitive patterns\n const isSensitive = SENSITIVE_FIELD_PATTERNS.some(pattern => pattern.test(key))\n\n if (isSensitive) {\n sanitized[key] = '[REDACTED]'\n } else if (typeof value === 'object' && value !== null) {\n sanitized[key] = sanitizeForLogging(value, depth + 1)\n } else {\n sanitized[key] = value\n }\n }\n\n return sanitized\n}\n\n/**\n * Sanitize user object for logging (keep only safe fields)\n */\nfunction sanitizeUser(user: any): any {\n if (!user) {\n return undefined\n }\n\n // Only log safe user fields\n return {\n id: user.id,\n role: user.role,\n // Don't log: email, name, phone, address, etc.\n }\n}\n\n/**\n * Log rule execution to database\n */\ninterface LogExecutionOptions {\n rule: BusinessRule\n context: RuleEngineContext\n conditionResult: boolean\n actionsExecuted: ActionExecutionOutcome | null\n executionTime: number\n error?: string\n}\n\nexport async function logRuleExecution(\n em: EntityManager,\n options: LogExecutionOptions\n): Promise<string> {\n const { rule, context, conditionResult, actionsExecuted, executionTime, error } = options\n\n const executionResult: 'SUCCESS' | 'FAILURE' | 'ERROR' = error\n ? EXECUTION_RESULT_ERROR\n : conditionResult\n ? EXECUTION_RESULT_SUCCESS\n : EXECUTION_RESULT_FAILURE\n\n const log = em.create(RuleExecutionLog, {\n rule,\n entityId: context.entityId || DEFAULT_ENTITY_ID,\n entityType: context.entityType,\n executionResult,\n inputContext: {\n data: sanitizeForLogging(context.data),\n eventType: context.eventType,\n user: sanitizeUser(context.user),\n },\n outputContext: actionsExecuted\n ? {\n conditionResult,\n actionsExecuted: actionsExecuted.results.map((r) => ({\n type: r.action.type,\n success: r.success,\n error: r.error,\n })),\n }\n : null,\n errorMessage: error || null,\n executionTimeMs: executionTime,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n executedBy: context.executedBy || null,\n })\n\n await em.persist(log).flush()\n\n return String(log.id)\n}\n\nasync function emitRuleExecutionFailed(\n eventBus: Pick<EventBus, 'emitEvent'> | null | undefined,\n payload: RuleExecutionFailedPayload\n): Promise<void> {\n if (!eventBus?.emitEvent) return\n\n await eventBus.emitEvent('business_rules.rule.execution_failed', payload).catch(() => undefined)\n}\n"],
|
|
5
|
-
"mappings": "AAEA,SAAS,cAAc,wBAAuC;AAC9D,YAAY,mBAAmB;AAC/B,YAAY,oBAAoB;AAGhC,SAAS,yBAAyB,4BAA4B,kCAAkC,oCAAoC;AAKpI,MAAM,oBAAoB;AAC1B,MAAM,kBAAkB;AACxB,MAAM,yBAAyB;AAC/B,MAAM,2BAA2B;AACjC,MAAM,2BAA2B;AAKjC,MAAM,0BAA0B;AAChC,MAAM,6BAA6B;AACnC,MAAM,iCAAiC;AA4IvC,eAAe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C;AAED,QAAM,GAAG,QAAQ,GAAG,EAAE,MAAM;AAE5B,SAAO,OAAO,IAAI,EAAE;AACtB;AAEA,eAAe,wBACb,UACA,SACe;AACf,MAAI,CAAC,UAAU,UAAW;AAE1B,QAAM,SAAS,UAAU,wCAAwC,OAAO,EAAE,MAAM,MAAM,MAAS;AACjG;",
|
|
4
|
+
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/core'\nimport { runWithCacheTenant, type CacheStrategy } from '@open-mercato/cache'\nimport type { EventBus } from '@open-mercato/events'\nimport { BusinessRule, RuleExecutionLog, type RuleType } from '../data/entities'\nimport * as ruleEvaluator from './rule-evaluator'\nimport * as actionExecutor from './action-executor'\nimport type { RuleEvaluationContext } from './rule-evaluator'\nimport type { ActionContext, ActionExecutionOutcome } from './action-executor'\nimport { ruleEngineContextSchema, ruleDiscoveryOptionsSchema, directRuleExecutionContextSchema, ruleIdExecutionContextSchema } from '../data/validators'\n\n/**\n * Constants\n */\nconst DEFAULT_ENTITY_ID = 'unknown'\nconst RULE_TYPE_GUARD = 'GUARD'\nconst EXECUTION_RESULT_ERROR = 'ERROR'\nconst EXECUTION_RESULT_SUCCESS = 'SUCCESS'\nconst EXECUTION_RESULT_FAILURE = 'FAILURE'\n\n/**\n * Execution limits\n */\nconst MAX_RULES_PER_EXECUTION = 100\nconst MAX_SINGLE_RULE_TIMEOUT_MS = 30000 // 30 seconds\nconst MAX_TOTAL_EXECUTION_TIMEOUT_MS = 60000 // 60 seconds\nconst RULE_DISCOVERY_CACHE_TTL = 5 * 60 * 1000\n\n/**\n * Rule execution context\n */\nexport interface RuleEngineContext {\n entityType: string\n entityId?: string\n eventType?: string\n data: any\n user?: {\n id?: string\n email?: string\n role?: string\n [key: string]: any\n }\n tenant?: {\n id?: string\n [key: string]: any\n }\n organization?: {\n id?: string\n [key: string]: any\n }\n tenantId: string\n organizationId: string\n executedBy?: string\n dryRun?: boolean\n [key: string]: any\n}\n\n/**\n * Single rule execution result\n */\nexport interface RuleExecutionResult {\n rule: BusinessRule\n conditionResult: boolean\n actionsExecuted: ActionExecutionOutcome | null\n executionTime: number\n error?: string\n logId?: string // Database log ID (if logged)\n}\n\n/**\n * Overall rule engine result\n */\nexport interface RuleEngineResult {\n allowed: boolean\n executedRules: RuleExecutionResult[]\n totalExecutionTime: number\n errors?: string[]\n logIds?: string[]\n}\n\n/**\n * Rule discovery options\n */\nexport interface RuleDiscoveryOptions {\n entityType: string\n eventType?: string\n tenantId: string\n organizationId: string\n ruleType?: RuleType\n}\n\ntype CachedRuleDiscovery = {\n ruleIds: string[]\n}\n\nexport type RuleDiscoveryCache = Pick<CacheStrategy, 'get' | 'set' | 'deleteByTags'>\n\nexport type RuleDiscoveryCacheOptions = {\n cache?: RuleDiscoveryCache | null\n}\n\nfunction normalizeCachePart(value: string | null | undefined): string {\n return encodeURIComponent(value?.trim() || '*')\n}\n\nfunction getRuleDiscoveryCacheKey(options: RuleDiscoveryOptions): string {\n return [\n normalizeCachePart(options.tenantId),\n normalizeCachePart(options.organizationId),\n normalizeCachePart(options.entityType),\n normalizeCachePart(options.eventType),\n normalizeCachePart(options.ruleType),\n ].join(':')\n}\n\nfunction isCachedRuleDiscovery(value: unknown): value is CachedRuleDiscovery {\n if (!value || typeof value !== 'object') return false\n const ruleIds = (value as CachedRuleDiscovery).ruleIds\n return Array.isArray(ruleIds) && ruleIds.every((entry) => typeof entry === 'string')\n}\n\nexport function isRuleDiscoveryCache(value: unknown): value is RuleDiscoveryCache {\n if (!value || typeof value !== 'object') return false\n const candidate = value as Partial<RuleDiscoveryCache>\n return (\n typeof candidate.get === 'function'\n && typeof candidate.set === 'function'\n && typeof candidate.deleteByTags === 'function'\n )\n}\n\nexport function resolveBusinessRuleDiscoveryCache(resolve: (token: string) => unknown): RuleDiscoveryCache | null {\n try {\n const cache = resolve('cache')\n return isRuleDiscoveryCache(cache) ? cache : null\n } catch {\n return null\n }\n}\n\nfunction getRuleDiscoveryCacheTags(options: Pick<RuleDiscoveryOptions, 'organizationId'>): string[] {\n return [\n 'business_rules:discovery',\n `business_rules:discovery:organization:${options.organizationId}`,\n ]\n}\n\nasync function getCachedRuleDiscovery(\n cache: RuleDiscoveryCache | null | undefined,\n options: RuleDiscoveryOptions,\n): Promise<CachedRuleDiscovery | null> {\n if (!cache) return null\n\n let value: unknown\n try {\n value = await runWithCacheTenant(options.tenantId, () =>\n cache.get(getRuleDiscoveryCacheKey(options))\n )\n } catch (error) {\n console.warn('[business_rules] Failed to read rule discovery cache:', error)\n return null\n }\n\n return isCachedRuleDiscovery(value) ? value : null\n}\n\nasync function cacheRuleDiscovery(\n cache: RuleDiscoveryCache | null | undefined,\n options: RuleDiscoveryOptions,\n rules: BusinessRule[],\n): Promise<void> {\n if (!cache) return\n\n const ruleIds = rules\n .map((rule) => rule.id)\n .filter((id): id is string => typeof id === 'string' && id.length > 0)\n\n if (ruleIds.length !== rules.length) return\n\n try {\n await runWithCacheTenant(options.tenantId, () =>\n cache.set(\n getRuleDiscoveryCacheKey(options),\n { ruleIds },\n {\n ttl: RULE_DISCOVERY_CACHE_TTL,\n tags: getRuleDiscoveryCacheTags(options),\n },\n )\n )\n } catch (error) {\n console.warn('[business_rules] Failed to write rule discovery cache:', error)\n }\n}\n\nexport async function invalidateBusinessRuleDiscoveryCache(\n cache: RuleDiscoveryCache | null | undefined,\n tenantId?: string | null,\n organizationId?: string | null,\n): Promise<void> {\n if (!cache) return\n\n const normalizedTenantId = tenantId?.trim()\n const normalizedOrganizationId = organizationId?.trim()\n\n if (!normalizedTenantId) {\n return\n }\n\n const tags = normalizedOrganizationId\n ? [`business_rules:discovery:organization:${normalizedOrganizationId}`]\n : ['business_rules:discovery']\n\n try {\n await runWithCacheTenant(normalizedTenantId, () => cache.deleteByTags(tags))\n } catch (error) {\n console.warn('[business_rules] Failed to invalidate rule discovery cache:', error)\n }\n}\n\nexport type RuleEngineExecutionOptions = {\n eventBus?: Pick<EventBus, 'emitEvent'> | null\n cache?: RuleDiscoveryCache | null\n}\n\ntype RuleExecutionFailedPayload = {\n ruleId: string\n ruleName: string\n entityType?: string | null\n errorMessage?: string | null\n tenantId: string\n organizationId?: string | null\n}\n\n/**\n * Direct rule execution context (for executing a specific rule by ID)\n */\nexport interface DirectRuleExecutionContext {\n ruleId: string // Database UUID of the rule\n data: any\n user?: {\n id?: string\n email?: string\n role?: string\n [key: string]: any\n }\n tenantId: string\n organizationId: string\n executedBy?: string\n dryRun?: boolean\n // Optional for logging (falls back to rule's entityType)\n entityType?: string\n entityId?: string\n eventType?: string\n}\n\n/**\n * Direct rule execution result\n */\nexport interface DirectRuleExecutionResult {\n success: boolean\n ruleId: string\n ruleName: string\n conditionResult: boolean\n actionsExecuted: ActionExecutionOutcome | null\n executionTime: number\n error?: string\n logId?: string\n}\n\n/**\n * Context for executing a rule by its string rule_id identifier\n * Unlike DirectRuleExecutionContext which uses database UUID,\n * this uses the string identifier (e.g., \"workflow_checkout_inventory_available\")\n */\nexport interface RuleIdExecutionContext {\n ruleId: string // String identifier (e.g., \"workflow_checkout_inventory_available\")\n data: any\n user?: {\n id?: string\n email?: string\n role?: string\n [key: string]: any\n }\n tenantId: string\n organizationId: string\n executedBy?: string\n dryRun?: boolean\n entityType?: string\n entityId?: string\n eventType?: string\n}\n\n/**\n * Execute a function with a timeout\n */\nasync function withTimeout<T>(\n promise: Promise<T>,\n timeoutMs: number,\n errorMessage: string\n): Promise<T> {\n let timeoutId: NodeJS.Timeout\n\n const timeoutPromise = new Promise<never>((_, reject) => {\n timeoutId = setTimeout(() => {\n reject(new Error(`${errorMessage} (timeout: ${timeoutMs}ms)`))\n }, timeoutMs)\n })\n\n try {\n return await Promise.race([promise, timeoutPromise])\n } finally {\n clearTimeout(timeoutId!)\n }\n}\n\n/**\n * Execute all applicable rules for the given context\n */\nexport async function executeRules(\n em: EntityManager,\n context: RuleEngineContext,\n options: RuleEngineExecutionOptions = {}\n): Promise<RuleEngineResult> {\n // Validate input\n const validation = ruleEngineContextSchema.safeParse(context)\n if (!validation.success) {\n const validationErrors = validation.error.issues.map(e => `${e.path.join('.')}: ${e.message}`)\n return {\n allowed: false,\n executedRules: [],\n totalExecutionTime: 0,\n errors: validationErrors,\n }\n }\n\n const startTime = Date.now()\n const executedRules: RuleExecutionResult[] = []\n const errors: string[] = []\n const logIds: string[] = []\n\n try {\n // Discover applicable rules\n const rules = await findApplicableRules(em, {\n entityType: context.entityType,\n eventType: context.eventType,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n }, { cache: options.cache })\n\n // Check rule count limit\n if (rules.length > MAX_RULES_PER_EXECUTION) {\n errors.push(\n `Rule count limit exceeded: ${rules.length} rules found, maximum is ${MAX_RULES_PER_EXECUTION}`\n )\n return {\n allowed: false,\n executedRules: [],\n totalExecutionTime: Date.now() - startTime,\n errors,\n }\n }\n\n // Rules already sorted by database query (priority DESC, ruleId ASC)\n // Execute each rule with total timeout\n const executionPromise = (async () => {\n for (const rule of rules) {\n try {\n const ruleResult = await executeSingleRule(em, rule, context, options)\n executedRules.push(ruleResult)\n\n if (ruleResult.logId) {\n logIds.push(ruleResult.logId)\n }\n\n if (ruleResult.error) {\n errors.push(\n `Rule execution failed [ruleId=${rule.ruleId}, type=${rule.ruleType}, entityType=${context.entityType}]: ${ruleResult.error}`\n )\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error)\n errors.push(\n `Unexpected error in rule execution [ruleId=${rule.ruleId}, type=${rule.ruleType}]: ${errorMessage}`\n )\n\n if (!context.dryRun) {\n await emitRuleExecutionFailed(options.eventBus, {\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n entityType: context.entityType ?? null,\n errorMessage,\n tenantId: context.tenantId,\n organizationId: context.organizationId ?? null,\n })\n }\n\n executedRules.push({\n rule,\n conditionResult: false,\n actionsExecuted: null,\n executionTime: 0,\n error: errorMessage,\n })\n }\n }\n })()\n\n // Execute with timeout\n await withTimeout(\n executionPromise,\n MAX_TOTAL_EXECUTION_TIMEOUT_MS,\n `Total rule execution timeout [entityType=${context.entityType}]`\n )\n\n // Determine overall allowed status\n // For GUARD rules: all must pass for operation to be allowed\n const guardRules = executedRules.filter((r) => r.rule.ruleType === RULE_TYPE_GUARD)\n const allowed = guardRules.length === 0 || guardRules.every((r) => r.conditionResult)\n\n const totalExecutionTime = Date.now() - startTime\n\n return {\n allowed,\n executedRules,\n totalExecutionTime,\n errors: errors.length > 0 ? errors : undefined,\n logIds: logIds.length > 0 ? logIds : undefined,\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error)\n const stack = error instanceof Error ? error.stack : undefined\n errors.push(\n `Critical rule engine error [entityType=${context.entityType}, entityId=${context.entityId || 'unknown'}]: ${errorMessage}${stack ? `\\nStack: ${stack}` : ''}`\n )\n\n const totalExecutionTime = Date.now() - startTime\n\n return {\n allowed: false,\n executedRules,\n totalExecutionTime,\n errors,\n }\n }\n}\n\n/**\n * Execute a single rule\n */\nexport async function executeSingleRule(\n em: EntityManager,\n rule: BusinessRule,\n context: RuleEngineContext,\n options: RuleEngineExecutionOptions = {}\n): Promise<RuleExecutionResult> {\n const startTime = Date.now()\n\n try {\n // Wrap execution in timeout\n const executeWithTimeout = async () => {\n // Build evaluation context\n const evalContext: RuleEvaluationContext = {\n entityType: context.entityType,\n entityId: context.entityId,\n eventType: context.eventType,\n user: context.user,\n tenant: context.tenant,\n organization: context.organization,\n }\n\n // Evaluate rule conditions\n const result = await ruleEvaluator.evaluateSingleRule(rule, context.data, evalContext)\n\n // Check if evaluation completed (not just if conditions passed)\n if (!result.evaluationCompleted) {\n const executionTime = Date.now() - startTime\n\n let logId: string | undefined\n\n // Log failure if not dry run\n if (!context.dryRun) {\n logId = await logRuleExecution(em, {\n rule,\n context,\n conditionResult: false,\n actionsExecuted: null,\n executionTime,\n error: result.error,\n })\n\n await emitRuleExecutionFailed(options.eventBus, {\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n entityType: context.entityType ?? null,\n errorMessage: result.error ?? null,\n tenantId: context.tenantId,\n organizationId: context.organizationId ?? null,\n })\n }\n\n return {\n rule,\n conditionResult: false,\n actionsExecuted: null,\n executionTime,\n error: result.error,\n logId,\n }\n }\n\n // Evaluation completed successfully - determine which actions to execute\n const actions = result.conditionsPassed ? rule.successActions : rule.failureActions\n\n let actionsExecuted: ActionExecutionOutcome | null = null\n\n if (actions && Array.isArray(actions) && actions.length > 0) {\n // Build action context\n const actionContext: ActionContext = {\n ...evalContext,\n data: context.data,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n }\n\n // Execute actions\n actionsExecuted = await actionExecutor.executeActions(actions, actionContext)\n }\n\n const executionTime = Date.now() - startTime\n\n let logId: string | undefined\n\n // Log execution if not dry run\n if (!context.dryRun) {\n logId = await logRuleExecution(em, {\n rule,\n context,\n conditionResult: result.conditionsPassed,\n actionsExecuted,\n executionTime,\n })\n }\n\n return {\n rule,\n conditionResult: result.conditionsPassed,\n actionsExecuted,\n executionTime,\n logId,\n }\n }\n\n // Execute with single rule timeout\n return await withTimeout(\n executeWithTimeout(),\n MAX_SINGLE_RULE_TIMEOUT_MS,\n `Single rule execution timeout [ruleId=${rule.ruleId}]`\n )\n } catch (error) {\n const executionTime = Date.now() - startTime\n const errorMessage = error instanceof Error ? error.message : String(error)\n const enhancedError = `Failed to execute rule [ruleId=${rule.ruleId}, entityType=${context.entityType}]: ${errorMessage}`\n\n let logId: string | undefined\n\n // Log error if not dry run\n if (!context.dryRun) {\n logId = await logRuleExecution(em, {\n rule,\n context,\n conditionResult: false,\n actionsExecuted: null,\n executionTime,\n error: enhancedError,\n })\n\n await emitRuleExecutionFailed(options.eventBus, {\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n entityType: context.entityType ?? null,\n errorMessage: enhancedError,\n tenantId: context.tenantId,\n organizationId: context.organizationId ?? null,\n })\n }\n\n return {\n rule,\n conditionResult: false,\n actionsExecuted: null,\n executionTime,\n error: enhancedError,\n logId,\n }\n }\n}\n\n/**\n * Find all applicable rules for the given criteria\n */\nexport async function findApplicableRules(\n em: EntityManager,\n options: RuleDiscoveryOptions,\n cacheOptions: RuleDiscoveryCacheOptions = {},\n): Promise<BusinessRule[]> {\n // Validate input\n ruleDiscoveryOptionsSchema.parse(options)\n\n const { entityType, eventType, tenantId, organizationId, ruleType } = options\n const baseWhere: Record<string, unknown> = {\n entityType,\n tenantId,\n organizationId,\n enabled: true,\n deletedAt: null,\n }\n\n if (eventType) {\n baseWhere.eventType = eventType\n }\n\n if (ruleType) {\n baseWhere.ruleType = ruleType\n }\n\n const cached = await getCachedRuleDiscovery(cacheOptions.cache, options)\n let rules: BusinessRule[]\n\n if (cached) {\n if (cached.ruleIds.length === 0) {\n rules = []\n } else {\n const cachedRules = await em.find(BusinessRule, {\n ...baseWhere,\n id: { $in: cached.ruleIds },\n }, {\n orderBy: { priority: 'DESC', ruleId: 'ASC' },\n } as any)\n const byId = new Map(cachedRules.map((rule) => [rule.id, rule]))\n rules = cached.ruleIds\n .map((id) => byId.get(id))\n .filter((rule): rule is BusinessRule => Boolean(rule))\n }\n } else {\n rules = await em.find(BusinessRule, baseWhere as Partial<BusinessRule>, {\n orderBy: { priority: 'DESC', ruleId: 'ASC' },\n })\n\n await cacheRuleDiscovery(cacheOptions.cache, options, rules)\n }\n\n // Filter by effective date range\n const now = new Date()\n return rules.filter((rule) => {\n if (rule.effectiveFrom && rule.effectiveFrom > now) {\n return false\n }\n if (rule.effectiveTo && rule.effectiveTo < now) {\n return false\n }\n return true\n })\n}\n\n/**\n * Execute a specific rule by its database UUID\n * This bypasses the entityType/eventType discovery mechanism and directly executes the rule\n */\nexport async function executeRuleById(\n em: EntityManager,\n context: DirectRuleExecutionContext\n): Promise<DirectRuleExecutionResult> {\n const startTime = Date.now()\n\n // Validate input\n const validation = directRuleExecutionContextSchema.safeParse(context)\n if (!validation.success) {\n const validationErrors = validation.error.issues.map(e => `${e.path.join('.')}: ${e.message}`)\n return {\n success: false,\n ruleId: context.ruleId,\n ruleName: 'Unknown',\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: `Validation failed: ${validationErrors.join(', ')}`,\n }\n }\n\n // Fetch rule by ID with tenant/org validation\n const rule = await em.findOne(BusinessRule, {\n id: context.ruleId,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n deletedAt: null,\n })\n\n if (!rule) {\n return {\n success: false,\n ruleId: context.ruleId,\n ruleName: 'Unknown',\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: 'Rule not found',\n }\n }\n\n if (!rule.enabled) {\n return {\n success: false,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: 'Rule is disabled',\n }\n }\n\n // Check effective date range\n const now = new Date()\n if (rule.effectiveFrom && rule.effectiveFrom > now) {\n return {\n success: false,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: `Rule is not yet effective (starts ${rule.effectiveFrom.toISOString()})`,\n }\n }\n if (rule.effectiveTo && rule.effectiveTo < now) {\n return {\n success: false,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: `Rule has expired (ended ${rule.effectiveTo.toISOString()})`,\n }\n }\n\n // Build RuleEngineContext (use provided entityType or fall back to rule's)\n const engineContext: RuleEngineContext = {\n entityType: context.entityType || rule.entityType,\n entityId: context.entityId,\n eventType: context.eventType || rule.eventType || undefined,\n data: context.data,\n user: context.user,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n executedBy: context.executedBy,\n dryRun: context.dryRun,\n }\n\n // Execute via existing executeSingleRule\n const result = await executeSingleRule(em, rule, engineContext)\n\n return {\n success: !result.error,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: result.conditionResult,\n actionsExecuted: result.actionsExecuted,\n executionTime: result.executionTime,\n error: result.error,\n logId: result.logId,\n }\n}\n\n/**\n * Execute a rule by its string rule_id identifier\n * Looks up rule by rule_id (string column) + tenant_id (unique constraint)\n * This is useful for workflow conditions that reference rules by their string identifiers\n */\nexport async function executeRuleByRuleId(\n em: EntityManager,\n context: RuleIdExecutionContext\n): Promise<DirectRuleExecutionResult> {\n const startTime = Date.now()\n\n // Validate input\n const validation = ruleIdExecutionContextSchema.safeParse(context)\n if (!validation.success) {\n const validationErrors = validation.error.issues.map(e => `${e.path.join('.')}: ${e.message}`)\n return {\n success: false,\n ruleId: context.ruleId || 'unknown',\n ruleName: 'Unknown',\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: `Validation failed: ${validationErrors.join(', ')}`,\n }\n }\n\n // Fetch rule by rule_id (string identifier) + tenant/org\n const rule = await em.findOne(BusinessRule, {\n ruleId: context.ruleId, // String identifier column\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n deletedAt: null,\n })\n\n if (!rule) {\n return {\n success: false,\n ruleId: context.ruleId,\n ruleName: 'Unknown',\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: 'Rule not found',\n }\n }\n\n if (!rule.enabled) {\n return {\n success: false,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: 'Rule is disabled',\n }\n }\n\n // Check effective date range\n const now = new Date()\n if (rule.effectiveFrom && rule.effectiveFrom > now) {\n return {\n success: false,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: `Rule is not yet effective (starts ${rule.effectiveFrom.toISOString()})`,\n }\n }\n if (rule.effectiveTo && rule.effectiveTo < now) {\n return {\n success: false,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: false,\n actionsExecuted: null,\n executionTime: Date.now() - startTime,\n error: `Rule has expired (ended ${rule.effectiveTo.toISOString()})`,\n }\n }\n\n // Build RuleEngineContext (use provided entityType or fall back to rule's)\n const engineContext: RuleEngineContext = {\n entityType: context.entityType || rule.entityType,\n entityId: context.entityId,\n eventType: context.eventType || rule.eventType || undefined,\n data: context.data,\n user: context.user,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n executedBy: context.executedBy,\n dryRun: context.dryRun,\n }\n\n // Execute via existing executeSingleRule\n const result = await executeSingleRule(em, rule, engineContext)\n\n return {\n success: !result.error,\n ruleId: rule.ruleId,\n ruleName: rule.ruleName,\n conditionResult: result.conditionResult,\n actionsExecuted: result.actionsExecuted,\n executionTime: result.executionTime,\n error: result.error,\n logId: result.logId,\n }\n}\n\n/**\n * Sensitive field patterns to exclude from logs\n */\nconst SENSITIVE_FIELD_PATTERNS = [\n /password/i,\n /passwd/i,\n /pwd/i,\n /secret/i,\n /token/i,\n /api[_-]?key/i,\n /auth/i,\n /credit[_-]?card/i,\n /card[_-]?number/i,\n /cvv/i,\n /ssn/i,\n /social[_-]?security/i,\n /tax[_-]?id/i,\n /driver[_-]?license/i,\n /passport/i,\n]\n\n/**\n * Maximum depth for nested object sanitization\n */\nconst MAX_SANITIZATION_DEPTH = 5\n\n/**\n * Sanitize data for logging by removing sensitive fields\n */\nfunction sanitizeForLogging(data: any, depth: number = 0): any {\n // Prevent infinite recursion\n if (depth > MAX_SANITIZATION_DEPTH) {\n return '[Max depth exceeded]'\n }\n\n // Handle null/undefined\n if (data === null || data === undefined) {\n return data\n }\n\n // Handle primitives\n if (typeof data !== 'object') {\n return data\n }\n\n // Handle arrays\n if (Array.isArray(data)) {\n return data.map(item => sanitizeForLogging(item, depth + 1))\n }\n\n // Handle objects\n const sanitized: Record<string, any> = {}\n\n for (const [key, value] of Object.entries(data)) {\n // Check if field name matches sensitive patterns\n const isSensitive = SENSITIVE_FIELD_PATTERNS.some(pattern => pattern.test(key))\n\n if (isSensitive) {\n sanitized[key] = '[REDACTED]'\n } else if (typeof value === 'object' && value !== null) {\n sanitized[key] = sanitizeForLogging(value, depth + 1)\n } else {\n sanitized[key] = value\n }\n }\n\n return sanitized\n}\n\n/**\n * Sanitize user object for logging (keep only safe fields)\n */\nfunction sanitizeUser(user: any): any {\n if (!user) {\n return undefined\n }\n\n // Only log safe user fields\n return {\n id: user.id,\n role: user.role,\n // Don't log: email, name, phone, address, etc.\n }\n}\n\n/**\n * Log rule execution to database\n */\ninterface LogExecutionOptions {\n rule: BusinessRule\n context: RuleEngineContext\n conditionResult: boolean\n actionsExecuted: ActionExecutionOutcome | null\n executionTime: number\n error?: string\n}\n\nexport async function logRuleExecution(\n em: EntityManager,\n options: LogExecutionOptions\n): Promise<string> {\n const { rule, context, conditionResult, actionsExecuted, executionTime, error } = options\n\n const executionResult: 'SUCCESS' | 'FAILURE' | 'ERROR' = error\n ? EXECUTION_RESULT_ERROR\n : conditionResult\n ? EXECUTION_RESULT_SUCCESS\n : EXECUTION_RESULT_FAILURE\n\n const log = em.create(RuleExecutionLog, {\n rule,\n entityId: context.entityId || DEFAULT_ENTITY_ID,\n entityType: context.entityType,\n executionResult,\n inputContext: {\n data: sanitizeForLogging(context.data),\n eventType: context.eventType,\n user: sanitizeUser(context.user),\n },\n outputContext: actionsExecuted\n ? {\n conditionResult,\n actionsExecuted: actionsExecuted.results.map((r) => ({\n type: r.action.type,\n success: r.success,\n error: r.error,\n })),\n }\n : null,\n errorMessage: error || null,\n executionTimeMs: executionTime,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n executedBy: context.executedBy || null,\n })\n\n await em.persist(log).flush()\n\n return String(log.id)\n}\n\nasync function emitRuleExecutionFailed(\n eventBus: Pick<EventBus, 'emitEvent'> | null | undefined,\n payload: RuleExecutionFailedPayload\n): Promise<void> {\n if (!eventBus?.emitEvent) return\n\n await eventBus.emitEvent('business_rules.rule.execution_failed', payload).catch(() => undefined)\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,0BAA8C;AAEvD,SAAS,cAAc,wBAAuC;AAC9D,YAAY,mBAAmB;AAC/B,YAAY,oBAAoB;AAGhC,SAAS,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mD,QACrD,yBACA,kBACE,2BACA;AAEN,QAAM,MAAM,GAAG,OAAO,kBAAkB;AAAA,IACtC;AAAA,IACA,UAAU,QAAQ,YAAY;AAAA,IAC9B,YAAY,QAAQ;AAAA,IACpB;AAAA,IACA,cAAc;AAAA,MACZ,MAAM,mBAAmB,QAAQ,IAAI;AAAA,MACrC,WAAW,QAAQ;AAAA,MACnB,MAAM,aAAa,QAAQ,IAAI;AAAA,IACjC;AAAA,IACA,eAAe,kBACX;AAAA,MACE;AAAA,MACA,iBAAiB,gBAAgB,QAAQ,IAAI,CAAC,OAAO;AAAA,QACnD,MAAM,EAAE,OAAO;AAAA,QACf,SAAS,EAAE;AAAA,QACX,OAAO,EAAE;AAAA,MACX,EAAE;AAAA,IACJ,IACA;AAAA,IACJ,cAAc,SAAS;AAAA,IACvB,iBAAiB;AAAA,IACjB,UAAU,QAAQ;AAAA,IAClB,gBAAgB,QAAQ;AAAA,IACxB,YAAY,QAAQ,cAAc;AAAA,EACpC,CAAC;AAED,QAAM,GAAG,QAAQ,GAAG,EAAE,MAAM;AAE5B,SAAO,OAAO,IAAI,EAAE;AACtB;AAEA,eAAe,wBACb,UACA,SACe;AACf,MAAI,CAAC,UAAU,UAAW;AAE1B,QAAM,SAAS,UAAU,wCAAwC,OAAO,EAAE,MAAM,MAAM,MAAS;AACjG;",
|
|
6
6
|
"names": ["executionTime", "logId"]
|
|
7
7
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { executeRules } from "../lib/rule-engine.js";
|
|
1
|
+
import { executeRules, resolveBusinessRuleDiscoveryCache } from "../lib/rule-engine.js";
|
|
2
2
|
const metadata = {
|
|
3
3
|
event: "*",
|
|
4
4
|
persistent: true,
|
|
@@ -33,6 +33,7 @@ async function handle(payload, ctx) {
|
|
|
33
33
|
const organizationId = typeof ctx.organizationId === "string" && ctx.organizationId.length > 0 ? ctx.organizationId : void 0;
|
|
34
34
|
if (!tenantId || !organizationId) return;
|
|
35
35
|
const em = ctx.resolve("em");
|
|
36
|
+
const cache = resolveBusinessRuleDiscoveryCache(ctx.resolve);
|
|
36
37
|
try {
|
|
37
38
|
await executeRules(em, {
|
|
38
39
|
entityType: parsed.entityType,
|
|
@@ -40,7 +41,7 @@ async function handle(payload, ctx) {
|
|
|
40
41
|
data,
|
|
41
42
|
tenantId,
|
|
42
43
|
organizationId
|
|
43
|
-
});
|
|
44
|
+
}, { cache });
|
|
44
45
|
} catch (error) {
|
|
45
46
|
console.error(`[business_rules] Rule execution failed for event ${eventName}:`, error);
|
|
46
47
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/business_rules/subscribers/crud-rule-trigger.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Business Rules Module - CRUD Event Trigger Subscriber\n *\n * Wildcard subscriber that listens to all domain events and evaluates\n * business rules configured for the matching entity + event type.\n * Mirrors the pattern used by workflows/subscribers/event-trigger.ts.\n *\n * Closes #662 \u2014 business rules configured with entity + event triggers\n * were never executed because no subscriber wired CRUD events to the\n * rule engine.\n */\n\nimport type { EntityManager } from '@mikro-orm/core'\nimport { executeRules } from '../lib/rule-engine'\n\nexport const metadata = {\n event: '*',\n persistent: true,\n id: 'business_rules:crud-rule-trigger',\n}\n\nconst EXCLUDED_EVENT_PREFIXES = [\n 'query_index.',\n 'search.',\n 'business_rules.',\n 'cache.',\n 'queue.',\n 'workflows.',\n]\n\nfunction isExcludedEvent(eventName: string): boolean {\n return EXCLUDED_EVENT_PREFIXES.some((prefix) => eventName.startsWith(prefix))\n}\n\nfunction parseEventName(eventName: string): { entityType: string; eventType: string } | null {\n const parts = eventName.split('.')\n if (parts.length < 3) return null\n const eventType = parts[parts.length - 1]\n const entityType = parts.slice(0, -1).join('.')\n return { entityType, eventType }\n}\n\ntype CrudRuleTriggerContext = {\n resolve: <T = unknown>(name: string) => T\n eventName?: string\n tenantId?: string | null\n organizationId?: string | null\n}\n\nexport default async function handle(\n payload: unknown,\n ctx: CrudRuleTriggerContext,\n): Promise<void> {\n const eventName = ctx.eventName\n if (!eventName) return\n if (isExcludedEvent(eventName)) return\n\n const parsed = parseEventName(eventName)\n if (!parsed) return\n\n const data = (payload && typeof payload === 'object' ? payload : {}) as Record<string, unknown>\n const tenantId = typeof ctx.tenantId === 'string' && ctx.tenantId.length > 0 ? ctx.tenantId : undefined\n const organizationId = typeof ctx.organizationId === 'string' && ctx.organizationId.length > 0 ? ctx.organizationId : undefined\n if (!tenantId || !organizationId) return\n\n const em = ctx.resolve<EntityManager>('em')\n\n try {\n await executeRules(em, {\n entityType: parsed.entityType,\n eventType: parsed.eventType,\n data,\n tenantId,\n organizationId,\n })\n } catch (error) {\n console.error(`[business_rules] Rule execution failed for event ${eventName}:`, error)\n }\n}\n"],
|
|
5
|
-
"mappings": "AAaA,SAAS,
|
|
4
|
+
"sourcesContent": ["/**\n * Business Rules Module - CRUD Event Trigger Subscriber\n *\n * Wildcard subscriber that listens to all domain events and evaluates\n * business rules configured for the matching entity + event type.\n * Mirrors the pattern used by workflows/subscribers/event-trigger.ts.\n *\n * Closes #662 \u2014 business rules configured with entity + event triggers\n * were never executed because no subscriber wired CRUD events to the\n * rule engine.\n */\n\nimport type { EntityManager } from '@mikro-orm/core'\nimport { executeRules, resolveBusinessRuleDiscoveryCache } from '../lib/rule-engine'\n\nexport const metadata = {\n event: '*',\n persistent: true,\n id: 'business_rules:crud-rule-trigger',\n}\n\nconst EXCLUDED_EVENT_PREFIXES = [\n 'query_index.',\n 'search.',\n 'business_rules.',\n 'cache.',\n 'queue.',\n 'workflows.',\n]\n\nfunction isExcludedEvent(eventName: string): boolean {\n return EXCLUDED_EVENT_PREFIXES.some((prefix) => eventName.startsWith(prefix))\n}\n\nfunction parseEventName(eventName: string): { entityType: string; eventType: string } | null {\n const parts = eventName.split('.')\n if (parts.length < 3) return null\n const eventType = parts[parts.length - 1]\n const entityType = parts.slice(0, -1).join('.')\n return { entityType, eventType }\n}\n\ntype CrudRuleTriggerContext = {\n resolve: <T = unknown>(name: string) => T\n eventName?: string\n tenantId?: string | null\n organizationId?: string | null\n}\n\nexport default async function handle(\n payload: unknown,\n ctx: CrudRuleTriggerContext,\n): Promise<void> {\n const eventName = ctx.eventName\n if (!eventName) return\n if (isExcludedEvent(eventName)) return\n\n const parsed = parseEventName(eventName)\n if (!parsed) return\n\n const data = (payload && typeof payload === 'object' ? payload : {}) as Record<string, unknown>\n const tenantId = typeof ctx.tenantId === 'string' && ctx.tenantId.length > 0 ? ctx.tenantId : undefined\n const organizationId = typeof ctx.organizationId === 'string' && ctx.organizationId.length > 0 ? ctx.organizationId : undefined\n if (!tenantId || !organizationId) return\n\n const em = ctx.resolve<EntityManager>('em')\n const cache = resolveBusinessRuleDiscoveryCache(ctx.resolve)\n\n try {\n await executeRules(em, {\n entityType: parsed.entityType,\n eventType: parsed.eventType,\n data,\n tenantId,\n organizationId,\n }, { cache })\n } catch (error) {\n console.error(`[business_rules] Rule execution failed for event ${eventName}:`, error)\n }\n}\n"],
|
|
5
|
+
"mappings": "AAaA,SAAS,cAAc,yCAAyC;AAEzD,MAAM,WAAW;AAAA,EACtB,OAAO;AAAA,EACP,YAAY;AAAA,EACZ,IAAI;AACN;AAEA,MAAM,0BAA0B;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,SAAS,gBAAgB,WAA4B;AACnD,SAAO,wBAAwB,KAAK,CAAC,WAAW,UAAU,WAAW,MAAM,CAAC;AAC9E;AAEA,SAAS,eAAe,WAAqE;AAC3F,QAAM,QAAQ,UAAU,MAAM,GAAG;AACjC,MAAI,MAAM,SAAS,EAAG,QAAO;AAC7B,QAAM,YAAY,MAAM,MAAM,SAAS,CAAC;AACxC,QAAM,aAAa,MAAM,MAAM,GAAG,EAAE,EAAE,KAAK,GAAG;AAC9C,SAAO,EAAE,YAAY,UAAU;AACjC;AASA,eAAO,OACL,SACA,KACe;AACf,QAAM,YAAY,IAAI;AACtB,MAAI,CAAC,UAAW;AAChB,MAAI,gBAAgB,SAAS,EAAG;AAEhC,QAAM,SAAS,eAAe,SAAS;AACvC,MAAI,CAAC,OAAQ;AAEb,QAAM,OAAQ,WAAW,OAAO,YAAY,WAAW,UAAU,CAAC;AAClE,QAAM,WAAW,OAAO,IAAI,aAAa,YAAY,IAAI,SAAS,SAAS,IAAI,IAAI,WAAW;AAC9F,QAAM,iBAAiB,OAAO,IAAI,mBAAmB,YAAY,IAAI,eAAe,SAAS,IAAI,IAAI,iBAAiB;AACtH,MAAI,CAAC,YAAY,CAAC,eAAgB;AAElC,QAAM,KAAK,IAAI,QAAuB,IAAI;AAC1C,QAAM,QAAQ,kCAAkC,IAAI,OAAO;AAE3D,MAAI;AACF,UAAM,aAAa,IAAI;AAAA,MACrB,YAAY,OAAO;AAAA,MACnB,WAAW,OAAO;AAAA,MAClB;AAAA,MACA;AAAA,MACA;AAAA,IACF,GAAG,EAAE,MAAM,CAAC;AAAA,EACd,SAAS,OAAO;AACd,YAAQ,MAAM,oDAAoD,SAAS,KAAK,KAAK;AAAA,EACvF;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -75,10 +75,19 @@ async function decorateOffersWithDetails(items, ctx) {
|
|
|
75
75
|
const productIds = items.map((item) => item?.productId ? String(item.productId) : null).filter((value) => !!value);
|
|
76
76
|
if (!offerIds.length && !productIds.length) return;
|
|
77
77
|
const em = ctx.container.resolve("em");
|
|
78
|
+
const scopeTenantId = ctx.auth?.tenantId ?? null;
|
|
79
|
+
if (!scopeTenantId) {
|
|
80
|
+
throw new CrudHttpError(403, "[internal] Missing tenant scope for offer decoration");
|
|
81
|
+
}
|
|
82
|
+
const scopeOrgIds = Array.isArray(ctx.organizationIds) && ctx.organizationIds.length ? Array.from(new Set(ctx.organizationIds)) : ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null ? [ctx.selectedOrganizationId ?? ctx.auth?.orgId] : [];
|
|
83
|
+
const scopeWhere = { tenantId: scopeTenantId };
|
|
84
|
+
if (scopeOrgIds.length === 1) scopeWhere.organizationId = scopeOrgIds[0];
|
|
85
|
+
else if (scopeOrgIds.length > 1) scopeWhere.organizationId = { $in: scopeOrgIds };
|
|
86
|
+
const scope = { tenantId: scopeTenantId, organizationId: scopeOrgIds.length === 1 ? scopeOrgIds[0] : null };
|
|
78
87
|
const [products, prices, defaultVariants] = await Promise.all([
|
|
79
88
|
productIds.length ? em.find(
|
|
80
89
|
CatalogProduct,
|
|
81
|
-
{ id: { $in: productIds } },
|
|
90
|
+
{ id: { $in: productIds }, ...scopeWhere },
|
|
82
91
|
{
|
|
83
92
|
fields: ["id", "title", "description", "defaultMediaId", "defaultMediaUrl", "sku"]
|
|
84
93
|
}
|
|
@@ -86,13 +95,13 @@ async function decorateOffersWithDetails(items, ctx) {
|
|
|
86
95
|
offerIds.length ? findWithDecryption(
|
|
87
96
|
em,
|
|
88
97
|
CatalogProductPrice,
|
|
89
|
-
{ offer: { $in: offerIds } },
|
|
98
|
+
{ offer: { $in: offerIds }, ...scopeWhere },
|
|
90
99
|
{ populate: ["priceKind"] },
|
|
91
|
-
|
|
100
|
+
scope
|
|
92
101
|
) : [],
|
|
93
102
|
productIds.length ? em.find(
|
|
94
103
|
CatalogProductVariant,
|
|
95
|
-
{ product: { $in: productIds }, isDefault: true },
|
|
104
|
+
{ product: { $in: productIds }, isDefault: true, ...scopeWhere },
|
|
96
105
|
{ fields: ["id", "product"] }
|
|
97
106
|
) : []
|
|
98
107
|
]);
|
|
@@ -174,6 +183,7 @@ async function decorateOffersWithDetails(items, ctx) {
|
|
|
174
183
|
CatalogProductPrice,
|
|
175
184
|
{
|
|
176
185
|
offer: null,
|
|
186
|
+
...scopeWhere,
|
|
177
187
|
$and: [
|
|
178
188
|
{ $or: fallbackTargets },
|
|
179
189
|
channelFilterValues.includes(null) ? {
|
|
@@ -185,7 +195,7 @@ async function decorateOffersWithDetails(items, ctx) {
|
|
|
185
195
|
]
|
|
186
196
|
},
|
|
187
197
|
{ populate: ["priceKind"] },
|
|
188
|
-
|
|
198
|
+
scope
|
|
189
199
|
) : [];
|
|
190
200
|
fallbackEntries.forEach((entry) => {
|
|
191
201
|
const entryChannelId = typeof entry.channelId === "string" && entry.channelId.length ? entry.channelId : null;
|