input-limits.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import type { z } from 'zod'
  2. /**
  3. * The largest figures and the longest texts the forms take.
  4. *
  5. * Well past anything a workshop writes, and short of what breaks a page or a
  6. * document: a quantity of a billion at a hundred million each once saved,
  7. * printed a total of 119,999,998,800,000,000 with float noise in it and
  8. * pushed the invoice off the page, and a 3,000-character address made a
  9. * customer's header 21,545 px wide. The schemas refuse past these, and the
  10. * forms carry the same numbers so the browser can say so first.
  11. */
  12. /** Of one part on a line. */
  13. export const QUANTITY_MAX = 100_000
  14. /** A unit price, a unit cost or an hourly rate. */
  15. export const UNIT_PRICE_MAX = 10_000_000
  16. /** Hours on one labour line, or the count of a line priced per service. */
  17. export const LABOR_HOURS_MAX = 100_000
  18. /**
  19. * One line's total. Money is rounded to the cent on twelve significant
  20. * digits (src/lib/money.ts), so a total has to stay below ten billion for
  21. * the cents to survive; this keeps it well inside.
  22. */
  23. export const LINE_TOTAL_MAX = 1_000_000_000
  24. /** An odometer or an hour meter, as the technician app already takes it. */
  25. export const READING_MAX = 10_000_000
  26. /**
  27. * A priced line's own rules, for every schema that takes one (work orders,
  28. * labour presets): its count times its price no more than LINE_TOTAL_MAX,
  29. * whatever total the caller sent, and, with `pricedNeedsCount`, a priced line
  30. * at a count of zero refused. Reported as zod's own size issues, so a form
  31. * says them in words (src/lib/field-errors.ts).
  32. */
  33. export function checkLineAmounts(
  34. line: { count: number; price: number },
  35. ctx: z.RefinementCtx,
  36. /** Where each problem is reported: the count field, and the field the line total is said under. */
  37. fields: { count: string; total: string },
  38. options: { pricedNeedsCount?: boolean } = {}
  39. ): void {
  40. if (options.pricedNeedsCount && line.price > 0 && line.count <= 0) {
  41. ctx.addIssue({
  42. code: 'too_small',
  43. origin: 'number',
  44. minimum: 0,
  45. inclusive: false,
  46. path: [fields.count],
  47. message: 'A priced line needs a quantity above zero',
  48. input: line.count,
  49. })
  50. }
  51. if (line.count * line.price > LINE_TOTAL_MAX) {
  52. ctx.addIssue({
  53. code: 'too_big',
  54. origin: 'number',
  55. maximum: LINE_TOTAL_MAX,
  56. inclusive: true,
  57. path: [fields.total],
  58. message: `A line can come to at most ${LINE_TOTAL_MAX}`,
  59. input: line.price,
  60. })
  61. }
  62. }
  63. /**
  64. * What is wrong with one figure on a line, in the terms of
  65. * src/lib/field-errors.ts, so a line editor can say it under the field.
  66. */
  67. export interface LineFigureIssue {
  68. issue: 'negative' | 'positive' | 'tooLarge'
  69. limit?: number
  70. }
  71. /** A figure as typed: an empty box or letters count as nothing, as the save reads them. */
  72. function figure(value: unknown): number {
  73. const parsed = Number(value ?? 0)
  74. return Number.isFinite(parsed) ? parsed : 0
  75. }
  76. function bounded(value: number, max: number): LineFigureIssue | undefined {
  77. if (value < 0) return { issue: 'negative' }
  78. if (value > max) return { issue: 'tooLarge', limit: max }
  79. return undefined
  80. }
  81. export type PartLineIssues = Partial<
  82. Record<'quantity' | 'unitPrice' | 'unitCost' | 'total', LineFigureIssue>
  83. >
  84. export type LaborLineIssues = Partial<Record<'hours' | 'rate' | 'total', LineFigureIssue>>
  85. /**
  86. * The figures on a part line the save would refuse, by field: the rules of
  87. * servicePartSchema and quotePartSchema, worked out as the line is typed so
  88. * the field is marked before anybody presses Save. The line's quantity times
  89. * its price is reported as "total", said under the line's total.
  90. */
  91. export function partLineIssues(part: {
  92. name?: string | null
  93. quantity: unknown
  94. unitPrice: unknown
  95. unitCost?: unknown
  96. }): PartLineIssues {
  97. const quantity = figure(part.quantity)
  98. const unitPrice = figure(part.unitPrice)
  99. const issues: PartLineIssues = {}
  100. const quantityIssue = bounded(quantity, QUANTITY_MAX)
  101. if (quantityIssue) issues.quantity = quantityIssue
  102. // Priced and billed nothing. A free part may sit at zero as a note of what
  103. // was used, and a row with no name yet is still being typed.
  104. else if (part.name?.trim() && unitPrice > 0 && quantity <= 0) {
  105. issues.quantity = { issue: 'positive' }
  106. }
  107. const priceIssue = bounded(unitPrice, UNIT_PRICE_MAX)
  108. if (priceIssue) issues.unitPrice = priceIssue
  109. const costIssue = bounded(figure(part.unitCost), UNIT_PRICE_MAX)
  110. if (costIssue) issues.unitCost = costIssue
  111. if (quantity * unitPrice > LINE_TOTAL_MAX) {
  112. issues.total = { issue: 'tooLarge', limit: LINE_TOTAL_MAX }
  113. }
  114. return issues
  115. }
  116. /**
  117. * The figures on a labour line the save would refuse, by field. Hours may be
  118. * zero: a line started and not yet clocked, or work done for nothing.
  119. */
  120. export function laborLineIssues(labor: { hours: unknown; rate: unknown }): LaborLineIssues {
  121. const hours = figure(labor.hours)
  122. const rate = figure(labor.rate)
  123. const issues: LaborLineIssues = {}
  124. const hoursIssue = bounded(hours, LABOR_HOURS_MAX)
  125. if (hoursIssue) issues.hours = hoursIssue
  126. const rateIssue = bounded(rate, UNIT_PRICE_MAX)
  127. if (rateIssue) issues.rate = rateIssue
  128. if (hours * rate > LINE_TOTAL_MAX) issues.total = { issue: 'tooLarge', limit: LINE_TOTAL_MAX }
  129. return issues
  130. }
  131. /** A person's or a company's name, a make, a model. */
  132. export const NAME_MAX_LENGTH = 200
  133. /** A postal address, street to country. */
  134. export const ADDRESS_MAX_LENGTH = 500
  135. /** Free notes. */
  136. export const NOTES_MAX_LENGTH = 10_000
  137. /** An email address, as long as the standard allows. */
  138. export const EMAIL_MAX_LENGTH = 254
  139. /** A phone number, a tax id, a plate, a code. */
  140. export const SHORT_TEXT_MAX_LENGTH = 50