@open-mercato/core 0.7.1-develop.7131.1.20abf9e885 → 0.7.1-develop.7135.1.19bf969756

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 (73) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/helpers/integration/communicationChannelsFixtures.js.map +2 -2
  3. package/dist/modules/communication_channels/api/post/test-seed/route.js +40 -2
  4. package/dist/modules/communication_channels/api/post/test-seed/route.js.map +2 -2
  5. package/dist/modules/communication_channels/lib/access-control.js +6 -1
  6. package/dist/modules/communication_channels/lib/access-control.js.map +2 -2
  7. package/dist/modules/communication_channels/lib/system-user.js +8 -3
  8. package/dist/modules/communication_channels/lib/system-user.js.map +2 -2
  9. package/dist/modules/warranty_claims/api/ai/assess/route.js +20 -18
  10. package/dist/modules/warranty_claims/api/ai/assess/route.js.map +2 -2
  11. package/dist/modules/warranty_claims/api/credit-memo/route.js +11 -12
  12. package/dist/modules/warranty_claims/api/credit-memo/route.js.map +2 -2
  13. package/dist/modules/warranty_claims/api/events/route.js +1 -1
  14. package/dist/modules/warranty_claims/api/events/route.js.map +2 -2
  15. package/dist/modules/warranty_claims/api/portal/attachments/route.js +24 -27
  16. package/dist/modules/warranty_claims/api/portal/attachments/route.js.map +2 -2
  17. package/dist/modules/warranty_claims/api/portal/claims/[id]/route.js +8 -10
  18. package/dist/modules/warranty_claims/api/portal/claims/[id]/route.js.map +2 -2
  19. package/dist/modules/warranty_claims/api/portal/claims/[id]/shared.js +4 -9
  20. package/dist/modules/warranty_claims/api/portal/claims/[id]/shared.js.map +2 -2
  21. package/dist/modules/warranty_claims/api/portal/claims/[id]/submit/route.js +3 -3
  22. package/dist/modules/warranty_claims/api/portal/claims/[id]/submit/route.js.map +2 -2
  23. package/dist/modules/warranty_claims/api/portal/claims/[id]/withdraw/route.js +3 -3
  24. package/dist/modules/warranty_claims/api/portal/claims/[id]/withdraw/route.js.map +2 -2
  25. package/dist/modules/warranty_claims/api/portal/claims/route.js +4 -9
  26. package/dist/modules/warranty_claims/api/portal/claims/route.js.map +2 -2
  27. package/dist/modules/warranty_claims/api/portal/events/route.js +12 -13
  28. package/dist/modules/warranty_claims/api/portal/events/route.js.map +2 -2
  29. package/dist/modules/warranty_claims/api/receiving/route.js +14 -15
  30. package/dist/modules/warranty_claims/api/receiving/route.js.map +2 -2
  31. package/dist/modules/warranty_claims/api/registrations/route.js +9 -4
  32. package/dist/modules/warranty_claims/api/registrations/route.js.map +2 -2
  33. package/dist/modules/warranty_claims/api/replacement-order/route.js +11 -12
  34. package/dist/modules/warranty_claims/api/replacement-order/route.js.map +2 -2
  35. package/dist/modules/warranty_claims/api/return-label/route.js +20 -19
  36. package/dist/modules/warranty_claims/api/return-label/route.js.map +2 -2
  37. package/dist/modules/warranty_claims/api/risk/route.js +2 -1
  38. package/dist/modules/warranty_claims/api/risk/route.js.map +2 -2
  39. package/dist/modules/warranty_claims/api/sales-return/route.js +11 -12
  40. package/dist/modules/warranty_claims/api/sales-return/route.js.map +2 -2
  41. package/dist/modules/warranty_claims/api/troubleshooting-guides/route.js +22 -1
  42. package/dist/modules/warranty_claims/api/troubleshooting-guides/route.js.map +2 -2
  43. package/dist/modules/warranty_claims/api/vendor-recovery-suggestions/route.js +2 -1
  44. package/dist/modules/warranty_claims/api/vendor-recovery-suggestions/route.js.map +2 -2
  45. package/dist/modules/warranty_claims/commands/shared.js +2 -2
  46. package/dist/modules/warranty_claims/commands/shared.js.map +2 -2
  47. package/dist/modules/warranty_claims/lib/portalAuthGuard.js +31 -0
  48. package/dist/modules/warranty_claims/lib/portalAuthGuard.js.map +7 -0
  49. package/package.json +7 -7
  50. package/src/helpers/integration/communicationChannelsFixtures.ts +13 -0
  51. package/src/modules/communication_channels/api/post/test-seed/route.ts +47 -2
  52. package/src/modules/communication_channels/lib/access-control.ts +28 -0
  53. package/src/modules/communication_channels/lib/system-user.ts +40 -22
  54. package/src/modules/warranty_claims/api/ai/assess/route.ts +23 -17
  55. package/src/modules/warranty_claims/api/credit-memo/route.ts +14 -13
  56. package/src/modules/warranty_claims/api/events/route.ts +1 -1
  57. package/src/modules/warranty_claims/api/portal/attachments/route.ts +29 -27
  58. package/src/modules/warranty_claims/api/portal/claims/[id]/route.ts +9 -10
  59. package/src/modules/warranty_claims/api/portal/claims/[id]/shared.ts +5 -9
  60. package/src/modules/warranty_claims/api/portal/claims/[id]/submit/route.ts +3 -3
  61. package/src/modules/warranty_claims/api/portal/claims/[id]/withdraw/route.ts +3 -3
  62. package/src/modules/warranty_claims/api/portal/claims/route.ts +5 -9
  63. package/src/modules/warranty_claims/api/portal/events/route.ts +13 -13
  64. package/src/modules/warranty_claims/api/receiving/route.ts +17 -15
  65. package/src/modules/warranty_claims/api/registrations/route.ts +9 -3
  66. package/src/modules/warranty_claims/api/replacement-order/route.ts +14 -13
  67. package/src/modules/warranty_claims/api/return-label/route.ts +21 -17
  68. package/src/modules/warranty_claims/api/risk/route.ts +3 -1
  69. package/src/modules/warranty_claims/api/sales-return/route.ts +14 -13
  70. package/src/modules/warranty_claims/api/troubleshooting-guides/route.ts +23 -1
  71. package/src/modules/warranty_claims/api/vendor-recovery-suggestions/route.ts +3 -1
  72. package/src/modules/warranty_claims/commands/shared.ts +7 -1
  73. package/src/modules/warranty_claims/lib/portalAuthGuard.ts +30 -0
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../../src/modules/warranty_claims/api/portal/attachments/route.ts"],
4
- "sourcesContent": ["import { NextResponse } from 'next/server'\nimport { z } from 'zod'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport type { AuthContext } from '@open-mercato/shared/lib/auth/server'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { runRouteMutationGuards, type RouteMutationGuardResult } from '@open-mercato/shared/lib/crud/route-mutation-guard'\nimport { emitCrudSideEffects } from '@open-mercato/shared/lib/commands/helpers'\nimport { findOneWithDecryption, findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport { getCustomerAuthFromRequest, type CustomerAuthContext } from '@open-mercato/core/modules/customer_accounts/lib/customerAuth'\nimport { Attachment, AttachmentPartition } from '@open-mercato/core/modules/attachments/data/entities'\nimport { buildAttachmentImageUrl, slugifyAttachmentFileName } from '@open-mercato/core/modules/attachments/lib/imageUrls'\nimport { StorageDriverFactory } from '@open-mercato/core/modules/attachments/lib/drivers'\nimport { checkAttachmentAccess } from '@open-mercato/core/modules/attachments/lib/access'\nimport { attachmentCrudEvents, attachmentCrudIndexer } from '@open-mercato/core/modules/attachments/lib/crud'\nimport { readAttachmentMetadata } from '@open-mercato/core/modules/attachments/lib/metadata'\nimport { clearAttachmentThumbnailCache } from '@open-mercato/core/modules/attachments/lib/thumbnailCache'\nimport { isMultipartRequestWithinUploadLimit } from '@open-mercato/core/modules/attachments/lib/upload-limits'\nimport {\n isScopedAttachmentUploadError,\n type ScopedAttachmentUploadErrorCode,\n} from '@open-mercato/core/modules/attachments/lib/scoped-upload-service'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport { WarrantyClaim } from '../../../data/entities'\nimport { WARRANTY_CLAIM_RESOURCE_KIND } from '../../../commands/shared'\nimport { CUSTOMER_VISIBLE_ATTACHMENT_TAG, isCustomerVisibleAttachment } from '../../../lib/attachmentVisibility'\nimport { loadPortalOwnedClaim } from '../../../lib/portalClaimAccess'\nimport { resolvePortalAttachmentUploadService } from '../../../lib/portalAttachmentUpload'\n\nconst CLAIM_ATTACHMENT_ENTITY_ID = 'warranty_claims:warranty_claim'\nconst logger = createLogger('warranty_claims').child({ route: 'portal-attachments' })\nconst ATTACHMENT_ERROR_TRANSLATIONS: Partial<Record<ScopedAttachmentUploadErrorCode, readonly [string, string]>> = {\n dangerous_executable: ['attachments.errors.dangerousExecutable', 'Executable file types are not allowed as attachments.'],\n max_upload_size: ['attachments.errors.maxUploadSize', 'Attachment exceeds the maximum upload size.'],\n active_content: ['attachments.errors.activeContentBlocked', 'Active content uploads are not allowed.'],\n}\n\nexport const metadata = {\n GET: { requireAuth: false },\n POST: { requireAuth: false },\n PUT: { requireAuth: false },\n DELETE: { requireAuth: false },\n}\n\nconst attachmentQuerySchema = z.object({\n claimId: z.string().uuid(),\n page: z.coerce.number().int().min(1).optional(),\n pageSize: z.coerce.number().int().min(1).max(100).optional(),\n})\n\nconst uploadBodySchema = z.object({\n claimId: z.string().uuid(),\n file: z.string().min(1).describe('Binary file payload; supplied as multipart form-data'),\n})\n\nconst replaceBodySchema = uploadBodySchema.extend({\n attachmentId: z.string().uuid(),\n})\n\nconst deleteQuerySchema = z.object({\n attachmentId: z.string().uuid(),\n})\n\ntype PortalContext = {\n auth: CustomerAuthContext\n customerId: string\n tenantId: string\n organizationId: string\n em: EntityManager\n container: Awaited<ReturnType<typeof createRequestContainer>>\n}\n\nfunction toIso(value: Date | string | null | undefined): string | null {\n if (!value) return null\n if (value instanceof Date) return value.toISOString()\n const date = new Date(value)\n return Number.isNaN(date.getTime()) ? value : date.toISOString()\n}\n\nasync function resolvePortalContext(req: Request): Promise<PortalContext | Response> {\n const auth = await getCustomerAuthFromRequest(req)\n if (!auth) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.unauthorized' }, { status: 401 })\n }\n if (!auth.customerEntityId) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.customerAccountNotLinked' }, { status: 403 })\n }\n const container = await createRequestContainer()\n const em = container.resolve('em') as EntityManager\n return {\n auth,\n customerId: auth.customerEntityId,\n tenantId: auth.tenantId,\n organizationId: auth.orgId,\n em,\n container,\n }\n}\n\nfunction attachmentAuth(context: PortalContext): NonNullable<AuthContext> {\n return {\n sub: context.auth.sub,\n tenantId: context.tenantId,\n orgId: context.organizationId,\n email: context.auth.email,\n }\n}\n\nasync function loadOwnedClaim(context: PortalContext, claimId: string): Promise<WarrantyClaim | null> {\n return loadPortalOwnedClaim(\n context.em,\n {\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n customerId: context.customerId,\n },\n claimId,\n )\n}\n\nasync function runPortalAttachmentGuard(\n req: Request,\n context: PortalContext,\n claimId: string,\n operation: 'create' | 'update' | 'delete',\n mutationPayload: Record<string, unknown>,\n): Promise<RouteMutationGuardResult> {\n return runRouteMutationGuards({\n container: context.container,\n req,\n auth: {\n userId: context.auth.sub,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n userFeatures: [],\n },\n input: {\n resourceKind: WARRANTY_CLAIM_RESOURCE_KIND,\n resourceId: claimId,\n operation,\n mutationPayload,\n },\n })\n}\n\nfunction serializeAttachment(attachment: Attachment) {\n const metadata = readAttachmentMetadata(attachment.storageMetadata)\n return {\n id: attachment.id,\n url: attachment.url,\n downloadUrl: `/api/warranty_claims/portal/attachments?attachmentId=${encodeURIComponent(attachment.id)}`,\n fileName: attachment.fileName,\n fileSize: attachment.fileSize,\n mimeType: attachment.mimeType ?? null,\n partitionCode: attachment.partitionCode,\n content: attachment.content ?? null,\n thumbnailUrl: buildAttachmentImageUrl(attachment.id, {\n width: 320,\n height: 320,\n slug: slugifyAttachmentFileName(attachment.fileName),\n }),\n tags: metadata.tags ?? [],\n assignments: metadata.assignments ?? [],\n createdAt: toIso(attachment.createdAt),\n }\n}\n\nasync function resolveStorageDriverFactory(context: PortalContext): Promise<StorageDriverFactory> {\n try {\n const resolved = context.container.resolve('storageDriverFactory') as StorageDriverFactory | null\n return resolved ?? new StorageDriverFactory(context.em)\n } catch {\n return new StorageDriverFactory(context.em)\n }\n}\n\nasync function loadOwnedVisibleAttachment(\n context: PortalContext,\n attachmentId: string,\n): Promise<{ attachment: Attachment; claim: WarrantyClaim } | null> {\n const scope = { tenantId: context.tenantId, organizationId: context.organizationId }\n const attachment = await findOneWithDecryption(\n context.em,\n Attachment,\n {\n id: attachmentId,\n entityId: CLAIM_ATTACHMENT_ENTITY_ID,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n },\n {},\n scope,\n )\n if (!attachment) return null\n const claim = await loadOwnedClaim(context, attachment.recordId)\n if (!claim) return null\n if (!isCustomerVisibleAttachment(readAttachmentMetadata(attachment.storageMetadata).tags)) return null\n return { attachment, claim }\n}\n\nasync function deletePortalAttachment(context: PortalContext, attachment: Attachment): Promise<void> {\n await context.em.remove(attachment).flush()\n await clearAttachmentThumbnailCache(attachment.partitionCode, attachment.id).catch((error) => {\n logger.error('Failed to clean portal attachment thumbnails', { err: error, attachmentId: attachment.id })\n })\n if (attachment.storagePath) {\n try {\n const driver = await (await resolveStorageDriverFactory(context)).resolveForPartition(attachment.partitionCode, {\n tenantId: attachment.tenantId ?? context.tenantId,\n organizationId: attachment.organizationId ?? context.organizationId,\n })\n await driver.delete(attachment.partitionCode, attachment.storagePath)\n } catch (error) {\n logger.error('Failed to remove portal attachment storage', { err: error, attachmentId: attachment.id })\n }\n }\n\n let dataEngine: DataEngine | null = null\n try {\n dataEngine = context.container.resolve<DataEngine>('dataEngine') ?? null\n } catch {\n dataEngine = null\n }\n if (dataEngine) {\n await emitCrudSideEffects({\n dataEngine,\n action: 'deleted',\n entity: attachment,\n identifiers: {\n id: attachment.id,\n organizationId: attachment.organizationId ?? null,\n tenantId: attachment.tenantId ?? null,\n },\n events: attachmentCrudEvents,\n indexer: attachmentCrudIndexer,\n })\n await dataEngine.flushOrmEntityChanges()\n }\n}\n\nasync function streamOwnedAttachment(context: PortalContext, attachmentId: string): Promise<Response> {\n const owned = await loadOwnedVisibleAttachment(context, attachmentId)\n if (!owned) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.notFound' }, { status: 404 })\n }\n const { attachment } = owned\n const partition = await context.em.findOne(AttachmentPartition, { code: attachment.partitionCode })\n if (!partition) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.load_failed' }, { status: 500 })\n }\n const access = checkAttachmentAccess(attachmentAuth(context), attachment, partition, { requireAuthForPublic: true })\n if (!access.ok) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.notFound' }, { status: 404 })\n }\n const driver = await (await resolveStorageDriverFactory(context)).resolveForPartition(attachment.partitionCode, {\n tenantId: attachment.tenantId ?? '',\n organizationId: attachment.organizationId ?? '',\n })\n let buffer: Buffer\n try {\n buffer = (await driver.read(attachment.partitionCode, attachment.storagePath)).buffer\n } catch {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.notFound' }, { status: 404 })\n }\n const headers: Record<string, string> = {\n 'Cache-Control': 'private, max-age=60',\n 'Content-Security-Policy': \"default-src 'none'; sandbox\",\n 'Content-Type': 'application/octet-stream',\n 'Content-Disposition': `attachment; filename=\"${slugifyAttachmentFileName(attachment.fileName)}\"`,\n 'X-Content-Type-Options': 'nosniff',\n }\n if (attachment.fileSize > 0) headers['Content-Length'] = String(attachment.fileSize)\n return new NextResponse(new Uint8Array(buffer), { status: 200, headers })\n}\n\nexport async function GET(req: Request) {\n const contextOrResponse = await resolvePortalContext(req)\n if (contextOrResponse instanceof Response) return contextOrResponse\n const context = contextOrResponse\n const url = new URL(req.url)\n const attachmentId = url.searchParams.get('attachmentId')\n if (attachmentId) {\n const attachmentIdParsed = z.string().uuid().safeParse(attachmentId)\n if (!attachmentIdParsed.success) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.invalidInput' }, { status: 400 })\n }\n return streamOwnedAttachment(context, attachmentIdParsed.data)\n }\n const parsed = attachmentQuerySchema.safeParse({\n claimId: url.searchParams.get('claimId') ?? undefined,\n page: url.searchParams.get('page') ?? undefined,\n pageSize: url.searchParams.get('pageSize') ?? undefined,\n })\n if (!parsed.success) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.invalidInput' }, { status: 400 })\n }\n const claim = await loadOwnedClaim(context, parsed.data.claimId)\n if (!claim) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.notFound' }, { status: 404 })\n }\n const filter = {\n entityId: CLAIM_ATTACHMENT_ENTITY_ID,\n recordId: claim.id,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n }\n const page = parsed.data.page ?? 1\n const pageSize = parsed.data.pageSize ?? 100\n const attachments = await findWithDecryption(\n context.em,\n Attachment,\n filter,\n { orderBy: { createdAt: 'DESC' } },\n { tenantId: context.tenantId, organizationId: context.organizationId },\n )\n const partitionCodes = Array.from(new Set(attachments.map((attachment) => attachment.partitionCode)))\n const partitions = partitionCodes.length\n ? await context.em.find(AttachmentPartition, { code: { $in: partitionCodes } })\n : []\n const partitionsByCode = new Map(partitions.map((partition) => [partition.code, partition]))\n const auth = attachmentAuth(context)\n const visible = attachments.filter((attachment) => {\n const partition = partitionsByCode.get(attachment.partitionCode)\n if (!partition) return false\n if (!isCustomerVisibleAttachment(readAttachmentMetadata(attachment.storageMetadata).tags)) return false\n return checkAttachmentAccess(auth, attachment, partition, { requireAuthForPublic: true }).ok\n })\n\n const pagedVisible = visible.slice((page - 1) * pageSize, page * pageSize)\n\n return NextResponse.json({\n items: pagedVisible.map(serializeAttachment),\n total: visible.length,\n page,\n pageSize,\n totalPages: Math.max(1, Math.ceil(visible.length / pageSize)),\n })\n}\n\nasync function handleUpload(req: Request, replacement: boolean): Promise<Response> {\n const contextOrResponse = await resolvePortalContext(req)\n if (contextOrResponse instanceof Response) return contextOrResponse\n const context = contextOrResponse\n const contentType = req.headers.get('content-type') || ''\n if (!contentType.toLowerCase().includes('multipart/form-data')) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.invalidInput' }, { status: 400 })\n }\n if (!isMultipartRequestWithinUploadLimit(req.headers.get('content-length'))) {\n const { t } = await resolveTranslations()\n return NextResponse.json({\n ok: false,\n error: t('attachments.errors.maxUploadSize', 'Attachment exceeds the maximum upload size.'),\n }, { status: 413 })\n }\n\n const form = await req.formData()\n const parsed = uploadBodySchema.safeParse({\n claimId: form.get('claimId') ?? undefined,\n file: form.get('file') instanceof File ? (form.get('file') as File).name : '',\n })\n const parsedAttachmentId = replacement\n ? z.string().uuid().safeParse(form.get('attachmentId') ?? undefined)\n : null\n if (!parsed.success || (replacement && !parsedAttachmentId?.success)) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.invalidInput' }, { status: 400 })\n }\n const claim = await loadOwnedClaim(context, parsed.data.claimId)\n if (!claim) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.notFound' }, { status: 404 })\n }\n const file = form.get('file')\n if (!(file instanceof File)) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.portal.detail.fileRequired' }, { status: 400 })\n }\n const buffer = Buffer.from(await file.arrayBuffer())\n\n const attachmentId = replacement && parsedAttachmentId?.success ? parsedAttachmentId.data : null\n const replacementTarget = attachmentId\n ? await loadOwnedVisibleAttachment(context, attachmentId)\n : null\n if (replacement && (!replacementTarget || replacementTarget.claim.id !== claim.id)) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.notFound' }, { status: 404 })\n }\n\n const guarded = await runPortalAttachmentGuard(req, context, claim.id, replacement ? 'update' : 'create', {\n claimId: claim.id,\n attachmentId,\n fileName: file.name,\n fileSize: file.size,\n mimeType: file.type,\n })\n if (!guarded.ok) {\n return guarded.response\n }\n\n const uploadService = resolvePortalAttachmentUploadService(context.container)\n if (!uploadService) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.portal.detail.attachmentError' }, { status: 503 })\n }\n\n let attachment: Attachment\n try {\n attachment = await uploadService.upload({\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n entityId: CLAIM_ATTACHMENT_ENTITY_ID,\n recordId: claim.id,\n fileName: file.name,\n buffer,\n declaredMimeType: file.type,\n tags: [CUSTOMER_VISIBLE_ATTACHMENT_TAG],\n })\n } catch (error) {\n if (isScopedAttachmentUploadError(error)) {\n const { t } = await resolveTranslations()\n const [key, fallback] = ATTACHMENT_ERROR_TRANSLATIONS[error.code]\n ?? ['warranty_claims.portal.detail.attachmentError', 'Attachment upload failed.']\n return NextResponse.json(\n { ok: false, error: t(key, fallback), code: error.code },\n { status: error.status },\n )\n }\n throw error\n }\n\n if (replacementTarget) {\n await deletePortalAttachment(context, replacementTarget.attachment)\n }\n await guarded.runAfterSuccess()\n\n return NextResponse.json({\n ok: true,\n item: {\n ...serializeAttachment(attachment),\n entityType: 'attachments:attachment',\n },\n })\n}\n\nexport async function POST(req: Request) {\n return handleUpload(req, false)\n}\n\nexport async function PUT(req: Request) {\n return handleUpload(req, true)\n}\n\nexport async function DELETE(req: Request) {\n const contextOrResponse = await resolvePortalContext(req)\n if (contextOrResponse instanceof Response) return contextOrResponse\n const context = contextOrResponse\n const url = new URL(req.url)\n const parsed = deleteQuerySchema.safeParse({\n attachmentId: url.searchParams.get('attachmentId') ?? undefined,\n })\n if (!parsed.success) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.invalidInput' }, { status: 400 })\n }\n const owned = await loadOwnedVisibleAttachment(context, parsed.data.attachmentId)\n if (!owned) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.notFound' }, { status: 404 })\n }\n const guarded = await runPortalAttachmentGuard(req, context, owned.claim.id, 'delete', {\n claimId: owned.claim.id,\n attachmentId: owned.attachment.id,\n })\n if (!guarded.ok) return guarded.response\n\n await deletePortalAttachment(context, owned.attachment)\n await guarded.runAfterSuccess()\n return NextResponse.json({ ok: true })\n}\n\nconst assignmentSchema = z.object({\n type: z.string(),\n id: z.string(),\n href: z.string().nullable().optional(),\n label: z.string().nullable().optional(),\n})\n\nconst attachmentItemSchema = z.object({\n id: z.string(),\n url: z.string(),\n downloadUrl: z.string(),\n fileName: z.string(),\n fileSize: z.number().int().nonnegative(),\n mimeType: z.string().nullable(),\n partitionCode: z.string(),\n content: z.string().nullable(),\n thumbnailUrl: z.string(),\n tags: z.array(z.string()),\n assignments: z.array(assignmentSchema),\n createdAt: z.string().nullable(),\n})\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Warranty Claims Portal',\n summary: 'Customer portal warranty claim attachments',\n methods: {\n GET: {\n summary: 'List attachments for an owned claim',\n query: attachmentQuerySchema,\n responses: [\n {\n status: 200,\n description: 'Claim attachments',\n schema: z.object({ items: z.array(attachmentItemSchema) }),\n },\n ],\n },\n POST: {\n summary: 'Upload an attachment for an owned claim',\n requestBody: { contentType: 'multipart/form-data', schema: uploadBodySchema },\n responses: [\n {\n status: 200,\n description: 'Attachment uploaded',\n schema: z.object({ ok: z.boolean(), item: attachmentItemSchema.extend({ entityType: z.string() }) }),\n },\n ],\n },\n PUT: {\n summary: 'Replace an attachment on an owned claim',\n requestBody: { contentType: 'multipart/form-data', schema: replaceBodySchema },\n responses: [\n {\n status: 200,\n description: 'Attachment replaced',\n schema: z.object({ ok: z.boolean(), item: attachmentItemSchema.extend({ entityType: z.string() }) }),\n },\n ],\n },\n DELETE: {\n summary: 'Delete an attachment from an owned claim',\n query: deleteQuerySchema,\n responses: [\n {\n status: 200,\n description: 'Attachment deleted',\n schema: z.object({ ok: z.literal(true) }),\n },\n ],\n },\n },\n}\n"],
5
- "mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAGlB,SAAS,8BAA8B;AAEvC,SAAS,2BAA2B;AACpC,SAAS,8BAA6D;AACtE,SAAS,2BAA2B;AACpC,SAAS,uBAAuB,0BAA0B;AAC1D,SAAS,oBAAoB;AAC7B,SAAS,kCAA4D;AACrE,SAAS,YAAY,2BAA2B;AAChD,SAAS,yBAAyB,iCAAiC;AACnE,SAAS,4BAA4B;AACrC,SAAS,6BAA6B;AACtC,SAAS,sBAAsB,6BAA6B;AAC5D,SAAS,8BAA8B;AACvC,SAAS,qCAAqC;AAC9C,SAAS,2CAA2C;AACpD;AAAA,EACE;AAAA,OAEK;AAGP,SAAS,oCAAoC;AAC7C,SAAS,iCAAiC,mCAAmC;AAC7E,SAAS,4BAA4B;AACrC,SAAS,4CAA4C;AAErD,MAAM,6BAA6B;AACnC,MAAM,SAAS,aAAa,iBAAiB,EAAE,MAAM,EAAE,OAAO,qBAAqB,CAAC;AACpF,MAAM,gCAA6G;AAAA,EACjH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ACA,KAAK;AAAA,MACH,SAAS;AAAA,MACT,aAAa,EAAE,aAAa,uBAAuB,QAAQ,kBAAkB;AAAA,MAC7E,WAAW;AAAA,QACT;AAAA,UACE,QAAQ;AAAA,UACR,aAAa;AAAA,UACb,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,GAAG,MAAM,qBAAqB,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;AAAA,QACrG;AAAA,MACF;AAAA,IACF;AAAA,IACA,QAAQ;AAAA,MACN,SAAS;AAAA,MACT,OAAO;AAAA,MACP,WAAW;AAAA,QACT;AAAA,UACE,QAAQ;AAAA,UACR,aAAa;AAAA,UACb,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,IAAI,EAAE,CAAC;AAAA,QAC1C;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import { NextResponse } from 'next/server'\nimport { z } from 'zod'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport type { AuthContext } from '@open-mercato/shared/lib/auth/server'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport type { DataEngine } from '@open-mercato/shared/lib/data/engine'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { runRouteMutationGuards, type RouteMutationGuardResult } from '@open-mercato/shared/lib/crud/route-mutation-guard'\nimport { emitCrudSideEffects } from '@open-mercato/shared/lib/commands/helpers'\nimport { findOneWithDecryption, findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport type { CustomerAuthContext } from '@open-mercato/core/modules/customer_accounts/lib/customerAuth'\nimport { Attachment, AttachmentPartition } from '@open-mercato/core/modules/attachments/data/entities'\nimport { buildAttachmentImageUrl, slugifyAttachmentFileName } from '@open-mercato/core/modules/attachments/lib/imageUrls'\nimport { StorageDriverFactory } from '@open-mercato/core/modules/attachments/lib/drivers'\nimport { checkAttachmentAccess } from '@open-mercato/core/modules/attachments/lib/access'\nimport { attachmentCrudEvents, attachmentCrudIndexer } from '@open-mercato/core/modules/attachments/lib/crud'\nimport { readAttachmentMetadata } from '@open-mercato/core/modules/attachments/lib/metadata'\nimport { clearAttachmentThumbnailCache } from '@open-mercato/core/modules/attachments/lib/thumbnailCache'\nimport { isMultipartRequestWithinUploadLimit } from '@open-mercato/core/modules/attachments/lib/upload-limits'\nimport {\n isScopedAttachmentUploadError,\n type ScopedAttachmentUploadErrorCode,\n} from '@open-mercato/core/modules/attachments/lib/scoped-upload-service'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport { WarrantyClaim } from '../../../data/entities'\nimport { WARRANTY_CLAIM_RESOURCE_KIND } from '../../../commands/shared'\nimport { CUSTOMER_VISIBLE_ATTACHMENT_TAG, isCustomerVisibleAttachment } from '../../../lib/attachmentVisibility'\nimport { loadPortalOwnedClaim } from '../../../lib/portalClaimAccess'\nimport { resolvePortalAttachmentUploadService } from '../../../lib/portalAttachmentUpload'\nimport { resolveLinkedCustomerAuth } from '../../../lib/portalAuthGuard'\n\nconst CLAIM_ATTACHMENT_ENTITY_ID = 'warranty_claims:warranty_claim'\nconst logger = createLogger('warranty_claims').child({ route: 'portal-attachments' })\nconst ATTACHMENT_ERROR_TRANSLATIONS: Partial<Record<ScopedAttachmentUploadErrorCode, readonly [string, string]>> = {\n dangerous_executable: ['attachments.errors.dangerousExecutable', 'Executable file types are not allowed as attachments.'],\n max_upload_size: ['attachments.errors.maxUploadSize', 'Attachment exceeds the maximum upload size.'],\n active_content: ['attachments.errors.activeContentBlocked', 'Active content uploads are not allowed.'],\n}\n\nexport const metadata = {\n GET: { requireAuth: false },\n POST: { requireAuth: false },\n PUT: { requireAuth: false },\n DELETE: { requireAuth: false },\n}\n\nconst attachmentQuerySchema = z.object({\n claimId: z.string().uuid(),\n page: z.coerce.number().int().min(1).optional(),\n pageSize: z.coerce.number().int().min(1).max(100).optional(),\n})\n\nconst uploadBodySchema = z.object({\n claimId: z.string().uuid(),\n file: z.string().min(1).describe('Binary file payload; supplied as multipart form-data'),\n})\n\nconst replaceBodySchema = uploadBodySchema.extend({\n attachmentId: z.string().uuid(),\n})\n\nconst deleteQuerySchema = z.object({\n attachmentId: z.string().uuid(),\n})\n\ntype PortalContext = {\n auth: CustomerAuthContext\n customerId: string\n tenantId: string\n organizationId: string\n em: EntityManager\n container: Awaited<ReturnType<typeof createRequestContainer>>\n}\n\nfunction toIso(value: Date | string | null | undefined): string | null {\n if (!value) return null\n if (value instanceof Date) return value.toISOString()\n const date = new Date(value)\n return Number.isNaN(date.getTime()) ? value : date.toISOString()\n}\n\nasync function resolvePortalContext(req: Request): Promise<PortalContext | Response> {\n const authOrResponse = await resolveLinkedCustomerAuth(req)\n if (authOrResponse instanceof Response) return authOrResponse\n const auth = authOrResponse\n const container = await createRequestContainer()\n const em = container.resolve('em') as EntityManager\n return {\n auth,\n customerId: auth.customerEntityId,\n tenantId: auth.tenantId,\n organizationId: auth.orgId,\n em,\n container,\n }\n}\n\nfunction attachmentAuth(context: PortalContext): NonNullable<AuthContext> {\n return {\n sub: context.auth.sub,\n tenantId: context.tenantId,\n orgId: context.organizationId,\n email: context.auth.email,\n }\n}\n\nasync function loadOwnedClaim(context: PortalContext, claimId: string): Promise<WarrantyClaim | null> {\n return loadPortalOwnedClaim(\n context.em,\n {\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n customerId: context.customerId,\n },\n claimId,\n )\n}\n\nasync function runPortalAttachmentGuard(\n req: Request,\n context: PortalContext,\n claimId: string,\n operation: 'create' | 'update' | 'delete',\n mutationPayload: Record<string, unknown>,\n): Promise<RouteMutationGuardResult> {\n return runRouteMutationGuards({\n container: context.container,\n req,\n auth: {\n userId: context.auth.sub,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n userFeatures: [],\n },\n input: {\n resourceKind: WARRANTY_CLAIM_RESOURCE_KIND,\n resourceId: claimId,\n operation,\n mutationPayload,\n },\n })\n}\n\nfunction serializeAttachment(attachment: Attachment) {\n const metadata = readAttachmentMetadata(attachment.storageMetadata)\n return {\n id: attachment.id,\n url: attachment.url,\n downloadUrl: `/api/warranty_claims/portal/attachments?attachmentId=${encodeURIComponent(attachment.id)}`,\n fileName: attachment.fileName,\n fileSize: attachment.fileSize,\n mimeType: attachment.mimeType ?? null,\n partitionCode: attachment.partitionCode,\n content: attachment.content ?? null,\n thumbnailUrl: buildAttachmentImageUrl(attachment.id, {\n width: 320,\n height: 320,\n slug: slugifyAttachmentFileName(attachment.fileName),\n }),\n tags: metadata.tags ?? [],\n assignments: metadata.assignments ?? [],\n createdAt: toIso(attachment.createdAt),\n }\n}\n\nasync function resolveStorageDriverFactory(context: PortalContext): Promise<StorageDriverFactory> {\n try {\n const resolved = context.container.resolve('storageDriverFactory') as StorageDriverFactory | null\n return resolved ?? new StorageDriverFactory(context.em)\n } catch {\n return new StorageDriverFactory(context.em)\n }\n}\n\nasync function loadOwnedVisibleAttachment(\n context: PortalContext,\n attachmentId: string,\n): Promise<{ attachment: Attachment; claim: WarrantyClaim } | null> {\n const scope = { tenantId: context.tenantId, organizationId: context.organizationId }\n const attachment = await findOneWithDecryption(\n context.em,\n Attachment,\n {\n id: attachmentId,\n entityId: CLAIM_ATTACHMENT_ENTITY_ID,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n },\n {},\n scope,\n )\n if (!attachment) return null\n const claim = await loadOwnedClaim(context, attachment.recordId)\n if (!claim) return null\n if (!isCustomerVisibleAttachment(readAttachmentMetadata(attachment.storageMetadata).tags)) return null\n return { attachment, claim }\n}\n\nasync function deletePortalAttachment(context: PortalContext, attachment: Attachment): Promise<void> {\n await context.em.remove(attachment).flush()\n await clearAttachmentThumbnailCache(attachment.partitionCode, attachment.id).catch((error) => {\n logger.error('Failed to clean portal attachment thumbnails', { err: error, attachmentId: attachment.id })\n })\n if (attachment.storagePath) {\n try {\n const driver = await (await resolveStorageDriverFactory(context)).resolveForPartition(attachment.partitionCode, {\n tenantId: attachment.tenantId ?? context.tenantId,\n organizationId: attachment.organizationId ?? context.organizationId,\n })\n await driver.delete(attachment.partitionCode, attachment.storagePath)\n } catch (error) {\n logger.error('Failed to remove portal attachment storage', { err: error, attachmentId: attachment.id })\n }\n }\n\n let dataEngine: DataEngine | null = null\n try {\n dataEngine = context.container.resolve<DataEngine>('dataEngine') ?? null\n } catch {\n dataEngine = null\n }\n if (dataEngine) {\n await emitCrudSideEffects({\n dataEngine,\n action: 'deleted',\n entity: attachment,\n identifiers: {\n id: attachment.id,\n organizationId: attachment.organizationId ?? null,\n tenantId: attachment.tenantId ?? null,\n },\n events: attachmentCrudEvents,\n indexer: attachmentCrudIndexer,\n })\n await dataEngine.flushOrmEntityChanges()\n }\n}\n\nasync function streamOwnedAttachment(\n context: PortalContext,\n attachmentId: string,\n translate: (key: string, fallback?: string) => string,\n): Promise<Response> {\n const owned = await loadOwnedVisibleAttachment(context, attachmentId)\n if (!owned) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.notFound', 'Claim not found.') }, { status: 404 })\n }\n const { attachment } = owned\n const partition = await context.em.findOne(AttachmentPartition, { code: attachment.partitionCode })\n if (!partition) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.load_failed', 'Failed to load warranty claim data.') }, { status: 500 })\n }\n const access = checkAttachmentAccess(attachmentAuth(context), attachment, partition, { requireAuthForPublic: true })\n if (!access.ok) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.notFound', 'Claim not found.') }, { status: 404 })\n }\n const driver = await (await resolveStorageDriverFactory(context)).resolveForPartition(attachment.partitionCode, {\n tenantId: attachment.tenantId ?? '',\n organizationId: attachment.organizationId ?? '',\n })\n let buffer: Buffer\n try {\n buffer = (await driver.read(attachment.partitionCode, attachment.storagePath)).buffer\n } catch {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.notFound', 'Claim not found.') }, { status: 404 })\n }\n const headers: Record<string, string> = {\n 'Cache-Control': 'private, max-age=60',\n 'Content-Security-Policy': \"default-src 'none'; sandbox\",\n 'Content-Type': 'application/octet-stream',\n 'Content-Disposition': `attachment; filename=\"${slugifyAttachmentFileName(attachment.fileName)}\"`,\n 'X-Content-Type-Options': 'nosniff',\n }\n if (attachment.fileSize > 0) headers['Content-Length'] = String(attachment.fileSize)\n return new NextResponse(new Uint8Array(buffer), { status: 200, headers })\n}\n\nexport async function GET(req: Request) {\n const contextOrResponse = await resolvePortalContext(req)\n if (contextOrResponse instanceof Response) return contextOrResponse\n const context = contextOrResponse\n const { translate } = await resolveTranslations()\n const url = new URL(req.url)\n const attachmentId = url.searchParams.get('attachmentId')\n if (attachmentId) {\n const attachmentIdParsed = z.string().uuid().safeParse(attachmentId)\n if (!attachmentIdParsed.success) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.invalidInput', 'Invalid input') }, { status: 400 })\n }\n return streamOwnedAttachment(context, attachmentIdParsed.data, translate)\n }\n const parsed = attachmentQuerySchema.safeParse({\n claimId: url.searchParams.get('claimId') ?? undefined,\n page: url.searchParams.get('page') ?? undefined,\n pageSize: url.searchParams.get('pageSize') ?? undefined,\n })\n if (!parsed.success) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.invalidInput', 'Invalid input') }, { status: 400 })\n }\n const claim = await loadOwnedClaim(context, parsed.data.claimId)\n if (!claim) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.notFound', 'Claim not found.') }, { status: 404 })\n }\n const filter = {\n entityId: CLAIM_ATTACHMENT_ENTITY_ID,\n recordId: claim.id,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n }\n const page = parsed.data.page ?? 1\n const pageSize = parsed.data.pageSize ?? 100\n const attachments = await findWithDecryption(\n context.em,\n Attachment,\n filter,\n { orderBy: { createdAt: 'DESC' } },\n { tenantId: context.tenantId, organizationId: context.organizationId },\n )\n const partitionCodes = Array.from(new Set(attachments.map((attachment) => attachment.partitionCode)))\n const partitions = partitionCodes.length\n ? await context.em.find(AttachmentPartition, { code: { $in: partitionCodes } })\n : []\n const partitionsByCode = new Map(partitions.map((partition) => [partition.code, partition]))\n const auth = attachmentAuth(context)\n const visible = attachments.filter((attachment) => {\n const partition = partitionsByCode.get(attachment.partitionCode)\n if (!partition) return false\n if (!isCustomerVisibleAttachment(readAttachmentMetadata(attachment.storageMetadata).tags)) return false\n return checkAttachmentAccess(auth, attachment, partition, { requireAuthForPublic: true }).ok\n })\n\n const pagedVisible = visible.slice((page - 1) * pageSize, page * pageSize)\n\n return NextResponse.json({\n items: pagedVisible.map(serializeAttachment),\n total: visible.length,\n page,\n pageSize,\n totalPages: Math.max(1, Math.ceil(visible.length / pageSize)),\n })\n}\n\nasync function handleUpload(req: Request, replacement: boolean): Promise<Response> {\n const contextOrResponse = await resolvePortalContext(req)\n if (contextOrResponse instanceof Response) return contextOrResponse\n const context = contextOrResponse\n const { t, translate } = await resolveTranslations()\n const contentType = req.headers.get('content-type') || ''\n if (!contentType.toLowerCase().includes('multipart/form-data')) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.invalidInput', 'Invalid input') }, { status: 400 })\n }\n if (!isMultipartRequestWithinUploadLimit(req.headers.get('content-length'))) {\n return NextResponse.json({\n ok: false,\n error: t('attachments.errors.maxUploadSize', 'Attachment exceeds the maximum upload size.'),\n }, { status: 413 })\n }\n\n const form = await req.formData()\n const parsed = uploadBodySchema.safeParse({\n claimId: form.get('claimId') ?? undefined,\n file: form.get('file') instanceof File ? (form.get('file') as File).name : '',\n })\n const parsedAttachmentId = replacement\n ? z.string().uuid().safeParse(form.get('attachmentId') ?? undefined)\n : null\n if (!parsed.success || (replacement && !parsedAttachmentId?.success)) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.invalidInput', 'Invalid input') }, { status: 400 })\n }\n const claim = await loadOwnedClaim(context, parsed.data.claimId)\n if (!claim) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.notFound', 'Claim not found.') }, { status: 404 })\n }\n const file = form.get('file')\n if (!(file instanceof File)) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.portal.detail.fileRequired', 'Choose a file before uploading.') }, { status: 400 })\n }\n const buffer = Buffer.from(await file.arrayBuffer())\n\n const attachmentId = replacement && parsedAttachmentId?.success ? parsedAttachmentId.data : null\n const replacementTarget = attachmentId\n ? await loadOwnedVisibleAttachment(context, attachmentId)\n : null\n if (replacement && (!replacementTarget || replacementTarget.claim.id !== claim.id)) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.notFound', 'Claim not found.') }, { status: 404 })\n }\n\n const guarded = await runPortalAttachmentGuard(req, context, claim.id, replacement ? 'update' : 'create', {\n claimId: claim.id,\n attachmentId,\n fileName: file.name,\n fileSize: file.size,\n mimeType: file.type,\n })\n if (!guarded.ok) {\n return guarded.response\n }\n\n const uploadService = resolvePortalAttachmentUploadService(context.container)\n if (!uploadService) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.portal.detail.attachmentError', 'Attachment upload failed.') }, { status: 503 })\n }\n\n let attachment: Attachment\n try {\n attachment = await uploadService.upload({\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n entityId: CLAIM_ATTACHMENT_ENTITY_ID,\n recordId: claim.id,\n fileName: file.name,\n buffer,\n declaredMimeType: file.type,\n tags: [CUSTOMER_VISIBLE_ATTACHMENT_TAG],\n })\n } catch (error) {\n if (isScopedAttachmentUploadError(error)) {\n const [key, fallback] = ATTACHMENT_ERROR_TRANSLATIONS[error.code]\n ?? ['warranty_claims.portal.detail.attachmentError', 'Attachment upload failed.']\n return NextResponse.json(\n { ok: false, error: t(key, fallback), code: error.code },\n { status: error.status },\n )\n }\n throw error\n }\n\n if (replacementTarget) {\n await deletePortalAttachment(context, replacementTarget.attachment)\n }\n await guarded.runAfterSuccess()\n\n return NextResponse.json({\n ok: true,\n item: {\n ...serializeAttachment(attachment),\n entityType: 'attachments:attachment',\n },\n })\n}\n\nexport async function POST(req: Request) {\n return handleUpload(req, false)\n}\n\nexport async function PUT(req: Request) {\n return handleUpload(req, true)\n}\n\nexport async function DELETE(req: Request) {\n const contextOrResponse = await resolvePortalContext(req)\n if (contextOrResponse instanceof Response) return contextOrResponse\n const context = contextOrResponse\n const { translate } = await resolveTranslations()\n const url = new URL(req.url)\n const parsed = deleteQuerySchema.safeParse({\n attachmentId: url.searchParams.get('attachmentId') ?? undefined,\n })\n if (!parsed.success) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.invalidInput', 'Invalid input') }, { status: 400 })\n }\n const owned = await loadOwnedVisibleAttachment(context, parsed.data.attachmentId)\n if (!owned) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.notFound', 'Claim not found.') }, { status: 404 })\n }\n const guarded = await runPortalAttachmentGuard(req, context, owned.claim.id, 'delete', {\n claimId: owned.claim.id,\n attachmentId: owned.attachment.id,\n })\n if (!guarded.ok) return guarded.response\n\n await deletePortalAttachment(context, owned.attachment)\n await guarded.runAfterSuccess()\n return NextResponse.json({ ok: true })\n}\n\nconst assignmentSchema = z.object({\n type: z.string(),\n id: z.string(),\n href: z.string().nullable().optional(),\n label: z.string().nullable().optional(),\n})\n\nconst attachmentItemSchema = z.object({\n id: z.string(),\n url: z.string(),\n downloadUrl: z.string(),\n fileName: z.string(),\n fileSize: z.number().int().nonnegative(),\n mimeType: z.string().nullable(),\n partitionCode: z.string(),\n content: z.string().nullable(),\n thumbnailUrl: z.string(),\n tags: z.array(z.string()),\n assignments: z.array(assignmentSchema),\n createdAt: z.string().nullable(),\n})\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Warranty Claims Portal',\n summary: 'Customer portal warranty claim attachments',\n methods: {\n GET: {\n summary: 'List attachments for an owned claim',\n query: attachmentQuerySchema,\n responses: [\n {\n status: 200,\n description: 'Claim attachments',\n schema: z.object({ items: z.array(attachmentItemSchema) }),\n },\n ],\n },\n POST: {\n summary: 'Upload an attachment for an owned claim',\n requestBody: { contentType: 'multipart/form-data', schema: uploadBodySchema },\n responses: [\n {\n status: 200,\n description: 'Attachment uploaded',\n schema: z.object({ ok: z.boolean(), item: attachmentItemSchema.extend({ entityType: z.string() }) }),\n },\n ],\n },\n PUT: {\n summary: 'Replace an attachment on an owned claim',\n requestBody: { contentType: 'multipart/form-data', schema: replaceBodySchema },\n responses: [\n {\n status: 200,\n description: 'Attachment replaced',\n schema: z.object({ ok: z.boolean(), item: attachmentItemSchema.extend({ entityType: z.string() }) }),\n },\n ],\n },\n DELETE: {\n summary: 'Delete an attachment from an owned claim',\n query: deleteQuerySchema,\n responses: [\n {\n status: 200,\n description: 'Attachment deleted',\n schema: z.object({ ok: z.literal(true) }),\n },\n ],\n },\n },\n}\n"],
5
+ "mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAGlB,SAAS,8BAA8B;AAEvC,SAAS,2BAA2B;AACpC,SAAS,8BAA6D;AACtE,SAAS,2BAA2B;AACpC,SAAS,uBAAuB,0BAA0B;AAC1D,SAAS,oBAAoB;AAE7B,SAAS,YAAY,2BAA2B;AAChD,SAAS,yBAAyB,iCAAiC;AACnE,SAAS,4BAA4B;AACrC,SAAS,6BAA6B;AACtC,SAAS,sBAAsB,6BAA6B;AAC5D,SAAS,8BAA8B;AACvC,SAAS,qCAAqC;AAC9C,SAAS,2CAA2C;AACpD;AAAA,EACE;AAAA,OAEK;AAGP,SAAS,oCAAoC;AAC7C,SAAS,iCAAiC,mCAAmC;AAC7E,SAAS,4BAA4B;AACrC,SAAS,4CAA4C;AACrD,SAAS,iCAAiC;AAE1C,MAAM,6BAA6B;AACnC,MAAM,SAAS,aAAa,iBAAiB,EAAE,MAAM,EAAE,OAAO,qBAAqB,CAAC;AACpF,MAAM,gCAA6G;AAAA,EACjH,sBAAsB,CAAC,0CAA0C,uDAAuD;AAAA,EACxH,iBAAiB,CAAC,oCAAoC,6CAA6C;AAAA,EACnG,gBAAgB,CAAC,2CAA2C,yCAAyC;AACvG;AAEO,MAAM,WAAW;AAAA,EACtB,KAAK,EAAE,aAAa,MAAM;AAAA,EAC1B,MAAM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AAE,aAAa,uBAAuB,QAAQ,kBAAkB;AAAA,MAC7E,WAAW;AAAA,QACT;AAAA,UACE,QAAQ;AAAA,UACR,aAAa;AAAA,UACb,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,GAAG,MAAM,qBAAqB,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC;AAAA,QACrG;AAAA,MACF;AAAA,IACF;AAAA,IACA,QAAQ;AAAA,MACN,SAAS;AAAA,MACT,OAAO;AAAA,MACP,WAAW;AAAA,QACT;AAAA,UACE,QAAQ;AAAA,UACR,aAAa;AAAA,UACb,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,IAAI,EAAE,CAAC;AAAA,QAC1C;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
6
6
  "names": ["metadata"]
