4
0

import.ts 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571
  1. import { isStage } from '@/features/work-order-statuses/Lib/stages'
  2. import { copyFile, mkdir, readdir, rename } from 'node:fs/promises'
  3. import path from 'node:path'
  4. import { db } from '@/lib/db'
  5. import type { TxClient } from '@/lib/db'
  6. import {
  7. keepsCurrentPortalMessages,
  8. portalMessageAuthorIds,
  9. portalMessageRowsToRestore,
  10. } from '@/lib/backup/portal-messages'
  11. import { clearPlanFor, UPLOAD_CATEGORIES } from '@/lib/backup/manifest'
  12. import {
  13. rewriteFileUrl,
  14. rewriteFileUrlsWithin,
  15. withFileUrls,
  16. withIssuedDataFileUrls,
  17. } from '@/lib/backup/file-urls'
  18. import { columnsOf } from '@/lib/backup/rows'
  19. import { toSafeDate } from '@/lib/invoice-utils'
  20. import { taxComponentsForCopy } from '@/features/settings/Lib/workshopTax'
  21. import { warrantyFromUntyped } from '@/lib/warranty'
  22. import { atZonedTime } from '@/lib/timezone'
  23. import { resolveWorkshopTimeZone } from '@/lib/workshop-timezone'
  24. import { upgradeLegacySettings } from '@/lib/backup/legacy-settings'
  25. import { SETTING_KEYS } from '@/features/settings/Schema/settingsSchema'
  26. import { generateWebhookSecret } from '@/features/webhooks/Lib/sign'
  27. import {
  28. isRedacted,
  29. isSecretSettingKey,
  30. keepCurrentSecrets,
  31. keepCurrentWebhookSecrets,
  32. } from '@/lib/backup/secrets'
  33. import { Prisma } from '@/generated/prisma/client'
  34. import { extractEntry, type OpenedZip, readJsonFile, type ZipEntry } from '@/lib/backup/zip-file'
  35. import { releaseFilesNotRestored } from '@/lib/files/manager'
  36. import { uploadsRoot } from '@/lib/upload-root'
  37. import { randomUUID } from 'node:crypto'
  38. import { normalizePlate } from '@/lib/license-plate'
  39. import { tenantScoped } from './tenant-guard'
  40. import {
  41. acquireRestoreLock,
  42. assertNoForeignRows,
  43. collectFileIds,
  44. danglingReferences,
  45. lookupOwnIds,
  46. mergeIds,
  47. releaseRestoreLock,
  48. RestoreRefusedError,
  49. sanitiseForeignIds,
  50. } from './tenancy'
  51. /**
  52. * Putting a backup back into a workshop.
  53. *
  54. * The same code serves the zip a person uploads under Settings, Data and a
  55. * snapshot restic restored from a backup destination: both end up as a
  56. * parsed `data.json` and a source of files, and from there on nothing
  57. * cares where they came from. The rows are replaced inside one transaction,
  58. * so a backup that cannot be read back leaves the workshop as it was; the
  59. * files are written afterwards, over whatever has the same name.
  60. */
  61. /** How long the row replacement may take: a large workshop with its files is minutes. */
  62. const TRANSACTION_TIMEOUT_MS = 60 * 60_000
  63. export interface BackupData {
  64. version: number
  65. exportedAt?: string
  66. /**
  67. * What the backup was written with: "data" when secret keys were left out
  68. * ("Data only"), "dataAndSecrets" when they are in it. Absent in a file
  69. * from before the header said so.
  70. */
  71. contents?: 'data' | 'dataAndSecrets'
  72. data: Record<string, any>
  73. }
  74. const BACKUP_CONTENTS: readonly unknown[] = ['data', 'dataAndSecrets']
  75. /** Whose workshop the backup goes into, and who is doing it. */
  76. export interface RestoreActor {
  77. organizationId: string
  78. userId: string
  79. }
  80. /** One uploaded file in a backup: `files/<category>/<name>` in a zip, `<category>/<name>` on disk. */
  81. export interface BackupFileEntry {
  82. category: string
  83. name: string
  84. }
  85. /** Where a backup's files come from: the archive, or a folder restic restored. */
  86. export interface BackupFileSource {
  87. entries: readonly BackupFileEntry[]
  88. /** Writes one entry to `target`, creating the folder it goes in. */
  89. extract(entry: BackupFileEntry, target: string): Promise<void>
  90. }
  91. /** Throws when the file is not a backup this version can read. */
  92. export function assertSupportedBackup(backup: unknown): asserts backup is BackupData {
  93. const b = backup as Partial<BackupData> | null | undefined
  94. if (!b?.version || (b.version !== 1 && b.version !== 2)) {
  95. throw new Error('Unsupported or missing backup version')
  96. }
  97. if (b.contents !== undefined && !BACKUP_CONTENTS.includes(b.contents)) {
  98. throw new Error('Unsupported backup contents')
  99. }
  100. if (!b.data) throw new Error('No data found in backup')
  101. }
  102. /**
  103. * The files inside an archive. Both layouts are read:
  104. * files/{category}/{filename} (v2 backup)
  105. * uploads/{category}/{filename} (legacy backup)
  106. */
  107. export function zipFileSource(zip: OpenedZip): BackupFileSource {
  108. const raw = new Map<BackupFileEntry, ZipEntry>()
  109. for (const e of zip.entries) {
  110. if (e.dir || !(e.name.startsWith('files/') || e.name.startsWith('uploads/'))) continue
  111. const parts = e.name.split('/')
  112. if (parts.length !== 3) continue
  113. raw.set({ category: parts[1], name: parts[2] }, e)
  114. }
  115. return {
  116. entries: [...raw.keys()],
  117. extract(entry, target) {
  118. const zipEntry = raw.get(entry)
  119. if (!zipEntry) throw new Error(`Unknown backup file ${entry.category}/${entry.name}`)
  120. return extractEntry(zip, zipEntry, target)
  121. },
  122. }
  123. }
  124. /**
  125. * The files in a restored uploads folder: one subfolder per category, as the
  126. * backup engine took them. Each file is moved into place, or copied when
  127. * the folder is on another disk; the restored folder is thrown away after.
  128. */
  129. export async function directoryFileSource(dir: string): Promise<BackupFileSource> {
  130. const entries: BackupFileEntry[] = []
  131. for (const category of await readdir(dir, { withFileTypes: true })) {
  132. if (!category.isDirectory()) continue
  133. for (const file of await readdir(path.join(dir, category.name), { withFileTypes: true })) {
  134. if (file.isFile()) entries.push({ category: category.name, name: file.name })
  135. }
  136. }
  137. return {
  138. entries,
  139. async extract(entry, target) {
  140. const source = path.join(dir, entry.category, entry.name)
  141. await mkdir(path.dirname(target), { recursive: true })
  142. try {
  143. await rename(source, target)
  144. } catch (err) {
  145. if ((err as NodeJS.ErrnoException).code !== 'EXDEV') throw err
  146. await copyFile(source, target)
  147. }
  148. },
  149. }
  150. }
  151. /**
  152. * A document's taxable subtotal as the backup holds it: the figure when some
  153. * of its lines were untaxed, null (the whole subtotal) otherwise, which is
  154. * also what every backup from before the column existed says by omission.
  155. */
  156. function storedTaxableSubtotalFromBackup(value: unknown): number | null {
  157. return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : null
  158. }
  159. /**
  160. * A number as the backup holds it, zero included. The fallback is for a file
  161. * that has none: written as `value || fallback`, a part line with a quantity
  162. * of zero came back as one and an odometer at zero as unknown.
  163. */
  164. function numberOr<T>(value: unknown, fallback: T): number | T {
  165. return typeof value === 'number' && Number.isFinite(value) ? value : fallback
  166. }
  167. /**
  168. * A vehicle's year as the backup holds it. The column is a non-null
  169. * integer, and 0 is the app's unknown-year marker, so a row without a
  170. * decodable year restores as 0 instead of failing the whole restore
  171. * mid-transaction; a year written as text (a hand-edited backup) is read
  172. * as its number, as the vehicle form's own coercion does. Values above
  173. * the column's signed 32-bit maximum are rejected the same way: they
  174. * would still fail the insert and roll the restore back.
  175. */
  176. function restoredVehicleYear(value: unknown): number {
  177. const n = typeof value === 'number' ? value : typeof value === 'string' ? Number(value) : NaN
  178. return Number.isInteger(n) && n > 0 && n <= 2147483647 ? n : 0
  179. }
  180. /** A foreign key from the backup, kept only when this import restored its target. */
  181. function keptReference(id: unknown, restored: ReadonlySet<string> | undefined): string | null {
  182. return typeof id === 'string' && id && restored?.has(id) ? id : null
  183. }
  184. /**
  185. * When a restored inspection's link started. A backup from before links
  186. * carried a start date has none: the link then counts from the inspection's
  187. * last change, as the migration did, or it would never expire.
  188. */
  189. function restoredInspectionSharedAt(insp: Record<string, unknown>): Date | null {
  190. const sharedAt = toSafeDate(insp.sharedAt as string)
  191. if (sharedAt || !insp.publicToken) return sharedAt ?? null
  192. return toSafeDate(insp.updatedAt as string) ?? new Date()
  193. }
  194. /**
  195. * Restores one service record with its nested parts/labor/attachments/payments.
  196. * Used for both vehicle-linked records (nested under vehicles in the backup)
  197. * and counter sales (top-level records without a vehicle).
  198. */
  199. async function importServiceRecordTree(
  200. tx: TxClient,
  201. sr: Record<string, unknown>,
  202. opts: {
  203. organizationId: string
  204. vehicleId: string | null
  205. customerId: string | null
  206. workDayStartTime: string
  207. /** The restored workshop's zone, which workDayStartTime is read in. */
  208. timeZone: string
  209. /** Technicians restored by this import. See the time entries below. */
  210. technicianIds: ReadonlySet<string>
  211. /** The workshop's own statuses restored by this import. */
  212. workOrderStatusIds: ReadonlySet<string>
  213. /**
  214. * The people in the restored workshop. Whoever opened or unlocked a job is
  215. * kept only when they are one of them: a backup brought to another install
  216. * names accounts that do not exist there.
  217. */
  218. memberUserIds: ReadonlySet<string>
  219. /**
  220. * Designs and snapshots restored by this import. A reference to one the
  221. * backup did not carry is dropped rather than left dangling: the record
  222. * then prints from the default, as any invoice without a design does.
  223. */
  224. designIds?: ReadonlySet<string>
  225. designSnapshotIds?: ReadonlySet<string>
  226. assetSnapshotIds?: ReadonlySet<string>
  227. }
  228. ) {
  229. // Derive startDateTime/endDateTime from backup or fall back to serviceDate + work day start
  230. let startDT: Date | undefined
  231. let endDT: Date | undefined
  232. if (sr.startDateTime) {
  233. startDT = toSafeDate(sr.startDateTime as string)
  234. }
  235. if (!startDT && sr.serviceDate) {
  236. const sd = toSafeDate(sr.serviceDate as string)
  237. if (sd) {
  238. startDT = atZonedTime(sd, opts.workDayStartTime, opts.timeZone)
  239. }
  240. }
  241. if (sr.endDateTime) {
  242. endDT = toSafeDate(sr.endDateTime as string)
  243. }
  244. if (!endDT && startDT) {
  245. endDT = new Date(startDT.getTime() + 3600000)
  246. }
  247. await tx.serviceRecord.create({
  248. data: {
  249. id: sr.id as string,
  250. organizationId: opts.organizationId,
  251. title: sr.title as string,
  252. description: (sr.description as string) || null,
  253. type: (sr.type as string) || 'maintenance',
  254. status: (sr.status as string) || 'completed',
  255. cost: (sr.cost as number) || 0,
  256. mileage: numberOr(sr.mileage, null),
  257. serviceDate: toSafeDate(sr.serviceDate as string),
  258. startDateTime: startDT ?? undefined,
  259. endDateTime: endDT ?? undefined,
  260. promisedAt: sr.promisedAt ? (toSafeDate(sr.promisedAt as string) ?? null) : null,
  261. shopName: (sr.shopName as string) || null,
  262. techName: (sr.techName as string) || null,
  263. parts: (sr.parts as string) || null,
  264. laborHours: numberOr(sr.laborHours, null),
  265. diagnosticNotes: (sr.diagnosticNotes as string) || null,
  266. invoiceNotes: (sr.invoiceNotes as string) || null,
  267. subtotal: (sr.subtotal as number) || 0,
  268. taxRate: (sr.taxRate as number) || 0,
  269. taxAmount: (sr.taxAmount as number) || 0,
  270. taxInclusive: (sr.taxInclusive as boolean) ?? false,
  271. taxComponents: taxComponentsForCopy(sr.taxComponents),
  272. taxableSubtotal: storedTaxableSubtotalFromBackup(sr.taxableSubtotal),
  273. totalAmount: (sr.totalAmount as number) || 0,
  274. invoiceNumber: (sr.invoiceNumber as string) || null,
  275. invoiceDate: sr.invoiceDate ? toSafeDate(sr.invoiceDate as string) : null,
  276. invoiceDueDate: sr.invoiceDueDate ? toSafeDate(sr.invoiceDueDate as string) : null,
  277. discountType: (sr.discountType as string) || null,
  278. discountValue: (sr.discountValue as number) || 0,
  279. discountAmount: (sr.discountAmount as number) || 0,
  280. // A backup from before the statement existed has months and no status,
  281. // which is read as the included warranty it was.
  282. ...warrantyFromUntyped(sr),
  283. warrantyExpiresAt: sr.warrantyExpiresAt ? toSafeDate(sr.warrantyExpiresAt as string) : null,
  284. conditionMapOnInvoice:
  285. typeof sr.conditionMapOnInvoice === 'boolean' ? sr.conditionMapOnInvoice : null,
  286. publicToken: (sr.publicToken as string) || null,
  287. // The link's own expiry answers travel with its token, or a restored
  288. // link would forget the date the workshop set on it.
  289. sharedAt: toSafeDate(sr.sharedAt as string) ?? null,
  290. shareExpiresAt: toSafeDate(sr.shareExpiresAt as string) ?? null,
  291. shareNeverExpires: sr.shareNeverExpires === true,
  292. shareRenewedAt: toSafeDate(sr.shareRenewedAt as string) ?? null,
  293. // Marked paid by hand is what closes an invoice's link under the
  294. // "after payment" rule; without it a settled invoice restores as owed
  295. // and its link stays open.
  296. manuallyPaid: sr.manuallyPaid === true,
  297. manuallyPaidAt: toSafeDate(sr.manuallyPaidAt as string) ?? null,
  298. viewCount: (sr.viewCount as number) || 0,
  299. lastViewedAt: sr.lastViewedAt ? toSafeDate(sr.lastViewedAt as string) : null,
  300. sentAt: sr.sentAt ? toSafeDate(sr.sentAt as string) : null,
  301. bookingSource: (sr.bookingSource as string) || null,
  302. createdById: keptReference(sr.createdById, opts.memberUserIds),
  303. editUnlockedAt: sr.editUnlockedAt ? toSafeDate(sr.editUnlockedAt as string) : null,
  304. editUnlockedById: keptReference(sr.editUnlockedById, opts.memberUserIds),
  305. // Only a technician this import restored: a job pointing at one the
  306. // backup does not carry would be refused, and take the import with it.
  307. technicianId: keptReference(sr.technicianId, opts.technicianIds),
  308. workBayId: (sr.workBayId as string) || null,
  309. // Only a status this import restored. A backup made without the workshop
  310. // configuration carries jobs that name statuses it does not hold; those
  311. // jobs keep their stage and lose the label, rather than failing the import.
  312. customStatusId: keptReference(sr.customStatusId, opts.workOrderStatusIds),
  313. customStatusSince:
  314. sr.customStatusSince && opts.workOrderStatusIds.has(sr.customStatusId as string)
  315. ? toSafeDate(sr.customStatusSince as string)
  316. : null,
  317. sortOrder: (sr.sortOrder as number) || 0,
  318. createdAt: toSafeDate(sr.createdAt as string),
  319. updatedAt: toSafeDate(sr.updatedAt as string),
  320. vehicleId: opts.vehicleId,
  321. customerId: opts.customerId,
  322. designId: keptReference(sr.designId, opts.designIds),
  323. issuedAt: sr.issuedAt ? toSafeDate(sr.issuedAt as string) : null,
  324. issuedDesignSnapshotId: keptReference(sr.issuedDesignSnapshotId, opts.designSnapshotIds),
  325. issuedLogoSnapshotId: keptReference(sr.issuedLogoSnapshotId, opts.assetSnapshotIds),
  326. issuedSignatureSnapshotId: keptReference(sr.issuedSignatureSnapshotId, opts.assetSnapshotIds),
  327. issuedData:
  328. sr.issuedData && typeof sr.issuedData === 'object'
  329. ? (withIssuedDataFileUrls(sr.issuedData, opts.organizationId) as Prisma.InputJsonValue)
  330. : undefined,
  331. },
  332. })
  333. // Service parts
  334. const partItems = sr.partItems as Record<string, unknown>[] | undefined
  335. if (partItems?.length) {
  336. await tx.servicePart.createMany({
  337. data: partItems.map((p) => ({
  338. id: p.id as string,
  339. partNumber: (p.partNumber as string) || null,
  340. name: p.name as string,
  341. quantity: numberOr(p.quantity, 1),
  342. unit: (p.unit as string) || null,
  343. category: (p.category as string) || null,
  344. unitPrice: (p.unitPrice as number) || 0,
  345. total: (p.total as number) || 0,
  346. unitCost: (p.unitCost as number) || 0,
  347. markupPercent: (p.markupPercent as number) || 0,
  348. // Inventory parts are restored earlier in this transaction with their
  349. // ids preserved, so the stock link survives a restore verbatim.
  350. inventoryPartId: (p.inventoryPartId as string) || null,
  351. // A backup taken before lines could be untaxed has no flag: every
  352. // line in it was taxed. One that says false is restored as it was.
  353. taxable: p.taxable !== false,
  354. serviceRecordId: sr.id as string,
  355. })),
  356. })
  357. }
  358. // Concerns. Restored before findings so a finding's concernId still has a
  359. // row to point at.
  360. const concerns = sr.concerns as Record<string, unknown>[] | undefined
  361. if (concerns?.length) {
  362. await tx.serviceConcern.createMany({
  363. data: concerns.map((c, index) => ({
  364. id: c.id as string,
  365. description: c.description as string,
  366. sortOrder: (c.sortOrder as number) ?? index,
  367. cause: (c.cause as string) || null,
  368. correction: (c.correction as string) || null,
  369. confirmation: (c.confirmation as string) || null,
  370. // When it was confirmed always comes back; who, only when that person
  371. // is in this workshop. A backup brought from another installation
  372. // names an account that is not here.
  373. confirmedAt: c.confirmedAt ? (toSafeDate(c.confirmedAt as string) ?? null) : null,
  374. confirmedById: keptReference(c.confirmedById, opts.memberUserIds),
  375. createdAt: toSafeDate(c.createdAt as string),
  376. updatedAt: toSafeDate(c.updatedAt as string),
  377. serviceRecordId: sr.id as string,
  378. })),
  379. })
  380. }
  381. // Service labor
  382. const laborItems = sr.laborItems as Record<string, unknown>[] | undefined
  383. if (laborItems?.length) {
  384. await tx.serviceLabor.createMany({
  385. data: laborItems.map((l) => ({
  386. id: l.id as string,
  387. description: l.description as string,
  388. hours: (l.hours as number) || 0,
  389. rate: (l.rate as number) || 0,
  390. total: (l.total as number) || 0,
  391. // How the line is priced is what makes a shop fee a shop fee; left
  392. // out, a restored fee came back as an hour of labor.
  393. pricingType: (l.pricingType as string) || 'hourly',
  394. taxable: l.taxable !== false,
  395. serviceRecordId: sr.id as string,
  396. })),
  397. })
  398. }
  399. // Service attachments
  400. const restoredConcernIds = new Set((concerns ?? []).map((c) => c.id as string))
  401. const attachments = sr.attachments as Record<string, unknown>[] | undefined
  402. if (attachments?.length) {
  403. await tx.serviceAttachment.createMany({
  404. data: attachments.map((a) => ({
  405. id: a.id as string,
  406. fileName: a.fileName as string,
  407. fileUrl: rewriteFileUrl(a.fileUrl as string, opts.organizationId) || (a.fileUrl as string),
  408. fileType: a.fileType as string,
  409. fileSize: (a.fileSize as number) || 0,
  410. category: (a.category as string) || 'diagnostic',
  411. description: (a.description as string) || null,
  412. includeInInvoice: a.includeInInvoice !== false,
  413. createdAt: toSafeDate(a.createdAt as string),
  414. serviceRecordId: sr.id as string,
  415. // The concern it was filed under, when this record restored it above.
  416. concernId: keptReference(a.concernId, restoredConcernIds),
  417. // What a scan for fault codes found in the file.
  418. scan: a.scan === null || a.scan === undefined ? undefined : (a.scan as object),
  419. })),
  420. })
  421. }
  422. // Status reports. They cascade from the service record, so a restore
  423. // without them loses every report the workshop sent.
  424. await restoreRows(
  425. 'status reports',
  426. (rows) => tx.statusReport.createMany({ data: rows as never }),
  427. sr.statusReports,
  428. {
  429. organizationId: opts.organizationId,
  430. },
  431. { fields: ['videoUrl'], organizationId: opts.organizationId }
  432. )
  433. // Clocked time. This is what a technician's hours were billed from, and in
  434. // Germany it is a statutory record, so losing it on a restore is not the
  435. // same as losing a cached total.
  436. //
  437. // Each entry needs its technician, and technicianId is not nullable. A
  438. // restore that took vehicles but not technicians has nowhere to attach
  439. // them, so those are dropped rather than failing the whole import: the
  440. // alternative is a foreign key error that rolls back everything and tells
  441. // the user nothing.
  442. const timeEntries = (sr.timeEntries as Record<string, unknown>[] | undefined)?.filter((entry) =>
  443. opts.technicianIds.has(entry.technicianId as string)
  444. )
  445. await restoreRows(
  446. 'time entries',
  447. (rows) => tx.timeEntry.createMany({ data: rows as never }),
  448. timeEntries,
  449. {
  450. organizationId: opts.organizationId,
  451. serviceRecordId: sr.id as string,
  452. }
  453. )
  454. // Who was on the job besides its technician. Each row needs its technician
  455. // as a time entry does, so the same rule holds: a restore without the
  456. // technicians drops these rather than failing on the foreign key.
  457. const helpers = (sr.helpers as Record<string, unknown>[] | undefined)?.filter((helper) =>
  458. opts.technicianIds.has(helper.technicianId as string)
  459. )
  460. await restoreRows(
  461. 'job helpers',
  462. (rows) => tx.serviceRecordHelper.createMany({ data: rows as never }),
  463. helpers,
  464. {
  465. organizationId: opts.organizationId,
  466. serviceRecordId: sr.id as string,
  467. }
  468. )
  469. // Payments
  470. const payments = sr.payments as Record<string, unknown>[] | undefined
  471. if (payments?.length) {
  472. await tx.payment.createMany({
  473. data: payments.map((p) => ({
  474. id: p.id as string,
  475. amount: p.amount as number,
  476. date: toSafeDate(p.date as string),
  477. method: (p.method as string) || 'other',
  478. note: (p.note as string) || null,
  479. // Which payment provider took it and under what reference. This pair
  480. // is how a webhook delivered twice is told from a second payment.
  481. provider: (p.provider as string) || null,
  482. externalId: (p.externalId as string) || null,
  483. createdAt: toSafeDate(p.createdAt as string),
  484. updatedAt: toSafeDate(p.updatedAt as string),
  485. serviceRecordId: sr.id as string,
  486. })),
  487. })
  488. }
  489. }
  490. /**
  491. * Writes rows back with the ids they had.
  492. *
  493. * Keeping ids is what lets everything else in the file keep pointing at them,
  494. * and it is why a table that is restored must also be cleared first.
  495. */
  496. async function restoreRows(
  497. what: string,
  498. create: (rows: Record<string, unknown>[]) => Promise<unknown>,
  499. rows: unknown,
  500. override: Record<string, unknown> = {},
  501. files?: { fields: readonly string[]; organizationId: string }
  502. ) {
  503. const list = rows as Record<string, unknown>[] | undefined
  504. if (!list?.length) return
  505. try {
  506. await create(
  507. list.map((row) => {
  508. const columns = columnsOf(row, override)
  509. return files ? withFileUrls(row, columns, files.fields, files.organizationId) : columns
  510. })
  511. )
  512. } catch (error) {
  513. // A restore rolls back as a whole, so the only thing left to salvage is
  514. // knowing which part of the file could not be read back.
  515. const reason = error instanceof Error ? error.message : String(error)
  516. throw new Error(`Could not restore ${what}: ${reason}`)
  517. }
  518. }
  519. async function restoreFiles(files: BackupFileSource, organizationId: string) {
  520. const uploadsDir = path.join(uploadsRoot(), organizationId)
  521. // The backup's files are written first, over whatever has the same name.
  522. const restored = new Map<string, Set<string>>()
  523. for (const entry of files.entries) {
  524. const { category, name: filename } = entry
  525. if (!UPLOAD_CATEGORIES.includes(category)) continue
  526. // Prevent directory traversal
  527. if (filename.includes('..') || filename.includes('/') || filename.includes('\\')) continue
  528. // Each file streams from its source to its place; none is held whole.
  529. await files.extract(entry, path.join(uploadsDir, category, filename))
  530. const names = restored.get(category) ?? new Set<string>()
  531. names.add(filename)
  532. restored.set(category, names)
  533. }
  534. // Then the files in those folders that the backup did not bring back go
  535. // through the file manager, which keeps any a row still uses. The folders
  536. // used to be emptied first, which also took files uploaded after the backup
  537. // was made, still used by rows this restore did not replace. Only the
  538. // folders the backup carries are looked at: it has nothing to say about
  539. // the others (the portal background, the tire photos, when absent).
  540. for (const [category, names] of restored) {
  541. await releaseFilesNotRestored(organizationId, category, names)
  542. }
  543. }
  544. /**
  545. * Replaces the workshop's data with the backup's, then its files. Secret
  546. * keys a "Data only" backup left out keep the values the install has now;
  547. * the keys it had none for are returned so the workshop can re-enter them.
  548. * Throws when the backup cannot be read back; the transaction has rolled
  549. * back by then and no file has been touched.
  550. */
  551. export async function restoreBackup(
  552. ctx: RestoreActor,
  553. backup: BackupData,
  554. files: BackupFileSource | null
  555. ): Promise<{ redactedSettings: string[] }> {
  556. assertSupportedBackup(backup)
  557. const data = backup.data
  558. // Secret keys a "Data only" backup left out, which the workshop re-enters.
  559. let redactedSettings: string[] = []
  560. // One restore at a time on this server, however it was started: two large
  561. // ones would fight over the database, and a second one is told so.
  562. const lock = randomUUID()
  563. if (!acquireRestoreLock(lock)) {
  564. throw new RestoreRefusedError('busy', 'A restore is already running on this server')
  565. }
  566. try {
  567. await db.$transaction(
  568. async (raw) => {
  569. const tx = tenantScoped(raw, ctx.organizationId)
  570. const organizationId = ctx.organizationId
  571. // Tenancy (lib/backup/tenancy.ts): nothing in the file may be, or
  572. // point at, another workshop's row. Checked before a row is touched.
  573. const fileIds = collectFileIds(data)
  574. await assertNoForeignRows(tx, organizationId, fileIds)
  575. const own = await lookupOwnIds(tx, organizationId, danglingReferences(data, fileIds))
  576. sanitiseForeignIds(data, mergeIds(fileIds, own))
  577. // 0. A redacted secret keeps what the install has now, so read those
  578. // before the tables are cleared. A "Data only" file also keeps every
  579. // secret the install has that the file does not carry at all, such
  580. // as a key entered after the backup was taken. The header says which
  581. // kind the file is; a file from before it did counts as "Data only"
  582. // when it has a marker.
  583. if (Array.isArray(data.settings)) {
  584. // Keys an older version wrote are renamed first, as the migrations
  585. // that renamed them did for the live rows: those do not run again.
  586. const settingRows = upgradeLegacySettings(
  587. data.settings as { key: string; value: unknown }[]
  588. )
  589. const marked = settingRows.some((s) => isRedacted(s.value))
  590. const dataOnly = backup.contents === 'data' || (backup.contents === undefined && marked)
  591. const current =
  592. dataOnly || marked
  593. ? (
  594. await tx.appSetting.findMany({
  595. where: { organizationId },
  596. select: { id: true, key: true, value: true },
  597. })
  598. ).filter((s) => isSecretSettingKey(s.key))
  599. : []
  600. const kept = keepCurrentSecrets(
  601. settingRows,
  602. new Map(current.map((s) => [s.key, s.value])),
  603. current,
  604. { dataOnly }
  605. )
  606. data.settings = kept.rows
  607. redactedSettings = kept.redacted
  608. }
  609. if (Array.isArray(data.webhooks)) {
  610. const webhookRows = data.webhooks as Record<string, unknown>[]
  611. const current = webhookRows.some((w) => isRedacted(w.secret))
  612. ? await tx.webhook.findMany({
  613. where: { organizationId },
  614. select: { id: true, url: true, secret: true },
  615. })
  616. : []
  617. data.webhooks = keepCurrentWebhookSecrets(
  618. webhookRows,
  619. current,
  620. generateWebhookSecret
  621. ).rows
  622. }
  623. // 1. Clear what this backup can put back, and nothing else.
  624. //
  625. // A selective export omits the keys it was not asked for. Clearing a
  626. // table the file says nothing about deletes records it cannot restore,
  627. // which is how importing a customers-only export used to wipe every
  628. // vehicle in the workshop.
  629. const clearable: Record<string, () => Promise<unknown>> = {
  630. Notification: () => tx.notification.deleteMany({ where: { organizationId } }),
  631. SmsMessage: () => tx.smsMessage.deleteMany({ where: { organizationId } }),
  632. WhatsappMessage: () => tx.whatsappMessage.deleteMany({ where: { organizationId } }),
  633. TelegramMessage: () => tx.telegramMessage.deleteMany({ where: { organizationId } }),
  634. PortalMessage: () => tx.portalMessage.deleteMany({ where: { organizationId } }),
  635. // Attachment rows go with their message by cascade.
  636. EmailMessage: () => tx.emailMessage.deleteMany({ where: { organizationId } }),
  637. ScheduledMessage: () => tx.scheduledMessage.deleteMany({ where: { organizationId } }),
  638. InspectionReminderSend: () =>
  639. tx.inspectionReminderSend.deleteMany({ where: { organizationId } }),
  640. InspectionReminderCampaign: () =>
  641. tx.inspectionReminderCampaign.deleteMany({ where: { organizationId } }),
  642. AuditLog: () => tx.auditLog.deleteMany({ where: { organizationId } }),
  643. Inspection: () => tx.inspection.deleteMany({ where: { organizationId } }),
  644. InspectionTemplate: () => tx.inspectionTemplate.deleteMany({ where: { organizationId } }),
  645. Quote: () => tx.quote.deleteMany({ where: { organizationId } }),
  646. // Every alert, on an appointment or on a job, before what it points at.
  647. AppointmentNotification: () =>
  648. tx.appointmentNotification.deleteMany({ where: { organizationId } }),
  649. Appointment: () => tx.appointment.deleteMany({ where: { organizationId } }),
  650. TireSet: () => tx.tireSet.deleteMany({ where: { organizationId } }),
  651. TireWarehouse: () => tx.tireWarehouse.deleteMany({ where: { organizationId } }),
  652. // Counter sales hang off a customer rather than a vehicle, so nothing
  653. // else deletes them and a second restore used to collide with them.
  654. ServiceRecord: () =>
  655. tx.serviceRecord.deleteMany({ where: { organizationId, vehicleId: null } }),
  656. Reminder: () => tx.reminder.deleteMany({ where: { organizationId, vehicleId: null } }),
  657. Vehicle: () => tx.vehicle.deleteMany({ where: { organizationId } }),
  658. CustomFieldDefinition: () =>
  659. tx.customFieldDefinition.deleteMany({ where: { organizationId } }),
  660. InventoryPart: () => tx.inventoryPart.deleteMany({ where: { organizationId } }),
  661. Technician: () => tx.technician.deleteMany({ where: { organizationId } }),
  662. WorkBay: () => tx.workBay.deleteMany({ where: { organizationId } }),
  663. WorkshopClosure: () => tx.workshopClosure.deleteMany({ where: { organizationId } }),
  664. Customer: () => tx.customer.deleteMany({ where: { organizationId } }),
  665. LaborPreset: () => tx.laborPreset.deleteMany({ where: { organizationId } }),
  666. ConditionMarkType: () => tx.conditionMarkType.deleteMany({ where: { organizationId } }),
  667. Webhook: () => tx.webhook.deleteMany({ where: { organizationId } }),
  668. ReportSchedule: () => tx.reportSchedule.deleteMany({ where: { organizationId } }),
  669. WorkOrderStatus: () => tx.workOrderStatus.deleteMany({ where: { organizationId } }),
  670. AppSetting: () => tx.appSetting.deleteMany({ where: { organizationId } }),
  671. DocumentDesign: () => tx.documentDesign.deleteMany({ where: { organizationId } }),
  672. EmailTemplate: () => tx.emailTemplate.deleteMany({ where: { organizationId } }),
  673. DocumentDesignSnapshot: () =>
  674. tx.documentDesignSnapshot.deleteMany({ where: { organizationId } }),
  675. DocumentAssetSnapshot: () =>
  676. tx.documentAssetSnapshot.deleteMany({ where: { organizationId } }),
  677. }
  678. // The portal chat goes with its customers by cascade. A file that
  679. // replaces the customers without carrying the chat would delete every
  680. // conversation, so those are read first and put back further down.
  681. //
  682. // The chat stays open while a restore runs, so the rows are locked
  683. // before they are read. A message being sent takes a share of its
  684. // customer's row, and a read receipt takes its message's row: locking
  685. // both waits for the writes already under way, which the read below
  686. // then sees, and holds new ones until the restore commits. One that
  687. // waited finds its customer or message replaced and fails or changes
  688. // nothing; it is never taken in and then silently dropped.
  689. let currentPortalMessages: Record<string, unknown>[] = []
  690. if (keepsCurrentPortalMessages(data)) {
  691. await raw.$queryRaw(Prisma.sql`
  692. SELECT count(*) FROM (
  693. SELECT 1 FROM "public"."customers" WHERE "organizationId" = ${organizationId} FOR UPDATE
  694. ) locked
  695. `)
  696. await raw.$queryRaw(Prisma.sql`
  697. SELECT count(*) FROM (
  698. SELECT 1 FROM "public"."portal_messages" WHERE "organizationId" = ${organizationId} FOR UPDATE
  699. ) locked
  700. `)
  701. currentPortalMessages = await tx.portalMessage.findMany({ where: { organizationId } })
  702. }
  703. for (const model of clearPlanFor(Object.keys(data))) {
  704. const clear = clearable[model]
  705. if (clear) await clear()
  706. }
  707. // 2. Insert settings
  708. if (data.settings?.length) {
  709. await tx.appSetting.createMany({
  710. data: (data.settings as Record<string, unknown>[]).map((s: Record<string, unknown>) => {
  711. // A setting's value can be a stored file (the workshop logo, the
  712. // portal background), in any of the shapes uploads have had.
  713. const value = (rewriteFileUrl(s.value as string, ctx.organizationId) ??
  714. s.value) as string
  715. return {
  716. id: s.id as string,
  717. key: s.key as string,
  718. value,
  719. userId: ctx.userId,
  720. organizationId: ctx.organizationId,
  721. }
  722. }),
  723. })
  724. }
  725. // 2b. Named designs, and what issued invoices were issued with. Before
  726. // customers and service records, which point at them. Backups from
  727. // before these existed have none of the keys and restore as before.
  728. const designIds = new Set<string>()
  729. if (data.documentDesigns?.length) {
  730. const rows = (data.documentDesigns as Record<string, unknown>[]).filter(
  731. (d) => typeof d.id === 'string' && typeof d.name === 'string'
  732. )
  733. await tx.documentDesign.createMany({
  734. data: rows.map((d) => ({
  735. id: d.id as string,
  736. organizationId: ctx.organizationId,
  737. documentType: (d.documentType as string) || 'invoice',
  738. name: d.name as string,
  739. layout: (d.layout ?? {}) as Prisma.InputJsonValue,
  740. // The design's own logo is a stored upload inside the template.
  741. template: rewriteFileUrlsWithin(
  742. d.template ?? {},
  743. ctx.organizationId
  744. ) as Prisma.InputJsonValue,
  745. // The rule that picks this design by itself, for counter sales.
  746. autoRule: (d.autoRule as string) || null,
  747. createdAt: toSafeDate(d.createdAt as string),
  748. updatedAt: toSafeDate(d.updatedAt as string),
  749. })),
  750. })
  751. for (const d of rows) designIds.add(d.id as string)
  752. }
  753. // Email templates: the setting that names the one in use came back with
  754. // settings, so the rows it points at have to come back with their ids.
  755. if (data.emailTemplates?.length) {
  756. const rows = (data.emailTemplates as Record<string, unknown>[]).filter(
  757. (t) =>
  758. typeof t.id === 'string' && typeof t.name === 'string' && typeof t.kind === 'string'
  759. )
  760. // Uploads travel under this organisation's id, so every stored
  761. // upload URL is rewritten the way logo settings are.
  762. const rewriteAssets = (value: unknown) => rewriteFileUrlsWithin(value, ctx.organizationId)
  763. await tx.emailTemplate.createMany({
  764. data: rows.map((t) => ({
  765. id: t.id as string,
  766. organizationId: ctx.organizationId,
  767. kind: t.kind as string,
  768. name: t.name as string,
  769. subject: (t.subject as string) || '',
  770. blocks: rewriteAssets(t.blocks ?? []) as Prisma.InputJsonValue,
  771. theme: rewriteAssets(t.theme ?? {}) as Prisma.InputJsonValue,
  772. createdAt: toSafeDate(t.createdAt as string),
  773. updatedAt: toSafeDate(t.updatedAt as string),
  774. })),
  775. })
  776. }
  777. const designSnapshotIds = new Set<string>()
  778. if (data.documentDesignSnapshots?.length) {
  779. const rows = (data.documentDesignSnapshots as Record<string, unknown>[]).filter(
  780. (d) => typeof d.id === 'string' && typeof d.hash === 'string'
  781. )
  782. // The look an invoice was issued with, kept exactly as it was: the
  783. // row's hash is taken over its own content, and the logo an issued
  784. // document prints comes from the frozen bytes beside it
  785. // (issuedLogoSnapshot), not from the URL in here. So the URLs in a
  786. // snapshot are left naming the workshop the backup came from.
  787. await tx.documentDesignSnapshot.createMany({
  788. data: rows.map((d) => ({
  789. id: d.id as string,
  790. organizationId: ctx.organizationId,
  791. hash: d.hash as string,
  792. layout: (d.layout ?? {}) as Prisma.InputJsonValue,
  793. template: (d.template ?? {}) as Prisma.InputJsonValue,
  794. createdAt: toSafeDate(d.createdAt as string),
  795. })),
  796. })
  797. for (const d of rows) designSnapshotIds.add(d.id as string)
  798. }
  799. const assetSnapshotIds = new Set<string>()
  800. if (data.documentAssetSnapshots?.length) {
  801. const rows = (data.documentAssetSnapshots as Record<string, unknown>[]).filter(
  802. (d) =>
  803. typeof d.id === 'string' && typeof d.hash === 'string' && typeof d.data === 'string'
  804. )
  805. await tx.documentAssetSnapshot.createMany({
  806. data: rows.map((d) => ({
  807. id: d.id as string,
  808. organizationId: ctx.organizationId,
  809. hash: d.hash as string,
  810. mimeType: (d.mimeType as string) || 'image/png',
  811. data: new Uint8Array(Buffer.from(d.data as string, 'base64')),
  812. createdAt: toSafeDate(d.createdAt as string),
  813. })),
  814. })
  815. for (const d of rows) assetSnapshotIds.add(d.id as string)
  816. }
  817. // 3. Insert customers
  818. if (data.customers?.length) {
  819. await tx.customer.createMany({
  820. data: (data.customers as Record<string, unknown>[]).map(
  821. (c: Record<string, unknown>) => ({
  822. id: c.id as string,
  823. customerNumber: (c.customerNumber as string) || null,
  824. name: c.name as string,
  825. email: (c.email as string) || null,
  826. phone: (c.phone as string) || null,
  827. address: (c.address as string) || null,
  828. company: (c.company as string) || null,
  829. notes: (c.notes as string) || null,
  830. // What the invoice prints and charges by: a tax number, and
  831. // whether this customer pays tax at all.
  832. taxId: (c.taxId as string) || null,
  833. taxExempt: (c.taxExempt as boolean) || false,
  834. // A customer who asked not to be reminded stays unreminded.
  835. reminderOptOut: (c.reminderOptOut as boolean) || false,
  836. // The Telegram chat the customer linked; without it their
  837. // messages arrive from nobody.
  838. telegramChatId: (c.telegramChatId as string) || null,
  839. invoiceDesignId: keptReference(c.invoiceDesignId, designIds),
  840. createdAt: toSafeDate(c.createdAt as string),
  841. updatedAt: toSafeDate(c.updatedAt as string),
  842. userId: ctx.userId,
  843. organizationId: ctx.organizationId,
  844. })
  845. ),
  846. })
  847. }
  848. // 4. Insert technicians
  849. if (data.technicians?.length) {
  850. // A technician can be linked to a person's account, and a backup only
  851. // carries that account's id. Restored into another installation, or
  852. // into this one after the person was removed, the id points at nobody
  853. // and the foreign key refuses the whole import. The link is kept only
  854. // for people who are members of this workshop today; anybody else
  855. // comes back as a board-only technician, name and all, and can be
  856. // linked again from the team page.
  857. const linkedUserIds = [
  858. ...new Set(
  859. (data.technicians as Record<string, unknown>[])
  860. .map((t) => t.userId)
  861. .filter((id): id is string => typeof id === 'string' && id.length > 0)
  862. ),
  863. ]
  864. const members = linkedUserIds.length
  865. ? await tx.organizationMember.findMany({
  866. where: { organizationId: ctx.organizationId, userId: { in: linkedUserIds } },
  867. select: { userId: true },
  868. })
  869. : []
  870. const memberUserIds = new Set(members.map((m) => m.userId))
  871. await tx.technician.createMany({
  872. data: (data.technicians as Record<string, unknown>[]).map(
  873. (t: Record<string, unknown>) => ({
  874. id: t.id as string,
  875. name: t.name as string,
  876. color: (t.color as string) || '#3b82f6',
  877. isActive: t.isActive !== false,
  878. sortOrder: (t.sortOrder as number) || 0,
  879. dailyCapacity: numberOr(t.dailyCapacity, 480),
  880. skills: (t.skills as string) || null,
  881. // memberId from old backups is ignored: it has no FK to users.
  882. userId:
  883. typeof t.userId === 'string' && memberUserIds.has(t.userId) ? t.userId : null,
  884. createdAt: toSafeDate(t.createdAt as string),
  885. updatedAt: toSafeDate(t.updatedAt as string),
  886. organizationId: ctx.organizationId,
  887. })
  888. ),
  889. })
  890. }
  891. // Which technicians this restore actually has, for the time entries
  892. // nested inside each service record. Read back rather than taken from
  893. // the file, so an older backup whose technicians were skipped does not
  894. // claim rows that were never inserted.
  895. const technicianIds = new Set(
  896. (
  897. await tx.technician.findMany({
  898. where: { organizationId: ctx.organizationId },
  899. select: { id: true },
  900. })
  901. ).map((t) => t.id)
  902. )
  903. // Who is in this workshop, for the jobs and quotes that name the
  904. // person who opened or unlocked them.
  905. const memberUserIds = new Set(
  906. (
  907. await tx.organizationMember.findMany({
  908. where: { organizationId: ctx.organizationId },
  909. select: { userId: true },
  910. })
  911. ).map((m) => m.userId)
  912. )
  913. // Each technician's time off, for the technicians this restore has.
  914. const absences = ((data.technicians as Record<string, unknown>[] | undefined) ?? [])
  915. .flatMap((t) => (t.absences as Record<string, unknown>[] | undefined) ?? [])
  916. .filter((a) => technicianIds.has(a.technicianId as string))
  917. await restoreRows(
  918. 'time off',
  919. (rows) => tx.technicianAbsence.createMany({ data: rows as never }),
  920. absences,
  921. { organizationId: ctx.organizationId }
  922. )
  923. // 4b. Insert work bays. Service records and inspections point at them, so
  924. // they have to exist before either is restored.
  925. if (data.workBays?.length) {
  926. await tx.workBay.createMany({
  927. data: (data.workBays as Record<string, unknown>[]).map(
  928. (b: Record<string, unknown>) => ({
  929. id: b.id as string,
  930. name: b.name as string,
  931. color: (b.color as string) || '#64748b',
  932. isActive: b.isActive !== false,
  933. sortOrder: (b.sortOrder as number) || 0,
  934. dailyCapacity: numberOr(b.dailyCapacity, 480),
  935. createdAt: toSafeDate(b.createdAt as string),
  936. updatedAt: toSafeDate(b.updatedAt as string),
  937. organizationId: ctx.organizationId,
  938. })
  939. ),
  940. })
  941. }
  942. // 4c. Insert the workshop's own work order statuses. Service records
  943. // point at them, so they have to exist before any job is restored.
  944. const workOrderStatusIds = new Set<string>()
  945. if (data.workOrderStatuses?.length) {
  946. const rows = (data.workOrderStatuses as Record<string, unknown>[]).filter(
  947. (s) => typeof s.id === 'string' && typeof s.name === 'string' && isStage(s.stage)
  948. )
  949. await tx.workOrderStatus.createMany({
  950. data: rows.map((s) => ({
  951. id: s.id as string,
  952. name: s.name as string,
  953. stage: s.stage as string,
  954. color: (s.color as string) || 'slate',
  955. sortOrder: (s.sortOrder as number) || 0,
  956. notifyCustomer: s.notifyCustomer === true,
  957. messageTemplate: (s.messageTemplate as string) || null,
  958. archivedAt: s.archivedAt ? toSafeDate(s.archivedAt as string) : null,
  959. createdAt: toSafeDate(s.createdAt as string),
  960. updatedAt: toSafeDate(s.updatedAt as string),
  961. organizationId: ctx.organizationId,
  962. })),
  963. })
  964. for (const s of rows) workOrderStatusIds.add(s.id as string)
  965. }
  966. // 5. Insert custom field definitions
  967. if (data.customFieldDefinitions?.length) {
  968. for (const def of data.customFieldDefinitions as Record<string, unknown>[]) {
  969. await tx.customFieldDefinition.create({
  970. data: {
  971. id: def.id as string,
  972. name: def.name as string,
  973. label: def.label as string,
  974. fieldType: (def.fieldType as string) || 'text',
  975. options: (def.options as string) || null,
  976. defaultValue: (def.defaultValue as string) || null,
  977. required: (def.required as boolean) || false,
  978. entityType: def.entityType as string,
  979. sortOrder: (def.sortOrder as number) || 0,
  980. isActive: def.isActive !== false,
  981. createdAt: toSafeDate(def.createdAt as string),
  982. updatedAt: toSafeDate(def.updatedAt as string),
  983. userId: ctx.userId,
  984. organizationId: ctx.organizationId,
  985. },
  986. })
  987. // Insert associated custom field values
  988. const values = def.values as Record<string, unknown>[] | undefined
  989. if (values?.length) {
  990. await tx.customFieldValue.createMany({
  991. data: values.map((v) => ({
  992. id: v.id as string,
  993. value: v.value as string,
  994. entityId: v.entityId as string,
  995. entityType: v.entityType as string,
  996. fieldId: def.id as string,
  997. })),
  998. })
  999. }
  1000. }
  1001. }
  1002. // 5. Insert inventory parts
  1003. if (data.inventoryParts?.length) {
  1004. await tx.inventoryPart.createMany({
  1005. data: (data.inventoryParts as Record<string, unknown>[]).map(
  1006. (p: Record<string, unknown>) => ({
  1007. id: p.id as string,
  1008. partNumber: (p.partNumber as string) || null,
  1009. barcode: (p.barcode as string) || null,
  1010. name: p.name as string,
  1011. description: (p.description as string) || null,
  1012. category: (p.category as string) || null,
  1013. quantity: (p.quantity as number) || 0,
  1014. minQuantity: (p.minQuantity as number) || 0,
  1015. // Backups from before the switch have none: every part counted.
  1016. trackStock: p.trackStock !== false,
  1017. unit: (p.unit as string) || null,
  1018. unitCost: (p.unitCost as number) || 0,
  1019. sellPrice: (p.sellPrice as number) || 0,
  1020. supplier: (p.supplier as string) || null,
  1021. supplierPhone: (p.supplierPhone as string) || null,
  1022. supplierEmail: (p.supplierEmail as string) || null,
  1023. supplierUrl: (p.supplierUrl as string) || null,
  1024. imageUrl: rewriteFileUrl(p.imageUrl as string, ctx.organizationId),
  1025. location: (p.location as string) || null,
  1026. isArchived: (p.isArchived as boolean) || false,
  1027. // Backups from before tags have none.
  1028. tags: Array.isArray(p.tags)
  1029. ? (p.tags as unknown[]).filter((tag): tag is string => typeof tag === 'string')
  1030. : [],
  1031. // When the low stock alert went out, so it is not sent again.
  1032. lowStockAlertedAt: p.lowStockAlertedAt
  1033. ? (toSafeDate(p.lowStockAlertedAt as string) ?? null)
  1034. : null,
  1035. createdAt: toSafeDate(p.createdAt as string),
  1036. updatedAt: toSafeDate(p.updatedAt as string),
  1037. userId: ctx.userId,
  1038. organizationId: ctx.organizationId,
  1039. })
  1040. ),
  1041. })
  1042. for (const part of data.inventoryParts as Record<string, unknown>[]) {
  1043. await restoreRows(
  1044. 'inventory images',
  1045. (rows) => tx.storedImage.createMany({ data: rows as never }),
  1046. part.gallery,
  1047. {},
  1048. { fields: ['url'], organizationId: ctx.organizationId }
  1049. )
  1050. }
  1051. }
  1052. // Resolve work day start time from imported settings for backfilling service record times
  1053. const workDayStartSetting = (data.settings as Record<string, unknown>[] | undefined)?.find(
  1054. (s) => s.key === 'workboard.workDayStart'
  1055. )
  1056. const workDayStartTime = (workDayStartSetting?.value as string) || '07:00'
  1057. const importedSetting = (key: string) =>
  1058. (data.settings as Record<string, unknown>[] | undefined)?.find((s) => s.key === key)
  1059. ?.value as string | undefined
  1060. const timeZone = resolveWorkshopTimeZone(
  1061. importedSetting(SETTING_KEYS.TIMEZONE),
  1062. importedSetting(SETTING_KEYS.TIMEZONE_DETECTED)
  1063. )
  1064. // 6. Insert vehicles with nested data
  1065. if (data.vehicles?.length) {
  1066. for (const v of data.vehicles as Record<string, unknown>[]) {
  1067. await tx.vehicle.create({
  1068. data: {
  1069. id: v.id as string,
  1070. make: v.make as string,
  1071. model: v.model as string,
  1072. year: restoredVehicleYear(v.year),
  1073. vin: (v.vin as string) || null,
  1074. licensePlate: normalizePlate(v.licensePlate as string | null | undefined),
  1075. color: (v.color as string) || null,
  1076. mileage: (v.mileage as number) || 0,
  1077. fuelType: (v.fuelType as string) || null,
  1078. transmission: (v.transmission as string) || null,
  1079. engineSize: (v.engineSize as string) || null,
  1080. engineCode: (v.engineCode as string) || null,
  1081. hsn: (v.hsn as string) || null,
  1082. tsn: (v.tsn as string) || null,
  1083. bodyType: (v.bodyType as string) || null,
  1084. purchaseDate: toSafeDate(v.purchaseDate as string) ?? null,
  1085. purchasePrice: numberOr(v.purchasePrice, null),
  1086. imageUrl: rewriteFileUrl(v.imageUrl as string, ctx.organizationId),
  1087. isArchived: (v.isArchived as boolean) || false,
  1088. archiveReason: (v.archiveReason as string) || null,
  1089. soldReportedAt: v.soldReportedAt
  1090. ? (toSafeDate(v.soldReportedAt as string) ?? null)
  1091. : null,
  1092. maintenanceDismissed: (v.maintenanceDismissed as boolean) || false,
  1093. maintenanceDismissedAt: v.maintenanceDismissedAt
  1094. ? (toSafeDate(v.maintenanceDismissedAt as string) ?? null)
  1095. : null,
  1096. createdAt: toSafeDate(v.createdAt as string),
  1097. updatedAt: toSafeDate(v.updatedAt as string),
  1098. userId: ctx.userId,
  1099. organizationId: ctx.organizationId,
  1100. customerId: (v.customerId as string) || null,
  1101. },
  1102. })
  1103. // Notes
  1104. const notes = v.notes as Record<string, unknown>[] | undefined
  1105. if (notes?.length) {
  1106. await tx.note.createMany({
  1107. data: notes.map((n) => ({
  1108. id: n.id as string,
  1109. title: n.title as string,
  1110. content: n.content as string,
  1111. isPinned: (n.isPinned as boolean) || false,
  1112. createdAt: toSafeDate(n.createdAt as string),
  1113. updatedAt: toSafeDate(n.updatedAt as string),
  1114. vehicleId: v.id as string,
  1115. })),
  1116. })
  1117. }
  1118. // Fuel logs
  1119. const fuelLogs = v.fuelLogs as Record<string, unknown>[] | undefined
  1120. if (fuelLogs?.length) {
  1121. await tx.fuelLog.createMany({
  1122. data: fuelLogs.map((f) => ({
  1123. id: f.id as string,
  1124. date: toSafeDate(f.date as string),
  1125. mileage: f.mileage as number,
  1126. gallons: f.gallons as number,
  1127. pricePerGallon: f.pricePerGallon as number,
  1128. totalCost: f.totalCost as number,
  1129. isFillUp: f.isFillUp !== false,
  1130. station: (f.station as string) || null,
  1131. notes: (f.notes as string) || null,
  1132. createdAt: toSafeDate(f.createdAt as string),
  1133. updatedAt: toSafeDate(f.updatedAt as string),
  1134. vehicleId: v.id as string,
  1135. })),
  1136. })
  1137. }
  1138. // Reminders
  1139. const reminders = v.reminders as Record<string, unknown>[] | undefined
  1140. if (reminders?.length) {
  1141. await tx.reminder.createMany({
  1142. data: reminders.map((r) => ({
  1143. id: r.id as string,
  1144. title: r.title as string,
  1145. description: (r.description as string) || null,
  1146. dueDate: toSafeDate(r.dueDate as string) ?? null,
  1147. hasDueTime: (r.hasDueTime as boolean) || false,
  1148. dueMileage: numberOr(r.dueMileage, null),
  1149. isCompleted: (r.isCompleted as boolean) || false,
  1150. notifyInApp: (r.notifyInApp as boolean) ?? true,
  1151. notifyEmail: (r.notifyEmail as boolean) ?? false,
  1152. // Already announced: left out, a restore announced it again.
  1153. notifiedAt: r.notifiedAt ? (toSafeDate(r.notifiedAt as string) ?? null) : null,
  1154. createdAt: toSafeDate(r.createdAt as string),
  1155. updatedAt: toSafeDate(r.updatedAt as string),
  1156. vehicleId: v.id as string,
  1157. customerId: (r.customerId as string) || null,
  1158. organizationId: ctx.organizationId,
  1159. })),
  1160. })
  1161. }
  1162. // Service records
  1163. const serviceRecords = v.serviceRecords as Record<string, unknown>[] | undefined
  1164. if (serviceRecords?.length) {
  1165. for (const sr of serviceRecords) {
  1166. await importServiceRecordTree(tx, sr, {
  1167. organizationId: ctx.organizationId,
  1168. vehicleId: v.id as string,
  1169. // Most vehicle jobs have none and follow the vehicle's
  1170. // customer; one booked online names who booked it.
  1171. customerId: (sr.customerId as string) || null,
  1172. workDayStartTime,
  1173. timeZone,
  1174. technicianIds,
  1175. workOrderStatusIds,
  1176. memberUserIds,
  1177. designIds,
  1178. designSnapshotIds,
  1179. assetSnapshotIds,
  1180. })
  1181. }
  1182. }
  1183. // What else a vehicle owns. All of it is deleted with the vehicle,
  1184. // so leaving any of it out makes a restore a one-way loss.
  1185. await restoreRows(
  1186. 'service requests',
  1187. (rows) => tx.serviceRequest.createMany({ data: rows as never }),
  1188. v.serviceRequests,
  1189. { organizationId },
  1190. // The customer's photos and video point at this workshop.
  1191. { fields: ['attachments'], organizationId }
  1192. )
  1193. // One row, not a list: the vehicle's inspection deadline and where it came from.
  1194. await restoreRows(
  1195. 'inspection status',
  1196. (rows) => tx.vehicleInspectionStatus.createMany({ data: rows as never }),
  1197. v.inspectionStatus ? [v.inspectionStatus] : [],
  1198. { organizationId }
  1199. )
  1200. await restoreRows(
  1201. 'AI drafted messages',
  1202. (rows) => tx.aiGeneratedMessage.createMany({ data: rows as never }),
  1203. v.aiMessages
  1204. )
  1205. const recurring = v.recurringInvoices as Record<string, unknown>[] | undefined
  1206. if (recurring?.length) {
  1207. // No organizationId on the row: a recurring invoice is the
  1208. // vehicle's, and the vehicle is this workshop's.
  1209. await restoreRows(
  1210. 'recurring invoices',
  1211. (rows) => tx.recurringInvoice.createMany({ data: rows as never }),
  1212. recurring
  1213. )
  1214. for (const invoice of recurring) {
  1215. await restoreRows(
  1216. 'recurring invoice parts',
  1217. (rows) => tx.recurringPart.createMany({ data: rows as never }),
  1218. invoice.templateParts
  1219. )
  1220. await restoreRows(
  1221. 'recurring invoice labour',
  1222. (rows) => tx.recurringLabor.createMany({ data: rows as never }),
  1223. invoice.templateLabor
  1224. )
  1225. }
  1226. }
  1227. }
  1228. }
  1229. // 6b. Customer/workshop reminders (no vehicle). Older backups don't
  1230. // have this key.
  1231. if (data.orgReminders?.length) {
  1232. await tx.reminder.createMany({
  1233. data: (data.orgReminders as Record<string, unknown>[]).map((r) => ({
  1234. id: r.id as string,
  1235. title: r.title as string,
  1236. description: (r.description as string) || null,
  1237. dueDate: toSafeDate(r.dueDate as string) ?? null,
  1238. hasDueTime: (r.hasDueTime as boolean) || false,
  1239. dueMileage: numberOr(r.dueMileage, null),
  1240. isCompleted: (r.isCompleted as boolean) || false,
  1241. notifyInApp: (r.notifyInApp as boolean) ?? true,
  1242. notifyEmail: (r.notifyEmail as boolean) ?? false,
  1243. notifiedAt: r.notifiedAt ? (toSafeDate(r.notifiedAt as string) ?? null) : null,
  1244. createdAt: toSafeDate(r.createdAt as string),
  1245. updatedAt: toSafeDate(r.updatedAt as string),
  1246. vehicleId: null,
  1247. customerId: (r.customerId as string) || null,
  1248. organizationId: ctx.organizationId,
  1249. })),
  1250. })
  1251. }
  1252. // 6c. Counter sales (service records without a vehicle, linked directly
  1253. // to a customer). Older backups don't have this key.
  1254. if (data.counterSales?.length) {
  1255. for (const sr of data.counterSales as Record<string, unknown>[]) {
  1256. await importServiceRecordTree(tx, sr, {
  1257. organizationId: ctx.organizationId,
  1258. vehicleId: null,
  1259. customerId: (sr.customerId as string) || null,
  1260. workDayStartTime,
  1261. timeZone,
  1262. technicianIds,
  1263. workOrderStatusIds,
  1264. memberUserIds,
  1265. designIds,
  1266. designSnapshotIds,
  1267. assetSnapshotIds,
  1268. })
  1269. }
  1270. }
  1271. // 6d. Rows that point at a service record, once every service record
  1272. // exists. A finding can be resolved in a counter sale, and a stock
  1273. // movement can belong to any job, so neither can be written while its
  1274. // service record is still to come.
  1275. if (data.inventoryParts?.length) {
  1276. for (const part of data.inventoryParts as Record<string, unknown>[]) {
  1277. // The importing user owns the history: the person who moved the
  1278. // stock has no account on this instance.
  1279. await restoreRows(
  1280. 'stock movements',
  1281. (rows) => tx.stockMovement.createMany({ data: rows as never }),
  1282. part.movements,
  1283. { organizationId, userId: ctx.userId }
  1284. )
  1285. }
  1286. }
  1287. if (data.vehicles?.length) {
  1288. const findings = (data.vehicles as Record<string, unknown>[]).flatMap((vehicle) =>
  1289. ((vehicle.findings as Record<string, unknown>[] | undefined) ?? []).map((finding) => ({
  1290. finding,
  1291. vehicleId: vehicle.id as string,
  1292. }))
  1293. )
  1294. if (findings.length > 0) {
  1295. // A finding names the job it was found in, the job that resolved it
  1296. // and the concern it answers. Any of the three can be absent from
  1297. // this restore — a counter sale the backup did not carry, a job
  1298. // whose vehicle was left out — and one dangling id fails the whole
  1299. // import, so each is kept only when the row it names is really here.
  1300. const referencedJobs = findings.flatMap(({ finding }) =>
  1301. [finding.serviceRecordId, finding.resolvedServiceRecordId].filter(
  1302. (id): id is string => typeof id === 'string'
  1303. )
  1304. )
  1305. const referencedConcerns = findings
  1306. .map(({ finding }) => finding.concernId)
  1307. .filter((id): id is string => typeof id === 'string')
  1308. // The diagnostic report a fault code was read from, likewise.
  1309. const referencedFiles = findings
  1310. .map(({ finding }) => finding.sourceAttachmentId)
  1311. .filter((id): id is string => typeof id === 'string')
  1312. const [jobRows, concernRows, fileRows] = await Promise.all([
  1313. tx.serviceRecord.findMany({
  1314. where: { id: { in: referencedJobs }, organizationId },
  1315. select: { id: true },
  1316. }),
  1317. tx.serviceConcern.findMany({
  1318. where: { id: { in: referencedConcerns }, serviceRecord: { organizationId } },
  1319. select: { id: true },
  1320. }),
  1321. referencedFiles.length > 0
  1322. ? tx.serviceAttachment.findMany({
  1323. where: { id: { in: referencedFiles }, serviceRecord: { organizationId } },
  1324. select: { id: true },
  1325. })
  1326. : Promise.resolve([]),
  1327. ])
  1328. const restoredJobIds = new Set(jobRows.map((row) => row.id))
  1329. const restoredConcernIds = new Set(concernRows.map((row) => row.id))
  1330. const restoredFileIds = new Set(fileRows.map((row) => row.id))
  1331. const text = (value: unknown) => (typeof value === 'string' && value ? value : null)
  1332. await tx.vehicleFinding.createMany({
  1333. data: findings.map(({ finding, vehicleId: findingVehicleId }) => ({
  1334. id: finding.id as string,
  1335. description: (finding.description as string) || '',
  1336. severity: (finding.severity as string) || 'needs_work',
  1337. status: (finding.status as string) || 'open',
  1338. notes: (finding.notes as string) || null,
  1339. imageUrls: ((finding.imageUrls as string[]) || []).map(
  1340. (url) => rewriteFileUrl(url, ctx.organizationId) ?? url
  1341. ),
  1342. createdAt: toSafeDate(finding.createdAt as string),
  1343. updatedAt: toSafeDate(finding.updatedAt as string),
  1344. vehicleId: findingVehicleId,
  1345. serviceRecordId: keptReference(finding.serviceRecordId, restoredJobIds),
  1346. resolvedServiceRecordId: keptReference(
  1347. finding.resolvedServiceRecordId,
  1348. restoredJobIds
  1349. ),
  1350. concernId: keptReference(finding.concernId, restoredConcernIds),
  1351. faultCode: text(finding.faultCode),
  1352. faultStatus: text(finding.faultStatus),
  1353. faultModule: text(finding.faultModule),
  1354. faultMeaning: text(finding.faultMeaning),
  1355. faultMeaningSource: text(finding.faultMeaningSource),
  1356. sourceAttachmentId: keptReference(finding.sourceAttachmentId, restoredFileIds),
  1357. })),
  1358. })
  1359. }
  1360. }
  1361. // 7. Insert quotes with nested data
  1362. if (data.quotes?.length) {
  1363. for (const q of data.quotes as Record<string, unknown>[]) {
  1364. await tx.quote.create({
  1365. data: {
  1366. id: q.id as string,
  1367. quoteNumber: (q.quoteNumber as string) || null,
  1368. title: q.title as string,
  1369. description: (q.description as string) || null,
  1370. status: (q.status as string) || 'draft',
  1371. validUntil: toSafeDate(q.validUntil as string) ?? null,
  1372. subtotal: (q.subtotal as number) || 0,
  1373. taxRate: (q.taxRate as number) || 0,
  1374. taxAmount: (q.taxAmount as number) || 0,
  1375. taxInclusive: (q.taxInclusive as boolean) ?? false,
  1376. taxComponents: taxComponentsForCopy(q.taxComponents),
  1377. taxableSubtotal: storedTaxableSubtotalFromBackup(q.taxableSubtotal),
  1378. discountType: (q.discountType as string) || null,
  1379. discountValue: (q.discountValue as number) || 0,
  1380. discountAmount: (q.discountAmount as number) || 0,
  1381. totalAmount: (q.totalAmount as number) || 0,
  1382. notes: (q.notes as string) || null,
  1383. ...warrantyFromUntyped(q),
  1384. // The link the customer already holds. Left out, every shared
  1385. // quote answered "not found" after a restore.
  1386. publicToken: (q.publicToken as string) || null,
  1387. sharedAt: toSafeDate(q.sharedAt as string) ?? null,
  1388. shareExpiresAt: toSafeDate(q.shareExpiresAt as string) ?? null,
  1389. shareNeverExpires: q.shareNeverExpires === true,
  1390. shareRenewedAt: toSafeDate(q.shareRenewedAt as string) ?? null,
  1391. viewCount: (q.viewCount as number) || 0,
  1392. lastViewedAt: q.lastViewedAt ? toSafeDate(q.lastViewedAt as string) : null,
  1393. customerMessage: (q.customerMessage as string) || null,
  1394. sentAt: q.sentAt ? toSafeDate(q.sentAt as string) : null,
  1395. responseDismissedAt: q.responseDismissedAt
  1396. ? toSafeDate(q.responseDismissedAt as string)
  1397. : null,
  1398. editUnlockedAt: q.editUnlockedAt ? toSafeDate(q.editUnlockedAt as string) : null,
  1399. editUnlockedById: keptReference(q.editUnlockedById, memberUserIds),
  1400. convertedToId: (q.convertedToId as string) || null,
  1401. createdAt: toSafeDate(q.createdAt as string),
  1402. updatedAt: toSafeDate(q.updatedAt as string),
  1403. userId: ctx.userId,
  1404. organizationId: ctx.organizationId,
  1405. customerId: (q.customerId as string) || null,
  1406. vehicleId: (q.vehicleId as string) || null,
  1407. },
  1408. })
  1409. // Quote parts
  1410. const partItems = q.partItems as Record<string, unknown>[] | undefined
  1411. if (partItems?.length) {
  1412. await tx.quotePart.createMany({
  1413. data: partItems.map((p, index) => ({
  1414. id: p.id as string,
  1415. partNumber: (p.partNumber as string) || null,
  1416. name: p.name as string,
  1417. quantity: numberOr(p.quantity, 1),
  1418. unit: (p.unit as string) || null,
  1419. category: (p.category as string) || null,
  1420. unitPrice: (p.unitPrice as number) || 0,
  1421. total: (p.total as number) || 0,
  1422. unitCost: (p.unitCost as number) || 0,
  1423. markupPercent: (p.markupPercent as number) || 0,
  1424. excluded: (p.excluded as boolean) || false,
  1425. // Same-transaction inventory restore keeps ids, so the link
  1426. // carried into ServicePart on conversion stays intact.
  1427. inventoryPartId: (p.inventoryPartId as string) || null,
  1428. taxable: p.taxable !== false,
  1429. // A backup from before lines had an order keeps the order
  1430. // it was written in.
  1431. sortOrder: numberOr(p.sortOrder, index),
  1432. quoteId: q.id as string,
  1433. })),
  1434. })
  1435. }
  1436. // Quote labor
  1437. const laborItems = q.laborItems as Record<string, unknown>[] | undefined
  1438. if (laborItems?.length) {
  1439. await tx.quoteLabor.createMany({
  1440. data: laborItems.map((l, index) => ({
  1441. id: l.id as string,
  1442. description: l.description as string,
  1443. hours: (l.hours as number) || 0,
  1444. rate: (l.rate as number) || 0,
  1445. total: (l.total as number) || 0,
  1446. pricingType: (l.pricingType as string) || 'hourly',
  1447. // A line the customer declined stays declined; left out, it
  1448. // came back counted in a total that never included it.
  1449. excluded: (l.excluded as boolean) || false,
  1450. taxable: l.taxable !== false,
  1451. sortOrder: numberOr(l.sortOrder, index),
  1452. quoteId: q.id as string,
  1453. })),
  1454. })
  1455. }
  1456. // Quote attachments: the drawings and photos a quote was argued
  1457. // with. Their files live in the uploads/quotes folder.
  1458. await restoreRows(
  1459. 'quote attachments',
  1460. (rows) => tx.quoteAttachment.createMany({ data: rows as never }),
  1461. q.attachments,
  1462. {},
  1463. { fields: ['fileUrl'], organizationId: ctx.organizationId }
  1464. )
  1465. }
  1466. }
  1467. // 9. Insert inspection templates with nested sections and items
  1468. if (data.inspectionTemplates?.length) {
  1469. for (const tmpl of data.inspectionTemplates as Record<string, unknown>[]) {
  1470. // Every column, including the regulatory profile and the package
  1471. // provenance: a template restored as a bare name would regrade every
  1472. // later inspection on the wrong scale and lose its library link.
  1473. await tx.inspectionTemplate.create({
  1474. data: {
  1475. id: tmpl.id as string,
  1476. name: tmpl.name as string,
  1477. description: (tmpl.description as string) || null,
  1478. isDefault: (tmpl.isDefault as boolean) || false,
  1479. country: (tmpl.country as string) || null,
  1480. standard: (tmpl.standard as string) ?? 'custom',
  1481. severityScale: (tmpl.severityScale as string) || 'eu',
  1482. packageId: (tmpl.packageId as string) || null,
  1483. packageVersion: (tmpl.packageVersion as string) || null,
  1484. packageSource: (tmpl.packageSource as string) || null,
  1485. createdAt: toSafeDate(tmpl.createdAt as string),
  1486. updatedAt: toSafeDate(tmpl.updatedAt as string),
  1487. organizationId: ctx.organizationId,
  1488. },
  1489. })
  1490. const sections = tmpl.sections as Record<string, unknown>[] | undefined
  1491. if (sections?.length) {
  1492. for (const sec of sections) {
  1493. await tx.inspectionTemplateSection.create({
  1494. data: {
  1495. id: sec.id as string,
  1496. name: sec.name as string,
  1497. description: (sec.description as string) || null,
  1498. code: (sec.code as string) || null,
  1499. sortOrder: (sec.sortOrder as number) || 0,
  1500. templateId: tmpl.id as string,
  1501. },
  1502. })
  1503. // The whole check definition: what it measures, its limits, its
  1504. // rules and its wording. A check restored as a name alone would
  1505. // stop grading readings and forget that a photo was required.
  1506. const items = sec.items as Record<string, unknown>[] | undefined
  1507. if (items?.length) {
  1508. await tx.inspectionTemplateItem.createMany({
  1509. data: items.map((item) => ({
  1510. id: item.id as string,
  1511. name: item.name as string,
  1512. description: (item.description as string) || null,
  1513. code: (item.code as string) || null,
  1514. sortOrder: (item.sortOrder as number) || 0,
  1515. inputType: (item.inputType as string) || 'condition',
  1516. unit: (item.unit as string) || null,
  1517. minValue: typeof item.minValue === 'number' ? item.minValue : null,
  1518. maxValue: typeof item.maxValue === 'number' ? item.maxValue : null,
  1519. choices: Array.isArray(item.choices) ? (item.choices as string[]) : [],
  1520. required: item.required === true,
  1521. photoRequired: item.photoRequired === true,
  1522. allowNotApplicable: item.allowNotApplicable !== false,
  1523. defaultSeverity: (item.defaultSeverity as string) || null,
  1524. defectSuggestions: Array.isArray(item.defectSuggestions)
  1525. ? (item.defectSuggestions as string[])
  1526. : [],
  1527. sectionId: sec.id as string,
  1528. })),
  1529. })
  1530. }
  1531. }
  1532. }
  1533. }
  1534. }
  1535. // 10. Insert inspections with items and quote requests
  1536. if (data.inspections?.length) {
  1537. for (const insp of data.inspections as Record<string, unknown>[]) {
  1538. // The certificate fields too: an issued certificate restored without
  1539. // its number, inspector and next test date is not the document that
  1540. // was handed to the customer.
  1541. await tx.inspection.create({
  1542. data: {
  1543. id: insp.id as string,
  1544. status: (insp.status as string) || 'in_progress',
  1545. mileage: numberOr(insp.mileage, null),
  1546. notes: (insp.notes as string) || null,
  1547. startDateTime: toSafeDate(insp.startDateTime as string) ?? null,
  1548. endDateTime: toSafeDate(insp.endDateTime as string) ?? null,
  1549. publicToken: (insp.publicToken as string) || null,
  1550. sharedAt: restoredInspectionSharedAt(insp),
  1551. shareExpiresAt: toSafeDate(insp.shareExpiresAt as string) ?? null,
  1552. shareNeverExpires: insp.shareNeverExpires === true,
  1553. shareRenewedAt: toSafeDate(insp.shareRenewedAt as string) ?? null,
  1554. completedAt: toSafeDate(insp.completedAt as string) ?? null,
  1555. sortOrder: (insp.sortOrder as number) || 0,
  1556. severityScale: (insp.severityScale as string) || null,
  1557. country: (insp.country as string) || null,
  1558. vehicleCategory: (insp.vehicleCategory as string) || null,
  1559. nextTestDue: toSafeDate(insp.nextTestDue as string) ?? null,
  1560. certificateNumber: (insp.certificateNumber as string) || null,
  1561. inspectorName: (insp.inspectorName as string) || null,
  1562. testLocation: (insp.testLocation as string) || null,
  1563. // Import batches are not in a backup, so the row cannot point at
  1564. // one; the same as the relation's own answer when a batch is deleted.
  1565. importBatchId: null,
  1566. // The frozen certificate design, when the snapshot came back too.
  1567. designSnapshotId: keptReference(insp.designSnapshotId, designSnapshotIds),
  1568. signatureSnapshotId: keptReference(insp.signatureSnapshotId, assetSnapshotIds),
  1569. markTypesSnapshot: Array.isArray(insp.markTypesSnapshot)
  1570. ? (insp.markTypesSnapshot as never)
  1571. : undefined,
  1572. createdAt: toSafeDate(insp.createdAt as string),
  1573. updatedAt: toSafeDate(insp.updatedAt as string),
  1574. vehicleId: insp.vehicleId as string,
  1575. templateId: insp.templateId as string,
  1576. technicianId: (insp.technicianId as string) || null,
  1577. workBayId: (insp.workBayId as string) || null,
  1578. organizationId: ctx.organizationId,
  1579. },
  1580. })
  1581. // Each check as it was copied from the template and then graded:
  1582. // the reading, the limits it was graded against, the rules and the
  1583. // free text, not only the grade and the note.
  1584. const inspItems = insp.items as Record<string, unknown>[] | undefined
  1585. if (inspItems?.length) {
  1586. await tx.inspectionItem.createMany({
  1587. data: inspItems.map((item) => ({
  1588. id: item.id as string,
  1589. name: item.name as string,
  1590. section: item.section as string,
  1591. sortOrder: (item.sortOrder as number) || 0,
  1592. condition: (item.condition as string) || 'not_inspected',
  1593. notes: (item.notes as string) || null,
  1594. imageUrls: ((item.imageUrls as string[]) || []).map(
  1595. (url) => rewriteFileUrl(url, ctx.organizationId) ?? url
  1596. ),
  1597. description: (item.description as string) || null,
  1598. code: (item.code as string) || null,
  1599. sectionCode: (item.sectionCode as string) || null,
  1600. inputType: (item.inputType as string) || 'condition',
  1601. unit: (item.unit as string) || null,
  1602. minValue: typeof item.minValue === 'number' ? item.minValue : null,
  1603. maxValue: typeof item.maxValue === 'number' ? item.maxValue : null,
  1604. choices: Array.isArray(item.choices) ? (item.choices as string[]) : [],
  1605. required: item.required === true,
  1606. photoRequired: item.photoRequired === true,
  1607. allowNotApplicable: item.allowNotApplicable !== false,
  1608. defaultSeverity: (item.defaultSeverity as string) || null,
  1609. defectSuggestions: Array.isArray(item.defectSuggestions)
  1610. ? (item.defectSuggestions as string[])
  1611. : [],
  1612. measuredValue: typeof item.measuredValue === 'number' ? item.measuredValue : null,
  1613. textValue: (item.textValue as string) || null,
  1614. inspectionId: insp.id as string,
  1615. })),
  1616. })
  1617. }
  1618. const inspFiles = insp.attachments as Record<string, unknown>[] | undefined
  1619. if (inspFiles?.length) {
  1620. await tx.inspectionAttachment.createMany({
  1621. data: inspFiles.map((file) => ({
  1622. id: file.id as string,
  1623. fileName: file.fileName as string,
  1624. fileUrl:
  1625. rewriteFileUrl(file.fileUrl as string, ctx.organizationId) ??
  1626. (file.fileUrl as string),
  1627. fileType: file.fileType as string,
  1628. fileSize: (file.fileSize as number) || 0,
  1629. category: (file.category as string) || 'image',
  1630. description: (file.description as string) || null,
  1631. includeInReport: file.includeInReport !== false,
  1632. createdAt: toSafeDate(file.createdAt as string),
  1633. inspectionId: insp.id as string,
  1634. })),
  1635. })
  1636. }
  1637. // Status reports sent from the inspection, as a job's are restored
  1638. // with the job.
  1639. await restoreRows(
  1640. 'inspection status reports',
  1641. (rows) => tx.statusReport.createMany({ data: rows as never }),
  1642. insp.statusReports,
  1643. { organizationId: ctx.organizationId, inspectionId: insp.id as string },
  1644. { fields: ['videoUrl'], organizationId: ctx.organizationId }
  1645. )
  1646. const quoteReqs = insp.quoteRequests as Record<string, unknown>[] | undefined
  1647. if (quoteReqs?.length) {
  1648. await tx.inspectionQuoteRequest.createMany({
  1649. data: quoteReqs.map((qr) => ({
  1650. id: qr.id as string,
  1651. status: (qr.status as string) || 'pending',
  1652. message: (qr.message as string) || null,
  1653. selectedItemIds: (qr.selectedItemIds as string[]) || [],
  1654. createdAt: toSafeDate(qr.createdAt as string),
  1655. inspectionId: insp.id as string,
  1656. organizationId: ctx.organizationId,
  1657. })),
  1658. })
  1659. }
  1660. }
  1661. }
  1662. // 10b. The jobs and quotes raised from an inspection point back at it.
  1663. // Both were restored before the inspections existed, so the link is
  1664. // written now, to any inspection the workshop holds at this point:
  1665. // one that came back with the backup, or one that was never cleared
  1666. // because the backup did not carry inspections at all.
  1667. {
  1668. const restoredInspections = new Set(
  1669. (
  1670. await tx.inspection.findMany({
  1671. where: { organizationId: ctx.organizationId },
  1672. select: { id: true },
  1673. })
  1674. ).map((insp) => insp.id)
  1675. )
  1676. const linked = (rows: unknown) =>
  1677. ((rows as Record<string, unknown>[] | undefined) ?? []).filter(
  1678. (row) =>
  1679. typeof row.inspectionId === 'string' && restoredInspections.has(row.inspectionId)
  1680. )
  1681. const jobs = [
  1682. ...((data.vehicles as Record<string, unknown>[] | undefined) ?? []).flatMap((vehicle) =>
  1683. linked(vehicle.serviceRecords)
  1684. ),
  1685. ...linked(data.counterSales),
  1686. ]
  1687. // The row's own "last changed" is written with the link: left to the
  1688. // database, every linked job and quote came back changed today.
  1689. for (const sr of jobs) {
  1690. await tx.serviceRecord.updateMany({
  1691. where: { id: sr.id as string, organizationId: ctx.organizationId },
  1692. data: {
  1693. inspectionId: sr.inspectionId as string,
  1694. updatedAt: toSafeDate(sr.updatedAt as string),
  1695. },
  1696. })
  1697. }
  1698. for (const q of linked(data.quotes)) {
  1699. await tx.quote.updateMany({
  1700. where: { id: q.id as string, organizationId: ctx.organizationId },
  1701. data: {
  1702. inspectionId: q.inspectionId as string,
  1703. updatedAt: toSafeDate(q.updatedAt as string),
  1704. },
  1705. })
  1706. }
  1707. }
  1708. // 10c. The condition map's marks. They hang off the vehicle but name
  1709. // the inspection check or the job they were drawn on, so they come
  1710. // after both; a sheet the backup did not carry leaves the mark with no
  1711. // sheet rather than failing the import. Who drew and cleared them
  1712. // comes back when that person is in this workshop.
  1713. if (data.vehicles?.length) {
  1714. const marks = (data.vehicles as Record<string, unknown>[]).flatMap((vehicle) =>
  1715. ((vehicle.conditionMarks as Record<string, unknown>[] | undefined) ?? []).map(
  1716. (mark) => ({
  1717. mark,
  1718. vehicleId: vehicle.id as string,
  1719. })
  1720. )
  1721. )
  1722. if (marks.length > 0) {
  1723. const ids = (key: string) =>
  1724. marks
  1725. .map(({ mark }) => mark[key])
  1726. .filter((id): id is string => typeof id === 'string')
  1727. const [inspectionRows, itemRows, jobRows] = await Promise.all([
  1728. tx.inspection.findMany({
  1729. where: { id: { in: ids('inspectionId') }, organizationId },
  1730. select: { id: true },
  1731. }),
  1732. tx.inspectionItem.findMany({
  1733. where: { id: { in: ids('inspectionItemId') }, inspection: { organizationId } },
  1734. select: { id: true },
  1735. }),
  1736. tx.serviceRecord.findMany({
  1737. where: { id: { in: ids('serviceRecordId') }, organizationId },
  1738. select: { id: true },
  1739. }),
  1740. ])
  1741. const restoredInspectionIds = new Set(inspectionRows.map((row) => row.id))
  1742. const restoredItemIds = new Set(itemRows.map((row) => row.id))
  1743. const restoredJobIds = new Set(jobRows.map((row) => row.id))
  1744. const restoredMarkIds = new Set(marks.map(({ mark }) => mark.id as string))
  1745. await tx.conditionMark.createMany({
  1746. data: marks.map(({ mark, vehicleId: markVehicleId }) => ({
  1747. id: mark.id as string,
  1748. organizationId,
  1749. vehicleId: markVehicleId,
  1750. inspectionId: keptReference(mark.inspectionId, restoredInspectionIds),
  1751. inspectionItemId: keptReference(mark.inspectionItemId, restoredItemIds),
  1752. serviceRecordId: keptReference(mark.serviceRecordId, restoredJobIds),
  1753. bodyType: (mark.bodyType as string) || 'sedan',
  1754. view: (mark.view as string) || 'left',
  1755. panel: (mark.panel as string) || 'hood',
  1756. x: typeof mark.x === 'number' ? mark.x : 0.5,
  1757. y: typeof mark.y === 'number' ? mark.y : 0.5,
  1758. kind: (mark.kind as string) || 'dent',
  1759. severity: (mark.severity as string) || 'minor',
  1760. note: (mark.note as string) || null,
  1761. imageUrls: ((mark.imageUrls as string[]) || []).map(
  1762. (url) => rewriteFileUrl(url, ctx.organizationId) ?? url
  1763. ),
  1764. recordedAt: toSafeDate(mark.recordedAt as string),
  1765. recordedById: keptReference(mark.recordedById, memberUserIds),
  1766. resolvedAt: toSafeDate(mark.resolvedAt as string) ?? null,
  1767. resolvedById: keptReference(mark.resolvedById, memberUserIds),
  1768. // The newer mark this one was recorded again as. Without it
  1769. // the same dent shows twice on the vehicle.
  1770. carriedToId: keptReference(mark.carriedToId, restoredMarkIds),
  1771. createdAt: toSafeDate(mark.createdAt as string),
  1772. updatedAt: toSafeDate(mark.updatedAt as string),
  1773. })),
  1774. })
  1775. }
  1776. }
  1777. // 11. Insert audit logs
  1778. if (data.auditLogs?.length) {
  1779. await tx.auditLog.createMany({
  1780. data: (data.auditLogs as Record<string, unknown>[]).map(
  1781. (log: Record<string, unknown>) => ({
  1782. id: log.id as string,
  1783. timestamp: toSafeDate(log.timestamp as string),
  1784. action: log.action as string,
  1785. entity: (log.entity as string) || null,
  1786. entityId: (log.entityId as string) || null,
  1787. message: (log.message as string) || null,
  1788. metadata:
  1789. log.metadata != null ? (log.metadata as Prisma.InputJsonValue) : Prisma.DbNull,
  1790. ip: (log.ip as string) || null,
  1791. userAgent: (log.userAgent as string) || null,
  1792. // Who did it, when that person is in this workshop; an entry
  1793. // by somebody who is not goes to whoever restores, as before.
  1794. userId: keptReference(log.userId, memberUserIds) ?? ctx.userId,
  1795. organizationId: ctx.organizationId,
  1796. })
  1797. ),
  1798. })
  1799. }
  1800. // 12. Insert SMS messages
  1801. if (data.smsMessages?.length) {
  1802. await tx.smsMessage.createMany({
  1803. data: (data.smsMessages as Record<string, unknown>[]).map(
  1804. (msg: Record<string, unknown>) => ({
  1805. id: msg.id as string,
  1806. direction: msg.direction as string,
  1807. fromNumber: msg.fromNumber as string,
  1808. toNumber: msg.toNumber as string,
  1809. body: msg.body as string,
  1810. status: (msg.status as string) || 'queued',
  1811. providerMsgId: (msg.providerMsgId as string) || null,
  1812. errorMessage: (msg.errorMessage as string) || null,
  1813. relatedEntityType: (msg.relatedEntityType as string) || null,
  1814. relatedEntityId: (msg.relatedEntityId as string) || null,
  1815. // Read stays read: left out, every text ever received came
  1816. // back as unread.
  1817. readAt: msg.readAt ? (toSafeDate(msg.readAt as string) ?? null) : null,
  1818. createdAt: toSafeDate(msg.createdAt as string),
  1819. updatedAt: toSafeDate(msg.updatedAt as string),
  1820. organizationId: ctx.organizationId,
  1821. customerId: (msg.customerId as string) || null,
  1822. })
  1823. ),
  1824. })
  1825. }
  1826. // The other two channels, which were never in a backup at all while SMS
  1827. // was. Customers are already in place, so their links survive.
  1828. await restoreRows(
  1829. 'WhatsApp messages',
  1830. (rows) => tx.whatsappMessage.createMany({ data: rows as never }),
  1831. data.whatsappMessages,
  1832. { organizationId },
  1833. { fields: ['mediaUrl'], organizationId }
  1834. )
  1835. await restoreRows(
  1836. 'Telegram messages',
  1837. (rows) => tx.telegramMessage.createMany({ data: rows as never }),
  1838. data.telegramMessages,
  1839. { organizationId }
  1840. )
  1841. // Mail, with the files that came with it. A message whose customer is
  1842. // not in this workshop after the restore keeps its thread, which reads
  1843. // by address, and only loses the link: a dangling id would fail the
  1844. // whole import on its foreign key.
  1845. if (data.emailMessages?.length) {
  1846. const customerIds = new Set(
  1847. (await tx.customer.findMany({ where: { organizationId }, select: { id: true } })).map(
  1848. (c) => c.id
  1849. )
  1850. )
  1851. const messages = (data.emailMessages as Record<string, unknown>[]).map(
  1852. (m): Record<string, unknown> => ({
  1853. ...m,
  1854. customerId: keptReference(m.customerId, customerIds),
  1855. })
  1856. )
  1857. await restoreRows(
  1858. 'email messages',
  1859. (rows) => tx.emailMessage.createMany({ data: rows as never }),
  1860. messages,
  1861. { organizationId }
  1862. )
  1863. // Messages keep their ids, so each attachment points at its own again.
  1864. await restoreRows(
  1865. 'email attachments',
  1866. (rows) => tx.emailAttachment.createMany({ data: rows as never }),
  1867. messages.flatMap((m) =>
  1868. ((m.attachments as Record<string, unknown>[] | undefined) ?? []).map((a) => ({
  1869. ...a,
  1870. messageId: m.id,
  1871. }))
  1872. ),
  1873. {},
  1874. { fields: ['url'], organizationId }
  1875. )
  1876. }
  1877. // The chat in the customer portal. A message belongs to a customer, so
  1878. // one whose customer is not in this workshop after the restore has no
  1879. // thread to sit in and is left out. Its author is kept only when that
  1880. // person is a member of this workshop; its photos point at this
  1881. // workshop. Without the chat in the file, the workshop's own messages
  1882. // (read before the customers were replaced) go back as they were.
  1883. const filePortalMessages = data.portalMessages as Record<string, unknown>[] | undefined
  1884. const portalMessages: Record<string, unknown>[] =
  1885. filePortalMessages ?? currentPortalMessages
  1886. if (portalMessages.length) {
  1887. const customerIds = new Set(
  1888. (await tx.customer.findMany({ where: { organizationId }, select: { id: true } })).map(
  1889. (c) => c.id
  1890. )
  1891. )
  1892. const authorIds = portalMessageAuthorIds(portalMessages)
  1893. const members = new Set(
  1894. authorIds.length
  1895. ? (
  1896. await tx.organizationMember.findMany({
  1897. where: { organizationId, userId: { in: authorIds } },
  1898. select: { userId: true },
  1899. })
  1900. ).map((m) => m.userId)
  1901. : []
  1902. )
  1903. await restoreRows(
  1904. 'portal messages',
  1905. (batch) => tx.portalMessage.createMany({ data: batch as never }),
  1906. portalMessageRowsToRestore(portalMessages, customerIds, members),
  1907. { organizationId },
  1908. { fields: ['attachments'], organizationId }
  1909. )
  1910. }
  1911. // Workshop configuration: the kinds of mark, closed days, labour
  1912. // presets, webhooks, report schedules.
  1913. await restoreRows(
  1914. 'closed days',
  1915. (rows) => tx.workshopClosure.createMany({ data: rows as never }),
  1916. data.workshopClosures,
  1917. { organizationId }
  1918. )
  1919. const markTypes = data.conditionMarkTypes as Record<string, unknown>[] | undefined
  1920. if (markTypes?.length) {
  1921. await restoreRows(
  1922. 'kinds of mark',
  1923. (rows) => tx.conditionMarkType.createMany({ data: rows as never }),
  1924. markTypes,
  1925. { organizationId }
  1926. )
  1927. }
  1928. const laborPresets = data.laborPresets as Record<string, unknown>[] | undefined
  1929. if (laborPresets?.length) {
  1930. await restoreRows(
  1931. 'labour presets',
  1932. (rows) => tx.laborPreset.createMany({ data: rows as never }),
  1933. laborPresets,
  1934. {
  1935. organizationId,
  1936. userId: ctx.userId,
  1937. }
  1938. )
  1939. for (const preset of laborPresets) {
  1940. await restoreRows(
  1941. 'labour preset items',
  1942. (rows) => tx.laborPresetItem.createMany({ data: rows as never }),
  1943. preset.items
  1944. )
  1945. await restoreRows(
  1946. 'labour preset parts',
  1947. (rows) => tx.laborPresetPart.createMany({ data: rows as never }),
  1948. preset.parts
  1949. )
  1950. }
  1951. }
  1952. await restoreRows(
  1953. 'webhooks',
  1954. (rows) => tx.webhook.createMany({ data: rows as never }),
  1955. data.webhooks,
  1956. {
  1957. organizationId,
  1958. }
  1959. )
  1960. await restoreRows(
  1961. 'report schedules',
  1962. (rows) => tx.reportSchedule.createMany({ data: rows as never }),
  1963. data.reportSchedules,
  1964. { organizationId }
  1965. )
  1966. // Roles fill gaps rather than replace: members still point at the roles
  1967. // they hold, and members are not in a backup.
  1968. const roles = data.roles as Record<string, unknown>[] | undefined
  1969. if (roles?.length) {
  1970. for (const role of roles) {
  1971. // A role is unique by id and again by name, and an organisation
  1972. // already has its default roles by the time anyone restores a file.
  1973. const existing = await tx.role.findFirst({
  1974. where: {
  1975. organizationId,
  1976. OR: [{ id: role.id as string }, { name: role.name as string }],
  1977. },
  1978. })
  1979. if (existing) continue
  1980. await tx.role.create({ data: columnsOf(role, { organizationId }) as never })
  1981. await restoreRows(
  1982. 'role permissions',
  1983. (rows) => tx.permission.createMany({ data: rows as never }),
  1984. role.permissions
  1985. )
  1986. }
  1987. }
  1988. // 13. Insert notifications
  1989. if (data.notifications?.length) {
  1990. await tx.notification.createMany({
  1991. data: (data.notifications as Record<string, unknown>[]).map(
  1992. (n: Record<string, unknown>) => ({
  1993. id: n.id as string,
  1994. type: n.type as string,
  1995. title: n.title as string,
  1996. message: n.message as string,
  1997. entityType: n.entityType as string,
  1998. entityId: n.entityId as string,
  1999. entityUrl: n.entityUrl as string,
  2000. read: (n.read as boolean) || false,
  2001. createdAt: toSafeDate(n.createdAt as string),
  2002. organizationId: ctx.organizationId,
  2003. })
  2004. ),
  2005. })
  2006. }
  2007. // 14. Insert scheduled messages. Last, because they point at customers
  2008. // and vehicles, which are already in by this stage.
  2009. if (data.scheduledMessages?.length) {
  2010. await tx.scheduledMessage.createMany({
  2011. // A message with no readable send time has nothing to act on, so it
  2012. // is dropped rather than restored to an invented moment
  2013. data: (data.scheduledMessages as Record<string, unknown>[])
  2014. .filter((msg: Record<string, unknown>) => !!toSafeDate(msg.sendAt as string))
  2015. .map((msg: Record<string, unknown>) => ({
  2016. id: msg.id as string,
  2017. channel: msg.channel as string,
  2018. subject: (msg.subject as string) || null,
  2019. body: msg.body as string,
  2020. recipient: (msg.recipient as string) || null,
  2021. status: (msg.status as string) || 'scheduled',
  2022. sendAt: toSafeDate(msg.sendAt as string)!,
  2023. frequency: (msg.frequency as string) || 'once',
  2024. endDate: msg.endDate ? toSafeDate(msg.endDate as string) : null,
  2025. lastRunAt: msg.lastRunAt ? toSafeDate(msg.lastRunAt as string) : null,
  2026. sentAt: msg.sentAt ? toSafeDate(msg.sentAt as string) : null,
  2027. runCount: (msg.runCount as number) || 0,
  2028. errorMessage: (msg.errorMessage as string) || null,
  2029. createdAt: toSafeDate(msg.createdAt as string),
  2030. updatedAt: toSafeDate(msg.updatedAt as string),
  2031. organizationId: ctx.organizationId,
  2032. customerId: (msg.customerId as string) || null,
  2033. vehicleId: (msg.vehicleId as string) || null,
  2034. // The author of the backup may not exist here; the importing
  2035. // user owns the restored rows, as everywhere else in this file
  2036. createdById: ctx.userId,
  2037. })),
  2038. })
  2039. }
  2040. // 14b. Reminder campaigns and their send rows, after the messages, the
  2041. // customers and the vehicles they point at. A send whose vehicle or
  2042. // customer is not in this restore is left out rather than invented,
  2043. // and one whose message is missing keeps everything but the message link.
  2044. if (data.inspectionReminderCampaigns?.length) {
  2045. await restoreRows(
  2046. 'reminder campaigns',
  2047. (rows) => tx.inspectionReminderCampaign.createMany({ data: rows as never }),
  2048. data.inspectionReminderCampaigns,
  2049. { organizationId, createdById: ctx.userId }
  2050. )
  2051. const vehicleIds = new Set(
  2052. ((data.vehicles as Record<string, unknown>[] | undefined) ?? []).map(
  2053. (v) => v.id as string
  2054. )
  2055. )
  2056. const customerIds = new Set(
  2057. ((data.customers as Record<string, unknown>[] | undefined) ?? []).map(
  2058. (c) => c.id as string
  2059. )
  2060. )
  2061. const messageIds = new Set(
  2062. ((data.scheduledMessages as Record<string, unknown>[] | undefined) ?? []).map(
  2063. (m) => m.id as string
  2064. )
  2065. )
  2066. const sends = (
  2067. (data.inspectionReminderSends as Record<string, unknown>[] | undefined) ?? []
  2068. )
  2069. .filter(
  2070. (row) =>
  2071. vehicleIds.has(row.vehicleId as string) && customerIds.has(row.customerId as string)
  2072. )
  2073. .map((row) => ({
  2074. ...row,
  2075. scheduledMessageId: messageIds.has(row.scheduledMessageId as string)
  2076. ? row.scheduledMessageId
  2077. : null,
  2078. }))
  2079. await restoreRows(
  2080. 'reminder sends',
  2081. (rows) => tx.inspectionReminderSend.createMany({ data: rows as never }),
  2082. sends,
  2083. { organizationId }
  2084. )
  2085. }
  2086. // 14c. The calendar's appointments, then the alerts set on them and on
  2087. // booked jobs. After the customers, vehicles and jobs they point at,
  2088. // and each link is kept only when its row is really here: one this
  2089. // restore cleared or left out would fail the whole import on its
  2090. // foreign key. An appointment with no readable time has nothing to
  2091. // put on the calendar and is left out; an alert left pointing at
  2092. // neither an appointment nor a job goes with it. The people who booked
  2093. // them and set the alerts are kept when they are members of this
  2094. // workshop today, and are the importing user otherwise, so an alert
  2095. // that mails the workshop still reaches someone.
  2096. if (data.appointments?.length || data.appointmentNotifications?.length) {
  2097. const idsIn = (rows: Record<string, unknown>[], column: string) => [
  2098. ...new Set(
  2099. rows
  2100. .map((row) => row[column])
  2101. .filter((id): id is string => typeof id === 'string' && id.length > 0)
  2102. ),
  2103. ]
  2104. const appointments = (
  2105. (data.appointments as Record<string, unknown>[] | undefined) ?? []
  2106. ).filter(
  2107. (a) =>
  2108. typeof a.id === 'string' &&
  2109. typeof a.title === 'string' &&
  2110. !!toSafeDate(a.startAt as string) &&
  2111. !!toSafeDate(a.endAt as string)
  2112. )
  2113. const alerts = (
  2114. (data.appointmentNotifications as Record<string, unknown>[] | undefined) ?? []
  2115. ).filter(
  2116. (n) =>
  2117. typeof n.id === 'string' &&
  2118. typeof n.channel === 'string' &&
  2119. typeof n.unit === 'string' &&
  2120. Number.isInteger(n.amount)
  2121. )
  2122. const people = [
  2123. ...new Set([...idsIn(appointments, 'createdById'), ...idsIn(alerts, 'userId')]),
  2124. ]
  2125. const customerIds = idsIn(appointments, 'customerId')
  2126. const vehicleIds = idsIn(appointments, 'vehicleId')
  2127. const [memberRows, customerRows, vehicleRows] = await Promise.all([
  2128. people.length
  2129. ? tx.organizationMember.findMany({
  2130. where: { organizationId, userId: { in: people } },
  2131. select: { userId: true },
  2132. })
  2133. : Promise.resolve([]),
  2134. customerIds.length
  2135. ? tx.customer.findMany({
  2136. where: { id: { in: customerIds }, organizationId },
  2137. select: { id: true },
  2138. })
  2139. : Promise.resolve([]),
  2140. vehicleIds.length
  2141. ? tx.vehicle.findMany({
  2142. where: { id: { in: vehicleIds }, organizationId },
  2143. select: { id: true },
  2144. })
  2145. : Promise.resolve([]),
  2146. ])
  2147. const memberIds = new Set(memberRows.map((m) => m.userId))
  2148. const restoredCustomers = new Set(customerRows.map((row) => row.id))
  2149. const restoredVehicles = new Set(vehicleRows.map((row) => row.id))
  2150. await restoreRows(
  2151. 'appointments',
  2152. (rows) => tx.appointment.createMany({ data: rows as never }),
  2153. appointments.map((a) => ({
  2154. id: a.id as string,
  2155. title: a.title as string,
  2156. description: typeof a.description === 'string' ? a.description : null,
  2157. startAt: toSafeDate(a.startAt as string),
  2158. endAt: toSafeDate(a.endAt as string),
  2159. createdAt: toSafeDate(a.createdAt as string),
  2160. updatedAt: toSafeDate(a.updatedAt as string),
  2161. customerId: keptReference(a.customerId, restoredCustomers),
  2162. vehicleId: keptReference(a.vehicleId, restoredVehicles),
  2163. createdById: keptReference(a.createdById, memberIds) ?? ctx.userId,
  2164. })),
  2165. { organizationId }
  2166. )
  2167. const appointmentIds = idsIn(alerts, 'appointmentId')
  2168. const jobIds = idsIn(alerts, 'serviceRecordId')
  2169. const [appointmentRows, jobRows] = await Promise.all([
  2170. appointmentIds.length
  2171. ? tx.appointment.findMany({
  2172. where: { id: { in: appointmentIds }, organizationId },
  2173. select: { id: true },
  2174. })
  2175. : Promise.resolve([]),
  2176. jobIds.length
  2177. ? tx.serviceRecord.findMany({
  2178. where: { id: { in: jobIds }, organizationId },
  2179. select: { id: true },
  2180. })
  2181. : Promise.resolve([]),
  2182. ])
  2183. const restoredAppointments = new Set(appointmentRows.map((row) => row.id))
  2184. const restoredJobs = new Set(jobRows.map((row) => row.id))
  2185. await restoreRows(
  2186. 'appointment alerts',
  2187. (rows) => tx.appointmentNotification.createMany({ data: rows as never }),
  2188. alerts
  2189. .map((n) => ({
  2190. id: n.id as string,
  2191. channel: n.channel as string,
  2192. amount: n.amount as number,
  2193. unit: n.unit as string,
  2194. atTime: typeof n.atTime === 'string' ? n.atTime : null,
  2195. // When it last went out, so a restore does not send it again.
  2196. sentForStart: toSafeDate(n.sentForStart as string) ?? null,
  2197. createdAt: toSafeDate(n.createdAt as string),
  2198. updatedAt: toSafeDate(n.updatedAt as string),
  2199. userId: keptReference(n.userId, memberIds) ?? ctx.userId,
  2200. appointmentId: keptReference(n.appointmentId, restoredAppointments),
  2201. serviceRecordId: keptReference(n.serviceRecordId, restoredJobs),
  2202. }))
  2203. .filter((n) => n.appointmentId !== null || n.serviceRecordId !== null),
  2204. { organizationId }
  2205. )
  2206. }
  2207. // 15. Tire hotel. Last, because sets point at customers and vehicles,
  2208. // and shelves have to exist before anything can sit on one.
  2209. if (data.tireWarehouses?.length) {
  2210. for (const wh of data.tireWarehouses as Record<string, unknown>[]) {
  2211. await tx.tireWarehouse.create({
  2212. data: {
  2213. id: wh.id as string,
  2214. name: wh.name as string,
  2215. address: (wh.address as string) || null,
  2216. notes: (wh.notes as string) || null,
  2217. isDefault: (wh.isDefault as boolean) || false,
  2218. isArchived: (wh.isArchived as boolean) || false,
  2219. createdAt: toSafeDate(wh.createdAt as string),
  2220. updatedAt: toSafeDate(wh.updatedAt as string),
  2221. organizationId: ctx.organizationId,
  2222. userId: ctx.userId,
  2223. },
  2224. })
  2225. const locations = wh.locations as Record<string, unknown>[] | undefined
  2226. if (locations?.length) {
  2227. await tx.tireLocation.createMany({
  2228. data: locations.map((loc) => ({
  2229. id: loc.id as string,
  2230. code: loc.code as string,
  2231. zone: (loc.zone as string) || null,
  2232. rack: (loc.rack as string) || null,
  2233. shelf: (loc.shelf as string) || null,
  2234. position: (loc.position as string) || null,
  2235. capacity: (loc.capacity as number) ?? 0,
  2236. notes: (loc.notes as string) || null,
  2237. isArchived: (loc.isArchived as boolean) || false,
  2238. createdAt: toSafeDate(loc.createdAt as string),
  2239. updatedAt: toSafeDate(loc.updatedAt as string),
  2240. warehouseId: wh.id as string,
  2241. organizationId: ctx.organizationId,
  2242. })),
  2243. })
  2244. }
  2245. }
  2246. }
  2247. if (data.tireSets?.length) {
  2248. // A set whose shelf did not come across is restored unplaced rather
  2249. // than dropped: losing the tires is worse than losing where they sat,
  2250. // and an unplaced set is visible and fixable.
  2251. const knownLocations = new Set(
  2252. (
  2253. await tx.tireLocation.findMany({
  2254. where: { organizationId: ctx.organizationId },
  2255. select: { id: true },
  2256. })
  2257. ).map((l) => l.id)
  2258. )
  2259. for (const set of data.tireSets as Record<string, unknown>[]) {
  2260. const locationId = set.locationId as string | null
  2261. await tx.tireSet.create({
  2262. data: {
  2263. id: set.id as string,
  2264. reference: (set.reference as string) || null,
  2265. season: (set.season as string) || 'summer',
  2266. studded: (set.studded as boolean) || false,
  2267. brand: (set.brand as string) || null,
  2268. model: (set.model as string) || null,
  2269. size: (set.size as string) || null,
  2270. dotCode: (set.dotCode as string) || null,
  2271. loadSpeedIndex: (set.loadSpeedIndex as string) || null,
  2272. withRims: (set.withRims as boolean) || false,
  2273. rimType: (set.rimType as string) || null,
  2274. hasTpms: (set.hasTpms as boolean) || false,
  2275. quantity: numberOr(set.quantity, 4),
  2276. status: (set.status as string) || 'stored',
  2277. notes: (set.notes as string) || null,
  2278. checkedInAt: set.checkedInAt ? toSafeDate(set.checkedInAt as string) : null,
  2279. checkedOutAt: set.checkedOutAt ? toSafeDate(set.checkedOutAt as string) : null,
  2280. createdAt: toSafeDate(set.createdAt as string),
  2281. updatedAt: toSafeDate(set.updatedAt as string),
  2282. locationId: locationId && knownLocations.has(locationId) ? locationId : null,
  2283. vehicleId: (set.vehicleId as string) || null,
  2284. customerId: (set.customerId as string) || null,
  2285. organizationId: ctx.organizationId,
  2286. userId: ctx.userId,
  2287. },
  2288. })
  2289. // Movements first: a reading names the movement it was taken at.
  2290. const movements = set.movements as Record<string, unknown>[] | undefined
  2291. if (movements?.length) {
  2292. await tx.tireMovement.createMany({
  2293. data: movements.map((mv) => ({
  2294. id: mv.id as string,
  2295. type: mv.type as string,
  2296. fromCode: (mv.fromCode as string) || null,
  2297. toCode: (mv.toCode as string) || null,
  2298. note: (mv.note as string) || null,
  2299. createdAt: toSafeDate(mv.createdAt as string),
  2300. tireSetId: set.id as string,
  2301. // The codes above carry the history in readable form; the
  2302. // links come back for the shelves that came across, and
  2303. // who moved the set when that person is in this workshop.
  2304. fromLocationId: keptReference(mv.fromLocationId, knownLocations),
  2305. toLocationId: keptReference(mv.toLocationId, knownLocations),
  2306. performedById: keptReference(mv.performedById, memberUserIds),
  2307. organizationId: ctx.organizationId,
  2308. })),
  2309. })
  2310. }
  2311. const restoredMovements = new Set((movements ?? []).map((mv) => mv.id as string))
  2312. const measurements = set.measurements as Record<string, unknown>[] | undefined
  2313. if (measurements?.length) {
  2314. await tx.tireMeasurement.createMany({
  2315. data: measurements.map((m) => ({
  2316. id: m.id as string,
  2317. position: (m.position as string) || 'unspecified',
  2318. treadDepthMm: (m.treadDepthMm as number) ?? null,
  2319. pressureBar: (m.pressureBar as number) ?? null,
  2320. condition: (m.condition as string) || 'good',
  2321. damage: (m.damage as string) || null,
  2322. notes: (m.notes as string) || null,
  2323. measuredAt: toSafeDate(m.measuredAt as string),
  2324. tireSetId: set.id as string,
  2325. // The person who took the reading may not exist here, and a
  2326. // wrong name on a condition record is worse than none: kept
  2327. // only when they are in this workshop.
  2328. measuredById: keptReference(m.measuredById, memberUserIds),
  2329. movementId: keptReference(m.movementId, restoredMovements),
  2330. })),
  2331. })
  2332. // The condition photos of each reading. They hang off the
  2333. // measurement and are deleted with it, so a restore without them
  2334. // leaves the reading with no evidence behind it.
  2335. for (const m of measurements) {
  2336. await restoreRows(
  2337. 'tire condition photos',
  2338. (rows) => tx.storedImage.createMany({ data: rows as never }),
  2339. m.images,
  2340. { tireMeasurementId: m.id as string, inventoryPartId: null },
  2341. { fields: ['url'], organizationId: ctx.organizationId }
  2342. )
  2343. }
  2344. }
  2345. const attachments = set.attachments as Record<string, unknown>[] | undefined
  2346. if (attachments?.length) {
  2347. await tx.tireSetAttachment.createMany({
  2348. data: attachments.map((att) => ({
  2349. id: att.id as string,
  2350. fileName: (att.fileName as string) || 'file',
  2351. fileUrl: rewriteFileUrl(att.fileUrl as string, ctx.organizationId) ?? '',
  2352. fileType: (att.fileType as string) || 'application/octet-stream',
  2353. fileSize: (att.fileSize as number) || 0,
  2354. description: (att.description as string) || null,
  2355. includeInInvoice: att.includeInInvoice !== false,
  2356. sortOrder: (att.sortOrder as number) || 0,
  2357. createdAt: toSafeDate(att.createdAt as string),
  2358. tireSetId: set.id as string,
  2359. organizationId: ctx.organizationId,
  2360. // The uploader may not exist in the target organization.
  2361. uploadedById: keptReference(att.uploadedById, memberUserIds),
  2362. })),
  2363. })
  2364. }
  2365. const treatments = set.treatments as Record<string, unknown>[] | undefined
  2366. if (treatments?.length) {
  2367. await tx.tireTreatment.createMany({
  2368. data: treatments.map((tr) => ({
  2369. id: tr.id as string,
  2370. type: tr.type as string,
  2371. status: (tr.status as string) || 'pending',
  2372. notes: (tr.notes as string) || null,
  2373. completedAt: tr.completedAt ? toSafeDate(tr.completedAt as string) : null,
  2374. completedById: keptReference(tr.completedById, memberUserIds),
  2375. createdAt: toSafeDate(tr.createdAt as string),
  2376. updatedAt: toSafeDate(tr.updatedAt as string),
  2377. tireSetId: set.id as string,
  2378. organizationId: ctx.organizationId,
  2379. })),
  2380. })
  2381. }
  2382. }
  2383. }
  2384. // The jobs and quotes raised for a stored set point back at it. Both
  2385. // were restored before the sets existed, so the link is written now,
  2386. // to any set the workshop holds at this point: one that came back with
  2387. // the backup, or one that was never cleared because the backup did not
  2388. // carry the tire hotel.
  2389. {
  2390. const restoredTireSets = new Set(
  2391. (
  2392. await tx.tireSet.findMany({
  2393. where: { organizationId: ctx.organizationId },
  2394. select: { id: true },
  2395. })
  2396. ).map((set) => set.id)
  2397. )
  2398. const aboutASet = (rows: unknown) =>
  2399. ((rows as Record<string, unknown>[] | undefined) ?? []).filter(
  2400. (row) => typeof row.tireSetId === 'string' && restoredTireSets.has(row.tireSetId)
  2401. )
  2402. const jobs = [
  2403. ...((data.vehicles as Record<string, unknown>[] | undefined) ?? []).flatMap((vehicle) =>
  2404. aboutASet(vehicle.serviceRecords)
  2405. ),
  2406. ...aboutASet(data.counterSales),
  2407. ]
  2408. for (const sr of jobs) {
  2409. await tx.serviceRecord.updateMany({
  2410. where: { id: sr.id as string, organizationId: ctx.organizationId },
  2411. data: {
  2412. tireSetId: sr.tireSetId as string,
  2413. updatedAt: toSafeDate(sr.updatedAt as string),
  2414. },
  2415. })
  2416. }
  2417. for (const q of aboutASet(data.quotes)) {
  2418. await tx.quote.updateMany({
  2419. where: { id: q.id as string, organizationId: ctx.organizationId },
  2420. data: {
  2421. tireSetId: q.tireSetId as string,
  2422. updatedAt: toSafeDate(q.updatedAt as string),
  2423. },
  2424. })
  2425. }
  2426. }
  2427. },
  2428. // A restore of years of records takes minutes, not Prisma's five seconds.
  2429. { maxWait: 30_000, timeout: TRANSACTION_TIMEOUT_MS }
  2430. )
  2431. } finally {
  2432. releaseRestoreLock(lock)
  2433. }
  2434. // The rows are committed by now. A failure here (a full disk, a folder
  2435. // that cannot be written) leaves records from the backup beside files that
  2436. // are only partly from it, and the message has to say so.
  2437. if (files) {
  2438. try {
  2439. await restoreFiles(files, ctx.organizationId)
  2440. } catch (err) {
  2441. const reason = err instanceof Error ? err.message : String(err)
  2442. throw new Error(
  2443. `The records were put back, but copying the files failed: ${reason}. The workshop's records are now from the backup while its files are only partly from it; fix the cause and restore again.`
  2444. )
  2445. }
  2446. }
  2447. return { redactedSettings }
  2448. }
  2449. /**
  2450. * A backup on disk: `data.json` and, when the backup carried photos and
  2451. * files, the restored uploads folder beside it. This is what a snapshot
  2452. * from a backup destination becomes once restic has written it out.
  2453. */
  2454. export async function restoreFromDisk(
  2455. ctx: RestoreActor,
  2456. input: { dataFile: string; filesDir: string | null }
  2457. ): Promise<{ redactedSettings: string[] }> {
  2458. const backup = await readJsonFile<unknown>(input.dataFile)
  2459. assertSupportedBackup(backup)
  2460. const files = input.filesDir ? await directoryFileSource(input.filesDir) : null
  2461. return restoreBackup(ctx, backup, files)
  2462. }