@open-mercato/core 0.4.2-canary-19353c5970 → 0.4.2-canary-19703ca707

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.
Files changed (30) hide show
  1. package/dist/modules/auth/api/login.js +25 -6
  2. package/dist/modules/auth/api/login.js.map +2 -2
  3. package/dist/modules/auth/data/validators.js +2 -1
  4. package/dist/modules/auth/data/validators.js.map +2 -2
  5. package/dist/modules/auth/frontend/login.js +85 -1
  6. package/dist/modules/auth/frontend/login.js.map +2 -2
  7. package/dist/modules/auth/lib/setup-app.js +25 -12
  8. package/dist/modules/auth/lib/setup-app.js.map +2 -2
  9. package/dist/modules/auth/services/authService.js +21 -0
  10. package/dist/modules/auth/services/authService.js.map +2 -2
  11. package/dist/modules/business_rules/cli.js +2 -1
  12. package/dist/modules/business_rules/cli.js.map +2 -2
  13. package/dist/modules/directory/api/get/tenants/lookup.js +68 -0
  14. package/dist/modules/directory/api/get/tenants/lookup.js.map +7 -0
  15. package/dist/modules/workflows/cli.js +12 -12
  16. package/dist/modules/workflows/cli.js.map +2 -2
  17. package/package.json +2 -2
  18. package/src/modules/auth/api/__tests__/login.test.ts +2 -0
  19. package/src/modules/auth/api/login.ts +26 -7
  20. package/src/modules/auth/data/validators.ts +1 -0
  21. package/src/modules/auth/frontend/login.tsx +106 -2
  22. package/src/modules/auth/i18n/de.json +5 -0
  23. package/src/modules/auth/i18n/en.json +5 -0
  24. package/src/modules/auth/i18n/es.json +5 -0
  25. package/src/modules/auth/i18n/pl.json +5 -0
  26. package/src/modules/auth/lib/setup-app.ts +37 -15
  27. package/src/modules/auth/services/authService.ts +23 -0
  28. package/src/modules/business_rules/cli.ts +2 -1
  29. package/src/modules/directory/api/get/tenants/lookup.ts +73 -0
  30. package/src/modules/workflows/cli.ts +12 -12
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/auth/lib/setup-app.ts"],
4
- "sourcesContent": ["import { hash } from 'bcryptjs'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { Role, RoleAcl, User, UserRole } from '@open-mercato/core/modules/auth/data/entities'\nimport { Tenant, Organization } from '@open-mercato/core/modules/directory/data/entities'\nimport { rebuildHierarchyForTenant } from '@open-mercato/core/modules/directory/lib/hierarchy'\nimport { normalizeTenantId } from './tenantAccess'\nimport { SalesSettings, SalesDocumentSequence } from '@open-mercato/core/modules/sales/data/entities'\nimport {\n DEFAULT_ORDER_NUMBER_FORMAT,\n DEFAULT_QUOTE_NUMBER_FORMAT,\n} from '@open-mercato/core/modules/sales/lib/documentNumberTokens'\nimport { computeEmailHash } from '@open-mercato/core/modules/auth/lib/emailHash'\nimport { isEncryptionDebugEnabled, isTenantDataEncryptionEnabled } from '@open-mercato/shared/lib/encryption/toggles'\nimport { EncryptionMap } from '@open-mercato/core/modules/entities/data/entities'\nimport { DEFAULT_ENCRYPTION_MAPS } from '@open-mercato/core/modules/entities/lib/encryptionDefaults'\nimport { createKmsService } from '@open-mercato/shared/lib/encryption/kms'\nimport { TenantDataEncryptionService } from '@open-mercato/shared/lib/encryption/tenantDataEncryptionService'\nimport { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\n\nconst DEFAULT_ROLE_NAMES = ['employee', 'admin', 'superadmin'] as const\nconst DEMO_SUPERADMIN_EMAIL = 'superadmin@acme.com'\n\nexport type EnsureRolesOptions = {\n roleNames?: string[]\n tenantId?: string | null\n}\n\nasync function ensureRolesInContext(\n em: EntityManager,\n roleNames: string[],\n tenantId: string | null,\n) {\n for (const name of roleNames) {\n const existing = await em.findOne(Role, { name, tenantId })\n if (existing) continue\n if (tenantId !== null) {\n const globalRole = await em.findOne(Role, { name, tenantId: null })\n if (globalRole) {\n globalRole.tenantId = tenantId\n em.persist(globalRole)\n continue\n }\n }\n em.persist(em.create(Role, { name, tenantId, createdAt: new Date() }))\n }\n}\n\nexport async function ensureRoles(em: EntityManager, options: EnsureRolesOptions = {}) {\n const roleNames = options.roleNames ?? [...DEFAULT_ROLE_NAMES]\n const tenantId = normalizeTenantId(options.tenantId ?? null) ?? null\n await em.transactional(async (tem) => {\n await ensureRolesInContext(tem, roleNames, tenantId)\n await tem.flush()\n })\n}\n\nasync function findRoleByName(\n em: EntityManager,\n name: string,\n tenantId: string | null,\n): Promise<Role | null> {\n const normalizedTenant = normalizeTenantId(tenantId ?? null) ?? null\n let role = await em.findOne(Role, { name, tenantId: normalizedTenant })\n if (!role && normalizedTenant !== null) {\n role = await em.findOne(Role, { name, tenantId: null })\n }\n return role\n}\n\nasync function findRoleByNameOrFail(\n em: EntityManager,\n name: string,\n tenantId: string | null,\n): Promise<Role> {\n const role = await findRoleByName(em, name, tenantId)\n if (!role) throw new Error(`ROLE_NOT_FOUND:${name}`)\n return role\n}\n\ntype PrimaryUserInput = {\n email: string\n password?: string\n hashedPassword?: string | null\n firstName?: string | null\n lastName?: string | null\n displayName?: string | null\n confirm?: boolean\n}\n\nconst DERIVED_EMAIL_ENV = {\n admin: 'OM_INIT_ADMIN_EMAIL',\n employee: 'OM_INIT_EMPLOYEE_EMAIL',\n} as const\n\nexport type SetupInitialTenantOptions = {\n orgName: string\n primaryUser: PrimaryUserInput\n roleNames?: string[]\n includeDerivedUsers?: boolean\n failIfUserExists?: boolean\n primaryUserRoles?: string[]\n includeSuperadminRole?: boolean\n}\n\nexport type SetupInitialTenantResult = {\n tenantId: string\n organizationId: string\n users: Array<{ user: User; roles: string[]; created: boolean }>\n reusedExistingUser: boolean\n}\n\nexport async function setupInitialTenant(\n em: EntityManager,\n options: SetupInitialTenantOptions,\n): Promise<SetupInitialTenantResult> {\n const {\n primaryUser,\n includeDerivedUsers = true,\n failIfUserExists = false,\n primaryUserRoles,\n includeSuperadminRole = true,\n } = options\n const primaryRolesInput = primaryUserRoles && primaryUserRoles.length ? primaryUserRoles : ['superadmin']\n const primaryRoles = includeSuperadminRole\n ? primaryRolesInput\n : primaryRolesInput.filter((role) => role !== 'superadmin')\n if (primaryRoles.length === 0) {\n throw new Error('PRIMARY_ROLES_REQUIRED')\n }\n const defaultRoleNames = options.roleNames ?? [...DEFAULT_ROLE_NAMES]\n const resolvedRoleNames = includeSuperadminRole\n ? defaultRoleNames\n : defaultRoleNames.filter((role) => role !== 'superadmin')\n const roleNames = Array.from(new Set([...resolvedRoleNames, ...primaryRoles]))\n\n const mainEmail = primaryUser.email\n const existingUser = await em.findOne(User, { email: mainEmail })\n if (existingUser && failIfUserExists) {\n throw new Error('USER_EXISTS')\n }\n\n let tenantId: string | undefined\n let organizationId: string | undefined\n let reusedExistingUser = false\n const userSnapshots: Array<{ user: User; roles: string[]; created: boolean }> = []\n\n await em.transactional(async (tem) => {\n if (!existingUser) return\n reusedExistingUser = true\n tenantId = existingUser.tenantId ? String(existingUser.tenantId) : undefined\n organizationId = existingUser.organizationId ? String(existingUser.organizationId) : undefined\n const roleTenantId = normalizeTenantId(existingUser.tenantId ?? null) ?? null\n\n await ensureRolesInContext(tem, roleNames, roleTenantId)\n await tem.flush()\n\n const requiredRoleSet = new Set([...roleNames, ...primaryRoles])\n const links = await findWithDecryption(\n tem,\n UserRole,\n { user: existingUser },\n { populate: ['role'] },\n { tenantId: roleTenantId, organizationId: null },\n )\n const currentRoles = new Set(links.map((link) => link.role.name))\n for (const roleName of requiredRoleSet) {\n if (!currentRoles.has(roleName)) {\n const role = await findRoleByNameOrFail(tem, roleName, roleTenantId)\n tem.persist(tem.create(UserRole, { user: existingUser, role, createdAt: new Date() }))\n }\n }\n await tem.flush()\n const roles = Array.from(new Set([...currentRoles, ...roleNames]))\n userSnapshots.push({ user: existingUser, roles, created: false })\n })\n\n if (!existingUser) {\n const baseUsers: Array<{ email: string; roles: string[]; name?: string | null }> = [\n { email: primaryUser.email, roles: primaryRoles, name: resolvePrimaryName(primaryUser) },\n ]\n if (includeDerivedUsers) {\n const [local, domain] = String(primaryUser.email).split('@')\n const isSuperadminLocal = (local || '').toLowerCase() === 'superadmin' && !!domain\n if (isSuperadminLocal) {\n const adminOverride = readEnvValue(DERIVED_EMAIL_ENV.admin)\n const employeeOverride = readEnvValue(DERIVED_EMAIL_ENV.employee)\n const adminEmail = adminOverride ?? `admin@${domain}`\n const employeeEmail = employeeOverride ?? `employee@${domain}`\n addUniqueBaseUser(baseUsers, { email: adminEmail, roles: ['admin'] })\n addUniqueBaseUser(baseUsers, { email: employeeEmail, roles: ['employee'] })\n }\n }\n const passwordHash = await resolvePasswordHash(primaryUser)\n\n await em.transactional(async (tem) => {\n const tenant = tem.create(Tenant, {\n name: `${options.orgName} Tenant`,\n isActive: true,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n tem.persist(tenant)\n await tem.flush()\n\n const organization = tem.create(Organization, {\n name: options.orgName,\n tenant,\n isActive: true,\n depth: 0,\n ancestorIds: [],\n childIds: [],\n descendantIds: [],\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n tem.persist(organization)\n await tem.flush()\n\n tenantId = String(tenant.id)\n organizationId = String(organization.id)\n const roleTenantId = tenantId\n\n if (isTenantDataEncryptionEnabled()) {\n try {\n const kms = createKmsService()\n if (kms.isHealthy()) {\n if (isEncryptionDebugEnabled()) {\n console.info('\uD83D\uDD11 [encryption][setup] provisioning tenant DEK', { tenantId: String(tenant.id) })\n }\n await kms.createTenantDek(String(tenant.id))\n if (isEncryptionDebugEnabled()) {\n console.info('\uD83D\uDD11 [encryption][setup] created tenant DEK during setup', { tenantId: String(tenant.id) })\n }\n } else {\n if (isEncryptionDebugEnabled()) {\n console.warn('\u26A0\uFE0F [encryption][setup] KMS not healthy, skipping tenant DEK creation', { tenantId: String(tenant.id) })\n }\n }\n } catch (err) {\n if (isEncryptionDebugEnabled()) {\n console.warn('\u26A0\uFE0F [encryption][setup] Failed to create tenant DEK', err)\n }\n }\n }\n\n await ensureRolesInContext(tem, roleNames, roleTenantId)\n await tem.flush()\n\n if (isTenantDataEncryptionEnabled()) {\n for (const spec of DEFAULT_ENCRYPTION_MAPS) {\n const existing = await tem.findOne(EncryptionMap, { entityId: spec.entityId, tenantId: tenant.id, organizationId: organization.id, deletedAt: null })\n if (!existing) {\n tem.persist(tem.create(EncryptionMap, {\n entityId: spec.entityId,\n tenantId: tenant.id,\n organizationId: organization.id,\n fieldsJson: spec.fields,\n isActive: true,\n createdAt: new Date(),\n updatedAt: new Date(),\n }))\n } else {\n existing.fieldsJson = spec.fields\n existing.isActive = true\n }\n }\n await tem.flush()\n }\n })\n\n await em.transactional(async (tem) => {\n if (!tenantId || !organizationId) return\n const roleTenantId = tenantId\n const encryptionService = isTenantDataEncryptionEnabled()\n ? new TenantDataEncryptionService(tem as any, { kms: createKmsService() })\n : null\n if (encryptionService) {\n await encryptionService.invalidateMap('auth:user', String(tenantId), String(organizationId))\n await encryptionService.invalidateMap('auth:user', String(tenantId), null)\n }\n\n for (const base of baseUsers) {\n let user = await tem.findOne(User, { email: base.email })\n const confirm = primaryUser.confirm ?? true\n const encryptedPayload = encryptionService\n ? await encryptionService.encryptEntityPayload('auth:user', { email: base.email }, tenantId, organizationId)\n : { email: base.email, emailHash: computeEmailHash(base.email) }\n if (user) {\n user.passwordHash = passwordHash\n user.organizationId = organizationId\n user.tenantId = tenantId\n if (isTenantDataEncryptionEnabled()) {\n user.email = encryptedPayload.email as any\n user.emailHash = (encryptedPayload as any).emailHash ?? computeEmailHash(base.email)\n }\n if (base.name) user.name = base.name\n if (confirm) user.isConfirmed = true\n tem.persist(user)\n userSnapshots.push({ user, roles: base.roles, created: false })\n } else {\n user = tem.create(User, {\n email: (encryptedPayload as any).email ?? base.email,\n emailHash: isTenantDataEncryptionEnabled() ? (encryptedPayload as any).emailHash ?? computeEmailHash(base.email) : undefined,\n passwordHash,\n organizationId,\n tenantId,\n name: base.name ?? undefined,\n isConfirmed: confirm,\n createdAt: new Date(),\n })\n tem.persist(user)\n userSnapshots.push({ user, roles: base.roles, created: true })\n }\n await tem.flush()\n for (const roleName of base.roles) {\n const role = await findRoleByNameOrFail(tem, roleName, roleTenantId)\n const existingLink = await tem.findOne(UserRole, { user, role })\n if (!existingLink) tem.persist(tem.create(UserRole, { user, role, createdAt: new Date() }))\n }\n await tem.flush()\n }\n })\n }\n\n if (!tenantId || !organizationId) {\n throw new Error('SETUP_FAILED')\n }\n\n if (!reusedExistingUser) {\n await rebuildHierarchyForTenant(em, tenantId)\n }\n\n await ensureDefaultRoleAcls(em, tenantId, { includeSuperadminRole })\n await deactivateDemoSuperAdminIfSelfOnboardingEnabled(em)\n await ensureSalesNumberingDefaults(em, { tenantId, organizationId })\n\n return {\n tenantId,\n organizationId,\n users: userSnapshots,\n reusedExistingUser,\n }\n}\n\nfunction resolvePrimaryName(input: PrimaryUserInput): string | null {\n if (input.displayName && input.displayName.trim()) return input.displayName.trim()\n const parts = [input.firstName, input.lastName].map((value) => value?.trim()).filter(Boolean)\n if (parts.length) return parts.join(' ')\n return null\n}\n\nfunction readEnvValue(key: string): string | undefined {\n const value = process.env[key]\n if (typeof value !== 'string') return undefined\n const trimmed = value.trim()\n return trimmed.length > 0 ? trimmed : undefined\n}\n\nfunction addUniqueBaseUser(\n baseUsers: Array<{ email: string; roles: string[]; name?: string | null }>,\n entry: { email: string; roles: string[]; name?: string | null },\n) {\n if (!entry.email) return\n const normalized = entry.email.toLowerCase()\n if (baseUsers.some((user) => user.email.toLowerCase() === normalized)) return\n baseUsers.push(entry)\n}\n\nasync function resolvePasswordHash(input: PrimaryUserInput): Promise<string | null> {\n if (typeof input.hashedPassword === 'string') return input.hashedPassword\n if (input.password) return hash(input.password, 10)\n return null\n}\n\nasync function ensureDefaultRoleAcls(\n em: EntityManager,\n tenantId: string,\n options: { includeSuperadminRole?: boolean } = {},\n) {\n const includeSuperadminRole = options.includeSuperadminRole ?? true\n const roleTenantId = normalizeTenantId(tenantId) ?? null\n const superadminRole = includeSuperadminRole ? await findRoleByName(em, 'superadmin', roleTenantId) : null\n const adminRole = await findRoleByName(em, 'admin', roleTenantId)\n const employeeRole = await findRoleByName(em, 'employee', roleTenantId)\n\n if (includeSuperadminRole && superadminRole) {\n await ensureRoleAclFor(em, superadminRole, tenantId, ['directory.tenants.*'], { isSuperAdmin: true })\n }\n if (adminRole) {\n const adminFeatures = [\n 'auth.*',\n 'entities.*',\n 'attachments.*',\n 'attachments.view',\n 'attachments.manage',\n 'query_index.*',\n 'search.*',\n 'vector.*',\n 'feature_toggles.*',\n 'configs.system_status.view',\n 'configs.cache.view',\n 'configs.cache.manage',\n 'configs.manage',\n 'catalog.*',\n 'catalog.variants.manage',\n 'catalog.pricing.manage',\n 'sales.*',\n 'audit_logs.*',\n 'directory.organizations.view',\n 'directory.organizations.manage',\n 'customers.*',\n 'customers.people.view',\n 'customers.people.manage',\n 'customers.companies.view',\n 'customers.companies.manage',\n 'customers.deals.view',\n 'customers.deals.manage',\n 'dictionaries.view',\n 'dictionaries.manage',\n 'example.*',\n 'dashboards.*',\n 'dashboards.admin.assign-widgets',\n 'analytics.view',\n 'api_keys.*',\n 'notifications.manage',\n 'perspectives.use',\n 'perspectives.role_defaults',\n 'business_rules.*',\n 'workflows.*',\n 'currencies.*',\n 'staff.*',\n 'staff.leave_requests.manage',\n 'resources.*',\n 'planner.*',\n ]\n await ensureRoleAclFor(em, adminRole, tenantId, adminFeatures, { remove: ['directory.organizations.*', 'directory.tenants.*'] })\n }\n if (employeeRole) {\n await ensureRoleAclFor(em, employeeRole, tenantId, [\n 'customers.*',\n 'customers.people.view',\n 'customers.people.manage',\n 'customers.companies.view',\n 'customers.companies.manage',\n 'vector.*',\n 'catalog.*',\n 'catalog.variants.manage',\n 'catalog.pricing.manage',\n 'sales.*',\n 'dictionaries.view',\n 'example.*',\n 'example.widgets.*',\n 'dashboards.view',\n 'dashboards.configure',\n 'analytics.view',\n 'audit_logs.undo_self',\n 'perspectives.use',\n 'staff.leave_requests.send',\n 'staff.my_availability.view',\n 'staff.my_availability.manage',\n 'staff.my_leave_requests.view',\n 'staff.my_leave_requests.send',\n 'planner.view',\n ])\n }\n}\n\nasync function ensureRoleAclFor(\n em: EntityManager,\n role: Role,\n tenantId: string,\n features: string[],\n options: { isSuperAdmin?: boolean; remove?: string[] } = {},\n) {\n const existing = await em.findOne(RoleAcl, { role, tenantId })\n if (!existing) {\n const acl = em.create(RoleAcl, {\n role,\n tenantId,\n featuresJson: features,\n isSuperAdmin: !!options.isSuperAdmin,\n createdAt: new Date(),\n })\n await em.persistAndFlush(acl)\n return\n }\n const currentFeatures = Array.isArray(existing.featuresJson) ? existing.featuresJson : []\n const merged = Array.from(new Set([...currentFeatures, ...features]))\n const removeSet = new Set(options.remove ?? [])\n const sanitized =\n removeSet.size\n ? merged.filter((value) => {\n if (removeSet.has(value)) return false\n for (const entry of removeSet) {\n if (entry.endsWith('.*')) {\n const prefix = entry.slice(0, -1) // keep trailing dot\n if (value === entry || value.startsWith(prefix)) return false\n }\n }\n return true\n })\n : merged\n const changed =\n sanitized.length !== currentFeatures.length ||\n sanitized.some((value, index) => value !== currentFeatures[index])\n if (changed) existing.featuresJson = sanitized\n if (options.isSuperAdmin && !existing.isSuperAdmin) {\n existing.isSuperAdmin = true\n }\n if (changed || options.isSuperAdmin) {\n await em.persistAndFlush(existing)\n }\n}\n\nasync function deactivateDemoSuperAdminIfSelfOnboardingEnabled(em: EntityManager) {\n if (process.env.SELF_SERVICE_ONBOARDING_ENABLED !== 'true') return\n try {\n const user = await em.findOne(User, { email: DEMO_SUPERADMIN_EMAIL })\n if (!user) return\n let dirty = false\n if (user.passwordHash) {\n user.passwordHash = null\n dirty = true\n }\n if (user.isConfirmed !== false) {\n user.isConfirmed = false\n dirty = true\n }\n if (dirty) {\n await em.persistAndFlush(user)\n }\n } catch (error) {\n console.error('[auth.setup] failed to deactivate demo superadmin user', error)\n }\n}\n\nasync function ensureSalesNumberingDefaults(\n em: EntityManager,\n scope: { tenantId: string; organizationId: string },\n) {\n const repo = (em as any).getRepository?.(SalesSettings)\n const findSettings = async () =>\n repo?.findOne({\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n }) ??\n (em as any).findOne?.(SalesSettings, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n })\n\n const exists = await findSettings()\n if (!exists) {\n const settings =\n repo?.create?.({\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n orderNumberFormat: DEFAULT_ORDER_NUMBER_FORMAT,\n quoteNumberFormat: DEFAULT_QUOTE_NUMBER_FORMAT,\n createdAt: new Date(),\n updatedAt: new Date(),\n }) ??\n (em as any).create?.(SalesSettings, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n orderNumberFormat: DEFAULT_ORDER_NUMBER_FORMAT,\n quoteNumberFormat: DEFAULT_QUOTE_NUMBER_FORMAT,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n if (settings && (em as any).persist) {\n em.persist(settings)\n }\n }\n\n const sequenceRepo = (em as any).getRepository?.(SalesDocumentSequence)\n const kinds: Array<'order' | 'quote'> = ['order', 'quote']\n for (const kind of kinds) {\n const seq =\n sequenceRepo?.findOne({\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n documentKind: kind,\n }) ??\n (em as any).findOne?.(SalesDocumentSequence, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n documentKind: kind,\n })\n if (!seq) {\n const entry =\n sequenceRepo?.create?.({\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n documentKind: kind,\n currentValue: 0,\n createdAt: new Date(),\n updatedAt: new Date(),\n }) ??\n (em as any).create?.(SalesDocumentSequence, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n documentKind: kind,\n currentValue: 0,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n if (entry && (em as any).persist) {\n em.persist(entry)\n }\n }\n }\n\n if ((em as any).flush) {\n await em.flush()\n }\n}\n"],
5
- "mappings": "AAAA,SAAS,YAAY;AAErB,SAAS,MAAM,SAAS,MAAM,gBAAgB;AAC9C,SAAS,QAAQ,oBAAoB;AACrC,SAAS,iCAAiC;AAC1C,SAAS,yBAAyB;AAClC,SAAS,eAAe,6BAA6B;AACrD;AAAA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AA,EACjB;AACF;",
4
+ "sourcesContent": ["import { hash } from 'bcryptjs'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { Role, RoleAcl, User, UserRole } from '@open-mercato/core/modules/auth/data/entities'\nimport { Tenant, Organization } from '@open-mercato/core/modules/directory/data/entities'\nimport { rebuildHierarchyForTenant } from '@open-mercato/core/modules/directory/lib/hierarchy'\nimport { normalizeTenantId } from './tenantAccess'\nimport { SalesSettings, SalesDocumentSequence } from '@open-mercato/core/modules/sales/data/entities'\nimport {\n DEFAULT_ORDER_NUMBER_FORMAT,\n DEFAULT_QUOTE_NUMBER_FORMAT,\n} from '@open-mercato/core/modules/sales/lib/documentNumberTokens'\nimport { computeEmailHash } from '@open-mercato/core/modules/auth/lib/emailHash'\nimport { isEncryptionDebugEnabled, isTenantDataEncryptionEnabled } from '@open-mercato/shared/lib/encryption/toggles'\nimport { EncryptionMap } from '@open-mercato/core/modules/entities/data/entities'\nimport { DEFAULT_ENCRYPTION_MAPS } from '@open-mercato/core/modules/entities/lib/encryptionDefaults'\nimport { createKmsService } from '@open-mercato/shared/lib/encryption/kms'\nimport { TenantDataEncryptionService } from '@open-mercato/shared/lib/encryption/tenantDataEncryptionService'\nimport { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { parseBooleanToken } from '@open-mercato/shared/lib/boolean'\n\nconst DEFAULT_ROLE_NAMES = ['employee', 'admin', 'superadmin'] as const\nconst DEMO_SUPERADMIN_EMAIL = 'superadmin@acme.com'\n\nexport type EnsureRolesOptions = {\n roleNames?: string[]\n tenantId?: string | null\n}\n\nasync function ensureRolesInContext(\n em: EntityManager,\n roleNames: string[],\n tenantId: string | null,\n) {\n for (const name of roleNames) {\n const existing = await em.findOne(Role, { name, tenantId })\n if (existing) continue\n if (tenantId !== null) {\n const globalRole = await em.findOne(Role, { name, tenantId: null })\n if (globalRole) {\n globalRole.tenantId = tenantId\n em.persist(globalRole)\n continue\n }\n }\n em.persist(em.create(Role, { name, tenantId, createdAt: new Date() }))\n }\n}\n\nexport async function ensureRoles(em: EntityManager, options: EnsureRolesOptions = {}) {\n const roleNames = options.roleNames ?? [...DEFAULT_ROLE_NAMES]\n const tenantId = normalizeTenantId(options.tenantId ?? null) ?? null\n await em.transactional(async (tem) => {\n await ensureRolesInContext(tem, roleNames, tenantId)\n await tem.flush()\n })\n}\n\nasync function findRoleByName(\n em: EntityManager,\n name: string,\n tenantId: string | null,\n): Promise<Role | null> {\n const normalizedTenant = normalizeTenantId(tenantId ?? null) ?? null\n let role = await em.findOne(Role, { name, tenantId: normalizedTenant })\n if (!role && normalizedTenant !== null) {\n role = await em.findOne(Role, { name, tenantId: null })\n }\n return role\n}\n\nasync function findRoleByNameOrFail(\n em: EntityManager,\n name: string,\n tenantId: string | null,\n): Promise<Role> {\n const role = await findRoleByName(em, name, tenantId)\n if (!role) throw new Error(`ROLE_NOT_FOUND:${name}`)\n return role\n}\n\ntype PrimaryUserInput = {\n email: string\n password?: string\n hashedPassword?: string | null\n firstName?: string | null\n lastName?: string | null\n displayName?: string | null\n confirm?: boolean\n}\n\nconst DERIVED_EMAIL_ENV = {\n admin: 'OM_INIT_ADMIN_EMAIL',\n employee: 'OM_INIT_EMPLOYEE_EMAIL',\n} as const\n\nexport type SetupInitialTenantOptions = {\n orgName: string\n primaryUser: PrimaryUserInput\n roleNames?: string[]\n includeDerivedUsers?: boolean\n failIfUserExists?: boolean\n primaryUserRoles?: string[]\n includeSuperadminRole?: boolean\n}\n\nexport type SetupInitialTenantResult = {\n tenantId: string\n organizationId: string\n users: Array<{ user: User; roles: string[]; created: boolean }>\n reusedExistingUser: boolean\n}\n\nexport async function setupInitialTenant(\n em: EntityManager,\n options: SetupInitialTenantOptions,\n): Promise<SetupInitialTenantResult> {\n const {\n primaryUser,\n includeDerivedUsers = true,\n failIfUserExists = false,\n primaryUserRoles,\n includeSuperadminRole = true,\n } = options\n const primaryRolesInput = primaryUserRoles && primaryUserRoles.length ? primaryUserRoles : ['superadmin']\n const primaryRoles = includeSuperadminRole\n ? primaryRolesInput\n : primaryRolesInput.filter((role) => role !== 'superadmin')\n if (primaryRoles.length === 0) {\n throw new Error('PRIMARY_ROLES_REQUIRED')\n }\n const defaultRoleNames = options.roleNames ?? [...DEFAULT_ROLE_NAMES]\n const resolvedRoleNames = includeSuperadminRole\n ? defaultRoleNames\n : defaultRoleNames.filter((role) => role !== 'superadmin')\n const roleNames = Array.from(new Set([...resolvedRoleNames, ...primaryRoles]))\n\n const mainEmail = primaryUser.email\n const existingUser = await em.findOne(User, { email: mainEmail })\n if (existingUser && failIfUserExists) {\n throw new Error('USER_EXISTS')\n }\n\n let tenantId: string | undefined\n let organizationId: string | undefined\n let reusedExistingUser = false\n const userSnapshots: Array<{ user: User; roles: string[]; created: boolean }> = []\n\n await em.transactional(async (tem) => {\n if (!existingUser) return\n reusedExistingUser = true\n tenantId = existingUser.tenantId ? String(existingUser.tenantId) : undefined\n organizationId = existingUser.organizationId ? String(existingUser.organizationId) : undefined\n const roleTenantId = normalizeTenantId(existingUser.tenantId ?? null) ?? null\n\n await ensureRolesInContext(tem, roleNames, roleTenantId)\n await tem.flush()\n\n const requiredRoleSet = new Set([...roleNames, ...primaryRoles])\n const links = await findWithDecryption(\n tem,\n UserRole,\n { user: existingUser },\n { populate: ['role'] },\n { tenantId: roleTenantId, organizationId: null },\n )\n const currentRoles = new Set(links.map((link) => link.role.name))\n for (const roleName of requiredRoleSet) {\n if (!currentRoles.has(roleName)) {\n const role = await findRoleByNameOrFail(tem, roleName, roleTenantId)\n tem.persist(tem.create(UserRole, { user: existingUser, role, createdAt: new Date() }))\n }\n }\n await tem.flush()\n const roles = Array.from(new Set([...currentRoles, ...roleNames]))\n userSnapshots.push({ user: existingUser, roles, created: false })\n })\n\n if (!existingUser) {\n const baseUsers: Array<{\n email: string\n roles: string[]\n name?: string | null\n passwordHash?: string | null\n }> = [\n { email: primaryUser.email, roles: primaryRoles, name: resolvePrimaryName(primaryUser) },\n ]\n if (includeDerivedUsers) {\n const [, domain] = String(primaryUser.email).split('@')\n const adminOverride = readEnvValue(DERIVED_EMAIL_ENV.admin)\n const employeeOverride = readEnvValue(DERIVED_EMAIL_ENV.employee)\n const adminEmail = adminOverride ?? (domain ? `admin@${domain}` : '')\n const employeeEmail = employeeOverride ?? (domain ? `employee@${domain}` : '')\n const adminPassword = readEnvValue('OM_INIT_ADMIN_PASSWORD')\n const employeePassword = readEnvValue('OM_INIT_EMPLOYEE_PASSWORD')\n const adminPasswordHash = adminPassword ? await resolvePasswordHash({ email: adminEmail, password: adminPassword }) : null\n const employeePasswordHash = employeePassword\n ? await resolvePasswordHash({ email: employeeEmail, password: employeePassword })\n : null\n addUniqueBaseUser(baseUsers, { email: adminEmail, roles: ['admin'], passwordHash: adminPasswordHash })\n addUniqueBaseUser(baseUsers, { email: employeeEmail, roles: ['employee'], passwordHash: employeePasswordHash })\n }\n const passwordHash = await resolvePasswordHash(primaryUser)\n\n await em.transactional(async (tem) => {\n const tenant = tem.create(Tenant, {\n name: `${options.orgName} Tenant`,\n isActive: true,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n tem.persist(tenant)\n await tem.flush()\n\n const organization = tem.create(Organization, {\n name: options.orgName,\n tenant,\n isActive: true,\n depth: 0,\n ancestorIds: [],\n childIds: [],\n descendantIds: [],\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n tem.persist(organization)\n await tem.flush()\n\n tenantId = String(tenant.id)\n organizationId = String(organization.id)\n const roleTenantId = tenantId\n\n if (isTenantDataEncryptionEnabled()) {\n try {\n const kms = createKmsService()\n if (kms.isHealthy()) {\n if (isEncryptionDebugEnabled()) {\n console.info('\uD83D\uDD11 [encryption][setup] provisioning tenant DEK', { tenantId: String(tenant.id) })\n }\n await kms.createTenantDek(String(tenant.id))\n if (isEncryptionDebugEnabled()) {\n console.info('\uD83D\uDD11 [encryption][setup] created tenant DEK during setup', { tenantId: String(tenant.id) })\n }\n } else {\n if (isEncryptionDebugEnabled()) {\n console.warn('\u26A0\uFE0F [encryption][setup] KMS not healthy, skipping tenant DEK creation', { tenantId: String(tenant.id) })\n }\n }\n } catch (err) {\n if (isEncryptionDebugEnabled()) {\n console.warn('\u26A0\uFE0F [encryption][setup] Failed to create tenant DEK', err)\n }\n }\n }\n\n await ensureRolesInContext(tem, roleNames, roleTenantId)\n await tem.flush()\n\n if (isTenantDataEncryptionEnabled()) {\n for (const spec of DEFAULT_ENCRYPTION_MAPS) {\n const existing = await tem.findOne(EncryptionMap, { entityId: spec.entityId, tenantId: tenant.id, organizationId: organization.id, deletedAt: null })\n if (!existing) {\n tem.persist(tem.create(EncryptionMap, {\n entityId: spec.entityId,\n tenantId: tenant.id,\n organizationId: organization.id,\n fieldsJson: spec.fields,\n isActive: true,\n createdAt: new Date(),\n updatedAt: new Date(),\n }))\n } else {\n existing.fieldsJson = spec.fields\n existing.isActive = true\n }\n }\n await tem.flush()\n }\n })\n\n await em.transactional(async (tem) => {\n if (!tenantId || !organizationId) return\n const roleTenantId = tenantId\n const encryptionService = isTenantDataEncryptionEnabled()\n ? new TenantDataEncryptionService(tem as any, { kms: createKmsService() })\n : null\n if (encryptionService) {\n await encryptionService.invalidateMap('auth:user', String(tenantId), String(organizationId))\n await encryptionService.invalidateMap('auth:user', String(tenantId), null)\n }\n\n for (const base of baseUsers) {\n const resolvedPasswordHash = base.passwordHash ?? passwordHash\n let user = await tem.findOne(User, { email: base.email })\n const confirm = primaryUser.confirm ?? true\n const encryptedPayload = encryptionService\n ? await encryptionService.encryptEntityPayload('auth:user', { email: base.email }, tenantId, organizationId)\n : { email: base.email, emailHash: computeEmailHash(base.email) }\n if (user) {\n user.passwordHash = resolvedPasswordHash\n user.organizationId = organizationId\n user.tenantId = tenantId\n if (isTenantDataEncryptionEnabled()) {\n user.email = encryptedPayload.email as any\n user.emailHash = (encryptedPayload as any).emailHash ?? computeEmailHash(base.email)\n }\n if (base.name) user.name = base.name\n if (confirm) user.isConfirmed = true\n tem.persist(user)\n userSnapshots.push({ user, roles: base.roles, created: false })\n } else {\n user = tem.create(User, {\n email: (encryptedPayload as any).email ?? base.email,\n emailHash: isTenantDataEncryptionEnabled() ? (encryptedPayload as any).emailHash ?? computeEmailHash(base.email) : undefined,\n passwordHash: resolvedPasswordHash,\n organizationId,\n tenantId,\n name: base.name ?? undefined,\n isConfirmed: confirm,\n createdAt: new Date(),\n })\n tem.persist(user)\n userSnapshots.push({ user, roles: base.roles, created: true })\n }\n await tem.flush()\n for (const roleName of base.roles) {\n const role = await findRoleByNameOrFail(tem, roleName, roleTenantId)\n const existingLink = await tem.findOne(UserRole, { user, role })\n if (!existingLink) tem.persist(tem.create(UserRole, { user, role, createdAt: new Date() }))\n }\n await tem.flush()\n }\n })\n }\n\n if (!tenantId || !organizationId) {\n throw new Error('SETUP_FAILED')\n }\n\n if (!reusedExistingUser) {\n await rebuildHierarchyForTenant(em, tenantId)\n }\n\n await ensureDefaultRoleAcls(em, tenantId, { includeSuperadminRole })\n await deactivateDemoSuperAdminIfSelfOnboardingEnabled(em)\n await ensureSalesNumberingDefaults(em, { tenantId, organizationId })\n\n return {\n tenantId,\n organizationId,\n users: userSnapshots,\n reusedExistingUser,\n }\n}\n\nfunction resolvePrimaryName(input: PrimaryUserInput): string | null {\n if (input.displayName && input.displayName.trim()) return input.displayName.trim()\n const parts = [input.firstName, input.lastName].map((value) => value?.trim()).filter(Boolean)\n if (parts.length) return parts.join(' ')\n return null\n}\n\nfunction readEnvValue(key: string): string | undefined {\n const value = process.env[key]\n if (typeof value !== 'string') return undefined\n const trimmed = value.trim()\n return trimmed.length > 0 ? trimmed : undefined\n}\n\nfunction addUniqueBaseUser(\n baseUsers: Array<{ email: string; roles: string[]; name?: string | null; passwordHash?: string | null }>,\n entry: { email: string; roles: string[]; name?: string | null; passwordHash?: string | null },\n) {\n if (!entry.email) return\n const normalized = entry.email.toLowerCase()\n if (baseUsers.some((user) => user.email.toLowerCase() === normalized)) return\n baseUsers.push(entry)\n}\n\nfunction isDemoModeEnabled(): boolean {\n const parsed = parseBooleanToken(process.env.DEMO_MODE ?? '')\n return parsed === false ? false : true\n}\n\nfunction shouldKeepDemoSuperadminDuringInit(): boolean {\n if (process.env.OM_INIT_FLOW !== 'true') return false\n if (!readEnvValue('OM_INIT_SUPERADMIN_EMAIL')) return false\n return isDemoModeEnabled()\n}\n\nasync function resolvePasswordHash(input: PrimaryUserInput): Promise<string | null> {\n if (typeof input.hashedPassword === 'string') return input.hashedPassword\n if (input.password) return hash(input.password, 10)\n return null\n}\n\nasync function ensureDefaultRoleAcls(\n em: EntityManager,\n tenantId: string,\n options: { includeSuperadminRole?: boolean } = {},\n) {\n const includeSuperadminRole = options.includeSuperadminRole ?? true\n const roleTenantId = normalizeTenantId(tenantId) ?? null\n const superadminRole = includeSuperadminRole ? await findRoleByName(em, 'superadmin', roleTenantId) : null\n const adminRole = await findRoleByName(em, 'admin', roleTenantId)\n const employeeRole = await findRoleByName(em, 'employee', roleTenantId)\n\n if (includeSuperadminRole && superadminRole) {\n await ensureRoleAclFor(em, superadminRole, tenantId, ['directory.tenants.*'], { isSuperAdmin: true })\n }\n if (adminRole) {\n const adminFeatures = [\n 'auth.*',\n 'entities.*',\n 'attachments.*',\n 'attachments.view',\n 'attachments.manage',\n 'query_index.*',\n 'search.*',\n 'vector.*',\n 'feature_toggles.*',\n 'configs.system_status.view',\n 'configs.cache.view',\n 'configs.cache.manage',\n 'configs.manage',\n 'catalog.*',\n 'catalog.variants.manage',\n 'catalog.pricing.manage',\n 'sales.*',\n 'audit_logs.*',\n 'directory.organizations.view',\n 'directory.organizations.manage',\n 'customers.*',\n 'customers.people.view',\n 'customers.people.manage',\n 'customers.companies.view',\n 'customers.companies.manage',\n 'customers.deals.view',\n 'customers.deals.manage',\n 'dictionaries.view',\n 'dictionaries.manage',\n 'example.*',\n 'dashboards.*',\n 'dashboards.admin.assign-widgets',\n 'analytics.view',\n 'api_keys.*',\n 'notifications.manage',\n 'perspectives.use',\n 'perspectives.role_defaults',\n 'business_rules.*',\n 'workflows.*',\n 'currencies.*',\n 'staff.*',\n 'staff.leave_requests.manage',\n 'resources.*',\n 'planner.*',\n ]\n await ensureRoleAclFor(em, adminRole, tenantId, adminFeatures, { remove: ['directory.organizations.*', 'directory.tenants.*'] })\n }\n if (employeeRole) {\n await ensureRoleAclFor(em, employeeRole, tenantId, [\n 'customers.*',\n 'customers.people.view',\n 'customers.people.manage',\n 'customers.companies.view',\n 'customers.companies.manage',\n 'vector.*',\n 'catalog.*',\n 'catalog.variants.manage',\n 'catalog.pricing.manage',\n 'sales.*',\n 'dictionaries.view',\n 'example.*',\n 'example.widgets.*',\n 'dashboards.view',\n 'dashboards.configure',\n 'analytics.view',\n 'audit_logs.undo_self',\n 'perspectives.use',\n 'staff.leave_requests.send',\n 'staff.my_availability.view',\n 'staff.my_availability.manage',\n 'staff.my_leave_requests.view',\n 'staff.my_leave_requests.send',\n 'planner.view',\n ])\n }\n}\n\nasync function ensureRoleAclFor(\n em: EntityManager,\n role: Role,\n tenantId: string,\n features: string[],\n options: { isSuperAdmin?: boolean; remove?: string[] } = {},\n) {\n const existing = await em.findOne(RoleAcl, { role, tenantId })\n if (!existing) {\n const acl = em.create(RoleAcl, {\n role,\n tenantId,\n featuresJson: features,\n isSuperAdmin: !!options.isSuperAdmin,\n createdAt: new Date(),\n })\n await em.persistAndFlush(acl)\n return\n }\n const currentFeatures = Array.isArray(existing.featuresJson) ? existing.featuresJson : []\n const merged = Array.from(new Set([...currentFeatures, ...features]))\n const removeSet = new Set(options.remove ?? [])\n const sanitized =\n removeSet.size\n ? merged.filter((value) => {\n if (removeSet.has(value)) return false\n for (const entry of removeSet) {\n if (entry.endsWith('.*')) {\n const prefix = entry.slice(0, -1) // keep trailing dot\n if (value === entry || value.startsWith(prefix)) return false\n }\n }\n return true\n })\n : merged\n const changed =\n sanitized.length !== currentFeatures.length ||\n sanitized.some((value, index) => value !== currentFeatures[index])\n if (changed) existing.featuresJson = sanitized\n if (options.isSuperAdmin && !existing.isSuperAdmin) {\n existing.isSuperAdmin = true\n }\n if (changed || options.isSuperAdmin) {\n await em.persistAndFlush(existing)\n }\n}\n\nasync function deactivateDemoSuperAdminIfSelfOnboardingEnabled(em: EntityManager) {\n if (process.env.SELF_SERVICE_ONBOARDING_ENABLED !== 'true') return\n if (shouldKeepDemoSuperadminDuringInit()) return\n try {\n const user = await em.findOne(User, { email: DEMO_SUPERADMIN_EMAIL })\n if (!user) return\n let dirty = false\n if (user.passwordHash) {\n user.passwordHash = null\n dirty = true\n }\n if (user.isConfirmed !== false) {\n user.isConfirmed = false\n dirty = true\n }\n if (dirty) {\n await em.persistAndFlush(user)\n }\n } catch (error) {\n console.error('[auth.setup] failed to deactivate demo superadmin user', error)\n }\n}\n\nasync function ensureSalesNumberingDefaults(\n em: EntityManager,\n scope: { tenantId: string; organizationId: string },\n) {\n const repo = (em as any).getRepository?.(SalesSettings)\n const findSettings = async () =>\n repo?.findOne({\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n }) ??\n (em as any).findOne?.(SalesSettings, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n })\n\n const exists = await findSettings()\n if (!exists) {\n const settings =\n repo?.create?.({\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n orderNumberFormat: DEFAULT_ORDER_NUMBER_FORMAT,\n quoteNumberFormat: DEFAULT_QUOTE_NUMBER_FORMAT,\n createdAt: new Date(),\n updatedAt: new Date(),\n }) ??\n (em as any).create?.(SalesSettings, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n orderNumberFormat: DEFAULT_ORDER_NUMBER_FORMAT,\n quoteNumberFormat: DEFAULT_QUOTE_NUMBER_FORMAT,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n if (settings && (em as any).persist) {\n em.persist(settings)\n }\n }\n\n const sequenceRepo = (em as any).getRepository?.(SalesDocumentSequence)\n const kinds: Array<'order' | 'quote'> = ['order', 'quote']\n for (const kind of kinds) {\n const seq =\n sequenceRepo?.findOne({\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n documentKind: kind,\n }) ??\n (em as any).findOne?.(SalesDocumentSequence, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n documentKind: kind,\n })\n if (!seq) {\n const entry =\n sequenceRepo?.create?.({\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n documentKind: kind,\n currentValue: 0,\n createdAt: new Date(),\n updatedAt: new Date(),\n }) ??\n (em as any).create?.(SalesDocumentSequence, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n documentKind: kind,\n currentValue: 0,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n if (entry && (em as any).persist) {\n em.persist(entry)\n }\n }\n }\n\n if ((em as any).flush) {\n await em.flush()\n }\n}\n"],
5
+ "mappings": "AAAA,SAAS,YAAY;AAErB,SAAS,MAAM,SAAS,MAAM,gBAAgB;AAC9C,SAAS,QAAQ,oBAAoB;AACrC,SAAS,iCAAiC;AAC1C,SAAS,yBAAyB;AAClC,SAAS,eAAe,6BAA6B;AACrD;AAAA,EACE;AAAA,EACA;AAAA,OACK;AACP,SAAS,wBAAwB;AACjC,SAAS,0BAA0B,qCAAqC;AACxE,SAAS,qBAAqB;AAC9B,SAAS,+BAA+B;AACxC,SAAS,wBAAwB;AACjC,SAAS,mCAAmC;AAC5C,SAAS,0BAA0B;AACnC,SAAS,yBAAyB;AAElC,MAAM,qBAAqB,CAAC,YAAY,SAAS,YAAY;AAC7D,MAAM,wBAAwB;AAO9B,eAAe,qBACb,IACA,WACA,UACA;AACA,aAAW,QAAQ,WAAW;AAC5B,UAAM,WAAW,MAAM,GAAG,QAAQ,MAAM,EAAE,MAAM,SAAS,CAAC;AAC1D,QAAI,SAAU;AACd,QAAI,aAAa,MAAM;AACrB,YAAM,aAAa,MAAM,GAAG,QAAQ,MAAM,EAAE,MAAM,UAAU,KAAK,CAAC;AAClE,UAAI,YAAY;AACd,mBAAW,WAAW;AACtB,WAAG,QAAQ,UAAU;AACrB;AAAA,MACF;AAAA,IACF;AACA,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
  ]