7
7
  }
@@ -2,9 +2,10 @@ import { NextResponse } from "next/server";
2
2
  import { z } from "zod";
3
3
  import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
4
4
  import { findWithDecryption } from "@open-mercato/shared/lib/encryption/find";
5
- import { getCustomerAuthFromRequest } from "@open-mercato/core/modules/customer_accounts/lib/customerAuth";
5
+ import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
6
6
  import { WarrantyClaimLine } from "../../../../data/entities.js";
7
7
  import { loadPortalOwnedClaim } from "../../../../lib/portalClaimAccess.js";
8
+ import { resolveLinkedCustomerAuth } from "../../../../lib/portalAuthGuard.js";
8
9
  const metadata = {
9
10
  GET: { requireAuth: false }
10
11
  };
@@ -23,13 +24,9 @@ async function resolveClaimId(ctx) {
23
24
  }
24
25
  }
25
26
  async function resolvePortalContext(req) {
26
- const auth = await getCustomerAuthFromRequest(req);
27
- if (!auth) {
28
- return NextResponse.json({ ok: false, error: "warranty_claims.errors.unauthorized" }, { status: 401 });
29
- }
30
- if (!auth.customerEntityId) {
31
- return NextResponse.json({ ok: false, error: "warranty_claims.errors.customerAccountNotLinked" }, { status: 403 });
32
- }
27
+ const authOrResponse = await resolveLinkedCustomerAuth(req);
28
+ if (authOrResponse instanceof Response) return authOrResponse;
29
+ const auth = authOrResponse;
33
30
  const container = await createRequestContainer();
34
31
  const em = container.resolve("em");
35
32
  return {
@@ -88,9 +85,10 @@ async function GET(req, ctx) {
88
85
  const contextOrResponse = await resolvePortalContext(req);
89
86
  if (contextOrResponse instanceof Response) return contextOrResponse;
90
87
  const context = contextOrResponse;
88
+ const { translate } = await resolveTranslations();
91
89
  const claimId = await resolveClaimId(ctx);
92
90
  if (!claimId) {
93
- return NextResponse.json({ ok: false, error: "warranty_claims.errors.notFound" }, { status: 404 });
91
+ return NextResponse.json({ ok: false, error: translate("warranty_claims.errors.notFound", "Claim not found.") }, { status: 404 });
94
92
  }
95
93
  const scope = { tenantId: context.tenantId, organizationId: context.organizationId };
96
94
  const claim = await loadPortalOwnedClaim(
@@ -103,7 +101,7 @@ async function GET(req, ctx) {
103
101
  claimId
104
102
  );
105
103
  if (!claim) {
106
- return NextResponse.json({ ok: false, error: "warranty_claims.errors.notFound" }, { status: 404 });
104
+ return NextResponse.json({ ok: false, error: translate("warranty_claims.errors.notFound", "Claim not found.") }, { status: 404 });
107
105
  }
108
106
  const lines = await findWithDecryption(
109
107
  context.em,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../../../src/modules/warranty_claims/api/portal/claims/%5Bid%5D/route.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 { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { getCustomerAuthFromRequest, type CustomerAuthContext } from '@open-mercato/core/modules/customer_accounts/lib/customerAuth'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport { WarrantyClaim, WarrantyClaimLine } from '../../../../data/entities'\nimport { loadPortalOwnedClaim } from '../../../../lib/portalClaimAccess'\n\nexport const metadata = {\n GET: { requireAuth: false },\n}\n\ntype RouteParams = { id: string }\ntype RouteContext = { params: Promise<RouteParams> }\n\ntype PortalContext = {\n auth: CustomerAuthContext\n customerId: string\n tenantId: string\n organizationId: string\n em: EntityManager\n}\n\nfunction toIso(value: Date | string | null | undefined): string | null {\n if (!value) return null\n if (value instanceof Date) return value.toISOString()\n const date = new Date(value)\n return Number.isNaN(date.getTime()) ? value : date.toISOString()\n}\n\nasync function resolveClaimId(ctx: RouteContext): Promise<string | null> {\n try {\n const params = await ctx.params\n return typeof params.id === 'string' && params.id.trim().length ? params.id.trim() : null\n } catch {\n return null\n }\n}\n\nasync function resolvePortalContext(req: Request): Promise<PortalContext | Response> {\n const auth = await getCustomerAuthFromRequest(req)\n if (!auth) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.unauthorized' }, { status: 401 })\n }\n if (!auth.customerEntityId) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.customerAccountNotLinked' }, { status: 403 })\n }\n const container = await createRequestContainer()\n const em = container.resolve('em') as EntityManager\n return {\n auth,\n customerId: auth.customerEntityId,\n tenantId: auth.tenantId,\n organizationId: auth.orgId,\n em,\n }\n}\n\nfunction serializeLine(line: WarrantyClaimLine) {\n return {\n id: line.id,\n lineNo: line.lineNo,\n productId: line.productId ?? null,\n sku: line.sku ?? null,\n productName: line.productName ?? null,\n orderLineId: line.orderLineId ?? null,\n serialNumber: line.serialNumber ?? null,\n faultCode: line.faultCode ?? null,\n faultDescription: line.faultDescription ?? null,\n qtyClaimed: line.qtyClaimed,\n qtyApproved: line.qtyApproved ?? null,\n disposition: line.disposition ?? null,\n lineStatus: line.lineStatus,\n creditAmount: line.creditAmount ?? null,\n createdAt: toIso(line.createdAt),\n updatedAt: toIso(line.updatedAt),\n }\n}\n\nfunction serializeClaim(claim: WarrantyClaim, lines: WarrantyClaimLine[]) {\n return {\n id: claim.id,\n claimNumber: claim.claimNumber,\n claimType: claim.claimType,\n status: claim.status,\n channel: claim.channel,\n priority: claim.priority,\n customerId: claim.customerId ?? null,\n customerName: claim.customerName ?? null,\n orderId: claim.orderId ?? null,\n orderNumber: claim.orderNumber ?? null,\n reasonCode: claim.reasonCode ?? null,\n rejectionReasonCode: claim.rejectionReasonCode ?? null,\n resolutionSummary: claim.resolutionSummary ?? null,\n slaDueAt: toIso(claim.slaDueAt),\n submittedAt: toIso(claim.submittedAt),\n resolvedAt: toIso(claim.resolvedAt),\n closedAt: toIso(claim.closedAt),\n createdAt: toIso(claim.createdAt),\n updatedAt: toIso(claim.updatedAt),\n lines: lines.map(serializeLine),\n }\n}\n\nexport async function GET(req: Request, ctx: RouteContext) {\n const contextOrResponse = await resolvePortalContext(req)\n if (contextOrResponse instanceof Response) return contextOrResponse\n const context = contextOrResponse\n const claimId = await resolveClaimId(ctx)\n if (!claimId) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.notFound' }, { status: 404 })\n }\n const scope = { tenantId: context.tenantId, organizationId: context.organizationId }\n const claim = await loadPortalOwnedClaim(\n context.em,\n {\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n customerId: context.customerId,\n },\n claimId,\n )\n if (!claim) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.notFound' }, { status: 404 })\n }\n const lines = await findWithDecryption(\n context.em,\n WarrantyClaimLine,\n { claim: claim.id, tenantId: context.tenantId, organizationId: context.organizationId, deletedAt: null },\n { orderBy: { lineNo: 'ASC' } },\n scope,\n )\n return NextResponse.json({ item: serializeClaim(claim, lines) })\n}\n\nconst lineSchema = z.object({\n id: z.string().uuid(),\n lineNo: z.number().int(),\n productId: z.string().uuid().nullable(),\n sku: z.string().nullable(),\n productName: z.string().nullable(),\n orderLineId: z.string().uuid().nullable(),\n serialNumber: z.string().nullable(),\n faultCode: z.string().nullable(),\n faultDescription: z.string().nullable(),\n qtyClaimed: z.string(),\n qtyApproved: z.string().nullable(),\n disposition: z.string().nullable(),\n lineStatus: z.string(),\n creditAmount: z.string().nullable(),\n createdAt: z.string().nullable(),\n updatedAt: z.string().nullable(),\n})\n\nconst claimSchema = z.object({\n id: z.string().uuid(),\n claimNumber: z.string(),\n claimType: z.string(),\n status: z.string(),\n channel: z.string(),\n priority: z.string(),\n customerId: z.string().uuid().nullable(),\n customerName: z.string().nullable(),\n orderId: z.string().uuid().nullable(),\n orderNumber: z.string().nullable(),\n reasonCode: z.string().nullable(),\n rejectionReasonCode: z.string().nullable(),\n resolutionSummary: z.string().nullable(),\n slaDueAt: z.string().nullable(),\n submittedAt: z.string().nullable(),\n resolvedAt: z.string().nullable(),\n closedAt: z.string().nullable(),\n createdAt: z.string().nullable(),\n updatedAt: z.string().nullable(),\n lines: z.array(lineSchema),\n})\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Warranty Claims Portal',\n summary: 'Customer portal warranty claim detail',\n methods: {\n GET: {\n summary: 'Read one authenticated customer claim',\n responses: [\n {\n status: 200,\n description: 'Customer claim detail',\n schema: z.object({ item: claimSchema }),\n },\n ],\n },\n },\n}\n"],
