All files / src/features/sharing functions.ts

0% Statements 0/24
0% Branches 0/2
0% Functions 0/11
0% Lines 0/24

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                                                                                                                                                                               
// Server functions for sharing flats, called by the Sharing page and the
// flats list. The logic is in sharing.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 { getRequest } from '@tanstack/react-start/server'
import { propertyRepository } from '#/features/properties/property.repository.server'
import { getLocale } from '#/paraglide/runtime.js'
import { getRequestContext } from '#/server/platform/context.server'
import { consumeLimit } from '#/server/platform/limits.server'
import {
  invitationIdInput,
  inviteInput,
  propertyRefInput,
  removeAccessInput,
} from './schemas'
import { sharingRepository } from './sharing.repository.server'
import {
  acceptInvitation as accept,
  cancelInvitation as cancel,
  declineInvitation as decline,
  getSharing as get,
  inviteToProperty as invite,
  leaveProperty as leave,
  listMyInvitations as listMine,
  removeAccess as remove,
  type SharingDeps,
} from './sharing.server'
 
/**
 * The signed-in user's dependencies. Pages are protected by the signed-in
 * layout, endpoints are not: without a session this throws.
 */
async function sharingDeps(): Promise<SharingDeps> {
  const context = getRequestContext()
  const session = await context.auth.getSession()
  if (!session) {
    throw new Error('Not signed in')
  }
  const { id, email, name } = session.user
  return {
    user: { id, email, name },
    sharing: sharingRepository(context.db),
    properties: propertyRepository(context.db),
    sendEmail: context.sendEmail,
    log: context.log,
    now: () => new Date(),
    consumeLimit: (key, rule) => consumeLimit(context.db, key, rule),
    locale: getLocale(),
    appUrl: new URL(getRequest().url).origin,
  }
}
 
export const getSharing = createServerFn({ method: 'GET' })
  .validator(propertyRefInput)
  .handler(async ({ data }) => get(await sharingDeps(), data.propertyId))
 
export const inviteToProperty = createServerFn({ method: 'POST' })
  .validator(inviteInput)
  .handler(async ({ data }) =>
    invite(await sharingDeps(), data.propertyId, data.email),
  )
 
export const cancelInvitation = createServerFn({ method: 'POST' })
  .validator(invitationIdInput)
  .handler(async ({ data }) => cancel(await sharingDeps(), data.invitationId))
 
export const listMyInvitations = createServerFn({ method: 'GET' }).handler(
  async () => listMine(await sharingDeps()),
)
 
export const acceptInvitation = createServerFn({ method: 'POST' })
  .validator(invitationIdInput)
  .handler(async ({ data }) => accept(await sharingDeps(), data.invitationId))
 
export const declineInvitation = createServerFn({ method: 'POST' })
  .validator(invitationIdInput)
  .handler(async ({ data }) => decline(await sharingDeps(), data.invitationId))
 
export const removeAccess = createServerFn({ method: 'POST' })
  .validator(removeAccessInput)
  .handler(async ({ data }) =>
    remove(await sharingDeps(), data.propertyId, data.userId),
  )
 
export const leaveProperty = createServerFn({ method: 'POST' })
  .validator(propertyRefInput)
  .handler(async ({ data }) => leave(await sharingDeps(), data.propertyId))