60 lines
3.1 KiB
TypeScript
60 lines
3.1 KiB
TypeScript
import { prisma } from '@/lib/prisma'
|
|
|
|
type ListingUpdate = {
|
|
eventId: string
|
|
sequence: number
|
|
state: 'listing' | 'listed' | 'failed'
|
|
listingId: string | null
|
|
message: string | null
|
|
}
|
|
|
|
export type ListingStatus = ListingUpdate & { updatedAt: string }
|
|
|
|
export const pendingListingStatus = { state: 'pending', sequence: 0 } as const
|
|
|
|
export function parseListingUpdate(body: unknown): ListingUpdate | null {
|
|
if (!body || typeof body !== 'object') return null
|
|
const { eventId, sequence, state, listingId, message } = body as Record<string, unknown>
|
|
if (typeof eventId !== 'string' || !/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(eventId)
|
|
|| typeof sequence !== 'number' || !Number.isSafeInteger(sequence) || sequence < 1
|
|
|| (state !== 'listing' && state !== 'listed' && state !== 'failed')) return null
|
|
if (listingId != null && (typeof listingId !== 'string' || !listingId.trim() || listingId.length > 160)) return null
|
|
if (message != null && (typeof message !== 'string' || !message.trim() || message.length > 500)) return null
|
|
return { eventId, sequence, state, listingId: listingId?.trim() ?? null, message: message?.trim() ?? null }
|
|
}
|
|
|
|
export async function recordListingStatus(materialId: string, update: ListingUpdate) {
|
|
return prisma.$transaction(async (tx) => {
|
|
const [row] = await tx.$queryRaw<Array<{ eventId: string; deletedAt: Date | null; listingStatus: ListingStatus | null }>>`
|
|
SELECT e.id AS "eventId", m."deletedAt", d.listing_status AS "listingStatus"
|
|
FROM desktop_material_deliveries d
|
|
JOIN xiaoyi_auto_listing_events e ON e.material_id = d.material_id
|
|
JOIN materials m ON m.id = d.material_id
|
|
WHERE d.material_id = ${materialId} AND m.access_scope = 'general' AND m.task_detail_private = false
|
|
FOR UPDATE OF d, m
|
|
`
|
|
if (!row) return { status: 404, code: 'MATERIAL_NOT_FOUND', message: '小壹交付素材不存在' } as const
|
|
if (row.deletedAt) return { status: 410, code: 'MATERIAL_DELETED', message: '素材已撤回' } as const
|
|
if (row.eventId !== update.eventId) return { status: 409, code: 'EVENT_MISMATCH', message: '事件 ID 与素材不匹配' } as const
|
|
|
|
const current = row.listingStatus
|
|
if (current && update.sequence <= current.sequence) {
|
|
if (update.sequence === current.sequence && (update.state !== current.state
|
|
|| update.listingId !== current.listingId || update.message !== current.message)) {
|
|
return { status: 409, code: 'SEQUENCE_CONFLICT', message: '同一状态序号的内容必须一致' } as const
|
|
}
|
|
return { status: 200, applied: false, listingStatus: current } as const
|
|
}
|
|
if (current?.state === 'listed') {
|
|
return { status: 409, code: 'ALREADY_LISTED', message: '上架成功后不能再更新此交付的状态' } as const
|
|
}
|
|
const listingStatus = { ...update, updatedAt: new Date().toISOString() }
|
|
await tx.$executeRaw`
|
|
UPDATE desktop_material_deliveries
|
|
SET listing_status = ${JSON.stringify(listingStatus)}::jsonb
|
|
WHERE material_id = ${materialId}
|
|
`
|
|
return { status: 200, applied: true, listingStatus } as const
|
|
})
|
|
}
|