All files / src/features/recurring-amounts functions.ts

0% Statements 0/20
0% Branches 0/2
0% Functions 0/9
0% Lines 0/20

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                                                                                                                                                                                                                           
// Server functions for rent, charges, financing charges and the treatment,
// called by the Rent and charges page and its forms. The logic is in
// recurring-amounts.server.ts; these validate the input with the shared
// schemas, check the session and wire in the request context.
import { createServerFn } from '@tanstack/react-start'
import { ownershipRepository } from '#/features/ownership/ownership.repository.server'
import { sharingRepository } from '#/features/sharing/sharing.repository.server'
import { getRequestContext } from '#/server/platform/context.server'
import { recurringAmountRepository } from './recurring-amount.repository.server'
import {
  addFinancingCharge as addCharge,
  getRecurringAmounts as get,
  renameFinancingCharge as renameCharge,
  setFinancingTreatment as setTreatment,
  setRecurringAmount as setAmount,
  undoFinancingTreatment as undoTreatment,
  undoRecurringAmount as undo,
  type RecurringAmountDeps,
} from './recurring-amounts.server'
import {
  addFinancingChargeInput,
  propertyRefInput,
  renameFinancingChargeInput,
  setAmountInput,
  setTreatmentInput,
  undoAmountInput,
} from './schemas'
 
/**
 * The signed-in user's dependencies. Pages are protected by the signed-in
 * layout, endpoints are not: without a session this throws.
 */
async function recurringAmountDeps(): Promise<RecurringAmountDeps> {
  const context = getRequestContext()
  const session = await context.auth.getSession()
  if (!session) {
    throw new Error('Not signed in')
  }
  return {
    userId: session.user.id,
    amounts: recurringAmountRepository(context.db),
    ownership: ownershipRepository(context.db),
    sharing: sharingRepository(context.db),
    log: context.log,
    now: () => new Date(),
  }
}
 
export const getRecurringAmounts = createServerFn({ method: 'GET' })
  .validator(propertyRefInput)
  .handler(async ({ data }) =>
    get(await recurringAmountDeps(), data.propertyId),
  )
 
export const setRecurringAmount = createServerFn({ method: 'POST' })
  .validator(setAmountInput)
  .handler(async ({ data }) =>
    setAmount(await recurringAmountDeps(), data.propertyId, {
      kind: data.kind,
      chargeId: data.chargeId,
      amount: data.amount,
      from: data.from,
    }),
  )
 
export const undoRecurringAmount = createServerFn({ method: 'POST' })
  .validator(undoAmountInput)
  .handler(async ({ data }) =>
    undo(
      await recurringAmountDeps(),
      data.propertyId,
      data.kind,
      data.chargeId,
    ),
  )
 
export const addFinancingCharge = createServerFn({ method: 'POST' })
  .validator(addFinancingChargeInput)
  .handler(async ({ data }) =>
    addCharge(await recurringAmountDeps(), data.propertyId, {
      name: data.name,
      amount: data.amount,
      from: data.from,
    }),
  )
 
export const renameFinancingCharge = createServerFn({ method: 'POST' })
  .validator(renameFinancingChargeInput)
  .handler(async ({ data }) =>
    renameCharge(await recurringAmountDeps(), data.propertyId, {
      chargeId: data.chargeId,
      name: data.name,
    }),
  )
 
export const setFinancingTreatment = createServerFn({ method: 'POST' })
  .validator(setTreatmentInput)
  .handler(async ({ data }) =>
    setTreatment(await recurringAmountDeps(), data.propertyId, {
      treatment: data.treatment,
      from: data.from,
    }),
  )
 
export const undoFinancingTreatment = createServerFn({ method: 'POST' })
  .validator(propertyRefInput)
  .handler(async ({ data }) =>
    undoTreatment(await recurringAmountDeps(), data.propertyId),
  )