17
17
  });
18
18
  }
19
+ async findUsersByEmail(email) {
20
+ const emailHash = computeEmailHash(email);
21
+ return this.em.find(User, {
22
+ deletedAt: null,
23
+ $or: [
24
+ { email },
25
+ { emailHash }
26
+ ]
27
+ });
28
+ }
29
+ async findUserByEmailAndTenant(email, tenantId) {
30
+ const emailHash = computeEmailHash(email);
31
+ return this.em.findOne(User, {
32
+ tenantId,
33
+ deletedAt: null,
34
+ $or: [
35
+ { email },
36
+ { emailHash }
37
+ ]
38
+ });
39
+ }
19
40
  async verifyPassword(user, password) {
20
41
  if (!user.passwordHash) return false;
21
42
  return compare(password, user.passwordHash);
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/auth/services/authService.ts"],
4
- "sourcesContent": ["import { EntityManager } from '@mikro-orm/postgresql'\nimport { compare, hash } from 'bcryptjs'\nimport { User, Role, UserRole, Session, PasswordReset } from '@open-mercato/core/modules/auth/data/entities'\nimport crypto from 'node:crypto'\nimport { computeEmailHash } from '@open-mercato/core/modules/auth/lib/emailHash'\nimport { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\n\nexport class AuthService {\n constructor(private em: EntityManager) {}\n\n async findUserByEmail(email: string) {\n const emailHash = computeEmailHash(email)\n return this.em.findOne(User, {\n $or: [\n { email },\n { emailHash },\n ],\n } as any)\n }\n\n async verifyPassword(user: User, password: string) {\n if (!user.passwordHash) return false\n return compare(password, user.passwordHash)\n }\n\n async updateLastLoginAt(user: User) {\n const now = new Date()\n // Use native update to avoid flushing unrelated entities that might be pending in this EM\n await this.em.nativeUpdate(User, { id: user.id }, { lastLoginAt: now })\n user.lastLoginAt = now\n }\n\n async getUserRoles(user: User, tenantId?: string | null): Promise<string[]> {\n const resolvedTenantId = tenantId ?? user.tenantId ?? null\n if (!resolvedTenantId) return []\n const links = await findWithDecryption(\n this.em,\n UserRole,\n { user, role: { tenantId: resolvedTenantId } as any },\n { populate: ['role'] },\n { tenantId: resolvedTenantId, organizationId: user.organizationId ?? null },\n )\n return links.map((l) => l.role.name)\n }\n\n\n async createSession(user: User, expiresAt: Date): Promise<Session> {\n const token = crypto.randomBytes(32).toString('hex')\n const sess = this.em.create(Session as any, { user, token, expiresAt, createdAt: new Date() } as any)\n await this.em.persistAndFlush(sess)\n return sess as Session\n }\n\n async deleteSessionByToken(token: string) {\n await this.em.nativeDelete(Session, { token })\n }\n\n async refreshFromSessionToken(token: string) {\n const now = new Date()\n const sess = await this.em.findOne(Session, { token })\n if (!sess || sess.expiresAt <= now) return null\n const user = await this.em.findOne(User, { id: sess.user.id })\n if (!user) return null\n const roles = await this.getUserRoles(user, user.tenantId ?? null)\n return { user, roles }\n }\n\n async requestPasswordReset(email: string) {\n const user = await this.findUserByEmail(email)\n if (!user) return null\n const token = crypto.randomBytes(32).toString('hex')\n const expiresAt = new Date(Date.now() + 60 * 60 * 1000)\n const row = this.em.create(PasswordReset as any, { user, token, expiresAt, createdAt: new Date() } as any)\n await this.em.persistAndFlush(row)\n return { user, token }\n }\n\n async confirmPasswordReset(token: string, newPassword: string): Promise<User | null> {\n const now = new Date()\n const row = await this.em.findOne(PasswordReset, { token })\n if (!row || (row.usedAt && row.usedAt <= now) || row.expiresAt <= now) return null\n const user = await this.em.findOne(User, { id: row.user.id })\n if (!user) return null\n user.passwordHash = await hash(newPassword, 10)\n row.usedAt = new Date()\n await this.em.flush()\n return user\n }\n}\n"],
5
- "mappings": "AACA,SAAS,SAAS,YAAY;AAC9B,SAAS,MAAY,UAAU,SAAS,qBAAqB;AAC7D,OAAO,YAAY;AACnB,SAAS,wBAAwB;AACjC,SAAS,0BAA0B;AAE5B,MAAM,YAAY;AAAA,EACvB,YAAoB,IAAmB;AAAnB;AAAA,EAAoB;AAAA,EAExC,MAAM,gBAAgB,OAAe;AACnC,UAAM,YAAY,iBAAiB,KAAK;AACxC,WAAO,KAAK,GAAG,QAAQ,MAAM;AAAA,MAC3B,KAAK;AAAA,QACH,EAAE,MAAM;AAAA,QACR,E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} from 'bcryptjs'\nimport { User, Role, UserRole, Session, PasswordReset } from '@open-mercato/core/modules/auth/data/entities'\nimport crypto from 'node:crypto'\nimport { computeEmailHash } from '@open-mercato/core/modules/auth/lib/emailHash'\nimport { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\n\nexport class AuthService {\n constructor(private em: EntityManager) {}\n\n async findUserByEmail(email: string) {\n const emailHash = computeEmailHash(email)\n return this.em.findOne(User, {\n $or: [\n { email },\n { emailHash },\n ],\n } as any)\n }\n\n async findUsersByEmail(email: string) {\n const emailHash = computeEmailHash(email)\n return this.em.find(User, {\n deletedAt: null,\n $or: [\n { email },\n { emailHash },\n ],\n } as any)\n }\n\n async findUserByEmailAndTenant(email: string, tenantId: string) {\n const emailHash = computeEmailHash(email)\n return this.em.findOne(User, {\n tenantId,\n deletedAt: null,\n $or: [\n { email },\n { emailHash },\n ],\n } as any)\n }\n\n async verifyPassword(user: User, password: string) {\n if (!user.passwordHash) return false\n return compare(password, user.passwordHash)\n }\n\n async updateLastLoginAt(user: User) {\n const now = new Date()\n // Use native update to avoid flushing unrelated entities that might be pending in this EM\n await this.em.nativeUpdate(User, { id: user.id }, { lastLoginAt: now })\n user.lastLoginAt = now\n }\n\n async getUserRoles(user: User, tenantId?: string | null): Promise<string[]> {\n const resolvedTenantId = tenantId ?? user.tenantId ?? null\n if (!resolvedTenantId) return []\n const links = await findWithDecryption(\n this.em,\n UserRole,\n { user, role: { tenantId: resolvedTenantId } as any },\n { populate: ['role'] },\n { tenantId: resolvedTenantId, organizationId: user.organizationId ?? null },\n )\n return links.map((l) => l.role.name)\n }\n\n\n async createSession(user: User, expiresAt: Date): Promise<Session> {\n const token = crypto.randomBytes(32).toString('hex')\n const sess = this.em.create(Session as any, { user, token, expiresAt, createdAt: new Date() } as any)\n await this.em.persistAndFlush(sess)\n return sess as Session\n }\n\n async deleteSessionByToken(token: string) {\n await this.em.nativeDelete(Session, { token })\n }\n\n async refreshFromSessionToken(token: string) {\n const now = new Date()\n const sess = await this.em.findOne(Session, { token })\n if (!sess || sess.expiresAt <= now) return null\n const user = await this.em.findOne(User, { id: sess.user.id })\n if (!user) return null\n const roles = await this.getUserRoles(user, user.tenantId ?? null)\n return { user, roles }\n }\n\n async requestPasswordReset(email: string) {\n const user = await this.findUserByEmail(email)\n if (!user) return null\n const token = crypto.randomBytes(32).toString('hex')\n const expiresAt = new Date(Date.now() + 60 * 60 * 1000)\n const row = this.em.create(PasswordReset as any, { user, token, expiresAt, createdAt: new Date() } as any)\n await this.em.persistAndFlush(row)\n return { user, token }\n }\n\n async confirmPasswordReset(token: string, newPassword: string): Promise<User | null> {\n const now = new Date()\n const row = await this.em.findOne(PasswordReset, { token })\n if (!row || (row.usedAt && row.usedAt <= now) || row.expiresAt <= now) return null\n const user = await this.em.findOne(User, { id: row.user.id })\n if (!user) return null\n user.passwordHash = await hash(newPassword, 10)\n row.usedAt = new Date()\n await this.em.flush()\n return user\n }\n}\n"],
5
+ "mappings": "AACA,SAAS,SAAS,YAAY;AAC9B,SAAS,MAAY,UAAU,SAAS,qBAAqB;AAC7D,OAAO,YAAY;AACnB,SAAS,wBAAwB;AACjC,SAAS,0BAA0B;AAE5B,MAAM,YAAY;AAAA,EACvB,YAAoB,IAAmB;AAAnB;AAAA,EAAoB;AAAA,EAExC,MAAM,gBAAgB,OAAe;AACnC,UAAM,YAAY,iBAAiB,KAAK;AACxC,WAAO,KAAK,GAAG,QAAQ,MAAM;AAAA,MAC3B,KAAK;AAAA,QACH,EAAE,MAAM;AAAA,QACR,EAAE,UAAU;AAAA,MACd;AAAA,IACF,CAAQ;AAAA,EACV;AAAA,EAEA,MAAM,iBAAiB,OAAe;AACpC,UAAM,YAAY,iBAAiB,KAAK;AACxC,WAAO,KAAK,GAAG,KAAK,MAAM;AAAA,MACxB,WAAW;AAAA,MACX,KAAK;AAAA,QACH,EAAE,MAAM;AAAA,QACR,EAAE,UAAU;AAAA,MACd;AAAA,IACF,CAAQ;AAAA,EACV;AAAA,EAEA,MA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AAe,MAAM,KAAK,aAAa,EAAE;AAC9C,QAAI,SAAS,oBAAI,KAAK;AACtB,UAAM,KAAK,GAAG,MAAM;AACpB,WAAO;AAAA,EACT;AACF;",
6
6
  "names": []