5
- "mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAElB,SAAS,8BAA8B;AACvC,SAAS,0BAA0B;AACnC,SAAS,kCAA4D;AAErE,SAAwB,yBAAyB;AACjD,SAAS,4BAA4B;AAE9B,MAAM,WAAW;AAAA,EACtB,KAAK,EAAE,aAAa,MAAM;AAC5B;AAaA,SAAS,MAAM,OAAwD;AACrE,MAAI,CAAC,MAAO,QAAO;AACnB,MAAI,iBAAiB,KAAM,QAAO,MAAM,YAAY;AACpD,QAAM,OAAO,IAAI,KAAK,KAAK;AAC3B,SAAO,OAAO,MAAM,KAAK,QAAQ,CAAC,IAAI,QAAQ,KAAK,YAAY;AACjE;AAEA,eAAe,eAAe,KAA2C;AACvE,MAAI;AACF,UAAM,SAAS,MAAM,IAAI;AACzB,WAAO,OAAO,OAAO,OAAO,YAAY,OAAO,GAAG,KAAK,EAAE,SAAS,OAAO,GAAG,KAAK,IAAI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EAAE,SAAS;AAAA,EACvC,cAAc,EAAE,OAAO,EAAE,SAAS;AAAA,EAClC,SAAS,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS;AAAA,EACpC,aAAa,EAAE,OAAO,EAAE,SAAS;AAAA,EACjC,YAAY,EAAE,OAAO,EAAE,SAAS;AAAA,EAChC,qBAAqB,EAAE,OAAO,EAAE,SAAS;AAAA,EACzC,mBAAmB,EAAE,OAAO,EAAE,SAAS;AAAA,EACvC,UAAU,EAAE,OAAO,EAAE,SAAS;AAAA,EAC9B,aAAa,EAAE,OAAO,EAAE,SAAS;AAAA,EACjC,YAAY,EAAE,OAAO,EAAE,SAAS;AAAA,EAChC,UAAU,EAAE,OAAO,EAAE,SAAS;AAAA,EAC9B,WAAW,EAAE,OAAO,EAAE,SAAS;AAAA,EAC/B,WAAW,EAAE,OAAO,EAAE,SAAS;AAAA,EAC/B,OAAO,EAAE,MAAM,UAAU;AAC3B,CAAC;AAEM,MAAM,UAA2B;AAAA,EACtC,KAAK;AAAA,EACL,SAAS;AAAA,EACT,SAAS;AAAA,IACP,KAAK;AAAA,MACH,SAAS;AAAA,MACT,WAAW;AAAA,QACT;AAAA,UACE,QAAQ;AAAA,UACR,aAAa;AAAA,UACb,QAAQ,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAAA,QACxC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
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 { findWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport type { CustomerAuthContext } from '@open-mercato/core/modules/customer_accounts/lib/customerAuth'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport { WarrantyClaim, WarrantyClaimLine } from '../../../../data/entities'\nimport { loadPortalOwnedClaim } from '../../../../lib/portalClaimAccess'\nimport { resolveLinkedCustomerAuth } from '../../../../lib/portalAuthGuard'\n\nexport const metadata = {\n GET: { requireAuth: false },\n}\n\ntype RouteParams = { id: string }\ntype RouteContext = { params: Promise<RouteParams> }\n\ntype PortalContext = {\n auth: CustomerAuthContext\n customerId: string\n tenantId: string\n organizationId: string\n em: EntityManager\n}\n\nfunction toIso(value: Date | string | null | undefined): string | null {\n if (!value) return null\n if (value instanceof Date) return value.toISOString()\n const date = new Date(value)\n return Number.isNaN(date.getTime()) ? value : date.toISOString()\n}\n\nasync function resolveClaimId(ctx: RouteContext): Promise<string | null> {\n try {\n const params = await ctx.params\n return typeof params.id === 'string' && params.id.trim().length ? params.id.trim() : null\n } catch {\n return null\n }\n}\n\nasync function resolvePortalContext(req: Request): Promise<PortalContext | Response> {\n const authOrResponse = await resolveLinkedCustomerAuth(req)\n if (authOrResponse instanceof Response) return authOrResponse\n const auth = authOrResponse\n const container = await createRequestContainer()\n const em = container.resolve('em') as EntityManager\n return {\n auth,\n customerId: auth.customerEntityId,\n tenantId: auth.tenantId,\n organizationId: auth.orgId,\n em,\n }\n}\n\nfunction serializeLine(line: WarrantyClaimLine) {\n return {\n id: line.id,\n lineNo: line.lineNo,\n productId: line.productId ?? null,\n sku: line.sku ?? null,\n productName: line.productName ?? null,\n orderLineId: line.orderLineId ?? null,\n serialNumber: line.serialNumber ?? null,\n faultCode: line.faultCode ?? null,\n faultDescription: line.faultDescription ?? null,\n qtyClaimed: line.qtyClaimed,\n qtyApproved: line.qtyApproved ?? null,\n disposition: line.disposition ?? null,\n lineStatus: line.lineStatus,\n creditAmount: line.creditAmount ?? null,\n createdAt: toIso(line.createdAt),\n updatedAt: toIso(line.updatedAt),\n }\n}\n\nfunction serializeClaim(claim: WarrantyClaim, lines: WarrantyClaimLine[]) {\n return {\n id: claim.id,\n claimNumber: claim.claimNumber,\n claimType: claim.claimType,\n status: claim.status,\n channel: claim.channel,\n priority: claim.priority,\n customerId: claim.customerId ?? null,\n customerName: claim.customerName ?? null,\n orderId: claim.orderId ?? null,\n orderNumber: claim.orderNumber ?? null,\n reasonCode: claim.reasonCode ?? null,\n rejectionReasonCode: claim.rejectionReasonCode ?? null,\n resolutionSummary: claim.resolutionSummary ?? null,\n slaDueAt: toIso(claim.slaDueAt),\n submittedAt: toIso(claim.submittedAt),\n resolvedAt: toIso(claim.resolvedAt),\n closedAt: toIso(claim.closedAt),\n createdAt: toIso(claim.createdAt),\n updatedAt: toIso(claim.updatedAt),\n lines: lines.map(serializeLine),\n }\n}\n\nexport async function GET(req: Request, ctx: RouteContext) {\n const contextOrResponse = await resolvePortalContext(req)\n if (contextOrResponse instanceof Response) return contextOrResponse\n const context = contextOrResponse\n const { translate } = await resolveTranslations()\n const claimId = await resolveClaimId(ctx)\n if (!claimId) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.notFound', 'Claim not found.') }, { status: 404 })\n }\n const scope = { tenantId: context.tenantId, organizationId: context.organizationId }\n const claim = await loadPortalOwnedClaim(\n context.em,\n {\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n customerId: context.customerId,\n },\n claimId,\n )\n if (!claim) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.notFound', 'Claim not found.') }, { status: 404 })\n }\n const lines = await findWithDecryption(\n context.em,\n WarrantyClaimLine,\n { claim: claim.id, tenantId: context.tenantId, organizationId: context.organizationId, deletedAt: null },\n { orderBy: { lineNo: 'ASC' } },\n scope,\n )\n return NextResponse.json({ item: serializeClaim(claim, lines) })\n}\n\nconst lineSchema = z.object({\n id: z.string().uuid(),\n lineNo: z.number().int(),\n productId: z.string().uuid().nullable(),\n sku: z.string().nullable(),\n productName: z.string().nullable(),\n orderLineId: z.string().uuid().nullable(),\n serialNumber: z.string().nullable(),\n faultCode: z.string().nullable(),\n faultDescription: z.string().nullable(),\n qtyClaimed: z.string(),\n qtyApproved: z.string().nullable(),\n disposition: z.string().nullable(),\n lineStatus: z.string(),\n creditAmount: z.string().nullable(),\n createdAt: z.string().nullable(),\n updatedAt: z.string().nullable(),\n})\n\nconst claimSchema = z.object({\n id: z.string().uuid(),\n claimNumber: z.string(),\n claimType: z.string(),\n status: z.string(),\n channel: z.string(),\n priority: z.string(),\n customerId: z.string().uuid().nullable(),\n customerName: z.string().nullable(),\n orderId: z.string().uuid().nullable(),\n orderNumber: z.string().nullable(),\n reasonCode: z.string().nullable(),\n rejectionReasonCode: z.string().nullable(),\n resolutionSummary: z.string().nullable(),\n slaDueAt: z.string().nullable(),\n submittedAt: z.string().nullable(),\n resolvedAt: z.string().nullable(),\n closedAt: z.string().nullable(),\n createdAt: z.string().nullable(),\n updatedAt: z.string().nullable(),\n lines: z.array(lineSchema),\n})\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Warranty Claims Portal',\n summary: 'Customer portal warranty claim detail',\n methods: {\n GET: {\n summary: 'Read one authenticated customer claim',\n responses: [\n {\n status: 200,\n description: 'Customer claim detail',\n schema: z.object({ item: claimSchema }),\n },\n ],\n },\n },\n}\n"],
5
+ "mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAElB,SAAS,8BAA8B;AACvC,SAAS,0BAA0B;AACnC,SAAS,2BAA2B;AAGpC,SAAwB,yBAAyB;AACjD,SAAS,4BAA4B;AACrC,SAAS,iCAAiC;AAEnC,MAAM,WAAW;AAAA,EACtB,KAAK,EAAE,aAAa,MAAM;AAC5B;AAaA,SAAS,MAAM,OAAwD;AACrE,MAAI,CAAC,MAAO,QAAO;AACnB,MAAI,iBAAiB,KAAM,QAAO,MAAM,YAAY;AACpD,QAAM,OAAO,IAAI,KAAK,KAAK;AAC3B,SAAO,OAAO,MAAM,KAAK,QAAQ,CAAC,IAAI,QAAQ,KAAK,YAAY;AACjE;AAEA,eAAe,eAAe,KAA2C;AACvE,MAAI;AACF,UAAM,SAAS,MAAM,IAAI;AACzB,WAAO,OAAO,OAAO,OAAO,YAAY,OAAO,GAAG,KAAK,EAAE,SAAS,OAAO,GAAG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SAAS;AAAA,EACpC,aAAa,EAAE,OAAO,EAAE,SAAS;AAAA,EACjC,YAAY,EAAE,OAAO,EAAE,SAAS;AAAA,EAChC,qBAAqB,EAAE,OAAO,EAAE,SAAS;AAAA,EACzC,mBAAmB,EAAE,OAAO,EAAE,SAAS;AAAA,EACvC,UAAU,EAAE,OAAO,EAAE,SAAS;AAAA,EAC9B,aAAa,EAAE,OAAO,EAAE,SAAS;AAAA,EACjC,YAAY,EAAE,OAAO,EAAE,SAAS;AAAA,EAChC,UAAU,EAAE,OAAO,EAAE,SAAS;AAAA,EAC9B,WAAW,EAAE,OAAO,EAAE,SAAS;AAAA,EAC/B,WAAW,EAAE,OAAO,EAAE,SAAS;AAAA,EAC/B,OAAO,EAAE,MAAM,UAAU;AAC3B,CAAC;AAEM,MAAM,UAA2B;AAAA,EACtC,KAAK;AAAA,EACL,SAAS;AAAA,EACT,SAAS;AAAA,IACP,KAAK;AAAA,MACH,SAAS;AAAA,MACT,WAAW;AAAA,QACT;AAAA,UACE,QAAQ;AAAA,UACR,aAAa;AAAA,UACb,QAAQ,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAAA,QACxC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
6
6
  "names": []
