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 | 3x 3x 3x 3x 42x 42x 7x 7x 35x 3x 21x 3x 5x 3x 3x 3x 3x | // Input for entries, shared by the form and the server functions. The
// receipt file travels next to these fields in the same FormData and is
// checked by checkUpload() on the server. Errors are reason codes
// (src/lib/reasons.ts), never text.
import { z } from 'zod'
import { typedDate } from '#/features/ownership/schemas'
import { parseDecimal } from '#/lib/decimal'
import type { FieldReason } from '#/lib/reasons'
import { TRANSACTION_CATEGORIES, TRANSACTION_TYPES } from '#/server/db/schema'
import { categoryFits } from './categories'
const id = z.string().min(1).max(64)
/** The largest amount: 99 999,99 €. */
export const MAX_TRANSACTION_AMOUNT = 9_999_999
/** The longest note, in characters. */
export const MAX_NOTE_LENGTH = 200
/** An amount as typed in euros, from 0,01: in cents. */
export const typedTransactionAmount = z
.string({ error: 'transaction_amount_invalid' satisfies FieldReason })
.transform((text, context) => {
const cents = parseDecimal(text, 2)
if (cents === undefined || cents < 1 || cents > MAX_TRANSACTION_AMOUNT) {
context.addIssue({
code: 'custom',
message: 'transaction_amount_invalid' satisfies FieldReason,
})
return z.NEVER
}
return cents
})
/** An optional note: trimmed, empty becomes null. */
export const typedNote = z
.string()
.trim()
.max(MAX_NOTE_LENGTH, {
error: 'transaction_note_too_long' satisfies FieldReason,
})
.transform((note) => (note === '' ? null : note))
/** The fields of an entry, as typed. */
export const transactionFields = z
.object({
type: z.enum(TRANSACTION_TYPES, {
error: 'transaction_category_invalid' satisfies FieldReason,
}),
category: z.enum(TRANSACTION_CATEGORIES, {
error: 'transaction_category_invalid' satisfies FieldReason,
}),
amount: typedTransactionAmount,
date: typedDate,
note: typedNote,
})
.refine((fields) => categoryFits(fields.type, fields.category), {
path: ['category'],
error: 'transaction_category_invalid' satisfies FieldReason,
})
export const createTransactionInput = transactionFields.and(
z.object({ propertyId: id }),
)
/** What to do with the receipt when an entry is saved again. */
export const RECEIPT_ACTIONS = ['keep', 'remove', 'replace'] as const
export const updateTransactionInput = transactionFields.and(
z.object({
propertyId: id,
transactionId: id,
receiptAction: z.enum(RECEIPT_ACTIONS),
}),
)
export const transactionRefInput = z.object({
propertyId: id,
transactionId: id,
})
export type TransactionFieldsInput = z.input<typeof transactionFields>
|