7
7
  }
@@ -29,6 +29,7 @@ const seedGuardRules = {
29
29
  const em = resolve("em");
30
30
  const rulesPath = path.join(__dirname, "../workflows/examples", "guard-rules-example.json");
31
31
  const rulesData = JSON.parse(fs.readFileSync(rulesPath, "utf8"));
32
+ console.log("\u{1F9E0} Seeding guard rules...");
32
33
  let seededCount = 0;
33
34
  let skippedCount = 0;
34
35
  for (const ruleData of rulesData) {
@@ -52,7 +53,7 @@ const seedGuardRules = {
52
53
  seededCount++;
53
54
  }
54
55
  console.log(`
55
- \u2713 Guard rules seeding complete:`);
56
+ \u2705 Guard rules seeding complete:`);
56
57
  console.log(` - Seeded: ${seededCount}`);
57
58
  console.log(` - Skipped (existing): ${skippedCount}`);
58
59
  console.log(` - Total: ${rulesData.length}`);
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/modules/business_rules/cli.ts"],
4
- "sourcesContent": ["import type { ModuleCli } from '@open-mercato/shared/modules/registry'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { BusinessRule } from './data/entities'\nimport * as fs from 'fs'\nimport * as path from 'path'\n\n/**\n * Parse CLI arguments\n */\nfunction parseArgs(args: string[]) {\n const result: Record<string, string> = {}\n for (let i = 0; i < args.length; i += 2) {\n const key = args[i]?.replace(/^-+/, '')\n const value = args[i + 1]\n if (key && value) {\n result[key] = value\n }\n }\n return result\n}\n\n/**\n * Seed guard rules for workflow checkout demo\n */\nconst seedGuardRules: ModuleCli = {\n command: 'seed-guard-rules',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? '')\n const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? '')\n\n if (!tenantId || !organizationId) {\n console.error('Usage: mercato business_rules seed-guard-rules --tenant <tenantId> --org <organizationId>')\n console.error(' or: mercato business_rules seed-guard-rules -t <tenantId> -o <organizationId>')\n return\n }\n\n try {\n const { resolve } = await createRequestContainer()\n const em = resolve<EntityManager>('em')\n\n // Read guard rules from workflows examples\n const rulesPath = path.join(__dirname, '../workflows/examples', 'guard-rules-example.json')\n const rulesData = JSON.parse(fs.readFileSync(rulesPath, 'utf8'))\n\n let seededCount = 0\n let skippedCount = 0\n\n for (const ruleData of rulesData) {\n // Check if rule already exists\n const existing = await em.findOne(BusinessRule, {\n ruleId: ruleData.ruleId,\n tenantId,\n organizationId,\n })\n\n if (existing) {\n console.log(` \u2298 Guard rule '${ruleData.ruleId}' already exists`)\n skippedCount++\n continue\n }\n\n // Create the business rule\n const rule = em.create(BusinessRule, {\n ...ruleData,\n tenantId,\n organizationId,\n })\n\n await em.persistAndFlush(rule)\n console.log(` \u2713 Seeded guard rule: ${rule.ruleName}`)\n seededCount++\n }\n\n console.log(`\\n\u2713 Guard rules seeding complete:`)\n console.log(` - Seeded: ${seededCount}`)\n console.log(` - Skipped (existing): ${skippedCount}`)\n console.log(` - Total: ${rulesData.length}`)\n } catch (error) {\n console.error('Error seeding guard rules:', error)\n throw error\n }\n },\n}\n\nconst businessRulesCliCommands = [\n seedGuardRules,\n]\n\nexport default businessRulesCliCommands\n"],
5
- "mappings": "AACA,SAAS,8BAA8B;AAEvC,SAAS,oBAAoB;AAC7B,YAAY,QAAQ;AACpB,YAAY,UAAU;AAKtB,SAAS,UAAU,MAAgB;AACjC,QAAM,SAAiC,CAAC;AACxC,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,GAAG;AACvC,UAAM,MAAM,KAAK,CAAC,GAAG,QAAQ,OAAO,EAAE;AACtC,UAAM,QAAQ,KAAK,IAAI,CAAC;AACxB,QAAI,OAAO,OAAO;AAChB,aAAO,GAAG,IAAI;AAAA,IAChB;AAAA,EACF;AAC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A8B,KAAK;AACjD,YAAM;AAAA,IACR;AAAA,EACF;AACF;AAEA,MAAM,2BAA2B;AAAA,EAC/B;AACF;AAEA,IAAO,cAAQ;",
4
+ "sourcesContent": ["import type { ModuleCli } from '@open-mercato/shared/modules/registry'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { BusinessRule } from './data/entities'\nimport * as fs from 'fs'\nimport * as path from 'path'\n\n/**\n * Parse CLI arguments\n */\nfunction parseArgs(args: string[]) {\n const result: Record<string, string> = {}\n for (let i = 0; i < args.length; i += 2) {\n const key = args[i]?.replace(/^-+/, '')\n const value = args[i + 1]\n if (key && value) {\n result[key] = value\n }\n }\n return result\n}\n\n/**\n * Seed guard rules for workflow checkout demo\n */\nconst seedGuardRules: ModuleCli = {\n command: 'seed-guard-rules',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? '')\n const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? '')\n\n if (!tenantId || !organizationId) {\n console.error('Usage: mercato business_rules seed-guard-rules --tenant <tenantId> --org <organizationId>')\n console.error(' or: mercato business_rules seed-guard-rules -t <tenantId> -o <organizationId>')\n return\n }\n\n try {\n const { resolve } = await createRequestContainer()\n const em = resolve<EntityManager>('em')\n\n // Read guard rules from workflows examples\n const rulesPath = path.join(__dirname, '../workflows/examples', 'guard-rules-example.json')\n const rulesData = JSON.parse(fs.readFileSync(rulesPath, 'utf8'))\n\n console.log('\uD83E\uDDE0 Seeding guard rules...')\n let seededCount = 0\n let skippedCount = 0\n\n for (const ruleData of rulesData) {\n // Check if rule already exists\n const existing = await em.findOne(BusinessRule, {\n ruleId: ruleData.ruleId,\n tenantId,\n organizationId,\n })\n\n if (existing) {\n console.log(` \u2298 Guard rule '${ruleData.ruleId}' already exists`)\n skippedCount++\n continue\n }\n\n // Create the business rule\n const rule = em.create(BusinessRule, {\n ...ruleData,\n tenantId,\n organizationId,\n })\n\n await em.persistAndFlush(rule)\n console.log(` \u2713 Seeded guard rule: ${rule.ruleName}`)\n seededCount++\n }\n\n console.log(`\\n\u2705 Guard rules seeding complete:`)\n console.log(` - Seeded: ${seededCount}`)\n console.log(` - Skipped (existing): ${skippedCount}`)\n console.log(` - Total: ${rulesData.length}`)\n } catch (error) {\n console.error('Error seeding guard rules:', error)\n throw error\n }\n },\n}\n\nconst businessRulesCliCommands = [\n seedGuardRules,\n]\n\nexport default businessRulesCliCommands\n"],
5
+ "mappings": "AACA,SAAS,8BAA8B;AAEvC,SAAS,oBAAoB;AAC7B,YAAY,QAAQ;AACpB,YAAY,UAAU;AAKtB,SAAS,UAAU,MAAgB;AACjC,QAAM,SAAiC,CAAC;AACxC,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,GAAG;AACvC,UAAM,MAAM,KAAK,CAAC,GAAG,QAAQ,OAAO,EAAE;AACtC,UAAM,QAAQ,KAAK,IAAI,CAAC;AACxB,QAAI,OAAO,OAAO;AAChB,aAAO,GAAG,IAAI;AAAA,IAChB;AAAA,EACF;AACA,SAAO;AACT;AAKA,MAAM,iBAA4B;AAAA,EAChC,SAAS;AAAA,EACT,MAAM,IAAI,MAAgB;AACxB,UAAM,OAAO,UAAU,IAAI;AAC3B,UAAM,WAAW,OAAO,KAAK,YAAY,KAAK,UAAU,KAAK,KAAK,EAAE;AACpE,UAAM,iBAAiB,OAAO,KAAK,kBAAkB,KAAK,SAAS,KAAK,OAAO,KAAK,KAAK,EAAE;AAE3F,QAAI,CAAC,YAAY,CAAC,gBAAgB;AAChC,cAAQ,MAAM,2FAA2F;AACzG,cAAQ,MAAM,kFAAkF;AAChG;AAAA,IACF;AAEA,QAAI;AACF,YAAM,EAAE,QAAQ,IAAI,MAAM,uBAAuB;AACjD,YAAM,KAAK,QAAuB,IAAI;AAGtC,YAAM,YAAY,KAAK,KAAK,WAAW,yBAAyB,0BAA0B;AAC1F,YAAM,YAAY,KAAK,MAAM,GAAG,aAAa,WAAW,MAAM,CAAC;AAE/D,cAAQ,IAAI,kCAA2B;AACvC,UAAI,cAAc;AAClB,UAAI,eAAe;AAEnB,iBAAW,YAAY,WAAW;AAEhC,cAAM,WAAW,MAAM,GAAG,QAAQ,cAAc;AAAA,UAC9C,QAAQ,SAAS;AAAA,UACjB;AAAA,UACA;AAAA,QACF,CAAC;AAED,YAAI,UAAU;AACZ,kBAAQ,IAAI,wBAAmB,SAAS,MAAM,kBAAkB;AAChE;AACA;AAAA,QACF;AAGA,cAAM,OAAO,GAAG,OAAO,cAAc;AAAA,UACnC,GAAG;AAAA,UACH;AAAA,UACA;AAAA,QACF,CAAC;AAED,cAAM,GAAG,gBAAgB,IAAI;AAC7B,gBAAQ,IAAI,+BAA0B,KAAK,QAAQ,EAAE;AACrD;AAAA,MACF;AAEA,cAAQ,IAAI;AAAA,qCAAmC;AAC/C,cAAQ,IAAI,eAAe,WAAW,EAAE;AACxC,cAAQ,IAAI,2BAA2B,YAAY,EAAE;AACrD,cAAQ,IAAI,cAAc,UAAU,MAAM,EAAE;AAAA,IAC9C,SAAS,OAAO;AACd,cAAQ,MAAM,8BAA8B,KAAK;AACjD,YAAM;AAAA,IACR;AAAA,EACF;AACF;AAEA,MAAM,2BAA2B;AAAA,EAC/B;AACF;AAEA,IAAO,cAAQ;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,68 @@
1
+ import { NextResponse } from "next/server";
2
+ import { z } from "zod";
3
+ import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
4
+ import { Tenant } from "@open-mercato/core/modules/directory/data/entities";
5
+ const metadata = {
6
+ GET: {
7
+ requireAuth: false
8
+ }
9
+ };
10
+ const tenantLookupQuerySchema = z.object({
11
+ tenantId: z.string().uuid()
12
+ });
13
+ async function GET(req) {
14
+ const url = new URL(req.url);
15
+ const tenantId = url.searchParams.get("tenantId") || url.searchParams.get("tenant") || "";
16
+ const parsed = tenantLookupQuerySchema.safeParse({ tenantId });
17
+ if (!parsed.success) {
18
+ return NextResponse.json({ ok: false, error: "Invalid tenant id." }, { status: 400 });
19
+ }
20
+ const container = await createRequestContainer();
21
+ const em = container.resolve("em");
22
+ const tenant = await em.findOne(Tenant, { id: parsed.data.tenantId, deletedAt: null });
23
+ if (!tenant) {
24
+ return NextResponse.json({ ok: false, error: "Tenant not found." }, { status: 404 });
25
+ }
26
+ return NextResponse.json({
27
+ ok: true,
28
+ tenant: { id: String(tenant.id), name: tenant.name }
29
+ });
30
+ }
31
+ const lookupTag = "Directory";
32
+ const tenantLookupSuccessSchema = z.object({
33
+ ok: z.literal(true),
34
+ tenant: z.object({
35
+ id: z.string().uuid(),
36
+ name: z.string()
37
+ })
38
+ });
39
+ const tenantLookupErrorSchema = z.object({
40
+ ok: z.literal(false),
41
+ error: z.string()
42
+ });
43
+ const tenantLookupDoc = {
44
+ summary: "Public tenant lookup",
45
+ description: "Resolves tenant metadata for login/activation flows.",
46
+ tags: [lookupTag],
47
+ query: tenantLookupQuerySchema,
48
+ responses: [
49
+ { status: 200, description: "Tenant resolved.", schema: tenantLookupSuccessSchema }
50
+ ],
51
+ errors: [
52
+ { status: 400, description: "Invalid tenant id", schema: tenantLookupErrorSchema },
53
+ { status: 404, description: "Tenant not found", schema: tenantLookupErrorSchema }
54
+ ]
55
+ };
56
+ const openApi = {
57
+ tag: lookupTag,
58
+ summary: "Public tenant lookup",
59
+ methods: {
60
+ GET: tenantLookupDoc
61
+ }
62
+ };
63
+ export {
64
+ GET,
65
+ metadata,
66
+ openApi
67
+ };
68
+ //# sourceMappingURL=lookup.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../../../src/modules/directory/api/get/tenants/lookup.ts"],
4
+ "sourcesContent": ["import { NextResponse } from 'next/server'\nimport { z } from 'zod'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport { Tenant } from '@open-mercato/core/modules/directory/data/entities'\nimport type { OpenApiMethodDoc, OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\n\nexport const metadata = {\n GET: {\n requireAuth: false,\n },\n}\n\nconst tenantLookupQuerySchema = z.object({\n tenantId: z.string().uuid(),\n})\n\nexport async function GET(req: Request) {\n const url = new URL(req.url)\n const tenantId = url.searchParams.get('tenantId') || url.searchParams.get('tenant') || ''\n const parsed = tenantLookupQuerySchema.safeParse({ tenantId })\n if (!parsed.success) {\n return NextResponse.json({ ok: false, error: 'Invalid tenant id.' }, { status: 400 })\n }\n\n const container = await createRequestContainer()\n const em = (container.resolve('em') as EntityManager)\n const tenant = await em.findOne(Tenant, { id: parsed.data.tenantId, deletedAt: null })\n if (!tenant) {\n return NextResponse.json({ ok: false, error: 'Tenant not found.' }, { status: 404 })\n }\n return NextResponse.json({\n ok: true,\n tenant: { id: String(tenant.id), name: tenant.name },\n })\n}\n\nconst lookupTag = 'Directory'\n\nconst tenantLookupSuccessSchema = z.object({\n ok: z.literal(true),\n tenant: z.object({\n id: z.string().uuid(),\n name: z.string(),\n }),\n})\n\nconst tenantLookupErrorSchema = z.object({\n ok: z.literal(false),\n error: z.string(),\n})\n\nconst tenantLookupDoc: OpenApiMethodDoc = {\n summary: 'Public tenant lookup',\n description: 'Resolves tenant metadata for login/activation flows.',\n tags: [lookupTag],\n query: tenantLookupQuerySchema,\n responses: [\n { status: 200, description: 'Tenant resolved.', schema: tenantLookupSuccessSchema },\n ],\n errors: [\n { status: 400, description: 'Invalid tenant id', schema: tenantLookupErrorSchema },\n { status: 404, description: 'Tenant not found', schema: tenantLookupErrorSchema },\n ],\n}\n\nexport const openApi: OpenApiRouteDoc = {\n tag: lookupTag,\n summary: 'Public tenant lookup',\n methods: {\n GET: tenantLookupDoc,\n },\n}\n"],
5
+ "mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAElB,SAAS,8BAA8B;AACvC,SAAS,cAAc;AAGhB,MAAM,WAAW;AAAA,EACtB,KAAK;AAAA,IACH,aAA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EACT,aAAa;AAAA,EACb,MAAM,CAAC,SAAS;AAAA,EAChB,OAAO;AAAA,EACP,WAAW;AAAA,IACT,EAAE,QAAQ,KAAK,aAAa,oBAAoB,QAAQ,0BAA0B;AAAA,EACpF;AAAA,EACA,QAAQ;AAAA,IACN,EAAE,QAAQ,KAAK,aAAa,qBAAqB,QAAQ,wBAAwB;AAAA,IACjF,EAAE,QAAQ,KAAK,aAAa,oBAAoB,QAAQ,wBAAwB;AAAA,EAClF;AACF;AAEO,MAAM,UAA2B;AAAA,EACtC,KAAK;AAAA,EACL,SAAS;AAAA,EACT,SAAS;AAAA,IACP,KAAK;AAAA,EACP;AACF;",
6
+ "names": []
7
+ }
@@ -38,7 +38,7 @@ const seedDemo = {
38
38
  organizationId
39
39
  });
40
40
  if (existing) {
41
- console.log(`\u2713 Demo workflow '${demoData.workflowId}' already exists (ID: ${existing.id})`);
41
+ console.log(`\u2139\uFE0F Demo workflow '${demoData.workflowId}' already exists (ID: ${existing.id})`);
42
42
  return;
43
43
  }
44
44
  const workflow = em.create(WorkflowDefinition, {
@@ -47,7 +47,7 @@ const seedDemo = {
47
47
  organizationId
48
48
  });
49
49
  await em.persistAndFlush(workflow);
50
- console.log(`\u2713 Seeded demo workflow: ${workflow.workflowName}`);
50
+ console.log(`\u2705 Seeded demo workflow: ${workflow.workflowName}`);
51
51
  console.log(` - ID: ${workflow.id}`);
52
52
  console.log(` - Workflow ID: ${workflow.workflowId}`);
53
53
  console.log(` - Version: ${workflow.version}`);
@@ -79,11 +79,11 @@ const seedDemoWithRules = {
79
79
  console.error(" or: mercato workflows seed-demo-with-rules -t <tenantId> -o <organizationId>");
80
80
  return;
81
81
  }
82
- console.log("Seeding demo workflow with guard rules...\n");
82
+ console.log("\u{1F9E9} Seeding demo workflow with guard rules...\n");
83
83
  try {
84
- console.log("1. Seeding demo workflow...");
84
+ console.log("1. \u{1F9E9} Seeding demo workflow...");
85
85
  await seedDemo.run(rest);
86
- console.log("\n2. Seeding guard rules...");
86
+ console.log("\n2. \u{1F9E0} Seeding guard rules...");
87
87
  const { resolve } = await createRequestContainer();
88
88
  const em = resolve("em");
89
89
  const { BusinessRule } = await import("../business_rules/data/entities.js");
@@ -112,7 +112,7 @@ const seedDemoWithRules = {
112
112
  seededCount++;
113
113
  }
114
114
  console.log(`
115
- \u2713 Demo workflow with guard rules seeded successfully!`);
115
+ \u2705 Demo workflow with guard rules seeded successfully!`);
116
116
  console.log(` - Workflow: checkout_simple_v1`);
117
117
  console.log(` - Guard rules seeded: ${seededCount}`);
118
118
  console.log(` - Guard rules skipped: ${skippedCount}`);
@@ -143,7 +143,7 @@ const seedSalesPipeline = {
143
143
  organizationId
144
144
  });