7
7
  }
@@ -1,10 +1,9 @@
1
- import { NextResponse } from "next/server";
2
1
  import { z } from "zod";
3
2
  import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
4
3
  import { runRouteMutationGuards } from "@open-mercato/shared/lib/crud/route-mutation-guard";
5
- import { getCustomerAuthFromRequest } from "@open-mercato/core/modules/customer_accounts/lib/customerAuth";
6
4
  import { WARRANTY_CLAIM_RESOURCE_KIND } from "../../../../commands/shared.js";
7
5
  import { loadPortalOwnedClaim } from "../../../../lib/portalClaimAccess.js";
6
+ import { resolveLinkedCustomerAuth } from "../../../../lib/portalAuthGuard.js";
8
7
  const claimIdParamSchema = z.string().uuid();
9
8
  async function resolvePortalClaimId(ctx) {
10
9
  try {
@@ -16,13 +15,9 @@ async function resolvePortalClaimId(ctx) {
16
15
  }
17
16
  }
18
17
  async function resolvePortalActionContext(req) {
19
- const auth = await getCustomerAuthFromRequest(req);
20
- if (!auth) {
21
- return NextResponse.json({ ok: false, error: "warranty_claims.errors.unauthorized" }, { status: 401 });
22
- }
23
- if (!auth.customerEntityId) {
24
- return NextResponse.json({ ok: false, error: "warranty_claims.errors.customerAccountNotLinked" }, { status: 403 });
25
- }
18
+ const authOrResponse = await resolveLinkedCustomerAuth(req);
19
+ if (authOrResponse instanceof Response) return authOrResponse;
20
+ const auth = authOrResponse;
26
21
  const container = await createRequestContainer();
27
22
  const em = container.resolve("em");
28
23
  const commandAuth = {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../../../src/modules/warranty_claims/api/portal/claims/%5Bid%5D/shared.ts"],
4
- "sourcesContent": ["import { NextResponse } from 'next/server'\nimport { z } from 'zod'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport type { AuthContext } from '@open-mercato/shared/lib/auth/server'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport type { CommandRuntimeContext } from '@open-mercato/shared/lib/commands'\nimport { runRouteMutationGuards, type RouteMutationGuardResult } from '@open-mercato/shared/lib/crud/route-mutation-guard'\nimport { getCustomerAuthFromRequest, type CustomerAuthContext } from '@open-mercato/core/modules/customer_accounts/lib/customerAuth'\nimport { WarrantyClaim } from '../../../../data/entities'\nimport { WARRANTY_CLAIM_RESOURCE_KIND } from '../../../../commands/shared'\nimport { loadPortalOwnedClaim } from '../../../../lib/portalClaimAccess'\n\nexport type PortalClaimActionRouteContext = { params: Promise<{ id: string }> }\n\nexport type PortalClaimActionContext = {\n auth: CustomerAuthContext\n customerId: string\n tenantId: string\n organizationId: string\n em: EntityManager\n commandCtx: CommandRuntimeContext\n}\n\nconst claimIdParamSchema = z.string().uuid()\n\nexport async function resolvePortalClaimId(ctx: PortalClaimActionRouteContext): Promise<string | null> {\n try {\n const params = await ctx.params\n const parsed = claimIdParamSchema.safeParse(typeof params.id === 'string' ? params.id.trim() : params.id)\n return parsed.success ? parsed.data : null\n } catch {\n return null\n }\n}\n\nexport async function resolvePortalActionContext(req: Request): Promise<PortalClaimActionContext | Response> {\n const auth = await getCustomerAuthFromRequest(req)\n if (!auth) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.unauthorized' }, { status: 401 })\n }\n if (!auth.customerEntityId) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.customerAccountNotLinked' }, { status: 403 })\n }\n const container = await createRequestContainer()\n const em = container.resolve('em') as EntityManager\n const commandAuth: NonNullable<AuthContext> = {\n sub: auth.sub,\n sid: auth.sid,\n tenantId: auth.tenantId,\n orgId: auth.orgId,\n email: auth.email,\n customerEntityId: auth.customerEntityId ?? null,\n personEntityId: auth.personEntityId ?? null,\n }\n return {\n auth,\n customerId: auth.customerEntityId,\n tenantId: auth.tenantId,\n organizationId: auth.orgId,\n em,\n commandCtx: {\n container,\n auth: commandAuth,\n organizationScope: null,\n selectedOrganizationId: auth.orgId,\n organizationIds: [auth.orgId],\n request: req,\n },\n }\n}\n\nexport async function loadOwnedClaim(\n context: PortalClaimActionContext,\n claimId: string,\n): Promise<WarrantyClaim | null> {\n return loadPortalOwnedClaim(\n context.em,\n {\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n customerId: context.customerId,\n },\n claimId,\n )\n}\n\nexport async function loadOwnedClaimFresh(\n context: PortalClaimActionContext,\n claimId: string,\n): Promise<WarrantyClaim | null> {\n const em = context.em.fork()\n return loadPortalOwnedClaim(\n em,\n {\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n customerId: context.customerId,\n },\n claimId,\n )\n}\n\nexport async function runPortalClaimActionGuard(\n req: Request,\n context: PortalClaimActionContext,\n claimId: string,\n mutationPayload: Record<string, unknown>,\n): Promise<RouteMutationGuardResult> {\n return runRouteMutationGuards({\n container: context.commandCtx.container,\n req,\n auth: {\n userId: context.auth.sub,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n userFeatures: [],\n },\n input: {\n resourceKind: WARRANTY_CLAIM_RESOURCE_KIND,\n resourceId: claimId,\n operation: 'custom',\n mutationPayload,\n },\n })\n}\n"],
5
- "mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAGlB,SAAS,8BAA8B;AAEvC,SAAS,8BAA6D;AACtE,SAAS,kCAA4D;AAErE,SAAS,oCAAoC;AAC7C,SAAS,4BAA4B;AAarC,MAAM,qBAAqB,EAAE,OAAO,EAAE,KAAK;AAE3C,eAAsB,qBAAqB,KAA4D;AACrG,MAAI;AACF,UAAM,SAAS,MAAM,IAAI;AACzB,UAAM,SAAS,mBAAmB,UAAU,OAAO,OAAO,OAAO,WAAW,OAAO,GAAG,KAAK,IAAI,OAAO,EAAE;AACxG,WAAO,OAAO,UAAU,OAAO,OAAO;AAAA,EACxC,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEA,eAAsB,2BAA2B,KAA4D;AAC3G,QAAM,OAAO,MAAM,2BAA2B,GAAG;AACjD,MAAI,CAAC,MAAM;AACT,WAAO,aAAa,KAAK,EAAE,IAAI,OAAO,OAAO,sCAAsC,GAAG,EAAE,QAAQ,IAAI,CAAC;AAAA,EACvG;AACA,MAAI,CAAC,KAAK,kBAAkB;AAC1B,WAAO,aAAa,KAAK,EAAE,IAAI,OAAO,OAAO,kDAAkD,GAAG,EAAE,QAAQ,IAAI,CAAC;AAAA,EACnH;AACA,QAAM,YAAY,MAAM,uBAAuB;AAC/C,QAAM,KAAK,UAAU,QAAQ,IAAI;AACjC,QAAM,cAAwC;AAAA,IAC5C,KAAK,KAAK;AAAA,IACV,KAAK,KAAK;AAAA,IACV,UAAU,KAAK;AAAA,IACf,OAAO,KAAK;AAAA,IACZ,OAAO,KAAK;AAAA,IACZ,kBAAkB,KAAK,oBAAoB;AAAA,IAC3C,gBAAgB,KAAK,kBAAkB;AAAA,EACzC;AACA,SAAO;AAAA,IACL;AAAA,IACA,YAAY,KAAK;AAAA,IACjB,UAAU,KAAK;AAAA,IACf,gBAAgB,KAAK;AAAA,IACrB;AAAA,IACA,YAAY;AAAA,MACV;AAAA,MACA,MAAM;AAAA,MACN,mBAAmB;AAAA,MACnB,wBAAwB,KAAK;AAAA,MAC7B,iBAAiB,CAAC,KAAK,KAAK;AAAA,MAC5B,SAAS;AAAA,IACX;AAAA,EACF;AACF;AAEA,eAAsB,eACpB,SACA,SAC+B;AAC/B,SAAO;AAAA,IACL,QAAQ;AAAA,IACR;AAAA,MACE,UAAU,QAAQ;AAAA,MAClB,gBAAgB,QAAQ;AAAA,MACxB,YAAY,QAAQ;AAAA,IACtB;AAAA,IACA;AAAA,EACF;AACF;AAEA,eAAsB,oBACpB,SACA,SAC+B;AAC/B,QAAM,KAAK,QAAQ,GAAG,KAAK;AAC3B,SAAO;AAAA,IACL;AAAA,IACA;AAAA,MACE,UAAU,QAAQ;AAAA,MAClB,gBAAgB,QAAQ;AAAA,MACxB,YAAY,QAAQ;AAAA,IACtB;AAAA,IACA;AAAA,EACF;AACF;AAEA,eAAsB,0BACpB,KACA,SACA,SACA,iBACmC;AACnC,SAAO,uBAAuB;AAAA,IAC5B,WAAW,QAAQ,WAAW;AAAA,IAC9B;AAAA,IACA,MAAM;AAAA,MACJ,QAAQ,QAAQ,KAAK;AAAA,MACrB,UAAU,QAAQ;AAAA,MAClB,gBAAgB,QAAQ;AAAA,MACxB,cAAc,CAAC;AAAA,IACjB;AAAA,IACA,OAAO;AAAA,MACL,cAAc;AAAA,MACd,YAAY;AAAA,MACZ,WAAW;AAAA,MACX;AAAA,IACF;AAAA,EACF,CAAC;AACH;",
4
+ "sourcesContent": ["import { z } from 'zod'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport type { AuthContext } from '@open-mercato/shared/lib/auth/server'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport type { CommandRuntimeContext } from '@open-mercato/shared/lib/commands'\nimport { runRouteMutationGuards, type RouteMutationGuardResult } from '@open-mercato/shared/lib/crud/route-mutation-guard'\nimport type { CustomerAuthContext } from '@open-mercato/core/modules/customer_accounts/lib/customerAuth'\nimport { WarrantyClaim } from '../../../../data/entities'\nimport { WARRANTY_CLAIM_RESOURCE_KIND } from '../../../../commands/shared'\nimport { loadPortalOwnedClaim } from '../../../../lib/portalClaimAccess'\nimport { resolveLinkedCustomerAuth } from '../../../../lib/portalAuthGuard'\n\nexport type PortalClaimActionRouteContext = { params: Promise<{ id: string }> }\n\nexport type PortalClaimActionContext = {\n auth: CustomerAuthContext\n customerId: string\n tenantId: string\n organizationId: string\n em: EntityManager\n commandCtx: CommandRuntimeContext\n}\n\nconst claimIdParamSchema = z.string().uuid()\n\nexport async function resolvePortalClaimId(ctx: PortalClaimActionRouteContext): Promise<string | null> {\n try {\n const params = await ctx.params\n const parsed = claimIdParamSchema.safeParse(typeof params.id === 'string' ? params.id.trim() : params.id)\n return parsed.success ? parsed.data : null\n } catch {\n return null\n }\n}\n\nexport async function resolvePortalActionContext(req: Request): Promise<PortalClaimActionContext | Response> {\n const authOrResponse = await resolveLinkedCustomerAuth(req)\n if (authOrResponse instanceof Response) return authOrResponse\n const auth = authOrResponse\n const container = await createRequestContainer()\n const em = container.resolve('em') as EntityManager\n const commandAuth: NonNullable<AuthContext> = {\n sub: auth.sub,\n sid: auth.sid,\n tenantId: auth.tenantId,\n orgId: auth.orgId,\n email: auth.email,\n customerEntityId: auth.customerEntityId ?? null,\n personEntityId: auth.personEntityId ?? null,\n }\n return {\n auth,\n customerId: auth.customerEntityId,\n tenantId: auth.tenantId,\n organizationId: auth.orgId,\n em,\n commandCtx: {\n container,\n auth: commandAuth,\n organizationScope: null,\n selectedOrganizationId: auth.orgId,\n organizationIds: [auth.orgId],\n request: req,\n },\n }\n}\n\nexport async function loadOwnedClaim(\n context: PortalClaimActionContext,\n claimId: string,\n): Promise<WarrantyClaim | null> {\n return loadPortalOwnedClaim(\n context.em,\n {\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n customerId: context.customerId,\n },\n claimId,\n )\n}\n\nexport async function loadOwnedClaimFresh(\n context: PortalClaimActionContext,\n claimId: string,\n): Promise<WarrantyClaim | null> {\n const em = context.em.fork()\n return loadPortalOwnedClaim(\n em,\n {\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n customerId: context.customerId,\n },\n claimId,\n )\n}\n\nexport async function runPortalClaimActionGuard(\n req: Request,\n context: PortalClaimActionContext,\n claimId: string,\n mutationPayload: Record<string, unknown>,\n): Promise<RouteMutationGuardResult> {\n return runRouteMutationGuards({\n container: context.commandCtx.container,\n req,\n auth: {\n userId: context.auth.sub,\n tenantId: context.tenantId,\n organizationId: context.organizationId,\n userFeatures: [],\n },\n input: {\n resourceKind: WARRANTY_CLAIM_RESOURCE_KIND,\n resourceId: claimId,\n operation: 'custom',\n mutationPayload,\n },\n })\n}\n"],
5
+ "mappings": "AAAA,SAAS,SAAS;AAGlB,SAAS,8BAA8B;AAEvC,SAAS,8BAA6D;AAGtE,SAAS,oCAAoC;AAC7C,SAAS,4BAA4B;AACrC,SAAS,iCAAiC;AAa1C,MAAM,qBAAqB,EAAE,OAAO,EAAE,KAAK;AAE3C,eAAsB,qBAAqB,KAA4D;AACrG,MAAI;AACF,UAAM,SAAS,MAAM,IAAI;AACzB,UAAM,SAAS,mBAAmB,UAAU,OAAO,OAAO,OAAO,WAAW,OAAO,GAAG,KAAK,IAAI,OAAO,EAAE;AACxG,WAAO,OAAO,UAAU,OAAO,OAAO;AAAA,EACxC,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEA,eAAsB,2BAA2B,KAA4D;AAC3G,QAAM,iBAAiB,MAAM,0BAA0B,GAAG;AAC1D,MAAI,0BAA0B,SAAU,QAAO;AAC/C,QAAM,OAAO;AACb,QAAM,YAAY,MAAM,uBAAuB;AAC/C,QAAM,KAAK,UAAU,QAAQ,IAAI;AACjC,QAAM,cAAwC;AAAA,IAC5C,KAAK,KAAK;AAAA,IACV,KAAK,KAAK;AAAA,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,16 @@ async function POST(req, ctx) {
17
17
  const contextOrResponse = await resolvePortalActionContext(req);
18
18
  if (contextOrResponse instanceof Response) return contextOrResponse;
19
19
  const context = contextOrResponse;
20
+ const { translate } = await resolveTranslations();
20
21
  const claimId = await resolvePortalClaimId(ctx);
21
22
  if (!claimId) {
22
- return NextResponse.json({ ok: false, error: "warranty_claims.errors.notFound" }, { status: 404 });
23
+ return NextResponse.json({ ok: false, error: translate("warranty_claims.errors.notFound", "Claim not found.") }, { status: 404 });
23
24
  }
24
25
  const claim = await loadOwnedClaim(context, claimId);
25
26
  if (!claim) {
26
- return NextResponse.json({ ok: false, error: "warranty_claims.errors.notFound" }, { status: 404 });
27
+ return NextResponse.json({ ok: false, error: translate("warranty_claims.errors.notFound", "Claim not found.") }, { status: 404 });
27
28
  }
28
29
  if (claim.status !== "draft") {
29
- const { translate } = await resolveTranslations();
30
30
  return NextResponse.json(
31
31
  { ok: false, error: translate("warranty_claims.errors.invalidTransition", "That status change is not allowed.") },
32
32
  { status: 400 }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../../../../src/modules/warranty_claims/api/portal/claims/%5Bid%5D/submit/route.ts"],
4
- "sourcesContent": ["import { NextResponse } from 'next/server'\nimport { z } from 'zod'\nimport type { CommandBus } from '@open-mercato/shared/lib/commands'\nimport { isCrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { getCommandInterceptorHttpRejection } from '@open-mercato/shared/lib/commands/errors'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport {\n loadOwnedClaim,\n loadOwnedClaimFresh,\n resolvePortalActionContext,\n resolvePortalClaimId,\n runPortalClaimActionGuard,\n type PortalClaimActionRouteContext,\n} from '../shared'\n\nexport const metadata = {\n POST: { requireAuth: false },\n}\n\nexport async function POST(req: Request, ctx: PortalClaimActionRouteContext) {\n const contextOrResponse = await resolvePortalActionContext(req)\n if (contextOrResponse instanceof Response) return contextOrResponse\n const context = contextOrResponse\n const claimId = await resolvePortalClaimId(ctx)\n if (!claimId) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.notFound' }, { status: 404 })\n }\n const claim = await loadOwnedClaim(context, claimId)\n if (!claim) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.notFound' }, { status: 404 })\n }\n if (claim.status !== 'draft') {\n const { translate } = await resolveTranslations()\n return NextResponse.json(\n { ok: false, error: translate('warranty_claims.errors.invalidTransition', 'That status change is not allowed.') },\n { status: 400 },\n )\n }\n\n const guarded = await runPortalClaimActionGuard(req, context, claim.id, {\n id: claim.id,\n action: 'submit',\n })\n if (!guarded.ok) {\n return guarded.response\n }\n\n const commandBus = context.commandCtx.container.resolve('commandBus') as CommandBus\n try {\n await commandBus.execute<{ id: string; actorCustomerId: string }, { claimId: string }>(\n 'warranty_claims.claim.submit',\n { input: { id: claim.id, actorCustomerId: context.customerId }, ctx: context.commandCtx },\n )\n } catch (err) {\n if (isCrudHttpError(err)) {\n return NextResponse.json(err.body, { status: err.status })\n }\n const interceptorRejection = getCommandInterceptorHttpRejection(err)\n if (interceptorRejection) {\n return NextResponse.json(interceptorRejection.body, { status: interceptorRejection.status })\n }\n throw err\n }\n\n await guarded.runAfterSuccess()\n\n const updated = await loadOwnedClaimFresh(context, claim.id)\n return NextResponse.json({ ok: true, claimId: claim.id, status: updated?.status ?? 'submitted' })\n}\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Warranty Claims Portal',\n summary: 'Customer portal warranty claim submit action',\n methods: {\n POST: {\n summary: 'Submit an owned draft claim from the customer portal',\n responses: [\n {\n status: 200,\n description: 'Claim submitted',\n schema: z.object({ ok: z.boolean(), claimId: z.string().uuid(), status: z.string() }),\n },\n ],\n },\n },\n}\n"],
5
- "mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAElB,SAAS,uBAAuB;AAChC,SAAS,0CAA0C;AACnD,SAAS,2BA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G;AACxB,aAAO,aAAa,KAAK,IAAI,MAAM,EAAE,QAAQ,IAAI,OAAO,CAAC;AAAA,IAC3D;AACA,UAAM,uBAAuB,mCAAmC,GAAG;AACnE,QAAI,sBAAsB;AACxB,aAAO,aAAa,KAAK,qBAAqB,MAAM,EAAE,QAAQ,qBAAqB,OAAO,CAAC;AAAA,IAC7F;AACA,UAAM;AAAA,EACR;AAEA,QAAM,QAAQ,gBAAgB;AAE9B,QAAM,UAAU,MAAM,oBAAoB,SAAS,MAAM,EAAE;AAC3D,SAAO,aAAa,KAAK,EAAE,IAAI,MAAM,SAAS,MAAM,IAAI,QAAQ,SAAS,UAAU,YAAY,CAAC;AAClG;AAEO,MAAM,UAA2B;AAAA,EACtC,KAAK;AAAA,EACL,SAAS;AAAA,EACT,SAAS;AAAA,IACP,MAAM;AAAA,MACJ,SAAS;AAAA,MACT,WAAW;AAAA,QACT;AAAA,UACE,QAAQ;AAAA,UACR,aAAa;AAAA,UACb,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,GAAG,SAAS,EAAE,OAAO,EAAE,KAAK,GAAG,QAAQ,EAAE,OAAO,EAAE,CAAC;AAAA,QACtF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import { NextResponse } from 'next/server'\nimport { z } from 'zod'\nimport type { CommandBus } from '@open-mercato/shared/lib/commands'\nimport { isCrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { getCommandInterceptorHttpRejection } from '@open-mercato/shared/lib/commands/errors'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport {\n loadOwnedClaim,\n loadOwnedClaimFresh,\n resolvePortalActionContext,\n resolvePortalClaimId,\n runPortalClaimActionGuard,\n type PortalClaimActionRouteContext,\n} from '../shared'\n\nexport const metadata = {\n POST: { requireAuth: false },\n}\n\nexport async function POST(req: Request, ctx: PortalClaimActionRouteContext) {\n const contextOrResponse = await resolvePortalActionContext(req)\n if (contextOrResponse instanceof Response) return contextOrResponse\n const context = contextOrResponse\n const { translate } = await resolveTranslations()\n const claimId = await resolvePortalClaimId(ctx)\n if (!claimId) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.notFound', 'Claim not found.') }, { status: 404 })\n }\n const claim = await loadOwnedClaim(context, claimId)\n if (!claim) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.notFound', 'Claim not found.') }, { status: 404 })\n }\n if (claim.status !== 'draft') {\n return NextResponse.json(\n { ok: false, error: translate('warranty_claims.errors.invalidTransition', 'That status change is not allowed.') },\n { status: 400 },\n )\n }\n\n const guarded = await runPortalClaimActionGuard(req, context, claim.id, {\n id: claim.id,\n action: 'submit',\n })\n if (!guarded.ok) {\n return guarded.response\n }\n\n const commandBus = context.commandCtx.container.resolve('commandBus') as CommandBus\n try {\n await commandBus.execute<{ id: string; actorCustomerId: string }, { claimId: string }>(\n 'warranty_claims.claim.submit',\n { input: { id: claim.id, actorCustomerId: context.customerId }, ctx: context.commandCtx },\n )\n } catch (err) {\n if (isCrudHttpError(err)) {\n return NextResponse.json(err.body, { status: err.status })\n }\n const interceptorRejection = getCommandInterceptorHttpRejection(err)\n if (interceptorRejection) {\n return NextResponse.json(interceptorRejection.body, { status: interceptorRejection.status })\n }\n throw err\n }\n\n await guarded.runAfterSuccess()\n\n const updated = await loadOwnedClaimFresh(context, claim.id)\n return NextResponse.json({ ok: true, claimId: claim.id, status: updated?.status ?? 'submitted' })\n}\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Warranty Claims Portal',\n summary: 'Customer portal warranty claim submit action',\n methods: {\n POST: {\n summary: 'Submit an owned draft claim from the customer portal',\n responses: [\n {\n status: 200,\n description: 'Claim submitted',\n schema: z.object({ ok: z.boolean(), claimId: z.string().uuid(), status: z.string() }),\n },\n ],\n },\n },\n}\n"],
5
+ "mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AA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AW,GAAG,KAAK,QAAQ,WAAW;AAAA,IAC1F;AAAA,EACF,SAAS,KAAK;AACZ,QAAI,gBAAgB,GAAG,GAAG;AACxB,aAAO,aAAa,KAAK,IAAI,MAAM,EAAE,QAAQ,IAAI,OAAO,CAAC;AAAA,IAC3D;AACA,UAAM,uBAAuB,mCAAmC,GAAG;AACnE,QAAI,sBAAsB;AACxB,aAAO,aAAa,KAAK,qBAAqB,MAAM,EAAE,QAAQ,qBAAqB,OAAO,CAAC;AAAA,IAC7F;AACA,UAAM;AAAA,EACR;AAEA,QAAM,QAAQ,gBAAgB;AAE9B,QAAM,UAAU,MAAM,oBAAoB,SAAS,MAAM,EAAE;AAC3D,SAAO,aAAa,KAAK,EAAE,IAAI,MAAM,SAAS,MAAM,IAAI,QAAQ,SAAS,UAAU,YAAY,CAAC;AAClG;AAEO,MAAM,UAA2B;AAAA,EACtC,KAAK;AAAA,EACL,SAAS;AAAA,EACT,SAAS;AAAA,IACP,MAAM;AAAA,MACJ,SAAS;AAAA,MACT,WAAW;AAAA,QACT;AAAA,UACE,QAAQ;AAAA,UACR,aAAa;AAAA,UACb,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,GAAG,SAAS,EAAE,OAAO,EAAE,KAAK,GAAG,QAAQ,EAAE,OAAO,EAAE,CAAC;AAAA,QACtF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
6
6
  "names": []
7
7
  }
@@ -17,16 +17,16 @@ async function POST(req, ctx) {
17
17
  const contextOrResponse = await resolvePortalActionContext(req);
18
18
  if (contextOrResponse instanceof Response) return contextOrResponse;
19
19
  const context = contextOrResponse;
20
+ const { translate } = await resolveTranslations();
20
21
  const claimId = await resolvePortalClaimId(ctx);
21
22
  if (!claimId) {
22
- return NextResponse.json({ ok: false, error: "warranty_claims.errors.notFound" }, { status: 404 });
23
+ return NextResponse.json({ ok: false, error: translate("warranty_claims.errors.notFound", "Claim not found.") }, { status: 404 });
23
24
  }
24
25
  const claim = await loadOwnedClaim(context, claimId);
25
26
  if (!claim) {
26
- return NextResponse.json({ ok: false, error: "warranty_claims.errors.notFound" }, { status: 404 });
27
+ return NextResponse.json({ ok: false, error: translate("warranty_claims.errors.notFound", "Claim not found.") }, { status: 404 });
27
28
  }
28
29
  if (!PORTAL_WITHDRAWABLE_STATUSES.has(claim.status)) {
29
- const { translate } = await resolveTranslations();
30
30
  return NextResponse.json(
31
31
  { ok: false, error: translate("warranty_claims.errors.invalidTransition", "That status change is not allowed.") },
32
32
  { status: 400 }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../../../../src/modules/warranty_claims/api/portal/claims/%5Bid%5D/withdraw/route.ts"],
4
- "sourcesContent": ["import { NextResponse } from 'next/server'\nimport { z } from 'zod'\nimport type { CommandBus } from '@open-mercato/shared/lib/commands'\nimport { isCrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { getCommandInterceptorHttpRejection } from '@open-mercato/shared/lib/commands/errors'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport type { TransitionClaimInput, WarrantyClaimStatus } from '../../../../../data/validators'\nimport {\n loadOwnedClaim,\n resolvePortalActionContext,\n resolvePortalClaimId,\n runPortalClaimActionGuard,\n type PortalClaimActionRouteContext,\n} from '../shared'\n\nexport const metadata = {\n POST: { requireAuth: false },\n}\n\nconst PORTAL_WITHDRAWABLE_STATUSES = new Set<WarrantyClaimStatus>(['draft', 'submitted'])\n\nexport async function POST(req: Request, ctx: PortalClaimActionRouteContext) {\n const contextOrResponse = await resolvePortalActionContext(req)\n if (contextOrResponse instanceof Response) return contextOrResponse\n const context = contextOrResponse\n const claimId = await resolvePortalClaimId(ctx)\n if (!claimId) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.notFound' }, { status: 404 })\n }\n const claim = await loadOwnedClaim(context, claimId)\n if (!claim) {\n return NextResponse.json({ ok: false, error: 'warranty_claims.errors.notFound' }, { status: 404 })\n }\n if (!PORTAL_WITHDRAWABLE_STATUSES.has(claim.status)) {\n const { translate } = await resolveTranslations()\n return NextResponse.json(\n { ok: false, error: translate('warranty_claims.errors.invalidTransition', 'That status change is not allowed.') },\n { status: 400 },\n )\n }\n\n const guarded = await runPortalClaimActionGuard(req, context, claim.id, {\n id: claim.id,\n action: 'withdraw',\n toStatus: 'cancelled',\n })\n if (!guarded.ok) {\n return guarded.response\n }\n\n const commandBus = context.commandCtx.container.resolve('commandBus') as CommandBus\n try {\n await commandBus.execute<TransitionClaimInput, { claimId: string }>(\n 'warranty_claims.claim.transition',\n {\n input: { id: claim.id, toStatus: 'cancelled', actorCustomerId: context.customerId },\n ctx: context.commandCtx,\n },\n )\n } catch (err) {\n if (isCrudHttpError(err)) {\n return NextResponse.json(err.body, { status: err.status })\n }\n const interceptorRejection = getCommandInterceptorHttpRejection(err)\n if (interceptorRejection) {\n return NextResponse.json(interceptorRejection.body, { status: interceptorRejection.status })\n }\n throw err\n }\n\n await guarded.runAfterSuccess()\n\n return NextResponse.json({ ok: true, claimId: claim.id, status: 'cancelled' })\n}\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Warranty Claims Portal',\n summary: 'Customer portal warranty claim withdraw action',\n methods: {\n POST: {\n summary: 'Withdraw (cancel) an owned claim while it is draft or submitted',\n responses: [\n {\n status: 200,\n description: 'Claim withdrawn',\n schema: z.object({ ok: z.boolean(), claimId: z.string().uuid(), status: z.literal('cancelled') }),\n },\n ],\n },\n },\n}\n"],
5
- "mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAElB,SAAS,uBAAuB;AAChC,SAAS,0CAA0C;AACnD,SAAS,2BAA2B;AAGpC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AAEA,MAAM,WAAW;AAAA,EACtB,MAAM,EAAE,aAAa,MAAM;AAC7B;AAEA,MAAM,+BAA+B,oBAAI,IAAyB,CAA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,OAAO,CAAC;AAAA,IAC3D;AACA,UAAM,uBAAuB,mCAAmC,GAAG;AACnE,QAAI,sBAAsB;AACxB,aAAO,aAAa,KAAK,qBAAqB,MAAM,EAAE,QAAQ,qBAAqB,OAAO,CAAC;AAAA,IAC7F;AACA,UAAM;AAAA,EACR;AAEA,QAAM,QAAQ,gBAAgB;AAE9B,SAAO,aAAa,KAAK,EAAE,IAAI,MAAM,SAAS,MAAM,IAAI,QAAQ,YAAY,CAAC;AAC/E;AAEO,MAAM,UAA2B;AAAA,EACtC,KAAK;AAAA,EACL,SAAS;AAAA,EACT,SAAS;AAAA,IACP,MAAM;AAAA,MACJ,SAAS;AAAA,MACT,WAAW;AAAA,QACT;AAAA,UACE,QAAQ;AAAA,UACR,aAAa;AAAA,UACb,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,GAAG,SAAS,EAAE,OAAO,EAAE,KAAK,GAAG,QAAQ,EAAE,QAAQ,WAAW,EAAE,CAAC;AAAA,QAClG;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import { NextResponse } from 'next/server'\nimport { z } from 'zod'\nimport type { CommandBus } from '@open-mercato/shared/lib/commands'\nimport { isCrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { getCommandInterceptorHttpRejection } from '@open-mercato/shared/lib/commands/errors'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport type { TransitionClaimInput, WarrantyClaimStatus } from '../../../../../data/validators'\nimport {\n loadOwnedClaim,\n resolvePortalActionContext,\n resolvePortalClaimId,\n runPortalClaimActionGuard,\n type PortalClaimActionRouteContext,\n} from '../shared'\n\nexport const metadata = {\n POST: { requireAuth: false },\n}\n\nconst PORTAL_WITHDRAWABLE_STATUSES = new Set<WarrantyClaimStatus>(['draft', 'submitted'])\n\nexport async function POST(req: Request, ctx: PortalClaimActionRouteContext) {\n const contextOrResponse = await resolvePortalActionContext(req)\n if (contextOrResponse instanceof Response) return contextOrResponse\n const context = contextOrResponse\n const { translate } = await resolveTranslations()\n const claimId = await resolvePortalClaimId(ctx)\n if (!claimId) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.notFound', 'Claim not found.') }, { status: 404 })\n }\n const claim = await loadOwnedClaim(context, claimId)\n if (!claim) {\n return NextResponse.json({ ok: false, error: translate('warranty_claims.errors.notFound', 'Claim not found.') }, { status: 404 })\n }\n if (!PORTAL_WITHDRAWABLE_STATUSES.has(claim.status)) {\n return NextResponse.json(\n { ok: false, error: translate('warranty_claims.errors.invalidTransition', 'That status change is not allowed.') },\n { status: 400 },\n )\n }\n\n const guarded = await runPortalClaimActionGuard(req, context, claim.id, {\n id: claim.id,\n action: 'withdraw',\n toStatus: 'cancelled',\n })\n if (!guarded.ok) {\n return guarded.response\n }\n\n const commandBus = context.commandCtx.container.resolve('commandBus') as CommandBus\n try {\n await commandBus.execute<TransitionClaimInput, { claimId: string }>(\n 'warranty_claims.claim.transition',\n {\n input: { id: claim.id, toStatus: 'cancelled', actorCustomerId: context.customerId },\n ctx: context.commandCtx,\n },\n )\n } catch (err) {\n if (isCrudHttpError(err)) {\n return NextResponse.json(err.body, { status: err.status })\n }\n const interceptorRejection = getCommandInterceptorHttpRejection(err)\n if (interceptorRejection) {\n return NextResponse.json(interceptorRejection.body, { status: interceptorRejection.status })\n }\n throw err\n }\n\n await guarded.runAfterSuccess()\n\n return NextResponse.json({ ok: true, claimId: claim.id, status: 'cancelled' })\n}\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Warranty Claims Portal',\n summary: 'Customer portal warranty claim withdraw action',\n methods: {\n POST: {\n summary: 'Withdraw (cancel) an owned claim while it is draft or submitted',\n responses: [\n {\n status: 200,\n description: 'Claim withdrawn',\n schema: z.object({ ok: z.boolean(), claimId: z.string().uuid(), status: z.literal('cancelled') }),\n },\n ],\n },\n },\n}\n"],
5
+ "mappings": "AAAA,SAAS,oBAA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YAAY;AACpE,MAAI;AACF,UAAM,WAAW;AAAA,MACf;AAAA,MACA;AAAA,QACE,OAAO,EAAE,IAAI,MAAM,IAAI,UAAU,aAAa,iBAAiB,QAAQ,WAAW;AAAA,QAClF,KAAK,QAAQ;AAAA,MACf;AAAA,IACF;AAAA,EACF,SAAS,KAAK;AACZ,QAAI,gBAAgB,GAAG,GAAG;AACxB,aAAO,aAAa,KAAK,IAAI,MAAM,EAAE,QAAQ,IAAI,OAAO,CAAC;AAAA,IAC3D;AACA,UAAM,uBAAuB,mCAAmC,GAAG;AACnE,QAAI,sBAAsB;AACxB,aAAO,aAAa,KAAK,qBAAqB,MAAM,EAAE,QAAQ,qBAAqB,OAAO,CAAC;AAAA,IAC7F;AACA,UAAM;AAAA,EACR;AAEA,QAAM,QAAQ,gBAAgB;AAE9B,SAAO,aAAa,KAAK,EAAE,IAAI,MAAM,SAAS,MAAM,IAAI,QAAQ,YAAY,CAAC;AAC/E;AAEO,MAAM,UAA2B;AAAA,EACtC,KAAK;AAAA,EACL,SAAS;AAAA,EACT,SAAS;AAAA,IACP,MAAM;AAAA,MACJ,SAAS;AAAA,MACT,WAAW;AAAA,QACT;AAAA,UACE,QAAQ;AAAA,UACR,aAAa;AAAA,UACb,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,GAAG,SAAS,EAAE,OAAO,EAAE,KAAK,GAAG,QAAQ,EAAE,QAAQ,WAAW,EAAE,CAAC;AAAA,QAClG;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;",
6
6
  "names": []
7
7
  }
@@ -8,7 +8,7 @@ import { runRouteMutationGuards } from "@open-mercato/shared/lib/crud/route-muta
8
8
  import { findWithDecryption } from "@open-mercato/shared/lib/encryption/find";
9
9
  import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
10
10
  import { createLogger } from "@open-mercato/shared/lib/logger";
11
- import { getCustomerAuthFromRequest } from "@open-mercato/core/modules/customer_accounts/lib/customerAuth";
11
+ import { resolveLinkedCustomerAuth } from "../../../lib/portalAuthGuard.js";
12
12
  import { WarrantyClaim, WarrantyClaimLine } from "../../../data/entities.js";
13
13
  import {
14
14
  claimStatusSchema,
@@ -100,14 +100,9 @@ function serializePortalClaim(claim, lines) {
100
100
  };
101
101
  }
102
102
  async function resolvePortalContext(req) {
103
- const { translate } = await resolveTranslations();
104
- const auth = await getCustomerAuthFromRequest(req);
105
- if (!auth) {
106
- return NextResponse.json({ ok: false, error: translate("warranty_claims.errors.unauthorized", "Unauthorized") }, { status: 401 });
107
- }
108
- if (!auth.customerEntityId) {
109
- return NextResponse.json({ ok: false, error: translate("warranty_claims.errors.customerAccountNotLinked", "Customer account is not linked to a customer record") }, { status: 403 });
110
- }
103
+ const authOrResponse = await resolveLinkedCustomerAuth(req);
104
+ if (authOrResponse instanceof Response) return authOrResponse;
105
+ const auth = authOrResponse;
111
106
  const container = await createRequestContainer();
112
107
  const em = container.resolve("em");
113
108
  const commandAuth = {