All files / src/features/ownership functions.ts

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

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                                                                                                                                                       
// Server functions for flat ownership, called by the Ownership and sharing
// page. The logic is in ownership.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 { sharingRepository } from '#/features/sharing/sharing.repository.server'
import { getRequestContext } from '#/server/platform/context.server'
import { ownershipRepository } from './ownership.repository.server'
import {
  changeOwnership as change,
  correctFirstOwnershipDate as correct,
  getOwnership as get,
  undoOwnershipChange as undo,
  type OwnershipDeps,
} from './ownership.server'
import {
  changeOwnershipInput,
  correctFirstDateInput,
  propertyRefInput,
} 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 ownershipDeps(): Promise<OwnershipDeps> {
  const context = getRequestContext()
  const session = await context.auth.getSession()
  if (!session) {
    throw new Error('Not signed in')
  }
  return {
    userId: session.user.id,
    ownership: ownershipRepository(context.db),
    sharing: sharingRepository(context.db),
    log: context.log,
    now: () => new Date(),
  }
}
 
export const getOwnership = createServerFn({ method: 'GET' })
  .validator(propertyRefInput)
  .handler(async ({ data }) => get(await ownershipDeps(), data.propertyId))
 
export const changeOwnership = createServerFn({ method: 'POST' })
  .validator(changeOwnershipInput)
  .handler(async ({ data }) =>
    change(await ownershipDeps(), data.propertyId, {
      from: data.from,
      reason: data.reason,
      split: data.shares.map((s) => ({ userId: s.userId, share: s.percent })),
      removeAccess: data.removeAccess,
    }),
  )
 
export const undoOwnershipChange = createServerFn({ method: 'POST' })
  .validator(propertyRefInput)
  .handler(async ({ data }) => undo(await ownershipDeps(), data.propertyId))
 
export const correctFirstOwnershipDate = createServerFn({ method: 'POST' })
  .validator(correctFirstDateInput)
  .handler(async ({ data }) =>
    correct(await ownershipDeps(), data.propertyId, data.date),
  )
 
/** The current owners of each of the user's flats, for the flat cards. */
export const listCurrentOwners = createServerFn({ method: 'GET' }).handler(
  async () => {
    const deps = await ownershipDeps()
    const shares = await deps.ownership.currentForUser(deps.userId)
    return shares.map(({ userId, ...share }) => ({
      ...share,
      isYou: userId === deps.userId,
    }))
  },
)