145
145
  if (existing) {
146
- console.log(`\u2713 Sales pipeline workflow '${pipelineData.workflowId}' already exists (ID: ${existing.id})`);
146
+ console.log(`\u2139\uFE0F Sales pipeline workflow '${pipelineData.workflowId}' already exists (ID: ${existing.id})`);
147
147
  return;
148
148
  }
149
149
  const workflow = em.create(WorkflowDefinition, {
@@ -152,7 +152,7 @@ const seedSalesPipeline = {
152
152
  organizationId
153
153
  });
154
154
  await em.persistAndFlush(workflow);
155
- console.log(`\u2713 Seeded sales pipeline workflow: ${workflow.workflowName}`);
155
+ console.log(`\u2705 Seeded sales pipeline workflow: ${workflow.workflowName}`);
156
156
  console.log(` - ID: ${workflow.id}`);
157
157
  console.log(` - Workflow ID: ${workflow.workflowId}`);
158
158
  console.log(` - Version: ${workflow.version}`);
@@ -188,7 +188,7 @@ const seedSimpleApproval = {
188
188
  organizationId
189
189
  });
190
190
  if (existing) {
191
- console.log(`\u2713 Simple approval workflow '${approvalData.workflowId}' already exists (ID: ${existing.id})`);
191
+ console.log(`\u2139\uFE0F Simple approval workflow '${approvalData.workflowId}' already exists (ID: ${existing.id})`);
192
192
  return;
193
193
  }
