All files / src/features/properties functions.ts

0% Statements 0/30
0% Branches 0/4
0% Functions 0/12
0% Lines 0/30

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                                                                                                                                                                                                                                               
// Server functions for flats, called by the flats pages. The logic is in
// properties.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 { documentRepository } from '#/features/documents/document.repository.server'
import { ownershipRepository } from '#/features/ownership/ownership.repository.server'
import { recurringAmountRepository } from '#/features/recurring-amounts/recurring-amount.repository.server'
import { rentPaymentRepository } from '#/features/rent-payments/rent-payment.repository.server'
import { sharingRepository } from '#/features/sharing/sharing.repository.server'
import { tenancyRepository } from '#/features/tenancies/tenancy.repository.server'
import { transactionRepository } from '#/features/transactions/transaction.repository.server'
import { INTL_LOCALES } from '#/lib/format'
import { getLocale } from '#/paraglide/runtime.js'
import { usageChargeRepository } from '#/features/usage-charges/usage-charge.repository.server'
import { getRequestContext } from '#/server/platform/context.server'
import { getFlatDetails as details } from './details.server'
import { propertyRepository } from './property.repository.server'
import {
  archiveProperty as archive,
  createProperty as create,
  deleteProperty as remove,
  getProperty as get,
  getPropertyDeletion as deletion,
  restoreProperty as restore,
  listProperties as list,
  updateProperty as update,
  type PropertyDeps,
} from './properties.server'
import {
  newPropertyInput,
  propertyIdInput,
  updatePropertyInput,
} 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 propertyDeps(): Promise<PropertyDeps> {
  const context = getRequestContext()
  const session = await context.auth.getSession()
  if (!session) {
    throw new Error('Not signed in')
  }
  return {
    userId: session.user.id,
    properties: propertyRepository(context.db),
    storage: context.storage,
    log: context.log,
    now: () => new Date(),
    collation: INTL_LOCALES[getLocale()],
  }
}
 
export const listProperties = createServerFn({ method: 'GET' }).handler(
  async () => list(await propertyDeps()),
)
 
export const getProperty = createServerFn({ method: 'GET' })
  .validator(propertyIdInput)
  .handler(async ({ data }) => get(await propertyDeps(), data.id))
 
export const createProperty = createServerFn({ method: 'POST' })
  .validator(newPropertyInput)
  .handler(async ({ data: { ownedSince, ...details } }) =>
    create(await propertyDeps(), details, ownedSince),
  )
 
export const updateProperty = createServerFn({ method: 'POST' })
  .validator(updatePropertyInput)
  .handler(async ({ data: { id, ...details } }) =>
    update(await propertyDeps(), id, details),
  )
 
/** Archives the flat for the signed-in user only. */
export const archiveProperty = createServerFn({ method: 'POST' })
  .validator(propertyIdInput)
  .handler(async ({ data }) => archive(await propertyDeps(), data.id))
 
export const restoreProperty = createServerFn({ method: 'POST' })
  .validator(propertyIdInput)
  .handler(async ({ data }) => restore(await propertyDeps(), data.id))
 
/** What deleting the flat would remove, or why it can't be deleted. */
export const getPropertyDeletion = createServerFn({ method: 'GET' })
  .validator(propertyIdInput)
  .handler(async ({ data }) => deletion(await propertyDeps(), data.id))
 
export const deleteProperty = createServerFn({ method: 'POST' })
  .validator(propertyIdInput)
  .handler(async ({ data }) => remove(await propertyDeps(), data.id))
 
/** Everything the flat details page shows, in one read. */
export const getFlatDetails = createServerFn({ method: 'GET' })
  .validator(propertyIdInput)
  .handler(async ({ data }) => {
    const context = getRequestContext()
    const session = await context.auth.getSession()
    if (!session) {
      throw new Error('Not signed in')
    }
    const db = context.db
    return details(
      {
        userId: session.user.id,
        properties: propertyRepository(db),
        sharing: sharingRepository(db),
        ownership: ownershipRepository(db),
        tenancies: tenancyRepository(db),
        amounts: recurringAmountRepository(db),
        payments: rentPaymentRepository(db),
        transactions: transactionRepository(db),
        documents: documentRepository(db),
        usageCharges: usageChargeRepository(db),
        now: () => new Date(),
      },
      data.id,
    )
  })