condition-map-db.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { Client } from 'pg'
  2. /**
  3. * Database peeks for the condition map's carry forward: what "Confirm all as
  4. * still there" wrote, read back with the column that links an earlier mark to
  5. * the one a later visit recorded in its place. Kept apart from `db.ts`, in the
  6. * same plain-pg style, so the carry specs can grow without touching it.
  7. */
  8. async function withDb<T>(fn: (db: Client) => Promise<T>): Promise<T> {
  9. const url = process.env.E2E_DATABASE_URL
  10. if (!url) throw new Error('E2E_DATABASE_URL is not set. See e2e/README.md.')
  11. const db = new Client({ connectionString: url })
  12. await db.connect()
  13. try {
  14. return await fn(db)
  15. } finally {
  16. await db.end()
  17. }
  18. }
  19. export interface CarryMarkRow {
  20. id: string
  21. serviceRecordId: string | null
  22. inspectionId: string | null
  23. inspectionItemId: string | null
  24. bodyType: string
  25. view: string
  26. panel: string
  27. x: number
  28. y: number
  29. kind: string
  30. severity: string
  31. note: string | null
  32. imageUrls: string[]
  33. resolvedAt: Date | null
  34. carriedToId: string | null
  35. }
  36. /** Every mark on the vehicle with its carry link, oldest first, resolved ones included. */
  37. export async function marksWithCarry(vehicleId: string): Promise<CarryMarkRow[]> {
  38. return withDb(async (db) => {
  39. const result = await db.query<CarryMarkRow>(
  40. `select id, "serviceRecordId", "inspectionId", "inspectionItemId", "bodyType", view, panel,
  41. x, y, kind, severity, note, "imageUrls", "resolvedAt", "carriedToId"
  42. from condition_marks where "vehicleId" = $1 order by "recordedAt", "createdAt"`,
  43. [vehicleId]
  44. )
  45. return result.rows
  46. })
  47. }
  48. /** Puts photos on a planted mark, as if they had been taken in its editor. */
  49. export async function setMarkPhotos(markId: string, urls: string[]): Promise<void> {
  50. await withDb((db) =>
  51. db.query(`update condition_marks set "imageUrls" = $2 where id = $1`, [markId, urls])
  52. )
  53. }