194
194
  const workflow = em.create(WorkflowDefinition, {
@@ -197,7 +197,7 @@ const seedSimpleApproval = {
197
197
  organizationId
198
198
  });
199
199
  await em.persistAndFlush(workflow);
200
- console.log(`\u2713 Seeded simple approval workflow: ${workflow.workflowName}`);
200
+ console.log(`\u2705 Seeded simple approval workflow: ${workflow.workflowName}`);
201
201
  console.log(` - ID: ${workflow.id}`);
202
202
  console.log(` - Workflow ID: ${workflow.workflowId}`);
203
203
  console.log(` - Version: ${workflow.version}`);
@@ -260,7 +260,7 @@ const seedAll = {
260
260
  console.error("Usage: mercato workflows seed-all --tenant <tenantId> --org <organizationId>");
261
261
  return;
262
262
  }
263
- console.log("Seeding all example workflows...\n");
263
+ console.log("\u{1F9E9} Seeding all example workflows...\n");
264
264
  try {
265
265
  await seedDemoWithRules.run(rest);
266
266
  console.log("");
@@ -268,7 +268,7 @@ const seedAll = {
268
268
  console.log("");
269
269
  await seedSimpleApproval.run(rest);
270
270
  console.log("");
271
- console.log("\u2713 All example workflows seeded successfully!");
271
+ console.log("\u2705 All example workflows seeded successfully!");
272
272
  } catch (error) {
273
273
  console.error("Error seeding workflows:", error);
274
274
  throw error;
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/modules/workflows/cli.ts"],
4
- "sourcesContent": ["import type { ModuleCli } from '@open-mercato/shared/modules/registry'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { WorkflowDefinition } from './data/entities'\nimport * as fs from 'fs'\nimport * as path from 'path'\nimport { fileURLToPath } from 'url'\n\nconst __filename = fileURLToPath(import.meta.url)\nconst __dirname = path.dirname(__filename)\n\n/**\n * Parse CLI arguments\n */\nfunction parseArgs(args: string[]) {\n const result: Record<string, string> = {}\n for (let i = 0; i < args.length; i += 2) {\n const key = args[i]?.replace(/^-+/, '') // Remove one or more dashes\n const value = args[i + 1]\n if (key && value) {\n result[key] = value\n }\n }\n return result\n}\n\n/**\n * Seed demo checkout workflow\n */\nconst seedDemo: ModuleCli = {\n command: 'seed-demo',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? '')\n const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? '')\n\n if (!tenantId || !organizationId) {\n console.error('Usage: mercato workflows seed-demo --tenant <tenantId> --org <organizationId>')\n console.error(' or: mercato workflows seed-demo -t <tenantId> -o <organizationId>')\n return\n }\n\n try {\n const { resolve } = await createRequestContainer()\n const em = resolve<EntityManager>('em')\n\n // Read the demo workflow definition\n const demoPath = path.join(__dirname, 'examples', 'checkout-demo-definition.json')\n const demoData = JSON.parse(fs.readFileSync(demoPath, 'utf8'))\n\n // Check if it already exists\n const existing = await em.findOne(WorkflowDefinition, {\n workflowId: demoData.workflowId,\n tenantId,\n organizationId,\n })\n\n if (existing) {\n console.log(`\u2713 Demo workflow '${demoData.workflowId}' already exists (ID: ${existing.id})`)\n return\n }\n\n // Create the workflow definition\n const workflow = em.create(WorkflowDefinition, {\n ...demoData,\n tenantId,\n organizationId,\n })\n\n await em.persistAndFlush(workflow)\n\n console.log(`\u2713 Seeded demo workflow: ${workflow.workflowName}`)\n console.log(` - ID: ${workflow.id}`)\n console.log(` - Workflow ID: ${workflow.workflowId}`)\n console.log(` - Version: ${workflow.version}`)\n console.log(` - Steps: ${workflow.definition.steps.length}`)\n console.log(` - Transitions: ${workflow.definition.transitions.length}`)\n console.log('')\n console.log('Demo workflow is ready! You can now:')\n console.log(' 1. View it in admin: /backend/definitions')\n console.log(' 2. Try the demo page: /checkout-demo')\n console.log(' 3. Start an instance via API: POST /api/workflows/instances')\n console.log('')\n console.log('Note: This workflow includes a USER_TASK step for customer information.')\n console.log('When the workflow reaches this step, it will pause and require user input.')\n console.log('Complete pending tasks at: /backend/tasks')\n } catch (error) {\n console.error('Error seeding demo workflow:', error)\n throw error\n }\n },\n}\n\n/**\n * Seed demo checkout workflow with guard rules\n */\nconst seedDemoWithRules: ModuleCli = {\n command: 'seed-demo-with-rules',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? '')\n const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? '')\n\n if (!tenantId || !organizationId) {\n console.error('Usage: mercato workflows seed-demo-with-rules --tenant <tenantId> --org <organizationId>')\n console.error(' or: mercato workflows seed-demo-with-rules -t <tenantId> -o <organizationId>')\n return\n }\n\n console.log('Seeding demo workflow with guard rules...\\n')\n\n try {\n // Seed the workflow definition\n console.log('1. Seeding demo workflow...')\n await seedDemo.run(rest)\n\n // Seed the guard rules\n console.log('\\n2. Seeding guard rules...')\n const { resolve } = await createRequestContainer()\n const em = resolve<EntityManager>('em')\n\n // Import BusinessRule entity\n const { BusinessRule } = await import('../business_rules/data/entities')\n\n // Read guard rules\n const rulesPath = path.join(__dirname, 'examples', 'guard-rules-example.json')\n const rulesData = JSON.parse(fs.readFileSync(rulesPath, 'utf8'))\n\n let seededCount = 0\n let skippedCount = 0\n\n for (const ruleData of rulesData) {\n const existing = await em.findOne(BusinessRule, {\n ruleId: ruleData.ruleId,\n tenantId,\n organizationId,\n })\n\n if (existing) {\n console.log(` \u2298 Guard rule '${ruleData.ruleId}' already exists`)\n skippedCount++\n continue\n }\n\n const rule = em.create(BusinessRule, {\n ...ruleData,\n tenantId,\n organizationId,\n })\n\n await em.persistAndFlush(rule)\n console.log(` \u2713 Seeded guard rule: ${rule.ruleName}`)\n seededCount++\n }\n\n console.log(`\\n\u2713 Demo workflow with guard rules seeded successfully!`)\n console.log(` - Workflow: checkout_simple_v1`)\n console.log(` - Guard rules seeded: ${seededCount}`)\n console.log(` - Guard rules skipped: ${skippedCount}`)\n } catch (error) {\n console.error('Error seeding demo with rules:', error)\n throw error\n }\n },\n}\n\n/**\n * Seed sales pipeline example\n */\nconst seedSalesPipeline: ModuleCli = {\n command: 'seed-sales-pipeline',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? '')\n const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? '')\n\n if (!tenantId || !organizationId) {\n console.error('Usage: mercato workflows seed-sales-pipeline --tenant <tenantId> --org <organizationId>')\n return\n }\n\n try {\n const { resolve } = await createRequestContainer()\n const em = resolve<EntityManager>('em')\n\n // Read the sales pipeline workflow definition\n const pipelinePath = path.join(__dirname, 'examples', 'sales-pipeline-definition.json')\n const pipelineData = JSON.parse(fs.readFileSync(pipelinePath, 'utf8'))\n\n // Check if it already exists\n const existing = await em.findOne(WorkflowDefinition, {\n workflowId: pipelineData.workflowId,\n tenantId,\n organizationId,\n })\n\n if (existing) {\n console.log(`\u2713 Sales pipeline workflow '${pipelineData.workflowId}' already exists (ID: ${existing.id})`)\n return\n }\n\n // Create the workflow definition\n const workflow = em.create(WorkflowDefinition, {\n ...pipelineData,\n tenantId,\n organizationId,\n })\n\n await em.persistAndFlush(workflow)\n\n console.log(`\u2713 Seeded sales pipeline workflow: ${workflow.workflowName}`)\n console.log(` - ID: ${workflow.id}`)\n console.log(` - Workflow ID: ${workflow.workflowId}`)\n console.log(` - Version: ${workflow.version}`)\n console.log(` - Steps: ${workflow.definition.steps.length}`)\n console.log(` - Transitions: ${workflow.definition.transitions.length}`)\n console.log(` - Activities: ${workflow.definition.transitions.reduce((sum, t) => sum + (t.activities?.length || 0), 0)}`)\n console.log('')\n console.log('Sales pipeline workflow is ready!')\n } catch (error) {\n console.error('Error seeding sales pipeline workflow:', error)\n throw error\n }\n },\n}\n\n/**\n * Seed simple approval example\n */\nconst seedSimpleApproval: ModuleCli = {\n command: 'seed-simple-approval',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? '')\n const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? '')\n\n if (!tenantId || !organizationId) {\n console.error('Usage: mercato workflows seed-simple-approval --tenant <tenantId> --org <organizationId>')\n return\n }\n\n try {\n const { resolve } = await createRequestContainer()\n const em = resolve<EntityManager>('em')\n\n // Read the simple approval workflow definition\n const approvalPath = path.join(__dirname, 'examples', 'simple-approval-definition.json')\n const approvalData = JSON.parse(fs.readFileSync(approvalPath, 'utf8'))\n\n // Check if it already exists\n const existing = await em.findOne(WorkflowDefinition, {\n workflowId: approvalData.workflowId,\n tenantId,\n organizationId,\n })\n\n if (existing) {\n console.log(`\u2713 Simple approval workflow '${approvalData.workflowId}' already exists (ID: ${existing.id})`)\n return\n }\n\n // Create the workflow definition\n const workflow = em.create(WorkflowDefinition, {\n ...approvalData,\n tenantId,\n organizationId,\n })\n\n await em.persistAndFlush(workflow)\n\n console.log(`\u2713 Seeded simple approval workflow: ${workflow.workflowName}`)\n console.log(` - ID: ${workflow.id}`)\n console.log(` - Workflow ID: ${workflow.workflowId}`)\n console.log(` - Version: ${workflow.version}`)\n console.log(` - Steps: ${workflow.definition.steps.length}`)\n console.log(` - Transitions: ${workflow.definition.transitions.length}`)\n console.log('')\n console.log('Simple approval workflow is ready!')\n } catch (error) {\n console.error('Error seeding simple approval workflow:', error)\n throw error\n }\n },\n}\n\n/**\n * Start workflow activity worker\n */\nconst startWorker: ModuleCli = {\n command: 'start-worker',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const concurrency = parseInt(args.concurrency ?? args.c ?? '5')\n\n const strategy = process.env.QUEUE_STRATEGY === 'async' ? 'async' : 'local'\n\n console.log('[Workflow Worker] Starting activity worker...')\n console.log(`[Workflow Worker] Strategy: ${strategy}`)\n\n if (strategy === 'local') {\n const pollMs = process.env.LOCAL_QUEUE_POLL_MS || '5000'\n console.log(`[Workflow Worker] Polling interval: ${pollMs}ms`)\n console.log('[Workflow Worker] NOTE: Local strategy is for development only.')\n console.log('[Workflow Worker] Use QUEUE_STRATEGY=async with Redis for production.')\n } else {\n console.log(`[Workflow Worker] Concurrency: ${concurrency}`)\n console.log(`[Workflow Worker] Redis: ${process.env.REDIS_URL || process.env.QUEUE_REDIS_URL}`)\n }\n\n try {\n const container = await createRequestContainer()\n const em = container.resolve<EntityManager>('em')\n\n // Import queue and handler\n const { runWorker } = await import('@open-mercato/queue/worker')\n const { createActivityWorkerHandler } = await import('./lib/activity-worker-handler')\n const { WORKFLOW_ACTIVITIES_QUEUE_NAME } = await import('./lib/activity-queue-types')\n\n // Create handler\n const handler = createActivityWorkerHandler(em, container)\n\n // Run worker\n await runWorker({\n queueName: WORKFLOW_ACTIVITIES_QUEUE_NAME,\n handler,\n connection: strategy === 'async' ? {\n url: process.env.REDIS_URL || process.env.QUEUE_REDIS_URL,\n } : undefined,\n concurrency,\n gracefulShutdown: true,\n })\n } catch (error) {\n console.error('[Workflow Worker] Failed to start worker:', error)\n throw error\n }\n },\n}\n\n/**\n * Seed all example workflows\n */\nconst seedAll: ModuleCli = {\n command: 'seed-all',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? '')\n const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? '')\n\n if (!tenantId || !organizationId) {\n console.error('Usage: mercato workflows seed-all --tenant <tenantId> --org <organizationId>')\n return\n }\n\n console.log('Seeding all example workflows...\\n')\n\n try {\n // Seed demo checkout with rules\n await seedDemoWithRules.run(rest)\n console.log('')\n\n // Seed sales pipeline\n await seedSalesPipeline.run(rest)\n console.log('')\n\n // Seed simple approval\n await seedSimpleApproval.run(rest)\n console.log('')\n\n console.log('\u2713 All example workflows seeded successfully!')\n } catch (error) {\n console.error('Error seeding workflows:', error)\n throw error\n }\n },\n}\n\n/**\n * Manually process pending workflow activities\n */\nconst processActivities: ModuleCli = {\n command: 'process-activities',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const limit = parseInt(args.limit ?? args.l ?? '0')\n\n console.log('[Workflow Activities] Processing pending activities...')\n if (limit > 0) {\n console.log(`[Workflow Activities] Limit: ${limit} jobs`)\n }\n\n try {\n const container = await createRequestContainer()\n const em = container.resolve<EntityManager>('em')\n\n // Import queue and handler\n const { createQueue } = await import('@open-mercato/queue')\n const { createActivityWorkerHandler } = await import('./lib/activity-worker-handler')\n const { WORKFLOW_ACTIVITIES_QUEUE_NAME } = await import('./lib/activity-queue-types')\n\n // Create queue instance\n const queue = createQueue(WORKFLOW_ACTIVITIES_QUEUE_NAME, 'local', {\n baseDir: process.env.QUEUE_BASE_DIR || '.queue',\n })\n\n // Create handler\n const handler = createActivityWorkerHandler(em, container)\n\n // Get initial counts\n const initialCounts = await queue.getJobCounts()\n console.log(`[Workflow Activities] Pending jobs: ${initialCounts.waiting}`)\n\n if (initialCounts.waiting === 0) {\n console.log('[Workflow Activities] No jobs to process')\n await queue.close()\n return\n }\n\n // Process jobs\n const result = await queue.process(handler as any, limit > 0 ? { limit } : undefined)\n\n console.log(`\\n[Workflow Activities] \u2713 Processed ${result.processed} activities`)\n if (result.failed > 0) {\n console.log(`[Workflow Activities] \u2717 Failed: ${result.failed} activities`)\n }\n\n // Show remaining\n const finalCounts = await queue.getJobCounts()\n if (finalCounts.waiting > 0) {\n console.log(`[Workflow Activities] Remaining: ${finalCounts.waiting} jobs`)\n }\n\n await queue.close()\n } catch (error) {\n console.error('[Workflow Activities] Error processing activities:', error)\n throw error\n }\n },\n}\n\nconst workflowsCliCommands = [\n startWorker,\n processActivities,\n seedDemo,\n seedDemoWithRules,\n seedSalesPipeline,\n seedSimpleApproval,\n seedAll,\n]\n\nexport default workflowsCliCommands\n"],
5
- "mappings": "AACA,SAAS,8BAA8B;AAEvC,SAAS,0BAA0B;AACnC,YAAY,QAAQ;AACpB,YAAY,UAAU;AACtB,SAAS,qBAAqB;AAE9B,MAAM,aAAa,cAAc,YAAY,GAAG;AAChD,MAAM,YAAY,KAAK,QAAQ,UAAU;AAKzC,SAAS,UAAU,MAAgB;AACjC,QAAM,SAAiC,CAAC;AACxC,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,GAAG;AACvC,UAAM,MAAM,KAAK,CAAC,GAAG,QAAQ,OAAO,EAAE;AACtC,UAAM,QAAQ,KAAK,IAAI,CAAC;AACxB,QAAI,OAAO,OAAO;AAChB,aAAO,GA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AAAA,MAC3E;AAGA,YAAM,cAAc,MAAM,MAAM,aAAa;AAC7C,UAAI,YAAY,UAAU,GAAG;AAC3B,gBAAQ,IAAI,oCAAoC,YAAY,OAAO,OAAO;AAAA,MAC5E;AAEA,YAAM,MAAM,MAAM;AAAA,IACpB,SAAS,OAAO;AACd,cAAQ,MAAM,sDAAsD,KAAK;AACzE,YAAM;AAAA,IACR;AAAA,EACF;AACF;AAEA,MAAM,uBAAuB;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,IAAO,cAAQ;",
4
+ "sourcesContent": ["import type { ModuleCli } from '@open-mercato/shared/modules/registry'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { WorkflowDefinition } from './data/entities'\nimport * as fs from 'fs'\nimport * as path from 'path'\nimport { fileURLToPath } from 'url'\n\nconst __filename = fileURLToPath(import.meta.url)\nconst __dirname = path.dirname(__filename)\n\n/**\n * Parse CLI arguments\n */\nfunction parseArgs(args: string[]) {\n const result: Record<string, string> = {}\n for (let i = 0; i < args.length; i += 2) {\n const key = args[i]?.replace(/^-+/, '') // Remove one or more dashes\n const value = args[i + 1]\n if (key && value) {\n result[key] = value\n }\n }\n return result\n}\n\n/**\n * Seed demo checkout workflow\n */\nconst seedDemo: ModuleCli = {\n command: 'seed-demo',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? '')\n const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? '')\n\n if (!tenantId || !organizationId) {\n console.error('Usage: mercato workflows seed-demo --tenant <tenantId> --org <organizationId>')\n console.error(' or: mercato workflows seed-demo -t <tenantId> -o <organizationId>')\n return\n }\n\n try {\n const { resolve } = await createRequestContainer()\n const em = resolve<EntityManager>('em')\n\n // Read the demo workflow definition\n const demoPath = path.join(__dirname, 'examples', 'checkout-demo-definition.json')\n const demoData = JSON.parse(fs.readFileSync(demoPath, 'utf8'))\n\n // Check if it already exists\n const existing = await em.findOne(WorkflowDefinition, {\n workflowId: demoData.workflowId,\n tenantId,\n organizationId,\n })\n\n if (existing) {\n console.log(`\u2139\uFE0F Demo workflow '${demoData.workflowId}' already exists (ID: ${existing.id})`)\n return\n }\n\n // Create the workflow definition\n const workflow = em.create(WorkflowDefinition, {\n ...demoData,\n tenantId,\n organizationId,\n })\n\n await em.persistAndFlush(workflow)\n\n console.log(`\u2705 Seeded demo workflow: ${workflow.workflowName}`)\n console.log(` - ID: ${workflow.id}`)\n console.log(` - Workflow ID: ${workflow.workflowId}`)\n console.log(` - Version: ${workflow.version}`)\n console.log(` - Steps: ${workflow.definition.steps.length}`)\n console.log(` - Transitions: ${workflow.definition.transitions.length}`)\n console.log('')\n console.log('Demo workflow is ready! You can now:')\n console.log(' 1. View it in admin: /backend/definitions')\n console.log(' 2. Try the demo page: /checkout-demo')\n console.log(' 3. Start an instance via API: POST /api/workflows/instances')\n console.log('')\n console.log('Note: This workflow includes a USER_TASK step for customer information.')\n console.log('When the workflow reaches this step, it will pause and require user input.')\n console.log('Complete pending tasks at: /backend/tasks')\n } catch (error) {\n console.error('Error seeding demo workflow:', error)\n throw error\n }\n },\n}\n\n/**\n * Seed demo checkout workflow with guard rules\n */\nconst seedDemoWithRules: ModuleCli = {\n command: 'seed-demo-with-rules',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? '')\n const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? '')\n\n if (!tenantId || !organizationId) {\n console.error('Usage: mercato workflows seed-demo-with-rules --tenant <tenantId> --org <organizationId>')\n console.error(' or: mercato workflows seed-demo-with-rules -t <tenantId> -o <organizationId>')\n return\n }\n\n console.log('\uD83E\uDDE9 Seeding demo workflow with guard rules...\\n')\n\n try {\n // Seed the workflow definition\n console.log('1. \uD83E\uDDE9 Seeding demo workflow...')\n await seedDemo.run(rest)\n\n // Seed the guard rules\n console.log('\\n2. \uD83E\uDDE0 Seeding guard rules...')\n const { resolve } = await createRequestContainer()\n const em = resolve<EntityManager>('em')\n\n // Import BusinessRule entity\n const { BusinessRule } = await import('../business_rules/data/entities')\n\n // Read guard rules\n const rulesPath = path.join(__dirname, 'examples', 'guard-rules-example.json')\n const rulesData = JSON.parse(fs.readFileSync(rulesPath, 'utf8'))\n\n let seededCount = 0\n let skippedCount = 0\n\n for (const ruleData of rulesData) {\n const existing = await em.findOne(BusinessRule, {\n ruleId: ruleData.ruleId,\n tenantId,\n organizationId,\n })\n\n if (existing) {\n console.log(` \u2298 Guard rule '${ruleData.ruleId}' already exists`)\n skippedCount++\n continue\n }\n\n const rule = em.create(BusinessRule, {\n ...ruleData,\n tenantId,\n organizationId,\n })\n\n await em.persistAndFlush(rule)\n console.log(` \u2713 Seeded guard rule: ${rule.ruleName}`)\n seededCount++\n }\n\n console.log(`\\n\u2705 Demo workflow with guard rules seeded successfully!`)\n console.log(` - Workflow: checkout_simple_v1`)\n console.log(` - Guard rules seeded: ${seededCount}`)\n console.log(` - Guard rules skipped: ${skippedCount}`)\n } catch (error) {\n console.error('Error seeding demo with rules:', error)\n throw error\n }\n },\n}\n\n/**\n * Seed sales pipeline example\n */\nconst seedSalesPipeline: ModuleCli = {\n command: 'seed-sales-pipeline',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? '')\n const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? '')\n\n if (!tenantId || !organizationId) {\n console.error('Usage: mercato workflows seed-sales-pipeline --tenant <tenantId> --org <organizationId>')\n return\n }\n\n try {\n const { resolve } = await createRequestContainer()\n const em = resolve<EntityManager>('em')\n\n // Read the sales pipeline workflow definition\n const pipelinePath = path.join(__dirname, 'examples', 'sales-pipeline-definition.json')\n const pipelineData = JSON.parse(fs.readFileSync(pipelinePath, 'utf8'))\n\n // Check if it already exists\n const existing = await em.findOne(WorkflowDefinition, {\n workflowId: pipelineData.workflowId,\n tenantId,\n organizationId,\n })\n\n if (existing) {\n console.log(`\u2139\uFE0F Sales pipeline workflow '${pipelineData.workflowId}' already exists (ID: ${existing.id})`)\n return\n }\n\n // Create the workflow definition\n const workflow = em.create(WorkflowDefinition, {\n ...pipelineData,\n tenantId,\n organizationId,\n })\n\n await em.persistAndFlush(workflow)\n\n console.log(`\u2705 Seeded sales pipeline workflow: ${workflow.workflowName}`)\n console.log(` - ID: ${workflow.id}`)\n console.log(` - Workflow ID: ${workflow.workflowId}`)\n console.log(` - Version: ${workflow.version}`)\n console.log(` - Steps: ${workflow.definition.steps.length}`)\n console.log(` - Transitions: ${workflow.definition.transitions.length}`)\n console.log(` - Activities: ${workflow.definition.transitions.reduce((sum, t) => sum + (t.activities?.length || 0), 0)}`)\n console.log('')\n console.log('Sales pipeline workflow is ready!')\n } catch (error) {\n console.error('Error seeding sales pipeline workflow:', error)\n throw error\n }\n },\n}\n\n/**\n * Seed simple approval example\n */\nconst seedSimpleApproval: ModuleCli = {\n command: 'seed-simple-approval',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? '')\n const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? '')\n\n if (!tenantId || !organizationId) {\n console.error('Usage: mercato workflows seed-simple-approval --tenant <tenantId> --org <organizationId>')\n return\n }\n\n try {\n const { resolve } = await createRequestContainer()\n const em = resolve<EntityManager>('em')\n\n // Read the simple approval workflow definition\n const approvalPath = path.join(__dirname, 'examples', 'simple-approval-definition.json')\n const approvalData = JSON.parse(fs.readFileSync(approvalPath, 'utf8'))\n\n // Check if it already exists\n const existing = await em.findOne(WorkflowDefinition, {\n workflowId: approvalData.workflowId,\n tenantId,\n organizationId,\n })\n\n if (existing) {\n console.log(`\u2139\uFE0F Simple approval workflow '${approvalData.workflowId}' already exists (ID: ${existing.id})`)\n return\n }\n\n // Create the workflow definition\n const workflow = em.create(WorkflowDefinition, {\n ...approvalData,\n tenantId,\n organizationId,\n })\n\n await em.persistAndFlush(workflow)\n\n console.log(`\u2705 Seeded simple approval workflow: ${workflow.workflowName}`)\n console.log(` - ID: ${workflow.id}`)\n console.log(` - Workflow ID: ${workflow.workflowId}`)\n console.log(` - Version: ${workflow.version}`)\n console.log(` - Steps: ${workflow.definition.steps.length}`)\n console.log(` - Transitions: ${workflow.definition.transitions.length}`)\n console.log('')\n console.log('Simple approval workflow is ready!')\n } catch (error) {\n console.error('Error seeding simple approval workflow:', error)\n throw error\n }\n },\n}\n\n/**\n * Start workflow activity worker\n */\nconst startWorker: ModuleCli = {\n command: 'start-worker',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const concurrency = parseInt(args.concurrency ?? args.c ?? '5')\n\n const strategy = process.env.QUEUE_STRATEGY === 'async' ? 'async' : 'local'\n\n console.log('[Workflow Worker] Starting activity worker...')\n console.log(`[Workflow Worker] Strategy: ${strategy}`)\n\n if (strategy === 'local') {\n const pollMs = process.env.LOCAL_QUEUE_POLL_MS || '5000'\n console.log(`[Workflow Worker] Polling interval: ${pollMs}ms`)\n console.log('[Workflow Worker] NOTE: Local strategy is for development only.')\n console.log('[Workflow Worker] Use QUEUE_STRATEGY=async with Redis for production.')\n } else {\n console.log(`[Workflow Worker] Concurrency: ${concurrency}`)\n console.log(`[Workflow Worker] Redis: ${process.env.REDIS_URL || process.env.QUEUE_REDIS_URL}`)\n }\n\n try {\n const container = await createRequestContainer()\n const em = container.resolve<EntityManager>('em')\n\n // Import queue and handler\n const { runWorker } = await import('@open-mercato/queue/worker')\n const { createActivityWorkerHandler } = await import('./lib/activity-worker-handler')\n const { WORKFLOW_ACTIVITIES_QUEUE_NAME } = await import('./lib/activity-queue-types')\n\n // Create handler\n const handler = createActivityWorkerHandler(em, container)\n\n // Run worker\n await runWorker({\n queueName: WORKFLOW_ACTIVITIES_QUEUE_NAME,\n handler,\n connection: strategy === 'async' ? {\n url: process.env.REDIS_URL || process.env.QUEUE_REDIS_URL,\n } : undefined,\n concurrency,\n gracefulShutdown: true,\n })\n } catch (error) {\n console.error('[Workflow Worker] Failed to start worker:', error)\n throw error\n }\n },\n}\n\n/**\n * Seed all example workflows\n */\nconst seedAll: ModuleCli = {\n command: 'seed-all',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const tenantId = String(args.tenantId ?? args.tenant ?? args.t ?? '')\n const organizationId = String(args.organizationId ?? args.orgId ?? args.org ?? args.o ?? '')\n\n if (!tenantId || !organizationId) {\n console.error('Usage: mercato workflows seed-all --tenant <tenantId> --org <organizationId>')\n return\n }\n\n console.log('\uD83E\uDDE9 Seeding all example workflows...\\n')\n\n try {\n // Seed demo checkout with rules\n await seedDemoWithRules.run(rest)\n console.log('')\n\n // Seed sales pipeline\n await seedSalesPipeline.run(rest)\n console.log('')\n\n // Seed simple approval\n await seedSimpleApproval.run(rest)\n console.log('')\n\n console.log('\u2705 All example workflows seeded successfully!')\n } catch (error) {\n console.error('Error seeding workflows:', error)\n throw error\n }\n },\n}\n\n/**\n * Manually process pending workflow activities\n */\nconst processActivities: ModuleCli = {\n command: 'process-activities',\n async run(rest: string[]) {\n const args = parseArgs(rest)\n const limit = parseInt(args.limit ?? args.l ?? '0')\n\n console.log('[Workflow Activities] Processing pending activities...')\n if (limit > 0) {\n console.log(`[Workflow Activities] Limit: ${limit} jobs`)\n }\n\n try {\n const container = await createRequestContainer()\n const em = container.resolve<EntityManager>('em')\n\n // Import queue and handler\n const { createQueue } = await import('@open-mercato/queue')\n const { createActivityWorkerHandler } = await import('./lib/activity-worker-handler')\n const { WORKFLOW_ACTIVITIES_QUEUE_NAME } = await import('./lib/activity-queue-types')\n\n // Create queue instance\n const queue = createQueue(WORKFLOW_ACTIVITIES_QUEUE_NAME, 'local', {\n baseDir: process.env.QUEUE_BASE_DIR || '.queue',\n })\n\n // Create handler\n const handler = createActivityWorkerHandler(em, container)\n\n // Get initial counts\n const initialCounts = await queue.getJobCounts()\n console.log(`[Workflow Activities] Pending jobs: ${initialCounts.waiting}`)\n\n if (initialCounts.waiting === 0) {\n console.log('[Workflow Activities] No jobs to process')\n await queue.close()\n return\n }\n\n // Process jobs\n const result = await queue.process(handler as any, limit > 0 ? { limit } : undefined)\n\n console.log(`\\n[Workflow Activities] \u2713 Processed ${result.processed} activities`)\n if (result.failed > 0) {\n console.log(`[Workflow Activities] \u2717 Failed: ${result.failed} activities`)\n }\n\n // Show remaining\n const finalCounts = await queue.getJobCounts()\n if (finalCounts.waiting > 0) {\n console.log(`[Workflow Activities] Remaining: ${finalCounts.waiting} jobs`)\n }\n\n await queue.close()\n } catch (error) {\n console.error('[Workflow Activities] Error processing activities:', error)\n throw error\n }\n },\n}\n\nconst workflowsCliCommands = [\n startWorker,\n processActivities,\n seedDemo,\n seedDemoWithRules,\n seedSalesPipeline,\n seedSimpleApproval,\n seedAll,\n]\n\nexport default workflowsCliCommands\n"],
5
+ "mappings": "AACA,SAAS,8BAA8B;AAEvC,SAAS,0BAA0B;AACnC,YAAY,QAAQ;AACpB,YAAY,UAAU;AACtB,SAAS,qBAAqB;AAE9B,MAAM,aAAa,cAAc,YAAY,GAAG;AAChD,MAAM,YAAY,KAAK,QAAQ,UAAU;AAKzC,SAAS,UAAU,MAAgB;AACjC,QAAM,SAAiC,CAAC;AACxC,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,GAAG;AACvC,UAAM,MAAM,KAAK,CAAC,GAAG,QAAQ,OAAO,EAAE;AACtC,UAAM,QAAQ,KAAK,IAAI,CAAC;AACxB,QAAI,OAAO,OAAO;AAChB,aAAO,GAAG,IAAI;AAAA,IAChB;AAAA,EACF;AACA,SAAO;AACT;AAKA,MAAM,WAAsB;AAAA,EAC1B,SAAS;AAAA,EACT,MAAM,IAAI,MAAgB;AACxB,UAAM,OAAO,UAAU,IAAI;AAC3B,UAAM,WAAW,OAAO,KAAK,YAAY,KAAK,UAAU,KAAK,KAAK,EAAE;AACpE,UA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AAK;AACzE,YAAM;AAAA,IACR;AAAA,EACF;AACF;AAEA,MAAM,uBAAuB;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,IAAO,cAAQ;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-mercato/core",
3
- "version": "0.4.2-canary-19353c5970",
3
+ "version": "0.4.2-canary-19703ca707",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {
@@ -207,7 +207,7 @@
207
207
  }
208
208
  },
209
209
  "dependencies": {
210
- "@open-mercato/shared": "0.4.2-canary-19353c5970",
210
+ "@open-mercato/shared": "0.4.2-canary-19703ca707",
211
211
  "@xyflow/react": "^12.6.0",
212
212
  "date-fns": "^4.1.0",
213
213
  "date-fns-tz": "^3.2.0"