Lumen & Gesso / website · lumengesso.com / app / lib / order-messages.ts
| 1 | export const ORDER_MESSAGE_LIMIT = 2000; |
| 2 | export const PROOF_PUBLISH_STATUSES = ['pending_design', 'revision_requested']; |
| 3 | export const STUDIO_MESSAGE_STATUSES = [...PROOF_PUBLISH_STATUSES, 'awaiting_approval', 'approved', 'in_production', 'shipped']; |
| 4 | |
| 5 | type Revision = { note: string; at: string; generationId?: string }; |
| 6 | type SavedMessage = Revision & { id: string; question?: boolean }; |
| 7 | export type OrderMessage = Revision & { author: 'studio' | 'customer'; kind: 'message' | 'proof' | 'revision' | 'question'; generationId?: string }; |
| 8 | type ProofImageSize = { width: number; height: number }; |
| 9 | export type PrintUpscale = { generationId: string; original: ProofImageSize; upscaled: ProofImageSize }; |
| 10 | type RevisionDraft = { generationId: string; baseGenerationId: string; feedbackAt: string; printUpscale?: PrintUpscale }; |
| 11 | type SentRevision = RevisionDraft & { feedback: Revision }; |
| 12 | |
| 13 | function isRevisionDraft(value: unknown): value is RevisionDraft { |
| 14 | return !!value && typeof value === 'object' |
| 15 | && 'generationId' in value && typeof value.generationId === 'string' |
| 16 | && 'baseGenerationId' in value && typeof value.baseGenerationId === 'string' |
| 17 | && 'feedbackAt' in value && typeof value.feedbackAt === 'string'; |
| 18 | } |
| 19 | |
| 20 | function isProofImageSize(value: unknown): value is ProofImageSize { |
| 21 | return !!value && typeof value === 'object' |
| 22 | && 'width' in value && typeof value.width === 'number' && Number.isSafeInteger(value.width) && value.width > 0 |
| 23 | && 'height' in value && typeof value.height === 'number' && Number.isSafeInteger(value.height) && value.height > 0; |
| 24 | } |
| 25 | |
| 26 | function readPrintUpscale(value: unknown): PrintUpscale | undefined { |
| 27 | if (!value || typeof value !== 'object' || !('generationId' in value) || typeof value.generationId !== 'string' |
| 28 | || !('original' in value) || !isProofImageSize(value.original) || !('upscaled' in value) || !isProofImageSize(value.upscaled)) return undefined; |
| 29 | return { generationId: value.generationId, original: value.original, upscaled: value.upscaled }; |
| 30 | } |
| 31 | |
| 32 | export function orderMessageConfig(config: unknown) { |
| 33 | const fields = config && typeof config === 'object' && !Array.isArray(config) |
| 34 | ? config as Record<string, unknown> |
| 35 | : {}; |
| 36 | const isRevision = (item: unknown): item is Revision => ( |
| 37 | !!item && typeof item === 'object' |
| 38 | && 'note' in item && typeof item.note === 'string' && !!item.note.trim() |
| 39 | && 'at' in item && typeof item.at === 'string' && Number.isFinite(Date.parse(item.at)) |
| 40 | && (!('generationId' in item) || typeof item.generationId === 'string') |
| 41 | ); |
| 42 | const isSavedMessage = (item: unknown): item is SavedMessage => ( |
| 43 | isRevision(item) && 'id' in item && typeof item.id === 'string' |
| 44 | ); |
| 45 | return { |
| 46 | ...fields, |
| 47 | proofPublishedAt: typeof fields.proofPublishedAt === 'string' ? fields.proofPublishedAt : undefined, |
| 48 | proofReviewResumedAt: typeof fields.proofReviewResumedAt === 'string' ? fields.proofReviewResumedAt : undefined, |
| 49 | revisionDraft: isRevisionDraft(fields.revisionDraft) ? fields.revisionDraft : null, |
| 50 | sentRevisions: Array.isArray(fields.sentRevisions) |
| 51 | ? fields.sentRevisions.filter((item): item is SentRevision => |
| 52 | isRevisionDraft(item) && 'feedback' in item && isRevision(item.feedback)) : [], |
| 53 | revisions: Array.isArray(fields.revisions) ? fields.revisions.filter(isRevision) : [], |
| 54 | studioMessages: Array.isArray(fields.studioMessages) ? fields.studioMessages.filter(isSavedMessage) : [], |
| 55 | customerMessages: Array.isArray(fields.customerMessages) ? fields.customerMessages.filter(isSavedMessage) : [], |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | export function customerRevisions(config: unknown, pending: string | null, updatedAt: string): Revision[] { |
| 60 | const { revisions } = orderMessageConfig(config); |
| 61 | const note = pending?.trim(); |
| 62 | return note && revisions.at(-1)?.note.trim() !== note ? [...revisions, { note, at: updatedAt }] : revisions; |
| 63 | } |
| 64 | |
| 65 | export function orderConversation(config: unknown, pending: string | null, updatedAt: string): OrderMessage[] { |
| 66 | const { studioMessages, customerMessages } = orderMessageConfig(config); |
| 67 | const messages: OrderMessage[] = [ |
| 68 | ...studioMessages.map(({ note, at, generationId }) => ({ note, at, generationId, author: 'studio' as const, kind: generationId ? 'proof' as const : 'message' as const })), |
| 69 | ...customerMessages.map(({ note, at, generationId, question }) => ({ note, at, generationId, author: 'customer' as const, kind: question === true ? 'question' as const : 'message' as const })), |
| 70 | ...customerRevisions(config, pending, updatedAt).map(({ note, at, generationId }) => ({ note, at, generationId, author: 'customer' as const, kind: 'revision' as const })), |
| 71 | ]; |
| 72 | return messages.sort((a, b) => Date.parse(a.at) - Date.parse(b.at)); |
| 73 | } |
| 74 | |
| 75 | type ProofOrder = { |
| 76 | status: string; current_generation_id: string | null; config: unknown; revision_note: string | null; updated_at: string; |
| 77 | }; |
| 78 | |
| 79 | export function feedbackOnCurrentProof(order: ProofOrder): Revision | null { |
| 80 | const baseGenerationId = order.current_generation_id; |
| 81 | if (!baseGenerationId) return null; |
| 82 | const history = orderMessageConfig(order.config); |
| 83 | if (order.revision_note?.trim()) { |
| 84 | return customerRevisions(order.config, order.revision_note, order.updated_at).at(-1) ?? null; |
| 85 | } |
| 86 | const publishedAt = history.proofPublishedAt ?? history.studioMessages.findLast(message => message.generationId === baseGenerationId)?.at; |
| 87 | // Older proof responses were saved as chat messages. Only recover a |
| 88 | // message when it belongs to this proof or follows a known publish time. |
| 89 | return orderConversation(order.config, null, order.updated_at).findLast(message => { |
| 90 | if (message.author !== 'customer' || message.kind === 'question') return false; |
| 91 | if (history.proofReviewResumedAt && Date.parse(message.at) <= Date.parse(history.proofReviewResumedAt)) return false; |
| 92 | if (message.generationId) return message.generationId === baseGenerationId; |
| 93 | return !!publishedAt && Date.parse(message.at) > Date.parse(publishedAt); |
| 94 | }) ?? null; |
| 95 | } |
| 96 | |
| 97 | export function activeProofRevision(order: ProofOrder) { |
| 98 | const baseGenerationId = order.current_generation_id; |
| 99 | if (order.status !== 'revision_requested' || !baseGenerationId) return null; |
| 100 | const feedback = feedbackOnCurrentProof(order); |
| 101 | if (!feedback) return null; |
| 102 | const { revisionDraft } = orderMessageConfig(order.config); |
| 103 | const draftId = revisionDraft?.baseGenerationId === baseGenerationId && revisionDraft.feedbackAt === feedback.at |
| 104 | ? revisionDraft.generationId : null; |
| 105 | return { baseGenerationId, feedback, draftId, printUpscale: draftId ? readPrintUpscale(revisionDraft?.printUpscale) : undefined }; |
| 106 | } |
| 107 | |
| 108 | export function pendingCustomerMessage(config: unknown, pending: string | null, updatedAt: string): OrderMessage | null { |
| 109 | const history = orderMessageConfig(config); |
| 110 | const proofPublishedAt = typeof history.proofPublishedAt === 'string' ? Date.parse(history.proofPublishedAt) : NaN; |
| 111 | const latest = orderConversation(config, pending, updatedAt).at(-1); |
| 112 | if (latest?.author !== 'customer' || Date.parse(latest.at) <= proofPublishedAt) return null; |
| 113 | return latest; |
| 114 | } |