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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | 8x 80x 8x 8x 8x 8x 29x 8x 8x 8x 17x 16x 1x 1x 1x 8x 26x 26x 3x 3x 23x 8x 8x 38x 17x 21x 21x 6x 6x 15x 8x 3x 2x 1x 1x 1x 1x 8x 8x 8x 18x 18x 7x 7x 11x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x | // Input for tenancies, shared by the forms and the server functions. Date
// rules that need the flat's other tenancies or today are in rules.ts and
// notice.ts. Errors are reason codes (src/lib/reasons.ts), never text.
import { z } from 'zod'
import { typedDate } from '#/features/ownership/schemas'
import { typedAmount } from '#/features/recurring-amounts/schemas'
import { chargeFields } from '#/lib/charge-fields'
import { parseDecimal } from '#/lib/decimal'
import type { FieldReason } from '#/lib/reasons'
import { CONTRACT_TYPES, NOTICE_GIVERS } from '#/server/db/schema'
const id = z.string().min(1).max(64)
const reason = (code: FieldReason) => ({ error: code })
/** The longest tenant name and phone number, in characters. */
export const MAX_TENANT_NAME = 100
export const MAX_TENANT_PHONE = 30
/** Trimmed text; empty becomes null. */
const optional = (max: number, code: FieldReason) =>
z
.string(reason(code))
.trim()
.max(max, reason(code))
.transform((text) => (text === '' ? null : text))
export const typedTenantName = z
.string(reason('tenant_name_required'))
.trim()
.min(1, reason('tenant_name_required'))
.max(MAX_TENANT_NAME, reason('tenant_name_too_long'))
export const typedTenantPhone = optional(
MAX_TENANT_PHONE,
'tenant_phone_too_long',
)
/** An optional email address: trimmed, empty becomes null. */
export const typedTenantEmail = z
.string(reason('email_invalid'))
.trim()
.max(254, reason('email_invalid'))
.transform((text, context) => {
if (text === '') {
return null
}
Eif (!z.email().safeParse(text).success) {
context.addIssue({ code: 'custom', message: 'email_invalid' })
return z.NEVER
}
return text
})
/** A due day as typed: a whole number from 1 to 31. */
export const typedDueDay = z
.string(reason('tenancy_due_day_invalid'))
.trim()
.transform((text, context) => {
const day = /^\d{1,2}$/.test(text) ? Number(text) : Number.NaN
if (!Number.isInteger(day) || day < 1 || day > 31) {
context.addIssue({ code: 'custom', message: 'tenancy_due_day_invalid' })
return z.NEVER
}
return day
})
/** The largest deposit, in cents. */
export const MAX_DEPOSIT = 10_000_000
/** An optional deposit as typed in euros; empty means none. */
export const typedDeposit = z
.string(reason('deposit_amount_invalid'))
.trim()
.transform((text, context) => {
if (text === '') {
return null
}
const cents = parseDecimal(text, 2)
if (cents === undefined || cents <= 0 || cents > MAX_DEPOSIT) {
context.addIssue({ code: 'custom', message: 'deposit_amount_invalid' })
return z.NEVER
}
return cents
})
/** A tenancy's details as typed; the term's end only for a fixed term. */
export const tenancyFields = z
.object({
tenantName: typedTenantName,
tenantPhone: typedTenantPhone,
tenantEmail: typedTenantEmail,
contractType: z.enum(CONTRACT_TYPES),
contractMade: typedDate,
validFrom: typedDate,
termEnd: z.string().trim(),
dueDay: typedDueDay,
deposit: typedDeposit.prefault(''),
})
.transform((fields, context) => {
if (fields.contractType === 'open_ended') {
return { ...fields, termEnd: null }
}
const termEnd = typedDate.safeParse(fields.termEnd)
Eif (!termEnd.success) {
context.addIssue({
code: 'custom',
path: ['termEnd'],
message: 'tenancy_end_before_start' satisfies FieldReason,
})
return z.NEVER
}
return { ...fields, termEnd: termEnd.data }
})
/** How a tenancy ended, or how notice was given: the end form's fields. */
const endingFields = z.object({
noticeGivenBy: z.enum(NOTICE_GIVERS),
noticeDate: typedDate,
lastDay: typedDate,
})
/** The number of persons living in the flat: a whole number from 1 to 20. */
export const MAX_OCCUPANTS = 20
export const typedOccupants = z
.string(reason('occupants_invalid'))
.trim()
.transform((text, context) => {
const persons = /^\d{1,3}$/.test(text) ? Number(text) : Number.NaN
if (!Number.isInteger(persons) || persons < 1 || persons > MAX_OCCUPANTS) {
context.addIssue({ code: 'custom', message: 'occupants_invalid' })
return z.NEVER
}
return persons
})
export const createTenancyInput = tenancyFields.and(
z.object({
propertyId: id,
/** The tenancy's first rent, from its start. */
rent: typedAmount,
/** The persons living in the flat from the start. */
occupants: typedOccupants,
/** An earlier tenancy's ending, recorded with it. */
ending: endingFields.optional(),
/** Fees of the previous tenancy to continue, by id. */
copyFeeIds: z.array(id).max(20).optional(),
}),
)
export const updateTenancyInput = tenancyFields.and(
z.object({ propertyId: id, tenancyId: id }),
)
export const endTenancyInput = endingFields.extend({
propertyId: id,
tenancyId: id,
})
export const tenancyRefInput = z.object({ propertyId: id, tenancyId: id })
/** A tenancy's new rent from a date (openspec: "Changing a tenancy's rent"). */
export const setTenancyRentInput = tenancyRefInput.extend({
amount: typedAmount,
from: typedDate,
})
/** A tenancy's new occupants from a date (openspec: "Changing the occupants"). */
export const setTenancyOccupantsInput = tenancyRefInput.extend({
persons: typedOccupants,
from: typedDate,
})
/** A new tenant fee with its first amount (openspec: "Adding a tenant fee"). */
export const addTenantFeeInput = tenancyRefInput
.and(chargeFields)
.and(z.object({ amount: typedAmount, from: typedDate }))
/** A tenant fee's new amount from a date ("Changing a tenant fee"). */
export const setTenantFeeInput = tenancyRefInput.extend({
feeId: id,
amount: typedAmount,
from: typedDate,
})
export const tenantFeeRefInput = tenancyRefInput.extend({ feeId: id })
export const propertyRefInput = z.object({ propertyId: id })
export type TenancyFieldsInput = z.input<typeof tenancyFields>
|