@open-mercato/core 0.6.6 → 0.6.7-develop.6553.1.2ec7246e04
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 +2 -2
- package/dist/generated/entities/gateway_session_initialization/index.js +23 -0
- package/dist/generated/entities/gateway_session_initialization/index.js.map +7 -0
- package/dist/generated/entities.ids.generated.js +1 -0
- package/dist/generated/entities.ids.generated.js.map +2 -2
- package/dist/generated/entity-fields-registry.js +12 -0
- package/dist/generated/entity-fields-registry.js.map +2 -2
- package/dist/modules/api_keys/api/keys/route.js +30 -13
- package/dist/modules/api_keys/api/keys/route.js.map +2 -2
- package/dist/modules/api_keys/cli.js +2 -1
- package/dist/modules/api_keys/cli.js.map +2 -2
- package/dist/modules/attachments/api/file/[id]/route.js +9 -7
- package/dist/modules/attachments/api/file/[id]/route.js.map +2 -2
- package/dist/modules/attachments/api/image/[id]/[[...slug]]/route.js +9 -7
- package/dist/modules/attachments/api/image/[id]/[[...slug]]/route.js.map +2 -2
- package/dist/modules/attachments/api/route.js +21 -17
- package/dist/modules/attachments/api/route.js.map +2 -2
- package/dist/modules/attachments/components/AttachmentContentPreview.js +6 -35
- package/dist/modules/attachments/components/AttachmentContentPreview.js.map +2 -2
- package/dist/modules/attachments/lib/reconcileOrganization.js +110 -0
- package/dist/modules/attachments/lib/reconcileOrganization.js.map +7 -0
- package/dist/modules/attachments/lib/requestScope.js +10 -0
- package/dist/modules/attachments/lib/requestScope.js.map +7 -0
- package/dist/modules/audit_logs/backend/audit-logs/page.js +14 -24
- package/dist/modules/audit_logs/backend/audit-logs/page.js.map +2 -2
- package/dist/modules/auth/api/autologin.js +85 -0
- package/dist/modules/auth/api/autologin.js.map +7 -0
- package/dist/modules/auth/api/users/route.js +6 -2
- package/dist/modules/auth/api/users/route.js.map +2 -2
- package/dist/modules/auth/cli.js +3 -2
- package/dist/modules/auth/cli.js.map +2 -2
- package/dist/modules/auth/lib/autologin.js +19 -0
- package/dist/modules/auth/lib/autologin.js.map +7 -0
- package/dist/modules/configs/lib/upgrade-actions.js +22 -0
- package/dist/modules/configs/lib/upgrade-actions.js.map +2 -2
- package/dist/modules/customer_accounts/api/admin/roles/[id]/acl.js +1 -0
- package/dist/modules/customer_accounts/api/admin/roles/[id]/acl.js.map +2 -2
- package/dist/modules/customer_accounts/api/admin/roles/[id].js +17 -3
- package/dist/modules/customer_accounts/api/admin/roles/[id].js.map +2 -2
- package/dist/modules/customer_accounts/api/portal/users/[id]/roles.js +6 -1
- package/dist/modules/customer_accounts/api/portal/users/[id]/roles.js.map +2 -2
- package/dist/modules/customer_accounts/api/portal/users-invite.js +6 -1
- package/dist/modules/customer_accounts/api/portal/users-invite.js.map +2 -2
- package/dist/modules/customer_accounts/services/customerInvitationService.js +1 -0
- package/dist/modules/customer_accounts/services/customerInvitationService.js.map +2 -2
- package/dist/modules/customer_accounts/services/customerRbacService.js +6 -1
- package/dist/modules/customer_accounts/services/customerRbacService.js.map +2 -2
- package/dist/modules/customer_accounts/widgets/injection-table.js +0 -2
- package/dist/modules/customer_accounts/widgets/injection-table.js.map +2 -2
- package/dist/modules/customers/backend/customers/companies-v2/[id]/page.js +7 -1
- package/dist/modules/customers/backend/customers/companies-v2/[id]/page.js.map +2 -2
- package/dist/modules/customers/backend/customers/deals/pipeline/components/ViewTabsRow.js +2 -2
- package/dist/modules/customers/backend/customers/deals/pipeline/components/ViewTabsRow.js.map +2 -2
- package/dist/modules/customers/components/detail/CompanyDetailTabs.js +37 -43
- package/dist/modules/customers/components/detail/CompanyDetailTabs.js.map +2 -2
- package/dist/modules/customers/components/detail/DealDetailTabs.js +23 -35
- package/dist/modules/customers/components/detail/DealDetailTabs.js.map +2 -2
- package/dist/modules/customers/components/detail/DetailTabsLayout.js +6 -21
- package/dist/modules/customers/components/detail/DetailTabsLayout.js.map +2 -2
- package/dist/modules/customers/components/detail/MobilePersonDetail.js +26 -32
- package/dist/modules/customers/components/detail/MobilePersonDetail.js.map +2 -2
- package/dist/modules/customers/components/detail/PersonDetailTabs.js +29 -39
- package/dist/modules/customers/components/detail/PersonDetailTabs.js.map +2 -2
- package/dist/modules/customers/components/detail/useDealsAccess.js +16 -0
- package/dist/modules/customers/components/detail/useDealsAccess.js.map +7 -0
- package/dist/modules/dashboards/api/roles/widgets/route.js +22 -3
- package/dist/modules/dashboards/api/roles/widgets/route.js.map +2 -2
- package/dist/modules/dashboards/api/users/widgets/route.js +22 -3
- package/dist/modules/dashboards/api/users/widgets/route.js.map +2 -2
- package/dist/modules/dashboards/cli.js +4 -3
- package/dist/modules/dashboards/cli.js.map +2 -2
- package/dist/modules/dashboards/lib/widgetAssignmentScope.js +9 -1
- package/dist/modules/dashboards/lib/widgetAssignmentScope.js.map +2 -2
- package/dist/modules/dictionaries/commands/entries.js +7 -14
- package/dist/modules/dictionaries/commands/entries.js.map +2 -2
- package/dist/modules/dictionaries/commands/entry-operations.js +7 -16
- package/dist/modules/dictionaries/commands/entry-operations.js.map +2 -2
- package/dist/modules/dictionaries/commands/factory.js +9 -3
- package/dist/modules/dictionaries/commands/factory.js.map +2 -2
- package/dist/modules/dictionaries/fields/dictionary.js +56 -41
- package/dist/modules/dictionaries/fields/dictionary.js.map +2 -2
- package/dist/modules/directory/utils/organizationScope.js +10 -3
- package/dist/modules/directory/utils/organizationScope.js.map +2 -2
- package/dist/modules/entities/api/records.js +5 -4
- package/dist/modules/entities/api/records.js.map +2 -2
- package/dist/modules/entities/backend/entities/system/[entityId]/page.meta.js +1 -0
- package/dist/modules/entities/backend/entities/system/[entityId]/page.meta.js.map +2 -2
- package/dist/modules/entities/backend/entities/user/[entityId]/page.js +49 -49
- package/dist/modules/entities/backend/entities/user/[entityId]/page.js.map +2 -2
- package/dist/modules/entities/backend/entities/user/[entityId]/page.meta.js +1 -0
- package/dist/modules/entities/backend/entities/user/[entityId]/page.meta.js.map +2 -2
- package/dist/modules/entities/cli.js +2 -1
- package/dist/modules/entities/cli.js.map +2 -2
- package/dist/modules/feature_toggles/api/check/boolean/route.js +0 -14
- package/dist/modules/feature_toggles/api/check/boolean/route.js.map +2 -2
- package/dist/modules/feature_toggles/defaults.json +8 -0
- package/dist/modules/integrations/backend/integrations/[id]/page.js +9 -80
- package/dist/modules/integrations/backend/integrations/[id]/page.js.map +2 -2
- package/dist/modules/payment_gateways/data/entities.js +45 -0
- package/dist/modules/payment_gateways/data/entities.js.map +2 -2
- package/dist/modules/payment_gateways/lib/gateway-service.js +173 -38
- package/dist/modules/payment_gateways/lib/gateway-service.js.map +3 -3
- package/dist/modules/payment_gateways/lib/session-idempotency.js +104 -0
- package/dist/modules/payment_gateways/lib/session-idempotency.js.map +7 -0
- package/dist/modules/payment_gateways/migrations/Migration20260709220938_payment_gateways.js +11 -0
- package/dist/modules/payment_gateways/migrations/Migration20260709220938_payment_gateways.js.map +7 -0
- package/dist/modules/planner/backend/planner/availability-rulesets/[id]/page.js +16 -14
- package/dist/modules/planner/backend/planner/availability-rulesets/[id]/page.js.map +2 -2
- package/dist/modules/planner/components/AvailabilityRulesEditor.js +18 -30
- package/dist/modules/planner/components/AvailabilityRulesEditor.js.map +2 -2
- package/dist/modules/query_index/api/status.js +2 -8
- package/dist/modules/query_index/api/status.js.map +2 -2
- package/dist/modules/resources/backend/resources/resources/[id]/page.js +24 -24
- package/dist/modules/resources/backend/resources/resources/[id]/page.js.map +2 -2
- package/dist/modules/sales/backend/sales/channels/create/page.js +6 -0
- package/dist/modules/sales/backend/sales/channels/create/page.js.map +2 -2
- package/dist/modules/sales/backend/sales/channels/create/page.meta.js +2 -0
- package/dist/modules/sales/backend/sales/channels/create/page.meta.js.map +2 -2
- package/dist/modules/sales/backend/sales/channels/offers/page.js +6 -0
- package/dist/modules/sales/backend/sales/channels/offers/page.js.map +2 -2
- package/dist/modules/sales/backend/sales/channels/offers/page.meta.js +2 -0
- package/dist/modules/sales/backend/sales/channels/offers/page.meta.js.map +2 -2
- package/dist/modules/sales/backend/sales/channels/page.js +6 -0
- package/dist/modules/sales/backend/sales/channels/page.js.map +2 -2
- package/dist/modules/sales/backend/sales/channels/page.meta.js +2 -0
- package/dist/modules/sales/backend/sales/channels/page.meta.js.map +2 -2
- package/dist/modules/sales/backend/sales/documents/[id]/page.js +6 -4
- package/dist/modules/sales/backend/sales/documents/[id]/page.js.map +2 -2
- package/dist/modules/sales/commands/documents.js +44 -0
- package/dist/modules/sales/commands/documents.js.map +2 -2
- package/dist/modules/sales/commands/returns.js +2 -27
- package/dist/modules/sales/commands/returns.js.map +2 -2
- package/dist/modules/sales/components/SalesChannelsDisabledNotice.js +15 -0
- package/dist/modules/sales/components/SalesChannelsDisabledNotice.js.map +7 -0
- package/dist/modules/sales/components/documents/AddressesSection.js +6 -2
- package/dist/modules/sales/components/documents/AddressesSection.js.map +2 -2
- package/dist/modules/sales/components/documents/ItemsSection.js +1 -0
- package/dist/modules/sales/components/documents/ItemsSection.js.map +2 -2
- package/dist/modules/sales/components/documents/LineItemDialog.js +9 -0
- package/dist/modules/sales/components/documents/LineItemDialog.js.map +2 -2
- package/dist/modules/sales/components/documents/SalesDocumentForm.js +51 -3
- package/dist/modules/sales/components/documents/SalesDocumentForm.js.map +2 -2
- package/dist/modules/sales/components/documents/SalesDocumentsTable.js +10 -8
- package/dist/modules/sales/components/documents/SalesDocumentsTable.js.map +2 -2
- package/dist/modules/sales/components/documents/ShipmentDialog.js +59 -38
- package/dist/modules/sales/components/documents/ShipmentDialog.js.map +2 -2
- package/dist/modules/sales/components/useSalesChannelsEnabled.js +44 -0
- package/dist/modules/sales/components/useSalesChannelsEnabled.js.map +7 -0
- package/dist/modules/sales/lib/calculations.js +6 -0
- package/dist/modules/sales/lib/calculations.js.map +2 -2
- package/dist/modules/sales/lib/salesChannelsToggle.js +18 -0
- package/dist/modules/sales/lib/salesChannelsToggle.js.map +7 -0
- package/dist/modules/sales/lib/salesChannelsToggleId.js +5 -0
- package/dist/modules/sales/lib/salesChannelsToggleId.js.map +7 -0
- package/dist/modules/sales/lib/shipments/snapshots.js +27 -1
- package/dist/modules/sales/lib/shipments/snapshots.js.map +2 -2
- package/dist/modules/staff/backend/staff/team-members/[id]/page.js +22 -25
- package/dist/modules/staff/backend/staff/team-members/[id]/page.js.map +2 -2
- package/dist/modules/staff/backend/staff/teams/[id]/edit/page.js +12 -14
- package/dist/modules/staff/backend/staff/teams/[id]/edit/page.js.map +2 -2
- package/dist/modules/staff/lib/timesheets-projects-ui/SavedViewTabs.js +10 -29
- package/dist/modules/staff/lib/timesheets-projects-ui/SavedViewTabs.js.map +2 -2
- package/dist/modules/sync_excel/lib/adapters/customers.js +19 -20
- package/dist/modules/sync_excel/lib/adapters/customers.js.map +2 -2
- package/dist/modules/sync_excel/lib/parser.js +185 -0
- package/dist/modules/sync_excel/lib/parser.js.map +2 -2
- package/dist/modules/sync_excel/lib/upload-storage.js +20 -1
- package/dist/modules/sync_excel/lib/upload-storage.js.map +2 -2
- package/generated/entities/gateway_session_initialization/index.ts +10 -0
- package/generated/entities.ids.generated.ts +1 -0
- package/generated/entity-fields-registry.ts +12 -0
- package/package.json +9 -8
- package/src/modules/api_keys/api/keys/route.ts +36 -13
- package/src/modules/api_keys/cli.ts +2 -3
- package/src/modules/attachments/api/file/[id]/route.ts +11 -7
- package/src/modules/attachments/api/image/[id]/[[...slug]]/route.ts +11 -7
- package/src/modules/attachments/api/route.ts +26 -21
- package/src/modules/attachments/components/AttachmentContentPreview.tsx +9 -44
- package/src/modules/attachments/lib/reconcileOrganization.ts +188 -0
- package/src/modules/attachments/lib/requestScope.ts +27 -0
- package/src/modules/audit_logs/backend/audit-logs/page.tsx +12 -22
- package/src/modules/auth/api/autologin.ts +100 -0
- package/src/modules/auth/api/users/route.ts +11 -2
- package/src/modules/auth/cli.ts +3 -2
- package/src/modules/auth/lib/autologin.ts +36 -0
- package/src/modules/configs/i18n/de.json +5 -1
- package/src/modules/configs/i18n/en.json +5 -1
- package/src/modules/configs/i18n/es.json +5 -1
- package/src/modules/configs/i18n/pl.json +5 -1
- package/src/modules/configs/lib/upgrade-actions.ts +24 -0
- package/src/modules/customer_accounts/AGENTS.md +8 -6
- package/src/modules/customer_accounts/api/admin/roles/[id]/acl.ts +1 -0
- package/src/modules/customer_accounts/api/admin/roles/[id].ts +17 -3
- package/src/modules/customer_accounts/api/portal/users/[id]/roles.ts +6 -1
- package/src/modules/customer_accounts/api/portal/users-invite.ts +6 -1
- package/src/modules/customer_accounts/services/customerInvitationService.ts +1 -0
- package/src/modules/customer_accounts/services/customerRbacService.ts +9 -2
- package/src/modules/customer_accounts/widgets/injection-table.ts +0 -2
- package/src/modules/customers/backend/customers/companies-v2/[id]/page.tsx +11 -1
- package/src/modules/customers/backend/customers/deals/pipeline/components/ViewTabsRow.tsx +6 -2
- package/src/modules/customers/components/detail/CompanyDetailTabs.tsx +39 -55
- package/src/modules/customers/components/detail/DealDetailTabs.tsx +24 -49
- package/src/modules/customers/components/detail/DetailTabsLayout.tsx +13 -24
- package/src/modules/customers/components/detail/MobilePersonDetail.tsx +25 -31
- package/src/modules/customers/components/detail/PersonDetailTabs.tsx +29 -50
- package/src/modules/customers/components/detail/useDealsAccess.ts +19 -0
- package/src/modules/dashboards/api/roles/widgets/route.ts +22 -3
- package/src/modules/dashboards/api/users/widgets/route.ts +22 -3
- package/src/modules/dashboards/cli.ts +4 -9
- package/src/modules/dashboards/lib/widgetAssignmentScope.ts +18 -0
- package/src/modules/dictionaries/commands/entries.ts +7 -16
- package/src/modules/dictionaries/commands/entry-operations.ts +8 -18
- package/src/modules/dictionaries/commands/factory.ts +8 -2
- package/src/modules/dictionaries/fields/dictionary.tsx +55 -32
- package/src/modules/dictionaries/i18n/de.json +2 -0
- package/src/modules/dictionaries/i18n/en.json +2 -0
- package/src/modules/dictionaries/i18n/es.json +2 -0
- package/src/modules/dictionaries/i18n/pl.json +2 -0
- package/src/modules/directory/utils/organizationScope.ts +36 -2
- package/src/modules/entities/api/records.ts +5 -7
- package/src/modules/entities/backend/entities/system/[entityId]/page.meta.ts +1 -0
- package/src/modules/entities/backend/entities/user/[entityId]/page.meta.ts +1 -0
- package/src/modules/entities/backend/entities/user/[entityId]/page.tsx +51 -49
- package/src/modules/entities/cli.ts +2 -1
- package/src/modules/entities/i18n/de.json +34 -0
- package/src/modules/entities/i18n/en.json +34 -0
- package/src/modules/entities/i18n/es.json +34 -0
- package/src/modules/entities/i18n/pl.json +34 -0
- package/src/modules/feature_toggles/api/check/boolean/route.ts +0 -15
- package/src/modules/feature_toggles/defaults.json +8 -0
- package/src/modules/integrations/backend/integrations/[id]/page.tsx +16 -59
- package/src/modules/payment_gateways/data/entities.ts +39 -0
- package/src/modules/payment_gateways/lib/gateway-service.ts +211 -40
- package/src/modules/payment_gateways/lib/session-idempotency.ts +142 -0
- package/src/modules/payment_gateways/migrations/.snapshot-open-mercato.json +236 -0
- package/src/modules/payment_gateways/migrations/Migration20260709220938_payment_gateways.ts +10 -0
- package/src/modules/planner/backend/planner/availability-rulesets/[id]/page.tsx +14 -20
- package/src/modules/planner/components/AvailabilityRulesEditor.tsx +16 -34
- package/src/modules/query_index/api/status.ts +2 -8
- package/src/modules/resources/backend/resources/resources/[id]/page.tsx +27 -37
- package/src/modules/sales/backend/sales/channels/create/page.meta.ts +3 -0
- package/src/modules/sales/backend/sales/channels/create/page.tsx +7 -0
- package/src/modules/sales/backend/sales/channels/offers/page.meta.ts +3 -0
- package/src/modules/sales/backend/sales/channels/offers/page.tsx +7 -0
- package/src/modules/sales/backend/sales/channels/page.meta.ts +4 -0
- package/src/modules/sales/backend/sales/channels/page.tsx +7 -0
- package/src/modules/sales/backend/sales/documents/[id]/page.tsx +7 -5
- package/src/modules/sales/commands/documents.ts +65 -0
- package/src/modules/sales/commands/returns.ts +2 -32
- package/src/modules/sales/components/SalesChannelsDisabledNotice.tsx +21 -0
- package/src/modules/sales/components/documents/AddressesSection.tsx +6 -2
- package/src/modules/sales/components/documents/ItemsSection.tsx +5 -0
- package/src/modules/sales/components/documents/LineItemDialog.tsx +10 -0
- package/src/modules/sales/components/documents/SalesDocumentForm.tsx +53 -3
- package/src/modules/sales/components/documents/SalesDocumentsTable.tsx +10 -8
- package/src/modules/sales/components/documents/ShipmentDialog.tsx +65 -40
- package/src/modules/sales/components/useSalesChannelsEnabled.ts +59 -0
- package/src/modules/sales/i18n/de.json +5 -0
- package/src/modules/sales/i18n/en.json +5 -0
- package/src/modules/sales/i18n/es.json +5 -0
- package/src/modules/sales/i18n/pl.json +5 -0
- package/src/modules/sales/lib/calculations.ts +15 -0
- package/src/modules/sales/lib/salesChannelsToggle.ts +26 -0
- package/src/modules/sales/lib/salesChannelsToggleId.ts +1 -0
- package/src/modules/sales/lib/shipments/snapshots.ts +30 -0
- package/src/modules/staff/backend/staff/team-members/[id]/page.tsx +25 -34
- package/src/modules/staff/backend/staff/teams/[id]/edit/page.tsx +11 -17
- package/src/modules/staff/lib/timesheets-projects-ui/SavedViewTabs.tsx +11 -28
- package/src/modules/sync_excel/lib/adapters/customers.ts +20 -22
- package/src/modules/sync_excel/lib/parser.ts +245 -0
- package/src/modules/sync_excel/lib/upload-storage.ts +23 -1
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
function resolveWidgetAssignmentTargetAccess(params) {
|
|
2
|
+
const { isSuperAdmin, scopeTenantId, target } = params;
|
|
3
|
+
if (!isSuperAdmin && !scopeTenantId) return "forbidden";
|
|
4
|
+
if (!target) return "not-found";
|
|
5
|
+
if (scopeTenantId && (target.tenantId ?? null) !== scopeTenantId) return "not-found";
|
|
6
|
+
return "allowed";
|
|
7
|
+
}
|
|
1
8
|
function resolveWidgetAssignmentReadScope(params) {
|
|
2
9
|
const { auth, isSuperAdmin, queryTenantId, queryOrganizationId } = params;
|
|
3
10
|
if (isSuperAdmin) {
|
|
@@ -12,6 +19,7 @@ function resolveWidgetAssignmentReadScope(params) {
|
|
|
12
19
|
};
|
|
13
20
|
}
|
|
14
21
|
export {
|
|
15
|
-
resolveWidgetAssignmentReadScope
|
|
22
|
+
resolveWidgetAssignmentReadScope,
|
|
23
|
+
resolveWidgetAssignmentTargetAccess
|
|
16
24
|
};
|
|
17
25
|
//# sourceMappingURL=widgetAssignmentScope.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/dashboards/lib/widgetAssignmentScope.ts"],
|
|
4
|
-
"sourcesContent": ["export type AuthScopeInput = {\n tenantId?: string | null\n orgId?: string | null\n}\n\nexport type ResolvedAssignmentScope = {\n tenantId: string | null\n organizationId: string | null\n}\n\nexport function resolveWidgetAssignmentReadScope(params: {\n auth: AuthScopeInput\n isSuperAdmin: boolean\n queryTenantId?: string | null\n queryOrganizationId?: string | null\n}): ResolvedAssignmentScope {\n const { auth, isSuperAdmin, queryTenantId, queryOrganizationId } = params\n if (isSuperAdmin) {\n return {\n tenantId: (queryTenantId && queryTenantId.length ? queryTenantId : auth.tenantId) ?? null,\n organizationId:\n (queryOrganizationId && queryOrganizationId.length ? queryOrganizationId : auth.orgId) ?? null,\n }\n }\n return {\n tenantId: auth.tenantId ?? null,\n organizationId: auth.orgId ?? null,\n }\n}\n"],
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["export type AuthScopeInput = {\n tenantId?: string | null\n orgId?: string | null\n}\n\nexport type ResolvedAssignmentScope = {\n tenantId: string | null\n organizationId: string | null\n}\n\nexport type WidgetAssignmentTarget = {\n tenantId?: string | null\n}\n\nexport type WidgetAssignmentTargetAccess = 'allowed' | 'forbidden' | 'not-found'\n\nexport function resolveWidgetAssignmentTargetAccess(params: {\n isSuperAdmin: boolean\n scopeTenantId: string | null\n target: WidgetAssignmentTarget | null | undefined\n}): WidgetAssignmentTargetAccess {\n const { isSuperAdmin, scopeTenantId, target } = params\n if (!isSuperAdmin && !scopeTenantId) return 'forbidden'\n if (!target) return 'not-found'\n if (scopeTenantId && (target.tenantId ?? null) !== scopeTenantId) return 'not-found'\n return 'allowed'\n}\n\nexport function resolveWidgetAssignmentReadScope(params: {\n auth: AuthScopeInput\n isSuperAdmin: boolean\n queryTenantId?: string | null\n queryOrganizationId?: string | null\n}): ResolvedAssignmentScope {\n const { auth, isSuperAdmin, queryTenantId, queryOrganizationId } = params\n if (isSuperAdmin) {\n return {\n tenantId: (queryTenantId && queryTenantId.length ? queryTenantId : auth.tenantId) ?? null,\n organizationId:\n (queryOrganizationId && queryOrganizationId.length ? queryOrganizationId : auth.orgId) ?? null,\n }\n }\n return {\n tenantId: auth.tenantId ?? null,\n organizationId: auth.orgId ?? null,\n }\n}\n"],
|
|
5
|
+
"mappings": "AAgBO,SAAS,oCAAoC,QAInB;AAC/B,QAAM,EAAE,cAAc,eAAe,OAAO,IAAI;AAChD,MAAI,CAAC,gBAAgB,CAAC,cAAe,QAAO;AAC5C,MAAI,CAAC,OAAQ,QAAO;AACpB,MAAI,kBAAkB,OAAO,YAAY,UAAU,cAAe,QAAO;AACzE,SAAO;AACT;AAEO,SAAS,iCAAiC,QAKrB;AAC1B,QAAM,EAAE,MAAM,cAAc,eAAe,oBAAoB,IAAI;AACnE,MAAI,cAAc;AAChB,WAAO;AAAA,MACL,WAAW,iBAAiB,cAAc,SAAS,gBAAgB,KAAK,aAAa;AAAA,MACrF,iBACG,uBAAuB,oBAAoB,SAAS,sBAAsB,KAAK,UAAU;AAAA,IAC9F;AAAA,EACF;AACA,SAAO;AAAA,IACL,UAAU,KAAK,YAAY;AAAA,IAC3B,gBAAgB,KAAK,SAAS;AAAA,EAChC;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -3,19 +3,12 @@ import {
|
|
|
3
3
|
dictionaryEntryCommandCreateSchema,
|
|
4
4
|
dictionaryEntryCommandUpdateSchema
|
|
5
5
|
} from "@open-mercato/core/modules/dictionaries/data/validators";
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
ensureDictionaryEntryScope,
|
|
8
|
+
registerDictionaryEntryCommands
|
|
9
|
+
} from "@open-mercato/core/modules/dictionaries/commands/factory";
|
|
7
10
|
import { CrudHttpError } from "@open-mercato/shared/lib/crud/errors";
|
|
8
11
|
import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
|
|
9
|
-
function ensureScope(ctx, scope) {
|
|
10
|
-
const tenantId = ctx.auth?.tenantId ?? null;
|
|
11
|
-
if (tenantId && tenantId !== scope.tenantId) {
|
|
12
|
-
throw new CrudHttpError(403, { error: "Forbidden" });
|
|
13
|
-
}
|
|
14
|
-
const organizationId = ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null;
|
|
15
|
-
if (organizationId && organizationId !== scope.organizationId) {
|
|
16
|
-
throw new CrudHttpError(403, { error: "Forbidden" });
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
12
|
async function ensureDictionary(em, id) {
|
|
20
13
|
const dictionary = await em.findOne(Dictionary, {
|
|
21
14
|
id,
|
|
@@ -32,12 +25,12 @@ registerDictionaryEntryCommands({
|
|
|
32
25
|
translationKeyPrefix: "dictionaries.entries",
|
|
33
26
|
createSchema: dictionaryEntryCommandCreateSchema,
|
|
34
27
|
updateSchema: dictionaryEntryCommandUpdateSchema,
|
|
35
|
-
ensureScope,
|
|
28
|
+
ensureScope: ensureDictionaryEntryScope,
|
|
36
29
|
duplicateError: "An entry with this value already exists.",
|
|
37
30
|
resolveDictionaryForCreate: async ({ em, ctx, parsed }) => {
|
|
38
31
|
const dictionary = await ensureDictionary(em, parsed.dictionaryId);
|
|
39
32
|
const scope = { tenantId: dictionary.tenantId, organizationId: dictionary.organizationId };
|
|
40
|
-
|
|
33
|
+
ensureDictionaryEntryScope(ctx, scope);
|
|
41
34
|
return { dictionary, scope };
|
|
42
35
|
},
|
|
43
36
|
resolveEntry: async ({ em, ctx, id }) => {
|
|
@@ -49,7 +42,7 @@ registerDictionaryEntryCommands({
|
|
|
49
42
|
throw new CrudHttpError(404, { error: "Dictionary not found" });
|
|
50
43
|
}
|
|
51
44
|
const scope = { tenantId: entry.tenantId, organizationId: entry.organizationId };
|
|
52
|
-
|
|
45
|
+
ensureDictionaryEntryScope(ctx, scope);
|
|
53
46
|
return { entry, dictionary: entry.dictionary, scope };
|
|
54
47
|
},
|
|
55
48
|
ensureDictionaryForUndo: async ({ em, snapshot }) => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/dictionaries/commands/entries.ts"],
|
|
4
|
-
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { Dictionary, DictionaryEntry } from '@open-mercato/core/modules/dictionaries/data/entities'\nimport {\n dictionaryEntryCommandCreateSchema,\n dictionaryEntryCommandUpdateSchema,\n} from '@open-mercato/core/modules/dictionaries/data/validators'\nimport {
|
|
5
|
-
"mappings": "AACA,SAAS,YAAY,uBAAuB;AAC5C;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP,
|
|
4
|
+
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { Dictionary, DictionaryEntry } from '@open-mercato/core/modules/dictionaries/data/entities'\nimport {\n dictionaryEntryCommandCreateSchema,\n dictionaryEntryCommandUpdateSchema,\n} from '@open-mercato/core/modules/dictionaries/data/validators'\nimport {\n ensureDictionaryEntryScope,\n registerDictionaryEntryCommands,\n} from '@open-mercato/core/modules/dictionaries/commands/factory'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\n\nasync function ensureDictionary(em: EntityManager, id: string): Promise<Dictionary> {\n const dictionary = await em.findOne(Dictionary, {\n id,\n deletedAt: null,\n })\n if (!dictionary) {\n throw new CrudHttpError(404, { error: 'Dictionary not found' })\n }\n return dictionary\n}\n\nregisterDictionaryEntryCommands({\n commandPrefix: 'dictionaries.entries',\n resourceKind: 'dictionaries.entry',\n translationKeyPrefix: 'dictionaries.entries',\n createSchema: dictionaryEntryCommandCreateSchema,\n updateSchema: dictionaryEntryCommandUpdateSchema,\n ensureScope: ensureDictionaryEntryScope,\n duplicateError: 'An entry with this value already exists.',\n resolveDictionaryForCreate: async ({ em, ctx, parsed }) => {\n const dictionary = await ensureDictionary(em, parsed.dictionaryId)\n const scope = { tenantId: dictionary.tenantId, organizationId: dictionary.organizationId }\n ensureDictionaryEntryScope(ctx, scope)\n return { dictionary, scope }\n },\n resolveEntry: async ({ em, ctx, id }) => {\n const entry = await findOneWithDecryption(em, DictionaryEntry, id, { populate: ['dictionary'] })\n if (!entry) {\n throw new CrudHttpError(404, { error: 'Dictionary entry not found' })\n }\n if (entry.dictionary.deletedAt) {\n throw new CrudHttpError(404, { error: 'Dictionary not found' })\n }\n const scope = { tenantId: entry.tenantId, organizationId: entry.organizationId }\n ensureDictionaryEntryScope(ctx, scope)\n return { entry, dictionary: entry.dictionary, scope }\n },\n ensureDictionaryForUndo: async ({ em, snapshot }) => {\n return em.findOne(Dictionary, snapshot.dictionaryId, { filters: false })\n },\n})\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,YAAY,uBAAuB;AAC5C;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP,SAAS,qBAAqB;AAC9B,SAAS,6BAA6B;AAEtC,eAAe,iBAAiB,IAAmB,IAAiC;AAClF,QAAM,aAAa,MAAM,GAAG,QAAQ,YAAY;AAAA,IAC9C;AAAA,IACA,WAAW;AAAA,EACb,CAAC;AACD,MAAI,CAAC,YAAY;AACf,UAAM,IAAI,cAAc,KAAK,EAAE,OAAO,uBAAuB,CAAC;AAAA,EAChE;AACA,SAAO;AACT;AAEA,gCAAgC;AAAA,EAC9B,eAAe;AAAA,EACf,cAAc;AAAA,EACd,sBAAsB;AAAA,EACtB,cAAc;AAAA,EACd,cAAc;AAAA,EACd,aAAa;AAAA,EACb,gBAAgB;AAAA,EAChB,4BAA4B,OAAO,EAAE,IAAI,KAAK,OAAO,MAAM;AACzD,UAAM,aAAa,MAAM,iBAAiB,IAAI,OAAO,YAAY;AACjE,UAAM,QAAQ,EAAE,UAAU,WAAW,UAAU,gBAAgB,WAAW,eAAe;AACzF,+BAA2B,KAAK,KAAK;AACrC,WAAO,EAAE,YAAY,MAAM;AAAA,EAC7B;AAAA,EACA,cAAc,OAAO,EAAE,IAAI,KAAK,GAAG,MAAM;AACvC,UAAM,QAAQ,MAAM,sBAAsB,IAAI,iBAAiB,IAAI,EAAE,UAAU,CAAC,YAAY,EAAE,CAAC;AAC/F,QAAI,CAAC,OAAO;AACV,YAAM,IAAI,cAAc,KAAK,EAAE,OAAO,6BAA6B,CAAC;AAAA,IACtE;AACA,QAAI,MAAM,WAAW,WAAW;AAC9B,YAAM,IAAI,cAAc,KAAK,EAAE,OAAO,uBAAuB,CAAC;AAAA,IAChE;AACA,UAAM,QAAQ,EAAE,UAAU,MAAM,UAAU,gBAAgB,MAAM,eAAe;AAC/E,+BAA2B,KAAK,KAAK;AACrC,WAAO,EAAE,OAAO,YAAY,MAAM,YAAY,MAAM;AAAA,EACtD;AAAA,EACA,yBAAyB,OAAO,EAAE,IAAI,SAAS,MAAM;AACnD,WAAO,GAAG,QAAQ,YAAY,SAAS,cAAc,EAAE,SAAS,MAAM,CAAC;AAAA,EACzE;AACF,CAAC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
reorderDictionaryEntriesCommandSchema,
|
|
4
4
|
setDefaultDictionaryEntryCommandSchema
|
|
5
5
|
} from "@open-mercato/core/modules/dictionaries/data/validators";
|
|
6
|
+
import { ensureDictionaryEntryScope } from "@open-mercato/core/modules/dictionaries/commands/factory";
|
|
6
7
|
import { registerCommand } from "@open-mercato/shared/lib/commands";
|
|
7
8
|
import { extractUndoPayload } from "@open-mercato/shared/lib/commands/undo";
|
|
8
9
|
import { withAtomicFlush } from "@open-mercato/shared/lib/commands/flush";
|
|
@@ -23,16 +24,6 @@ const dictionaryCrudEvents = {
|
|
|
23
24
|
...ctx.syncOrigin ? { syncOrigin: ctx.syncOrigin } : {}
|
|
24
25
|
})
|
|
25
26
|
};
|
|
26
|
-
function ensureScope(ctx, scope) {
|
|
27
|
-
const tenantId = ctx.auth?.tenantId ?? null;
|
|
28
|
-
if (tenantId && tenantId !== scope.tenantId) {
|
|
29
|
-
throw new CrudHttpError(403, { error: "Forbidden" });
|
|
30
|
-
}
|
|
31
|
-
const organizationId = ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null;
|
|
32
|
-
if (organizationId && organizationId !== scope.organizationId) {
|
|
33
|
-
throw new CrudHttpError(403, { error: "Forbidden" });
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
27
|
async function requireDictionary(em, id, scope) {
|
|
37
28
|
const dictionary = await findOneWithDecryption(
|
|
38
29
|
em,
|
|
@@ -51,7 +42,7 @@ const reorderDictionaryEntriesCommand = {
|
|
|
51
42
|
async prepare(rawInput, ctx) {
|
|
52
43
|
const parsed = reorderDictionaryEntriesCommandSchema.parse(rawInput);
|
|
53
44
|
const scope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId };
|
|
54
|
-
|
|
45
|
+
ensureDictionaryEntryScope(ctx, scope);
|
|
55
46
|
const em = ctx.container.resolve("em").fork();
|
|
56
47
|
const ids = parsed.entries.map((e) => e.id);
|
|
57
48
|
const existing = await findWithDecryption(
|
|
@@ -75,7 +66,7 @@ const reorderDictionaryEntriesCommand = {
|
|
|
75
66
|
async execute(rawInput, ctx) {
|
|
76
67
|
const parsed = reorderDictionaryEntriesCommandSchema.parse(rawInput);
|
|
77
68
|
const scope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId };
|
|
78
|
-
|
|
69
|
+
ensureDictionaryEntryScope(ctx, scope);
|
|
79
70
|
const em = ctx.container.resolve("em").fork();
|
|
80
71
|
const dictionary = await requireDictionary(em, parsed.dictionaryId, scope);
|
|
81
72
|
const ids = parsed.entries.map((e) => e.id);
|
|
@@ -164,7 +155,7 @@ const reorderDictionaryEntriesCommand = {
|
|
|
164
155
|
const undo = extractUndoPayload(logEntry);
|
|
165
156
|
const before = undo?.before ?? logEntry?.snapshotBefore ?? null;
|
|
166
157
|
if (!before) return;
|
|
167
|
-
|
|
158
|
+
ensureDictionaryEntryScope(ctx, before.scope);
|
|
168
159
|
const em = ctx.container.resolve("em").fork();
|
|
169
160
|
const ids = before.positions.map((p) => p.id);
|
|
170
161
|
const entries = await findWithDecryption(
|
|
@@ -209,7 +200,7 @@ const setDefaultDictionaryEntryCommand = {
|
|
|
209
200
|
async prepare(rawInput, ctx) {
|
|
210
201
|
const parsed = setDefaultDictionaryEntryCommandSchema.parse(rawInput);
|
|
211
202
|
const scope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId };
|
|
212
|
-
|
|
203
|
+
ensureDictionaryEntryScope(ctx, scope);
|
|
213
204
|
const em = ctx.container.resolve("em").fork();
|
|
214
205
|
const dictionary = await requireDictionary(em, parsed.dictionaryId, scope);
|
|
215
206
|
const previousDefaults = await findWithDecryption(
|
|
@@ -235,7 +226,7 @@ const setDefaultDictionaryEntryCommand = {
|
|
|
235
226
|
async execute(rawInput, ctx) {
|
|
236
227
|
const parsed = setDefaultDictionaryEntryCommandSchema.parse(rawInput);
|
|
237
228
|
const scope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId };
|
|
238
|
-
|
|
229
|
+
ensureDictionaryEntryScope(ctx, scope);
|
|
239
230
|
const em = ctx.container.resolve("em").fork();
|
|
240
231
|
const dictionary = await requireDictionary(em, parsed.dictionaryId, scope);
|
|
241
232
|
const targetEntry = await findOneWithDecryption(
|
|
@@ -330,7 +321,7 @@ const setDefaultDictionaryEntryCommand = {
|
|
|
330
321
|
const undo = extractUndoPayload(logEntry);
|
|
331
322
|
const before = undo?.before ?? logEntry?.snapshotBefore ?? null;
|
|
332
323
|
if (!before) return;
|
|
333
|
-
|
|
324
|
+
ensureDictionaryEntryScope(ctx, before.scope);
|
|
334
325
|
const em = ctx.container.resolve("em").fork();
|
|
335
326
|
const dictionary = await requireDictionary(em, before.dictionaryId, before.scope);
|
|
336
327
|
const newDefault = before.newDefaultId ? await findOneWithDecryption(
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/dictionaries/commands/entry-operations.ts"],
|
|
4
|
-
"sourcesContent": ["import type { EntityManager, FilterQuery } from '@mikro-orm/postgresql'\nimport { Dictionary, DictionaryEntry } from '@open-mercato/core/modules/dictionaries/data/entities'\nimport {\n reorderDictionaryEntriesCommandSchema,\n setDefaultDictionaryEntryCommandSchema,\n type ReorderDictionaryEntriesCommandInput,\n type SetDefaultDictionaryEntryCommandInput,\n} from '@open-mercato/core/modules/dictionaries/data/validators'\nimport { registerCommand, type CommandHandler, type CommandRuntimeContext } from '@open-mercato/shared/lib/commands'\nimport { extractUndoPayload } from '@open-mercato/shared/lib/commands/undo'\nimport { withAtomicFlush } from '@open-mercato/shared/lib/commands/flush'\nimport { emitCrudSideEffects, emitCrudUndoSideEffects } from '@open-mercato/shared/lib/commands/helpers'\nimport type { CrudEventsConfig } from '@open-mercato/shared/lib/crud/types'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport { findOneWithDecryption, findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\n\nconst RESOURCE_KIND = 'dictionaries.dictionary'\nconst INDEXER_ENTITY_TYPE = 'dictionaries:entry'\n\ntype DictionaryScope = {\n tenantId: string\n organizationId: string\n}\n\nconst dictionaryCrudEvents: CrudEventsConfig = {\n module: 'dictionaries',\n entity: 'entry',\n persistent: true,\n buildPayload: (ctx) => ({\n id: ctx.identifiers.id,\n organizationId: ctx.identifiers.organizationId,\n tenantId: ctx.identifiers.tenantId,\n ...(ctx.syncOrigin ? { syncOrigin: ctx.syncOrigin } : {}),\n }),\n}\n\nfunction ensureScope(ctx: CommandRuntimeContext, scope: DictionaryScope): void {\n const tenantId = ctx.auth?.tenantId ?? null\n if (tenantId && tenantId !== scope.tenantId) {\n throw new CrudHttpError(403, { error: 'Forbidden' })\n }\n const organizationId = ctx.selectedOrganizationId ?? ctx.auth?.orgId ?? null\n if (organizationId && organizationId !== scope.organizationId) {\n throw new CrudHttpError(403, { error: 'Forbidden' })\n }\n}\n\nasync function requireDictionary(\n em: EntityManager,\n id: string,\n scope: DictionaryScope,\n): Promise<Dictionary> {\n const dictionary = await findOneWithDecryption(\n em,\n Dictionary,\n { id, tenantId: scope.tenantId, organizationId: scope.organizationId, deletedAt: null },\n undefined,\n scope,\n )\n if (!dictionary) {\n throw new CrudHttpError(404, { error: 'Dictionary not found' })\n }\n return dictionary\n}\n\ntype ReorderSnapshot = {\n dictionaryId: string\n scope: DictionaryScope\n positions: Array<{ id: string; position: number }>\n}\n\ntype ReorderUndoPayload = {\n before?: ReorderSnapshot | null\n after?: ReorderSnapshot | null\n}\n\nconst reorderDictionaryEntriesCommand: CommandHandler<\n ReorderDictionaryEntriesCommandInput,\n { dictionaryId: string; updatedIds: string[] }\n> = {\n id: 'dictionaries.entries.reorder',\n async prepare(rawInput, ctx) {\n const parsed = reorderDictionaryEntriesCommandSchema.parse(rawInput)\n const scope: DictionaryScope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId }\n ensureScope(ctx, scope)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const ids = parsed.entries.map((e) => e.id)\n const existing = await findWithDecryption(\n em,\n DictionaryEntry,\n {\n id: { $in: ids },\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n scope,\n )\n const before: ReorderSnapshot = {\n dictionaryId: parsed.dictionaryId,\n scope,\n positions: existing.map((entry) => ({ id: entry.id, position: entry.position ?? 0 })),\n }\n return { before }\n },\n async execute(rawInput, ctx) {\n const parsed = reorderDictionaryEntriesCommandSchema.parse(rawInput)\n const scope: DictionaryScope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId }\n ensureScope(ctx, scope)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const dictionary = await requireDictionary(em, parsed.dictionaryId, scope)\n const ids = parsed.entries.map((e) => e.id)\n const entries = await findWithDecryption(\n em,\n DictionaryEntry,\n {\n id: { $in: ids },\n dictionary,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n scope,\n )\n const entryMap = new Map<string, DictionaryEntry>()\n for (const entry of entries) entryMap.set(entry.id, entry)\n\n const updatedIds: string[] = []\n await withAtomicFlush(em, [\n () => {\n for (const { id, position } of parsed.entries) {\n const entry = entryMap.get(id)\n if (!entry) continue\n entry.position = position\n entry.updatedAt = new Date()\n updatedIds.push(id)\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n for (const entry of entries) {\n await emitCrudSideEffects({\n dataEngine,\n action: 'updated',\n entity: entry,\n identifiers: { id: entry.id, tenantId: entry.tenantId, organizationId: entry.organizationId },\n syncOrigin: ctx.syncOrigin,\n events: dictionaryCrudEvents,\n indexer: { entityType: INDEXER_ENTITY_TYPE },\n })\n }\n\n return { dictionaryId: parsed.dictionaryId, updatedIds }\n },\n captureAfter: async (rawInput, _result, ctx) => {\n const parsed = reorderDictionaryEntriesCommandSchema.parse(rawInput)\n const em = ctx.container.resolve('em') as EntityManager\n const scope: DictionaryScope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId }\n const ids = parsed.entries.map((e) => e.id)\n const current = await findWithDecryption(\n em,\n DictionaryEntry,\n {\n id: { $in: ids },\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n scope,\n )\n const after: ReorderSnapshot = {\n dictionaryId: parsed.dictionaryId,\n scope,\n positions: current.map((entry) => ({ id: entry.id, position: entry.position ?? 0 })),\n }\n return after\n },\n buildLog: async ({ snapshots, result }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as ReorderSnapshot | undefined\n const after = snapshots.after as ReorderSnapshot | undefined\n if (!before || !after) return null\n return {\n actionLabel: translate('dictionaries.entries.audit.reorder', 'Reorder dictionary entries'),\n resourceKind: RESOURCE_KIND,\n resourceId: result.dictionaryId,\n tenantId: before.scope.tenantId,\n organizationId: before.scope.organizationId,\n snapshotBefore: before,\n snapshotAfter: after,\n payload: {\n undo: { before, after } satisfies ReorderUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const undo = extractUndoPayload<ReorderUndoPayload>(logEntry)\n const before = undo?.before ?? (logEntry?.snapshotBefore as ReorderSnapshot | null | undefined) ?? null\n if (!before) return\n ensureScope(ctx, before.scope)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const ids = before.positions.map((p) => p.id)\n const entries = await findWithDecryption(\n em,\n DictionaryEntry,\n {\n id: { $in: ids },\n tenantId: before.scope.tenantId,\n organizationId: before.scope.organizationId,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n before.scope,\n )\n const entryMap = new Map<string, DictionaryEntry>()\n for (const entry of entries) entryMap.set(entry.id, entry)\n\n await withAtomicFlush(em, [\n () => {\n for (const { id, position } of before.positions) {\n const entry = entryMap.get(id)\n if (!entry) continue\n entry.position = position\n entry.updatedAt = new Date()\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n for (const entry of entries) {\n await emitCrudUndoSideEffects({\n dataEngine,\n action: 'updated',\n entity: entry,\n identifiers: { id: entry.id, tenantId: entry.tenantId, organizationId: entry.organizationId },\n syncOrigin: ctx.syncOrigin,\n events: dictionaryCrudEvents,\n indexer: { entityType: INDEXER_ENTITY_TYPE },\n })\n }\n },\n}\n\ntype DefaultSnapshot = {\n dictionaryId: string\n scope: DictionaryScope\n previousDefaultIds: string[]\n newDefaultId: string\n}\n\ntype DefaultUndoPayload = {\n before?: DefaultSnapshot | null\n after?: DefaultSnapshot | null\n}\n\nconst setDefaultDictionaryEntryCommand: CommandHandler<\n SetDefaultDictionaryEntryCommandInput,\n { dictionaryId: string; entryId: string; clearedIds: string[] }\n> = {\n id: 'dictionaries.entries.set_default',\n async prepare(rawInput, ctx) {\n const parsed = setDefaultDictionaryEntryCommandSchema.parse(rawInput)\n const scope: DictionaryScope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId }\n ensureScope(ctx, scope)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const dictionary = await requireDictionary(em, parsed.dictionaryId, scope)\n const previousDefaults = await findWithDecryption(\n em,\n DictionaryEntry,\n {\n dictionary,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n isDefault: true,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n scope,\n )\n const before: DefaultSnapshot = {\n dictionaryId: parsed.dictionaryId,\n scope,\n previousDefaultIds: previousDefaults.map((entry) => entry.id),\n newDefaultId: parsed.entryId,\n }\n return { before }\n },\n async execute(rawInput, ctx) {\n const parsed = setDefaultDictionaryEntryCommandSchema.parse(rawInput)\n const scope: DictionaryScope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId }\n ensureScope(ctx, scope)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const dictionary = await requireDictionary(em, parsed.dictionaryId, scope)\n const targetEntry = await findOneWithDecryption(\n em,\n DictionaryEntry,\n {\n id: parsed.entryId,\n dictionary,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n scope,\n )\n if (!targetEntry) {\n throw new CrudHttpError(404, { error: 'Dictionary entry not found' })\n }\n\n const existingDefaults = await findWithDecryption(\n em,\n DictionaryEntry,\n {\n dictionary,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n isDefault: true,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n scope,\n )\n const clearedIds: string[] = []\n\n // The partial unique index `dictionary_entries_one_default_per_dict`\n // (dictionary_id, organization_id, tenant_id) WHERE is_default = true forbids\n // two default entries in the same dictionary. PostgreSQL checks partial unique\n // indexes per-statement (no deferral) and MikroORM does not guarantee that the\n // is_default=false UPDATEs run before the is_default=true UPDATE within a single\n // flush, so clearing and setting in one flush races to a 23505. Clear the prior\n // default(s) in their own flush first, then set the new default in a second\n // flush, so Postgres never observes two default rows at once.\n await withAtomicFlush(em, [\n () => {\n for (const entry of existingDefaults) {\n if (entry.id === targetEntry.id) continue\n entry.isDefault = false\n entry.updatedAt = new Date()\n clearedIds.push(entry.id)\n }\n },\n ], { transaction: true })\n await withAtomicFlush(em, [\n () => {\n targetEntry.isDefault = true\n targetEntry.updatedAt = new Date()\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n const touched = [targetEntry, ...existingDefaults.filter((entry) => entry.id !== targetEntry.id)]\n for (const entry of touched) {\n await emitCrudSideEffects({\n dataEngine,\n action: 'updated',\n entity: entry,\n identifiers: { id: entry.id, tenantId: entry.tenantId, organizationId: entry.organizationId },\n syncOrigin: ctx.syncOrigin,\n events: dictionaryCrudEvents,\n indexer: { entityType: INDEXER_ENTITY_TYPE },\n })\n }\n\n return { dictionaryId: parsed.dictionaryId, entryId: targetEntry.id, clearedIds }\n },\n captureAfter: async (rawInput, result, _ctx) => {\n const parsed = setDefaultDictionaryEntryCommandSchema.parse(rawInput)\n const scope: DictionaryScope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId }\n const after: DefaultSnapshot = {\n dictionaryId: parsed.dictionaryId,\n scope,\n previousDefaultIds: result.clearedIds,\n newDefaultId: result.entryId,\n }\n return after\n },\n buildLog: async ({ snapshots, result }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as DefaultSnapshot | undefined\n const after = snapshots.after as DefaultSnapshot | undefined\n if (!before || !after) return null\n return {\n actionLabel: translate('dictionaries.entries.audit.set_default', 'Set default dictionary entry'),\n resourceKind: RESOURCE_KIND,\n resourceId: result.dictionaryId,\n tenantId: before.scope.tenantId,\n organizationId: before.scope.organizationId,\n snapshotBefore: before,\n snapshotAfter: after,\n payload: {\n undo: { before, after } satisfies DefaultUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const undo = extractUndoPayload<DefaultUndoPayload>(logEntry)\n const before = undo?.before ?? (logEntry?.snapshotBefore as DefaultSnapshot | null | undefined) ?? null\n if (!before) return\n ensureScope(ctx, before.scope)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const dictionary = await requireDictionary(em, before.dictionaryId, before.scope)\n\n const newDefault = before.newDefaultId\n ? await findOneWithDecryption(\n em,\n DictionaryEntry,\n {\n id: before.newDefaultId,\n dictionary,\n tenantId: before.scope.tenantId,\n organizationId: before.scope.organizationId,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n before.scope,\n )\n : null\n const previousDefaults = before.previousDefaultIds.length > 0\n ? await findWithDecryption(\n em,\n DictionaryEntry,\n {\n id: { $in: before.previousDefaultIds },\n dictionary,\n tenantId: before.scope.tenantId,\n organizationId: before.scope.organizationId,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n before.scope,\n )\n : []\n\n // Same partial-unique-index ordering constraint as `execute`: clear the\n // current default in its own flush before restoring the previous default(s),\n // so Postgres never sees two is_default=true rows in the dictionary at once.\n await withAtomicFlush(em, [\n () => {\n if (newDefault) {\n newDefault.isDefault = false\n newDefault.updatedAt = new Date()\n }\n },\n ], { transaction: true })\n await withAtomicFlush(em, [\n () => {\n for (const entry of previousDefaults) {\n entry.isDefault = true\n entry.updatedAt = new Date()\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n const touched = [\n ...(newDefault ? [newDefault] : []),\n ...previousDefaults,\n ]\n for (const entry of touched) {\n await emitCrudUndoSideEffects({\n dataEngine,\n action: 'updated',\n entity: entry,\n identifiers: { id: entry.id, tenantId: entry.tenantId, organizationId: entry.organizationId },\n syncOrigin: ctx.syncOrigin,\n events: dictionaryCrudEvents,\n indexer: { entityType: INDEXER_ENTITY_TYPE },\n })\n }\n },\n}\n\nregisterCommand(reorderDictionaryEntriesCommand)\nregisterCommand(setDefaultDictionaryEntryCommand)\n"],
|
|
5
|
-
"mappings": "AACA,SAAS,YAAY,uBAAuB;AAC5C;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AACP,SAAS,
|
|
4
|
+
"sourcesContent": ["import type { EntityManager, FilterQuery } from '@mikro-orm/postgresql'\nimport { Dictionary, DictionaryEntry } from '@open-mercato/core/modules/dictionaries/data/entities'\nimport {\n reorderDictionaryEntriesCommandSchema,\n setDefaultDictionaryEntryCommandSchema,\n type ReorderDictionaryEntriesCommandInput,\n type SetDefaultDictionaryEntryCommandInput,\n} from '@open-mercato/core/modules/dictionaries/data/validators'\nimport { ensureDictionaryEntryScope } from '@open-mercato/core/modules/dictionaries/commands/factory'\nimport { registerCommand, type CommandHandler } from '@open-mercato/shared/lib/commands'\nimport { extractUndoPayload } from '@open-mercato/shared/lib/commands/undo'\nimport { withAtomicFlush } from '@open-mercato/shared/lib/commands/flush'\nimport { emitCrudSideEffects, emitCrudUndoSideEffects } from '@open-mercato/shared/lib/commands/helpers'\nimport type { CrudEventsConfig } from '@open-mercato/shared/lib/crud/types'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport { findOneWithDecryption, findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\n\nconst RESOURCE_KIND = 'dictionaries.dictionary'\nconst INDEXER_ENTITY_TYPE = 'dictionaries:entry'\n\ntype DictionaryScope = {\n tenantId: string\n organizationId: string\n}\n\nconst dictionaryCrudEvents: CrudEventsConfig = {\n module: 'dictionaries',\n entity: 'entry',\n persistent: true,\n buildPayload: (ctx) => ({\n id: ctx.identifiers.id,\n organizationId: ctx.identifiers.organizationId,\n tenantId: ctx.identifiers.tenantId,\n ...(ctx.syncOrigin ? { syncOrigin: ctx.syncOrigin } : {}),\n }),\n}\n\nasync function requireDictionary(\n em: EntityManager,\n id: string,\n scope: DictionaryScope,\n): Promise<Dictionary> {\n const dictionary = await findOneWithDecryption(\n em,\n Dictionary,\n { id, tenantId: scope.tenantId, organizationId: scope.organizationId, deletedAt: null },\n undefined,\n scope,\n )\n if (!dictionary) {\n throw new CrudHttpError(404, { error: 'Dictionary not found' })\n }\n return dictionary\n}\n\ntype ReorderSnapshot = {\n dictionaryId: string\n scope: DictionaryScope\n positions: Array<{ id: string; position: number }>\n}\n\ntype ReorderUndoPayload = {\n before?: ReorderSnapshot | null\n after?: ReorderSnapshot | null\n}\n\nconst reorderDictionaryEntriesCommand: CommandHandler<\n ReorderDictionaryEntriesCommandInput,\n { dictionaryId: string; updatedIds: string[] }\n> = {\n id: 'dictionaries.entries.reorder',\n async prepare(rawInput, ctx) {\n const parsed = reorderDictionaryEntriesCommandSchema.parse(rawInput)\n const scope: DictionaryScope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId }\n ensureDictionaryEntryScope(ctx, scope)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const ids = parsed.entries.map((e) => e.id)\n const existing = await findWithDecryption(\n em,\n DictionaryEntry,\n {\n id: { $in: ids },\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n scope,\n )\n const before: ReorderSnapshot = {\n dictionaryId: parsed.dictionaryId,\n scope,\n positions: existing.map((entry) => ({ id: entry.id, position: entry.position ?? 0 })),\n }\n return { before }\n },\n async execute(rawInput, ctx) {\n const parsed = reorderDictionaryEntriesCommandSchema.parse(rawInput)\n const scope: DictionaryScope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId }\n ensureDictionaryEntryScope(ctx, scope)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const dictionary = await requireDictionary(em, parsed.dictionaryId, scope)\n const ids = parsed.entries.map((e) => e.id)\n const entries = await findWithDecryption(\n em,\n DictionaryEntry,\n {\n id: { $in: ids },\n dictionary,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n scope,\n )\n const entryMap = new Map<string, DictionaryEntry>()\n for (const entry of entries) entryMap.set(entry.id, entry)\n\n const updatedIds: string[] = []\n await withAtomicFlush(em, [\n () => {\n for (const { id, position } of parsed.entries) {\n const entry = entryMap.get(id)\n if (!entry) continue\n entry.position = position\n entry.updatedAt = new Date()\n updatedIds.push(id)\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n for (const entry of entries) {\n await emitCrudSideEffects({\n dataEngine,\n action: 'updated',\n entity: entry,\n identifiers: { id: entry.id, tenantId: entry.tenantId, organizationId: entry.organizationId },\n syncOrigin: ctx.syncOrigin,\n events: dictionaryCrudEvents,\n indexer: { entityType: INDEXER_ENTITY_TYPE },\n })\n }\n\n return { dictionaryId: parsed.dictionaryId, updatedIds }\n },\n captureAfter: async (rawInput, _result, ctx) => {\n const parsed = reorderDictionaryEntriesCommandSchema.parse(rawInput)\n const em = ctx.container.resolve('em') as EntityManager\n const scope: DictionaryScope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId }\n const ids = parsed.entries.map((e) => e.id)\n const current = await findWithDecryption(\n em,\n DictionaryEntry,\n {\n id: { $in: ids },\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n scope,\n )\n const after: ReorderSnapshot = {\n dictionaryId: parsed.dictionaryId,\n scope,\n positions: current.map((entry) => ({ id: entry.id, position: entry.position ?? 0 })),\n }\n return after\n },\n buildLog: async ({ snapshots, result }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as ReorderSnapshot | undefined\n const after = snapshots.after as ReorderSnapshot | undefined\n if (!before || !after) return null\n return {\n actionLabel: translate('dictionaries.entries.audit.reorder', 'Reorder dictionary entries'),\n resourceKind: RESOURCE_KIND,\n resourceId: result.dictionaryId,\n tenantId: before.scope.tenantId,\n organizationId: before.scope.organizationId,\n snapshotBefore: before,\n snapshotAfter: after,\n payload: {\n undo: { before, after } satisfies ReorderUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const undo = extractUndoPayload<ReorderUndoPayload>(logEntry)\n const before = undo?.before ?? (logEntry?.snapshotBefore as ReorderSnapshot | null | undefined) ?? null\n if (!before) return\n ensureDictionaryEntryScope(ctx, before.scope)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const ids = before.positions.map((p) => p.id)\n const entries = await findWithDecryption(\n em,\n DictionaryEntry,\n {\n id: { $in: ids },\n tenantId: before.scope.tenantId,\n organizationId: before.scope.organizationId,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n before.scope,\n )\n const entryMap = new Map<string, DictionaryEntry>()\n for (const entry of entries) entryMap.set(entry.id, entry)\n\n await withAtomicFlush(em, [\n () => {\n for (const { id, position } of before.positions) {\n const entry = entryMap.get(id)\n if (!entry) continue\n entry.position = position\n entry.updatedAt = new Date()\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n for (const entry of entries) {\n await emitCrudUndoSideEffects({\n dataEngine,\n action: 'updated',\n entity: entry,\n identifiers: { id: entry.id, tenantId: entry.tenantId, organizationId: entry.organizationId },\n syncOrigin: ctx.syncOrigin,\n events: dictionaryCrudEvents,\n indexer: { entityType: INDEXER_ENTITY_TYPE },\n })\n }\n },\n}\n\ntype DefaultSnapshot = {\n dictionaryId: string\n scope: DictionaryScope\n previousDefaultIds: string[]\n newDefaultId: string\n}\n\ntype DefaultUndoPayload = {\n before?: DefaultSnapshot | null\n after?: DefaultSnapshot | null\n}\n\nconst setDefaultDictionaryEntryCommand: CommandHandler<\n SetDefaultDictionaryEntryCommandInput,\n { dictionaryId: string; entryId: string; clearedIds: string[] }\n> = {\n id: 'dictionaries.entries.set_default',\n async prepare(rawInput, ctx) {\n const parsed = setDefaultDictionaryEntryCommandSchema.parse(rawInput)\n const scope: DictionaryScope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId }\n ensureDictionaryEntryScope(ctx, scope)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const dictionary = await requireDictionary(em, parsed.dictionaryId, scope)\n const previousDefaults = await findWithDecryption(\n em,\n DictionaryEntry,\n {\n dictionary,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n isDefault: true,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n scope,\n )\n const before: DefaultSnapshot = {\n dictionaryId: parsed.dictionaryId,\n scope,\n previousDefaultIds: previousDefaults.map((entry) => entry.id),\n newDefaultId: parsed.entryId,\n }\n return { before }\n },\n async execute(rawInput, ctx) {\n const parsed = setDefaultDictionaryEntryCommandSchema.parse(rawInput)\n const scope: DictionaryScope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId }\n ensureDictionaryEntryScope(ctx, scope)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const dictionary = await requireDictionary(em, parsed.dictionaryId, scope)\n const targetEntry = await findOneWithDecryption(\n em,\n DictionaryEntry,\n {\n id: parsed.entryId,\n dictionary,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n scope,\n )\n if (!targetEntry) {\n throw new CrudHttpError(404, { error: 'Dictionary entry not found' })\n }\n\n const existingDefaults = await findWithDecryption(\n em,\n DictionaryEntry,\n {\n dictionary,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n isDefault: true,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n scope,\n )\n const clearedIds: string[] = []\n\n // The partial unique index `dictionary_entries_one_default_per_dict`\n // (dictionary_id, organization_id, tenant_id) WHERE is_default = true forbids\n // two default entries in the same dictionary. PostgreSQL checks partial unique\n // indexes per-statement (no deferral) and MikroORM does not guarantee that the\n // is_default=false UPDATEs run before the is_default=true UPDATE within a single\n // flush, so clearing and setting in one flush races to a 23505. Clear the prior\n // default(s) in their own flush first, then set the new default in a second\n // flush, so Postgres never observes two default rows at once.\n await withAtomicFlush(em, [\n () => {\n for (const entry of existingDefaults) {\n if (entry.id === targetEntry.id) continue\n entry.isDefault = false\n entry.updatedAt = new Date()\n clearedIds.push(entry.id)\n }\n },\n ], { transaction: true })\n await withAtomicFlush(em, [\n () => {\n targetEntry.isDefault = true\n targetEntry.updatedAt = new Date()\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n const touched = [targetEntry, ...existingDefaults.filter((entry) => entry.id !== targetEntry.id)]\n for (const entry of touched) {\n await emitCrudSideEffects({\n dataEngine,\n action: 'updated',\n entity: entry,\n identifiers: { id: entry.id, tenantId: entry.tenantId, organizationId: entry.organizationId },\n syncOrigin: ctx.syncOrigin,\n events: dictionaryCrudEvents,\n indexer: { entityType: INDEXER_ENTITY_TYPE },\n })\n }\n\n return { dictionaryId: parsed.dictionaryId, entryId: targetEntry.id, clearedIds }\n },\n captureAfter: async (rawInput, result, _ctx) => {\n const parsed = setDefaultDictionaryEntryCommandSchema.parse(rawInput)\n const scope: DictionaryScope = { tenantId: parsed.tenantId, organizationId: parsed.organizationId }\n const after: DefaultSnapshot = {\n dictionaryId: parsed.dictionaryId,\n scope,\n previousDefaultIds: result.clearedIds,\n newDefaultId: result.entryId,\n }\n return after\n },\n buildLog: async ({ snapshots, result }) => {\n const { translate } = await resolveTranslations()\n const before = snapshots.before as DefaultSnapshot | undefined\n const after = snapshots.after as DefaultSnapshot | undefined\n if (!before || !after) return null\n return {\n actionLabel: translate('dictionaries.entries.audit.set_default', 'Set default dictionary entry'),\n resourceKind: RESOURCE_KIND,\n resourceId: result.dictionaryId,\n tenantId: before.scope.tenantId,\n organizationId: before.scope.organizationId,\n snapshotBefore: before,\n snapshotAfter: after,\n payload: {\n undo: { before, after } satisfies DefaultUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const undo = extractUndoPayload<DefaultUndoPayload>(logEntry)\n const before = undo?.before ?? (logEntry?.snapshotBefore as DefaultSnapshot | null | undefined) ?? null\n if (!before) return\n ensureDictionaryEntryScope(ctx, before.scope)\n\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const dictionary = await requireDictionary(em, before.dictionaryId, before.scope)\n\n const newDefault = before.newDefaultId\n ? await findOneWithDecryption(\n em,\n DictionaryEntry,\n {\n id: before.newDefaultId,\n dictionary,\n tenantId: before.scope.tenantId,\n organizationId: before.scope.organizationId,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n before.scope,\n )\n : null\n const previousDefaults = before.previousDefaultIds.length > 0\n ? await findWithDecryption(\n em,\n DictionaryEntry,\n {\n id: { $in: before.previousDefaultIds },\n dictionary,\n tenantId: before.scope.tenantId,\n organizationId: before.scope.organizationId,\n } as FilterQuery<DictionaryEntry>,\n undefined,\n before.scope,\n )\n : []\n\n // Same partial-unique-index ordering constraint as `execute`: clear the\n // current default in its own flush before restoring the previous default(s),\n // so Postgres never sees two is_default=true rows in the dictionary at once.\n await withAtomicFlush(em, [\n () => {\n if (newDefault) {\n newDefault.isDefault = false\n newDefault.updatedAt = new Date()\n }\n },\n ], { transaction: true })\n await withAtomicFlush(em, [\n () => {\n for (const entry of previousDefaults) {\n entry.isDefault = true\n entry.updatedAt = new Date()\n }\n },\n ], { transaction: true })\n\n const dataEngine = ctx.container.resolve('dataEngine') as DataEngine\n const touched = [\n ...(newDefault ? [newDefault] : []),\n ...previousDefaults,\n ]\n for (const entry of touched) {\n await emitCrudUndoSideEffects({\n dataEngine,\n action: 'updated',\n entity: entry,\n identifiers: { id: entry.id, tenantId: entry.tenantId, organizationId: entry.organizationId },\n syncOrigin: ctx.syncOrigin,\n events: dictionaryCrudEvents,\n indexer: { entityType: INDEXER_ENTITY_TYPE },\n })\n }\n },\n}\n\nregisterCommand(reorderDictionaryEntriesCommand)\nregisterCommand(setDefaultDictionaryEntryCommand)\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,YAAY,uBAAuB;AAC5C;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AACP,SAAS,kCAAkC;AAC3C,SAAS,uBAA4C;AACrD,SAAS,0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",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -9,13 +9,17 @@ import {
|
|
|
9
9
|
} from "@open-mercato/shared/lib/commands";
|
|
10
10
|
import { extractUndoPayload } from "@open-mercato/shared/lib/commands/undo";
|
|
11
11
|
import { buildChanges, requireId } from "@open-mercato/shared/lib/commands/helpers";
|
|
12
|
+
import { ensureOrganizationScope, ensureTenantScope } from "@open-mercato/shared/lib/commands/scope";
|
|
12
13
|
import { CrudHttpError } from "@open-mercato/shared/lib/crud/errors";
|
|
13
14
|
import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
|
|
14
15
|
import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
|
|
16
|
+
function ensureDictionaryEntryScope(ctx, scope) {
|
|
17
|
+
ensureTenantScope(ctx, scope.tenantId);
|
|
18
|
+
ensureOrganizationScope(ctx, scope.organizationId);
|
|
19
|
+
}
|
|
15
20
|
const ENTRY_CHANGE_KEYS = ["value", "label", "color", "icon", "position", "isDefault"];
|
|
16
21
|
function toScopeEnsurer(fn) {
|
|
17
|
-
return typeof fn === "function" ? fn :
|
|
18
|
-
};
|
|
22
|
+
return typeof fn === "function" ? fn : ensureDictionaryEntryScope;
|
|
19
23
|
}
|
|
20
24
|
async function loadSnapshot(em, id) {
|
|
21
25
|
const entry = await findOneWithDecryption(em, DictionaryEntry, id, { populate: ["dictionary"] });
|
|
@@ -365,6 +369,8 @@ function registerDictionaryEntryCommands(config) {
|
|
|
365
369
|
registerCommand(deleteCommand);
|
|
366
370
|
}
|
|
367
371
|
export {
|
|
368
|
-
|
|
372
|
+
ensureDictionaryEntryScope,
|
|
373
|
+
registerDictionaryEntryCommands,
|
|
374
|
+
toScopeEnsurer
|
|
369
375
|
};
|
|
370
376
|
//# sourceMappingURL=factory.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/dictionaries/commands/factory.ts"],
|
|
4
|
-
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { Dictionary, DictionaryEntry } from '@open-mercato/core/modules/dictionaries/data/entities'\nimport {\n normalizeDictionaryValue,\n sanitizeDictionaryColor,\n sanitizeDictionaryIcon,\n} from '@open-mercato/core/modules/dictionaries/lib/utils'\nimport {\n registerCommand,\n type CommandHandler,\n type CommandRuntimeContext,\n} from '@open-mercato/shared/lib/commands'\nimport { extractUndoPayload } from '@open-mercato/shared/lib/commands/undo'\nimport { buildChanges, requireId } from '@open-mercato/shared/lib/commands/helpers'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport type { z } from 'zod'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\n\ntype DictionaryScope = {\n tenantId: string\n organizationId: string\n}\n\nexport type DictionaryEntrySnapshot = {\n id: string\n dictionaryId: string\n dictionaryKey: string\n organizationId: string\n tenantId: string\n value: string\n label: string\n color: string | null\n icon: string | null\n createdAt: string\n updatedAt: string\n position?: number\n isDefault?: boolean\n}\n\nexport type DictionaryEntryUndoPayload = {\n before?: DictionaryEntrySnapshot | null\n after?: DictionaryEntrySnapshot | null\n}\n\nconst ENTRY_CHANGE_KEYS = ['value', 'label', 'color', 'icon', 'position', 'isDefault'] as const\n\ntype ResolveDictionaryForCreate<TCreate> = (options: {\n em: EntityManager\n ctx: CommandRuntimeContext\n parsed: TCreate\n}) => Promise<{ dictionary: Dictionary; scope: DictionaryScope }>\n\ntype ResolveEntry<TInput> = (options: {\n em: EntityManager\n ctx: CommandRuntimeContext\n id: string\n parsed?: TInput\n}) => Promise<{ entry: DictionaryEntry; dictionary: Dictionary; scope: DictionaryScope }>\n\ntype EnsureDictionaryForUndo = (options: {\n em: EntityManager\n ctx: CommandRuntimeContext\n snapshot: DictionaryEntrySnapshot\n}) => Promise<Dictionary | null>\n\ntype EnsureScopeFn = (ctx: CommandRuntimeContext, scope: DictionaryScope) => void\n\ntype DictionaryEntryCommandLabels = {\n singular?: string\n create?: string\n update?: string\n delete?: string\n}\n\nexport type DictionaryEntryCommandConfig<TCreate, TUpdate> = {\n commandPrefix: string\n resourceKind: string\n translationKeyPrefix?: string\n createSchema: z.ZodType<TCreate>\n updateSchema: z.ZodType<TUpdate>\n resolveDictionaryForCreate: ResolveDictionaryForCreate<TCreate>\n resolveEntry: ResolveEntry<TUpdate>\n ensureScope?: EnsureScopeFn\n ensureDictionaryForUndo?: EnsureDictionaryForUndo\n duplicateError?: string\n labels?: DictionaryEntryCommandLabels\n}\n\nfunction toScopeEnsurer(fn: EnsureScopeFn | undefined): EnsureScopeFn {\n return typeof fn === 'function' ? fn : () => {}\n}\n\nasync function loadSnapshot(em: EntityManager, id: string): Promise<DictionaryEntrySnapshot | null> {\n const entry = await findOneWithDecryption(em, DictionaryEntry, id, { populate: ['dictionary'] })\n if (!entry) return null\n return {\n id: entry.id,\n dictionaryId: entry.dictionary.id,\n dictionaryKey: entry.dictionary.key,\n organizationId: entry.organizationId,\n tenantId: entry.tenantId,\n value: entry.value,\n label: entry.label,\n color: entry.color ?? null,\n icon: entry.icon ?? null,\n position: entry.position ?? 0,\n isDefault: entry.isDefault ?? false,\n createdAt:\n entry.createdAt instanceof Date\n ? entry.createdAt.toISOString()\n : new Date(entry.createdAt).toISOString(),\n updatedAt:\n entry.updatedAt instanceof Date\n ? entry.updatedAt.toISOString()\n : new Date(entry.updatedAt).toISOString(),\n }\n}\n\nfunction applySnapshot(entry: DictionaryEntry, snapshot: DictionaryEntrySnapshot): void {\n entry.value = snapshot.value\n entry.normalizedValue = normalizeDictionaryValue(snapshot.value)\n entry.label = snapshot.label\n entry.color = snapshot.color ?? null\n entry.icon = snapshot.icon ?? null\n if (snapshot.position !== undefined) entry.position = snapshot.position\n if (snapshot.isDefault !== undefined) entry.isDefault = snapshot.isDefault\n entry.organizationId = snapshot.organizationId\n entry.tenantId = snapshot.tenantId\n entry.createdAt = new Date(snapshot.createdAt)\n entry.updatedAt = new Date(snapshot.updatedAt)\n}\n\nfunction ensureDictionaryForUndoFactory(\n fallback?: EnsureDictionaryForUndo\n): EnsureDictionaryForUndo {\n if (fallback) return fallback\n return async ({ em, snapshot }) => {\n const dictionary = await em.findOne(Dictionary, snapshot.dictionaryId)\n return dictionary ?? null\n }\n}\n\nfunction getDuplicateError(config: DictionaryEntryCommandConfig<any, any>): string {\n return config.duplicateError ?? 'Value already exists in this dictionary.'\n}\n\nfunction sanitizeCreatePayload(input: any): {\n value: string\n normalized: string\n label: string\n color: string | null\n icon: string | null\n position: number | undefined\n} {\n const rawValue = typeof input?.value === 'string' ? input.value.trim() : ''\n if (!rawValue) {\n throw new CrudHttpError(400, { error: 'Dictionary entry value is required.' })\n }\n const normalized = normalizeDictionaryValue(rawValue)\n const rawLabel = typeof input?.label === 'string' ? input.label.trim() : ''\n const label = rawLabel || rawValue\n const color = sanitizeDictionaryColor(input?.color ?? null)\n const icon = sanitizeDictionaryIcon(input?.icon ?? null)\n const position = typeof input?.position === 'number' ? input.position : undefined\n return { value: rawValue, normalized, label, color, icon, position }\n}\n\nfunction sanitizeUpdatePayload(input: any): {\n value?: string\n normalized?: string\n label?: string\n color?: string | null\n icon?: string | null\n position?: number\n isDefault?: boolean\n} {\n const payload: {\n value?: string\n normalized?: string\n label?: string\n color?: string | null\n icon?: string | null\n position?: number\n isDefault?: boolean\n } = {}\n if (Object.prototype.hasOwnProperty.call(input, 'value')) {\n const rawValue = typeof input?.value === 'string' ? input.value.trim() : ''\n if (!rawValue) {\n throw new CrudHttpError(400, { error: 'Dictionary entry value is required.' })\n }\n payload.value = rawValue\n payload.normalized = normalizeDictionaryValue(rawValue)\n }\n if (Object.prototype.hasOwnProperty.call(input, 'label')) {\n const rawLabel = typeof input?.label === 'string' ? input.label.trim() : ''\n payload.label = rawLabel\n }\n if (Object.prototype.hasOwnProperty.call(input, 'color')) {\n payload.color = sanitizeDictionaryColor(input?.color ?? null)\n }\n if (Object.prototype.hasOwnProperty.call(input, 'icon')) {\n payload.icon = sanitizeDictionaryIcon(input?.icon ?? null)\n }\n if (Object.prototype.hasOwnProperty.call(input, 'position')) {\n const rawPosition = typeof input?.position === 'number' ? input.position : undefined\n if (rawPosition !== undefined) {\n payload.position = rawPosition\n }\n }\n if (Object.prototype.hasOwnProperty.call(input, 'isDefault')) {\n payload.isDefault = typeof input?.isDefault === 'boolean' ? input.isDefault : undefined\n }\n return payload\n}\n\nexport function registerDictionaryEntryCommands<TCreate, TUpdate>(\n config: DictionaryEntryCommandConfig<TCreate, TUpdate>\n): void {\n const scopeEnsurer = toScopeEnsurer(config.ensureScope)\n const ensureDictionaryForUndo = ensureDictionaryForUndoFactory(config.ensureDictionaryForUndo)\n const translationKeyPrefix = config.translationKeyPrefix ?? config.commandPrefix\n const fallbackSingular = config.labels?.singular ?? 'Dictionary entry'\n const fallbackCreate = config.labels?.create ?? `Create ${fallbackSingular}`\n const fallbackUpdate = config.labels?.update ?? `Update ${fallbackSingular}`\n const fallbackDelete = config.labels?.delete ?? `Delete ${fallbackSingular}`\n const duplicateMessage = getDuplicateError(config)\n\n const createCommand: CommandHandler<\n ReturnType<typeof config.createSchema['parse']>,\n { entryId: string }\n > = {\n id: `${config.commandPrefix}.create`,\n async execute(rawInput, ctx) {\n const parsed = config.createSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const { dictionary, scope } = await config.resolveDictionaryForCreate({ em, ctx, parsed })\n scopeEnsurer(ctx, scope)\n\n const { value, normalized, label, color, icon, position } = sanitizeCreatePayload(parsed)\n const duplicate = await em.findOne(DictionaryEntry, {\n dictionary,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n normalizedValue: normalized,\n })\n if (duplicate) {\n throw new CrudHttpError(409, { error: duplicateMessage })\n }\n\n const entry = em.create(DictionaryEntry, {\n dictionary,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n value,\n normalizedValue: normalized,\n label,\n color,\n icon,\n ...(position !== undefined ? { position } : {}),\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n em.persist(entry)\n await em.flush()\n return { entryId: entry.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return loadSnapshot(em, result.entryId)\n },\n buildLog: async ({ result, snapshots }) => {\n const after = snapshots.after as DictionaryEntrySnapshot | undefined\n if (!after) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate(`${translationKeyPrefix}.audit.create`, fallbackCreate),\n resourceKind: config.resourceKind,\n resourceId: result.entryId,\n tenantId: after.tenantId,\n organizationId: after.organizationId,\n snapshotAfter: after,\n payload: {\n undo: { after } satisfies DictionaryEntryUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const undo = extractUndoPayload<DictionaryEntryUndoPayload>(logEntry)\n const after =\n undo?.after ??\n (logEntry?.snapshotAfter as DictionaryEntrySnapshot | null | undefined) ??\n null\n if (!after) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n scopeEnsurer(ctx, { tenantId: after.tenantId, organizationId: after.organizationId })\n const entry = await em.findOne(DictionaryEntry, after.id)\n if (entry) {\n await em.remove(entry).flush()\n return\n }\n await em.nativeDelete(DictionaryEntry, { id: after.id })\n },\n}\n\n const updateCommand: CommandHandler<\n ReturnType<typeof config.updateSchema['parse']>,\n { entryId: string }\n > = {\n id: `${config.commandPrefix}.update`,\n async prepare(input, ctx) {\n const id = requireId(input, 'Dictionary entry id is required')\n const em = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadSnapshot(em, id)\n if (snapshot) {\n scopeEnsurer(ctx, { tenantId: snapshot.tenantId, organizationId: snapshot.organizationId })\n }\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const parsed = config.updateSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const id = requireId(parsed, 'Dictionary entry id is required')\n const { entry, dictionary, scope } = await config.resolveEntry({ em, ctx, id, parsed })\n scopeEnsurer(ctx, scope)\n\n const updates = sanitizeUpdatePayload(parsed)\n if (updates.value !== undefined && updates.normalized !== undefined) {\n if (updates.normalized !== entry.normalizedValue) {\n const duplicate = await em.findOne(DictionaryEntry, {\n dictionary,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n normalizedValue: updates.normalized,\n id: { $ne: entry.id },\n } as any)\n if (duplicate) {\n throw new CrudHttpError(409, { error: duplicateMessage })\n }\n entry.value = updates.value\n entry.normalizedValue = updates.normalized\n if (updates.label === undefined) {\n entry.label = entry.value\n }\n }\n }\n if (updates.label !== undefined) {\n entry.label = updates.label || entry.value\n }\n if (Object.prototype.hasOwnProperty.call(updates, 'color')) {\n entry.color = updates.color ?? null\n }\n if (Object.prototype.hasOwnProperty.call(updates, 'icon')) {\n entry.icon = updates.icon ?? null\n }\n if (updates.position !== undefined) {\n entry.position = updates.position\n }\n if (updates.isDefault !== undefined) {\n entry.isDefault = updates.isDefault\n }\n await em.flush()\n return { entryId: entry.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return loadSnapshot(em, result.entryId)\n },\n buildLog: async ({ snapshots }) => {\n const before = snapshots.before as DictionaryEntrySnapshot | undefined\n const after = snapshots.after as DictionaryEntrySnapshot | undefined\n if (!before || !after) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate(`${translationKeyPrefix}.audit.update`, fallbackUpdate),\n resourceKind: config.resourceKind,\n resourceId: before.id,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n snapshotAfter: after,\n changes: buildChanges(\n before as Record<string, unknown>,\n after as Record<string, unknown>,\n ENTRY_CHANGE_KEYS,\n ),\n payload: {\n undo: { before, after } satisfies DictionaryEntryUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const undo = extractUndoPayload<DictionaryEntryUndoPayload>(logEntry)\n const before =\n undo?.before ??\n (logEntry?.snapshotBefore as DictionaryEntrySnapshot | null | undefined) ??\n null\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n scopeEnsurer(ctx, { tenantId: before.tenantId, organizationId: before.organizationId })\n const dictionary = await ensureDictionaryForUndo({ em, ctx, snapshot: before })\n if (!dictionary) return\n let entry = await em.findOne(DictionaryEntry, before.id)\n if (!entry) {\n entry = em.create(DictionaryEntry, {\n id: before.id,\n dictionary,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n value: before.value,\n normalizedValue: normalizeDictionaryValue(before.value),\n label: before.label,\n color: before.color,\n icon: before.icon,\n createdAt: new Date(before.createdAt),\n updatedAt: new Date(before.updatedAt),\n })\n em.persist(entry)\n } else {\n entry.dictionary = dictionary\n applySnapshot(entry, before)\n }\n await em.flush()\n },\n }\n\n const deleteCommand: CommandHandler<\n { body?: Record<string, unknown>; query?: Record<string, unknown> },\n { entryId: string }\n > = {\n id: `${config.commandPrefix}.delete`,\n async prepare(input, ctx) {\n const id = requireId(input, 'Dictionary entry id is required')\n const em = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadSnapshot(em, id)\n if (snapshot) {\n scopeEnsurer(ctx, { tenantId: snapshot.tenantId, organizationId: snapshot.organizationId })\n }\n return snapshot ? { before: snapshot } : {}\n },\n async execute(input, ctx) {\n const id = requireId(input, 'Dictionary entry id is required')\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const { entry, scope } = await config.resolveEntry({ em, ctx, id })\n scopeEnsurer(ctx, scope)\n em.remove(entry)\n await em.flush()\n return { entryId: id }\n },\n buildLog: async ({ snapshots }) => {\n const before = snapshots.before as DictionaryEntrySnapshot | undefined\n if (!before) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate(`${translationKeyPrefix}.audit.delete`, fallbackDelete),\n resourceKind: config.resourceKind,\n resourceId: before.id,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n payload: {\n undo: { before } satisfies DictionaryEntryUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const undo = extractUndoPayload<DictionaryEntryUndoPayload>(logEntry)\n const before =\n undo?.before ??\n (logEntry?.snapshotBefore as DictionaryEntrySnapshot | null | undefined) ??\n null\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n scopeEnsurer(ctx, { tenantId: before.tenantId, organizationId: before.organizationId })\n const dictionary = await ensureDictionaryForUndo({ em, ctx, snapshot: before })\n if (!dictionary) return\n let entry = await em.findOne(DictionaryEntry, before.id)\n if (!entry) {\n entry = em.create(DictionaryEntry, {\n id: before.id,\n dictionary,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n value: before.value,\n normalizedValue: normalizeDictionaryValue(before.value),\n label: before.label,\n color: before.color,\n icon: before.icon,\n createdAt: new Date(before.createdAt),\n updatedAt: new Date(before.updatedAt),\n })\n em.persist(entry)\n } else {\n entry.dictionary = dictionary\n applySnapshot(entry, before)\n }\n await em.flush()\n },\n }\n\n registerCommand(createCommand)\n registerCommand(updateCommand)\n registerCommand(deleteCommand)\n}\n"],
|
|
5
|
-
"mappings": "AACA,SAAS,YAAY,uBAAuB;AAC5C;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,OAGK;AACP,SAAS,0BAA0B;AACnC,SAAS,cAAc,iBAAiB;AACxC,SAAS,qBAAqB;AAC9B,SAAS,2BAA2B;AAEpC,SAAS,6BAA6B;
|
|
4
|
+
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { Dictionary, DictionaryEntry } from '@open-mercato/core/modules/dictionaries/data/entities'\nimport {\n normalizeDictionaryValue,\n sanitizeDictionaryColor,\n sanitizeDictionaryIcon,\n} from '@open-mercato/core/modules/dictionaries/lib/utils'\nimport {\n registerCommand,\n type CommandHandler,\n type CommandRuntimeContext,\n} from '@open-mercato/shared/lib/commands'\nimport { extractUndoPayload } from '@open-mercato/shared/lib/commands/undo'\nimport { buildChanges, requireId } from '@open-mercato/shared/lib/commands/helpers'\nimport { ensureOrganizationScope, ensureTenantScope } from '@open-mercato/shared/lib/commands/scope'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport type { z } from 'zod'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\n\ntype DictionaryScope = {\n tenantId: string\n organizationId: string\n}\n\nexport function ensureDictionaryEntryScope(ctx: CommandRuntimeContext, scope: DictionaryScope): void {\n ensureTenantScope(ctx, scope.tenantId)\n ensureOrganizationScope(ctx, scope.organizationId)\n}\n\nexport type DictionaryEntrySnapshot = {\n id: string\n dictionaryId: string\n dictionaryKey: string\n organizationId: string\n tenantId: string\n value: string\n label: string\n color: string | null\n icon: string | null\n createdAt: string\n updatedAt: string\n position?: number\n isDefault?: boolean\n}\n\nexport type DictionaryEntryUndoPayload = {\n before?: DictionaryEntrySnapshot | null\n after?: DictionaryEntrySnapshot | null\n}\n\nconst ENTRY_CHANGE_KEYS = ['value', 'label', 'color', 'icon', 'position', 'isDefault'] as const\n\ntype ResolveDictionaryForCreate<TCreate> = (options: {\n em: EntityManager\n ctx: CommandRuntimeContext\n parsed: TCreate\n}) => Promise<{ dictionary: Dictionary; scope: DictionaryScope }>\n\ntype ResolveEntry<TInput> = (options: {\n em: EntityManager\n ctx: CommandRuntimeContext\n id: string\n parsed?: TInput\n}) => Promise<{ entry: DictionaryEntry; dictionary: Dictionary; scope: DictionaryScope }>\n\ntype EnsureDictionaryForUndo = (options: {\n em: EntityManager\n ctx: CommandRuntimeContext\n snapshot: DictionaryEntrySnapshot\n}) => Promise<Dictionary | null>\n\ntype EnsureScopeFn = (ctx: CommandRuntimeContext, scope: DictionaryScope) => void\n\ntype DictionaryEntryCommandLabels = {\n singular?: string\n create?: string\n update?: string\n delete?: string\n}\n\nexport type DictionaryEntryCommandConfig<TCreate, TUpdate> = {\n commandPrefix: string\n resourceKind: string\n translationKeyPrefix?: string\n createSchema: z.ZodType<TCreate>\n updateSchema: z.ZodType<TUpdate>\n resolveDictionaryForCreate: ResolveDictionaryForCreate<TCreate>\n resolveEntry: ResolveEntry<TUpdate>\n ensureScope?: EnsureScopeFn\n ensureDictionaryForUndo?: EnsureDictionaryForUndo\n duplicateError?: string\n labels?: DictionaryEntryCommandLabels\n}\n\nexport function toScopeEnsurer(fn: EnsureScopeFn | undefined): EnsureScopeFn {\n return typeof fn === 'function' ? fn : ensureDictionaryEntryScope\n}\n\nasync function loadSnapshot(em: EntityManager, id: string): Promise<DictionaryEntrySnapshot | null> {\n const entry = await findOneWithDecryption(em, DictionaryEntry, id, { populate: ['dictionary'] })\n if (!entry) return null\n return {\n id: entry.id,\n dictionaryId: entry.dictionary.id,\n dictionaryKey: entry.dictionary.key,\n organizationId: entry.organizationId,\n tenantId: entry.tenantId,\n value: entry.value,\n label: entry.label,\n color: entry.color ?? null,\n icon: entry.icon ?? null,\n position: entry.position ?? 0,\n isDefault: entry.isDefault ?? false,\n createdAt:\n entry.createdAt instanceof Date\n ? entry.createdAt.toISOString()\n : new Date(entry.createdAt).toISOString(),\n updatedAt:\n entry.updatedAt instanceof Date\n ? entry.updatedAt.toISOString()\n : new Date(entry.updatedAt).toISOString(),\n }\n}\n\nfunction applySnapshot(entry: DictionaryEntry, snapshot: DictionaryEntrySnapshot): void {\n entry.value = snapshot.value\n entry.normalizedValue = normalizeDictionaryValue(snapshot.value)\n entry.label = snapshot.label\n entry.color = snapshot.color ?? null\n entry.icon = snapshot.icon ?? null\n if (snapshot.position !== undefined) entry.position = snapshot.position\n if (snapshot.isDefault !== undefined) entry.isDefault = snapshot.isDefault\n entry.organizationId = snapshot.organizationId\n entry.tenantId = snapshot.tenantId\n entry.createdAt = new Date(snapshot.createdAt)\n entry.updatedAt = new Date(snapshot.updatedAt)\n}\n\nfunction ensureDictionaryForUndoFactory(\n fallback?: EnsureDictionaryForUndo\n): EnsureDictionaryForUndo {\n if (fallback) return fallback\n return async ({ em, snapshot }) => {\n const dictionary = await em.findOne(Dictionary, snapshot.dictionaryId)\n return dictionary ?? null\n }\n}\n\nfunction getDuplicateError(config: DictionaryEntryCommandConfig<any, any>): string {\n return config.duplicateError ?? 'Value already exists in this dictionary.'\n}\n\nfunction sanitizeCreatePayload(input: any): {\n value: string\n normalized: string\n label: string\n color: string | null\n icon: string | null\n position: number | undefined\n} {\n const rawValue = typeof input?.value === 'string' ? input.value.trim() : ''\n if (!rawValue) {\n throw new CrudHttpError(400, { error: 'Dictionary entry value is required.' })\n }\n const normalized = normalizeDictionaryValue(rawValue)\n const rawLabel = typeof input?.label === 'string' ? input.label.trim() : ''\n const label = rawLabel || rawValue\n const color = sanitizeDictionaryColor(input?.color ?? null)\n const icon = sanitizeDictionaryIcon(input?.icon ?? null)\n const position = typeof input?.position === 'number' ? input.position : undefined\n return { value: rawValue, normalized, label, color, icon, position }\n}\n\nfunction sanitizeUpdatePayload(input: any): {\n value?: string\n normalized?: string\n label?: string\n color?: string | null\n icon?: string | null\n position?: number\n isDefault?: boolean\n} {\n const payload: {\n value?: string\n normalized?: string\n label?: string\n color?: string | null\n icon?: string | null\n position?: number\n isDefault?: boolean\n } = {}\n if (Object.prototype.hasOwnProperty.call(input, 'value')) {\n const rawValue = typeof input?.value === 'string' ? input.value.trim() : ''\n if (!rawValue) {\n throw new CrudHttpError(400, { error: 'Dictionary entry value is required.' })\n }\n payload.value = rawValue\n payload.normalized = normalizeDictionaryValue(rawValue)\n }\n if (Object.prototype.hasOwnProperty.call(input, 'label')) {\n const rawLabel = typeof input?.label === 'string' ? input.label.trim() : ''\n payload.label = rawLabel\n }\n if (Object.prototype.hasOwnProperty.call(input, 'color')) {\n payload.color = sanitizeDictionaryColor(input?.color ?? null)\n }\n if (Object.prototype.hasOwnProperty.call(input, 'icon')) {\n payload.icon = sanitizeDictionaryIcon(input?.icon ?? null)\n }\n if (Object.prototype.hasOwnProperty.call(input, 'position')) {\n const rawPosition = typeof input?.position === 'number' ? input.position : undefined\n if (rawPosition !== undefined) {\n payload.position = rawPosition\n }\n }\n if (Object.prototype.hasOwnProperty.call(input, 'isDefault')) {\n payload.isDefault = typeof input?.isDefault === 'boolean' ? input.isDefault : undefined\n }\n return payload\n}\n\nexport function registerDictionaryEntryCommands<TCreate, TUpdate>(\n config: DictionaryEntryCommandConfig<TCreate, TUpdate>\n): void {\n const scopeEnsurer = toScopeEnsurer(config.ensureScope)\n const ensureDictionaryForUndo = ensureDictionaryForUndoFactory(config.ensureDictionaryForUndo)\n const translationKeyPrefix = config.translationKeyPrefix ?? config.commandPrefix\n const fallbackSingular = config.labels?.singular ?? 'Dictionary entry'\n const fallbackCreate = config.labels?.create ?? `Create ${fallbackSingular}`\n const fallbackUpdate = config.labels?.update ?? `Update ${fallbackSingular}`\n const fallbackDelete = config.labels?.delete ?? `Delete ${fallbackSingular}`\n const duplicateMessage = getDuplicateError(config)\n\n const createCommand: CommandHandler<\n ReturnType<typeof config.createSchema['parse']>,\n { entryId: string }\n > = {\n id: `${config.commandPrefix}.create`,\n async execute(rawInput, ctx) {\n const parsed = config.createSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const { dictionary, scope } = await config.resolveDictionaryForCreate({ em, ctx, parsed })\n scopeEnsurer(ctx, scope)\n\n const { value, normalized, label, color, icon, position } = sanitizeCreatePayload(parsed)\n const duplicate = await em.findOne(DictionaryEntry, {\n dictionary,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n normalizedValue: normalized,\n })\n if (duplicate) {\n throw new CrudHttpError(409, { error: duplicateMessage })\n }\n\n const entry = em.create(DictionaryEntry, {\n dictionary,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n value,\n normalizedValue: normalized,\n label,\n color,\n icon,\n ...(position !== undefined ? { position } : {}),\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n em.persist(entry)\n await em.flush()\n return { entryId: entry.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return loadSnapshot(em, result.entryId)\n },\n buildLog: async ({ result, snapshots }) => {\n const after = snapshots.after as DictionaryEntrySnapshot | undefined\n if (!after) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate(`${translationKeyPrefix}.audit.create`, fallbackCreate),\n resourceKind: config.resourceKind,\n resourceId: result.entryId,\n tenantId: after.tenantId,\n organizationId: after.organizationId,\n snapshotAfter: after,\n payload: {\n undo: { after } satisfies DictionaryEntryUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const undo = extractUndoPayload<DictionaryEntryUndoPayload>(logEntry)\n const after =\n undo?.after ??\n (logEntry?.snapshotAfter as DictionaryEntrySnapshot | null | undefined) ??\n null\n if (!after) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n scopeEnsurer(ctx, { tenantId: after.tenantId, organizationId: after.organizationId })\n const entry = await em.findOne(DictionaryEntry, after.id)\n if (entry) {\n await em.remove(entry).flush()\n return\n }\n await em.nativeDelete(DictionaryEntry, { id: after.id })\n },\n}\n\n const updateCommand: CommandHandler<\n ReturnType<typeof config.updateSchema['parse']>,\n { entryId: string }\n > = {\n id: `${config.commandPrefix}.update`,\n async prepare(input, ctx) {\n const id = requireId(input, 'Dictionary entry id is required')\n const em = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadSnapshot(em, id)\n if (snapshot) {\n scopeEnsurer(ctx, { tenantId: snapshot.tenantId, organizationId: snapshot.organizationId })\n }\n return snapshot ? { before: snapshot } : {}\n },\n async execute(rawInput, ctx) {\n const parsed = config.updateSchema.parse(rawInput)\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const id = requireId(parsed, 'Dictionary entry id is required')\n const { entry, dictionary, scope } = await config.resolveEntry({ em, ctx, id, parsed })\n scopeEnsurer(ctx, scope)\n\n const updates = sanitizeUpdatePayload(parsed)\n if (updates.value !== undefined && updates.normalized !== undefined) {\n if (updates.normalized !== entry.normalizedValue) {\n const duplicate = await em.findOne(DictionaryEntry, {\n dictionary,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n normalizedValue: updates.normalized,\n id: { $ne: entry.id },\n } as any)\n if (duplicate) {\n throw new CrudHttpError(409, { error: duplicateMessage })\n }\n entry.value = updates.value\n entry.normalizedValue = updates.normalized\n if (updates.label === undefined) {\n entry.label = entry.value\n }\n }\n }\n if (updates.label !== undefined) {\n entry.label = updates.label || entry.value\n }\n if (Object.prototype.hasOwnProperty.call(updates, 'color')) {\n entry.color = updates.color ?? null\n }\n if (Object.prototype.hasOwnProperty.call(updates, 'icon')) {\n entry.icon = updates.icon ?? null\n }\n if (updates.position !== undefined) {\n entry.position = updates.position\n }\n if (updates.isDefault !== undefined) {\n entry.isDefault = updates.isDefault\n }\n await em.flush()\n return { entryId: entry.id }\n },\n captureAfter: async (_input, result, ctx) => {\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n return loadSnapshot(em, result.entryId)\n },\n buildLog: async ({ snapshots }) => {\n const before = snapshots.before as DictionaryEntrySnapshot | undefined\n const after = snapshots.after as DictionaryEntrySnapshot | undefined\n if (!before || !after) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate(`${translationKeyPrefix}.audit.update`, fallbackUpdate),\n resourceKind: config.resourceKind,\n resourceId: before.id,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n snapshotAfter: after,\n changes: buildChanges(\n before as Record<string, unknown>,\n after as Record<string, unknown>,\n ENTRY_CHANGE_KEYS,\n ),\n payload: {\n undo: { before, after } satisfies DictionaryEntryUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const undo = extractUndoPayload<DictionaryEntryUndoPayload>(logEntry)\n const before =\n undo?.before ??\n (logEntry?.snapshotBefore as DictionaryEntrySnapshot | null | undefined) ??\n null\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n scopeEnsurer(ctx, { tenantId: before.tenantId, organizationId: before.organizationId })\n const dictionary = await ensureDictionaryForUndo({ em, ctx, snapshot: before })\n if (!dictionary) return\n let entry = await em.findOne(DictionaryEntry, before.id)\n if (!entry) {\n entry = em.create(DictionaryEntry, {\n id: before.id,\n dictionary,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n value: before.value,\n normalizedValue: normalizeDictionaryValue(before.value),\n label: before.label,\n color: before.color,\n icon: before.icon,\n createdAt: new Date(before.createdAt),\n updatedAt: new Date(before.updatedAt),\n })\n em.persist(entry)\n } else {\n entry.dictionary = dictionary\n applySnapshot(entry, before)\n }\n await em.flush()\n },\n }\n\n const deleteCommand: CommandHandler<\n { body?: Record<string, unknown>; query?: Record<string, unknown> },\n { entryId: string }\n > = {\n id: `${config.commandPrefix}.delete`,\n async prepare(input, ctx) {\n const id = requireId(input, 'Dictionary entry id is required')\n const em = (ctx.container.resolve('em') as EntityManager)\n const snapshot = await loadSnapshot(em, id)\n if (snapshot) {\n scopeEnsurer(ctx, { tenantId: snapshot.tenantId, organizationId: snapshot.organizationId })\n }\n return snapshot ? { before: snapshot } : {}\n },\n async execute(input, ctx) {\n const id = requireId(input, 'Dictionary entry id is required')\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n const { entry, scope } = await config.resolveEntry({ em, ctx, id })\n scopeEnsurer(ctx, scope)\n em.remove(entry)\n await em.flush()\n return { entryId: id }\n },\n buildLog: async ({ snapshots }) => {\n const before = snapshots.before as DictionaryEntrySnapshot | undefined\n if (!before) return null\n const { translate } = await resolveTranslations()\n return {\n actionLabel: translate(`${translationKeyPrefix}.audit.delete`, fallbackDelete),\n resourceKind: config.resourceKind,\n resourceId: before.id,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n snapshotBefore: before,\n payload: {\n undo: { before } satisfies DictionaryEntryUndoPayload,\n },\n }\n },\n undo: async ({ logEntry, ctx }) => {\n const undo = extractUndoPayload<DictionaryEntryUndoPayload>(logEntry)\n const before =\n undo?.before ??\n (logEntry?.snapshotBefore as DictionaryEntrySnapshot | null | undefined) ??\n null\n if (!before) return\n const em = (ctx.container.resolve('em') as EntityManager).fork()\n scopeEnsurer(ctx, { tenantId: before.tenantId, organizationId: before.organizationId })\n const dictionary = await ensureDictionaryForUndo({ em, ctx, snapshot: before })\n if (!dictionary) return\n let entry = await em.findOne(DictionaryEntry, before.id)\n if (!entry) {\n entry = em.create(DictionaryEntry, {\n id: before.id,\n dictionary,\n tenantId: before.tenantId,\n organizationId: before.organizationId,\n value: before.value,\n normalizedValue: normalizeDictionaryValue(before.value),\n label: before.label,\n color: before.color,\n icon: before.icon,\n createdAt: new Date(before.createdAt),\n updatedAt: new Date(before.updatedAt),\n })\n em.persist(entry)\n } else {\n entry.dictionary = dictionary\n applySnapshot(entry, before)\n }\n await em.flush()\n },\n }\n\n registerCommand(createCommand)\n registerCommand(updateCommand)\n registerCommand(deleteCommand)\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,YAAY,uBAAuB;AAC5C;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,OAGK;AACP,SAAS,0BAA0B;AACnC,SAAS,cAAc,iBAAiB;AACxC,SAAS,yBAAyB,yBAAyB;AAC3D,SAAS,qBAAqB;AAC9B,SAAS,2BAA2B;AAEpC,SAAS,6BAA6B;AAO/B,SAAS,2BAA2B,KAA4B,OAA8B;AACnG,oBAAkB,KAAK,MAAM,QAAQ;AACrC,0BAAwB,KAAK,MAAM,cAAc;AACnD;AAuBA,MAAM,oBAAoB,CAAC,SAAS,SAAS,SAAS,QAAQ,YAAY,WAAW;AA4C9E,SAAS,eAAe,IAA8C;AAC3E,SAAO,OAAO,OAAO,aAAa,KAAK;AACzC;AAEA,eAAe,aAAa,IAAmB,IAAqD;AAClG,QAAM,QAAQ,MAAM,sBAAsB,IAAI,iBAAiB,IAAI,EAAE,UAAU,CAAC,YAAY,EAAE,CAAC;AAC/F,MAAI,CAAC,MAAO,QAAO;AACnB,SAAO;AAAA,IACL,IAAI,MAAM;AAAA,IACV,cAAc,MAAM,WAAW;AAAA,IAChC,eAAe,MAAM,WAAW;AAAA,IAC/B,gBA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wB,EAAE,IAAI,KAAK,UAAU,OAAO,CAAC;AAC9E,UAAI,CAAC,WAAY;AACjB,UAAI,QAAQ,MAAM,GAAG,QAAQ,iBAAiB,OAAO,EAAE;AACvD,UAAI,CAAC,OAAO;AACV,gBAAQ,GAAG,OAAO,iBAAiB;AAAA,UACjC,IAAI,OAAO;AAAA,UACX;AAAA,UACA,UAAU,OAAO;AAAA,UACjB,gBAAgB,OAAO;AAAA,UACvB,OAAO,OAAO;AAAA,UACd,iBAAiB,yBAAyB,OAAO,KAAK;AAAA,UACtD,OAAO,OAAO;AAAA,UACd,OAAO,OAAO;AAAA,UACd,MAAM,OAAO;AAAA,UACb,WAAW,IAAI,KAAK,OAAO,SAAS;AAAA,UACpC,WAAW,IAAI,KAAK,OAAO,SAAS;AAAA,QACtC,CAAC;AACD,WAAG,QAAQ,KAAK;AAAA,MAClB,OAAO;AACL,cAAM,aAAa;AACnB,sBAAc,OAAO,MAAM;AAAA,MAC7B;AACA,YAAM,GAAG,MAAM;AAAA,IACjB;AAAA,EACF;AAEA,kBAAgB,aAAa;AAC7B,kBAAgB,aAAa;AAC7B,kBAAgB,aAAa;AAC/B;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -49,9 +49,12 @@ function DictionaryFieldDefEditor({ def, onChange }) {
|
|
|
49
49
|
const [error, setError] = React.useState(null);
|
|
50
50
|
const multiId = React.useId();
|
|
51
51
|
const inlineCreateId = React.useId();
|
|
52
|
+
const inlineCreateHintId = React.useId();
|
|
53
|
+
const defaultValueHintId = React.useId();
|
|
52
54
|
const selectedId = typeof def?.configJson?.dictionaryId === "string" ? def?.configJson?.dictionaryId : "";
|
|
53
55
|
const inlineCreate = def?.configJson?.dictionaryInlineCreate !== false;
|
|
54
56
|
const isMulti = def?.configJson?.multi === true;
|
|
57
|
+
const inlineCreateDisabled = !selectedId || isMulti;
|
|
55
58
|
const errorLoadLabel = t("dictionaries.customFields.errorLoad", "Failed to load dictionaries.");
|
|
56
59
|
React.useEffect(() => {
|
|
57
60
|
let cancelled = false;
|
|
@@ -120,47 +123,56 @@ function DictionaryFieldDefEditor({ def, onChange }) {
|
|
|
120
123
|
/* @__PURE__ */ jsx("span", { children: t("dictionaries.customFields.selectedHint", "Entries from this dictionary populate the field.") }),
|
|
121
124
|
/* @__PURE__ */ jsx("a", { href: manageHref, className: "font-medium text-primary hover:underline", target: "_blank", rel: "noreferrer", children: t("dictionaries.customFields.manageLink", "Manage dictionaries") })
|
|
122
125
|
] }) : null,
|
|
123
|
-
/* @__PURE__ */ jsxs("div", { className: "
|
|
124
|
-
/* @__PURE__ */
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
126
|
+
/* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
|
|
127
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-2 sm:flex-row sm:flex-wrap sm:items-center sm:gap-x-4", children: [
|
|
128
|
+
/* @__PURE__ */ jsxs("div", { className: "inline-flex items-center gap-2 text-xs", children: [
|
|
129
|
+
/* @__PURE__ */ jsx(
|
|
130
|
+
Checkbox,
|
|
131
|
+
{
|
|
132
|
+
id: multiId,
|
|
133
|
+
checked: isMulti,
|
|
134
|
+
onCheckedChange: (checked) => {
|
|
135
|
+
const enabled = checked === true;
|
|
136
|
+
onChange({ multi: enabled, defaultValue: enabled ? void 0 : def?.configJson?.defaultValue });
|
|
137
|
+
},
|
|
138
|
+
disabled: !selectedId
|
|
139
|
+
}
|
|
140
|
+
),
|
|
141
|
+
/* @__PURE__ */ jsx(
|
|
142
|
+
"label",
|
|
143
|
+
{
|
|
144
|
+
htmlFor: multiId,
|
|
145
|
+
className: selectedId ? "cursor-pointer select-none" : "cursor-not-allowed opacity-60",
|
|
146
|
+
children: t("dictionaries.customFields.allowMultiple", "Allow selecting multiple entries")
|
|
147
|
+
}
|
|
148
|
+
)
|
|
149
|
+
] }),
|
|
150
|
+
/* @__PURE__ */ jsxs("div", { className: "inline-flex items-center gap-2 text-xs", children: [
|
|
151
|
+
/* @__PURE__ */ jsx(
|
|
152
|
+
Checkbox,
|
|
153
|
+
{
|
|
154
|
+
id: inlineCreateId,
|
|
155
|
+
checked: !isMulti && inlineCreate,
|
|
156
|
+
onCheckedChange: (checked) => onChange({ dictionaryInlineCreate: checked === true }),
|
|
157
|
+
disabled: inlineCreateDisabled,
|
|
158
|
+
"aria-describedby": isMulti ? inlineCreateHintId : void 0
|
|
159
|
+
}
|
|
160
|
+
),
|
|
161
|
+
/* @__PURE__ */ jsx(
|
|
162
|
+
"label",
|
|
163
|
+
{
|
|
164
|
+
htmlFor: inlineCreateId,
|
|
165
|
+
className: !inlineCreateDisabled ? "cursor-pointer select-none" : "cursor-not-allowed opacity-60",
|
|
166
|
+
children: t("dictionaries.customFields.allowInlineCreate", "Allow inline creation inside forms")
|
|
167
|
+
}
|
|
168
|
+
)
|
|
169
|
+
] })
|
|
170
|
+
] }),
|
|
171
|
+
isMulti ? /* @__PURE__ */ jsx("p", { id: inlineCreateHintId, className: "text-xs text-muted-foreground", children: t(
|
|
172
|
+
"dictionaries.customFields.inlineCreateSingleOnly",
|
|
173
|
+
"Inline creation is available for single-entry dictionary fields only."
|
|
174
|
+
) }) : null
|
|
144
175
|
] }),
|
|
145
|
-
!isMulti ? /* @__PURE__ */ jsxs("div", { className: "inline-flex items-center gap-2 text-xs", children: [
|
|
146
|
-
/* @__PURE__ */ jsx(
|
|
147
|
-
Checkbox,
|
|
148
|
-
{
|
|
149
|
-
id: inlineCreateId,
|
|
150
|
-
checked: inlineCreate,
|
|
151
|
-
onCheckedChange: (checked) => onChange({ dictionaryInlineCreate: checked === true }),
|
|
152
|
-
disabled: !selectedId
|
|
153
|
-
}
|
|
154
|
-
),
|
|
155
|
-
/* @__PURE__ */ jsx(
|
|
156
|
-
"label",
|
|
157
|
-
{
|
|
158
|
-
htmlFor: inlineCreateId,
|
|
159
|
-
className: selectedId ? "cursor-pointer select-none" : "cursor-not-allowed opacity-60",
|
|
160
|
-
children: t("dictionaries.customFields.allowInlineCreate", "Allow inline creation inside forms")
|
|
161
|
-
}
|
|
162
|
-
)
|
|
163
|
-
] }) : null,
|
|
164
176
|
selectedId && !isMulti ? /* @__PURE__ */ jsx(
|
|
165
177
|
DictionaryDefaultSelector,
|
|
166
178
|
{
|
|
@@ -168,7 +180,10 @@ function DictionaryFieldDefEditor({ def, onChange }) {
|
|
|
168
180
|
defaultValue: typeof def?.configJson?.defaultValue === "string" ? def.configJson.defaultValue : "",
|
|
169
181
|
onChange: (value) => onChange({ defaultValue: value || void 0 })
|
|
170
182
|
}
|
|
171
|
-
) :
|
|
183
|
+
) : selectedId && isMulti ? /* @__PURE__ */ jsx("div", { className: "inline-flex items-center gap-2 text-xs", children: /* @__PURE__ */ jsx("p", { id: defaultValueHintId, className: "text-xs text-muted-foreground", children: t(
|
|
184
|
+
"dictionaries.customFields.defaultValueMultiUnavailable",
|
|
185
|
+
"Default values are not available for multi-select dictionary fields."
|
|
186
|
+
) }) }) : null
|
|
172
187
|
] });
|
|
173
188
|
}
|
|
174
189
|
function DictionaryFieldInput({ value, setValue, disabled, def }) {
|