All files / src/features/ownership schemas.ts

100% Statements 16/16
100% Branches 4/4
100% Functions 2/2
100% Lines 16/16

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 43 44 45 46 47 48 49 50 51 52                26x     26x     722x 722x 567x 567x   155x       26x     12x 12x 2x 2x   10x     26x   26x                     26x        
// Input for ownership changes, shared by the forms and the server functions.
// Errors are reason codes (src/lib/reasons.ts), never text.
import { z } from 'zod'
import { parseFiDate } from '#/lib/dates'
import type { FieldReason } from '#/lib/reasons'
import { OWNERSHIP_REASONS } from '#/server/db/schema'
import { parsePercent } from './periods'
 
const id = z.string().min(1).max(64)
 
/** A date as typed: "12.6.2022" or "2022-06-12". */
export const typedDate = z
  .string({ error: 'date_invalid' satisfies FieldReason })
  .transform((text, context) => {
    const date = parseFiDate(text)
    if (date === undefined) {
      context.addIssue({ code: 'custom', message: 'date_invalid' })
      return z.NEVER
    }
    return date
  })
 
/** A share as typed in percent: "50", "33,33". */
export const typedPercent = z
  .string({ error: 'ownership_share_invalid' satisfies FieldReason })
  .transform((text, context) => {
    const share = parsePercent(text)
    if (share === undefined) {
      context.addIssue({ code: 'custom', message: 'ownership_share_invalid' })
      return z.NEVER
    }
    return share
  })
 
export const propertyRefInput = z.object({ propertyId: id })
 
export const changeOwnershipInput = z.object({
  propertyId: id,
  from: typedDate,
  reason: z.enum(OWNERSHIP_REASONS),
  shares: z.array(z.object({ userId: id, percent: typedPercent })).max(100),
  /** People going to 0 % whose access goes too. */
  removeAccess: z.array(id).max(100),
})
 
export type ChangeOwnershipInput = z.input<typeof changeOwnershipInput>
 
export const correctFirstDateInput = z.object({
  propertyId: id,
  date: typedDate,
})