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 | 23x 165x 3x 162x 2x 160x 160x 2x 158x 158x 23x 7x 151x 50x 50x 50x 7x | // Date rules for adding and correcting a tenancy (openspec: tenancies,
// "Tenancy dates and details"), checked on the server against today in
// Finland, the flat's first ownership start and its other tenancies. The
// write checks the overlap again. Pure.
import type { IsoDate } from '#/lib/dates'
import type { FieldReason } from '#/lib/reasons'
import type { ContractType } from '#/server/db/schema'
export type TenancyDateProblem = Extract<
FieldReason,
| 'tenancy_contract_made_in_future'
| 'tenancy_end_before_start'
| 'tenancy_start_before_ownership'
| 'tenancy_overlaps'
>
/** A period; `to` null runs indefinitely. */
interface Span {
from: IsoDate
to: IsoDate | null
}
/** Whether two periods share a day. */
export function overlaps(a: Span, b: Span): boolean {
return (a.to === null || b.from <= a.to) && (b.to === null || b.to >= a.from)
}
export function validateTenancyDates(
tenancy: {
contractType: ContractType
contractMade: IsoDate
validFrom: IsoDate
termEnd: IsoDate | null
/** The last day after this save: the term's end, or a recorded ending. */
validTo: IsoDate | null
},
limits: {
today: IsoDate
ownershipStart: IsoDate
/** The flat's other tenancies. */
others: readonly { validFrom: IsoDate; validTo: IsoDate | null }[]
},
): TenancyDateProblem | undefined {
if (tenancy.contractMade > limits.today) {
return 'tenancy_contract_made_in_future'
}
if (
tenancy.contractType === 'fixed_term' &&
(tenancy.termEnd === null || tenancy.termEnd <= tenancy.validFrom)
) {
return 'tenancy_end_before_start'
}
Iif (tenancy.validTo !== null && tenancy.validTo < tenancy.validFrom) {
return 'tenancy_end_before_start'
}
if (tenancy.validFrom < limits.ownershipStart) {
return 'tenancy_start_before_ownership'
}
const span = { from: tenancy.validFrom, to: tenancy.validTo }
if (
limits.others.some((other) =>
overlaps(span, { from: other.validFrom, to: other.validTo }),
)
) {
return 'tenancy_overlaps'
}
return undefined
}
/**
* Whether any of these payment months (`YYYY-MM`) falls outside the months
* of a tenancy running `from`–`to` (none: indefinitely); payments stay
* inside their tenancy (openspec: rent-payments).
*/
export function paymentsOutside(
months: readonly string[],
from: IsoDate,
to: IsoDate | null,
): boolean {
const first = from.slice(0, 7)
const last = to?.slice(0, 7)
return months.some(
(month) => month < first || (last !== undefined && month > last),
)
}
|