All files / src/lib charge-fields.ts

60% Statements 6/10
37.5% Branches 3/8
100% Functions 2/2
60% Lines 6/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42                  11x     11x             4x 4x                                 11x     128x  
// What a usage charge or a tenant fee is: its kind, basis and description
// (openspec: usage-charges, tenancies "Tenant fees are recorded as periods").
// Shared by both, because their fields and rules are the same. Errors are
// reason codes (src/lib/reasons.ts), never text.
import { z } from 'zod'
import type { FieldReason } from '#/lib/reasons'
import { CHARGE_BASES, CHARGE_KINDS } from '#/server/db/schema'
 
/** The longest description of an `other` charge, in characters. */
export const MAX_CHARGE_DESCRIPTION = 60
 
/** A fee or charge as typed: the description only for the kind `other`. */
export const chargeFields = z
  .object({
    kind: z.enum(CHARGE_KINDS),
    basis: z.enum(CHARGE_BASES),
    description: z.string().trim(),
  })
  .transform((fields, context) => {
    Eif (fields.kind !== 'other') {
      return { ...fields, description: null }
    }
    if (
      fields.description.length === 0 ||
      fields.description.length > MAX_CHARGE_DESCRIPTION
    ) {
      context.addIssue({
        code: 'custom',
        path: ['description'],
        message: 'description_required' satisfies FieldReason,
      })
      return z.NEVER
    }
    return fields
  })
 
/** The basis a kind is added with: water per occupant, the rest fixed. */
export const defaultBasis = (
  kind: (typeof CHARGE_KINDS)[number],
): (typeof CHARGE_BASES)[number] =>
  kind === 'water' ? 'per_occupant' : 'fixed'