All files / src/features/usage-charges usage-charges.server.ts

86.07% Statements 68/79
72.72% Branches 48/66
84.61% Functions 11/13
88% Lines 66/75

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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268                                                                      1x 1x 1x 1x 4x                                     30x 30x     30x       26x 26x               1x 1x 1x                       2x         2x 1x   1x       1x                     1x 24x                               19x 19x 1x   18x       18x 18x       1x   17x 17x     17x       17x 2x   15x                 15x     15x 15x               9x 9x                   8x 8x 1x   7x       7x 7x 1x   6x     6x       6x 1x   5x 5x                 5x     5x       5x                       3x 3x 1x   2x 2x     2x 2x     2x 2x             2x     2x       2x    
// A flat's usage charges: the logic behind the server functions in
// functions.ts, with its dependencies passed in so Workers tests can drive it
// (openspec: usage-charges).
import type { OwnershipRepository } from '#/features/ownership/ownership.repository.server'
import type { Property } from '#/features/properties/property'
import type { PropertyRepository } from '#/features/properties/property.repository.server'
import { validateAmountDate } from '#/features/recurring-amounts/periods'
import { MAX_AMOUNT } from '#/features/recurring-amounts/schemas'
import type { SharingRepository } from '#/features/sharing/sharing.repository.server'
import type { TenancyRepository } from '#/features/tenancies/tenancy.repository.server'
import { MAX_CHARGE_DESCRIPTION } from '#/lib/charge-fields'
import { isoDateInFinland, type IsoDate } from '#/lib/dates'
import type { AlertReason, FieldReason } from '#/lib/reasons'
import type { ChargeBasis, ChargeKind, PropertyRole } from '#/server/db/schema'
import type { Logger } from '#/server/log/log'
import type { OccupiedTenancy, UsageChargePeriods } from './months'
import type { UsageChargeRepository } from './usage-charge.repository.server'
 
export interface UsageChargeDeps {
  /** The signed-in user. */
  userId: string
  charges: UsageChargeRepository
  tenancies: TenancyRepository
  properties: PropertyRepository
  ownership: OwnershipRepository
  sharing: SharingRepository
  log: Logger
  now: () => Date
}
 
interface Failure<R extends FieldReason | AlertReason = AlertReason> {
  ok: false
  reason: R
}
 
const notFound = { ok: false, reason: 'property_not_found' } as const
const ownerOnly = { ok: false, reason: 'sharing_owner_only' } as const
const chargeNotFound = { ok: false, reason: 'usage_charge_not_found' } as const
const changedMeanwhile = { ok: false, reason: 'amount_changed' } as const
const fail = <R extends FieldReason | AlertReason>(reason: R): Failure<R> => ({
  ok: false,
  reason,
})
 
/** What the usage charge pages compute from. */
export interface UsageChargesView {
  role: PropertyRole
  property: Property
  /** Today in Finland. */
  today: IsoDate
  /** The start of the flat's first ownership period. */
  ownershipStart: IsoDate
  charges: UsageChargePeriods[]
  /** The flat's tenancies with their occupants, for per-occupant amounts. */
  tenancies: OccupiedTenancy[]
}
 
async function roleCheck(deps: UsageChargeDeps, propertyId: string) {
  const role = await deps.sharing.roleOf(deps.userId, propertyId)
  Iif (!role) {
    return notFound
  }
  return role === 'owner' ? undefined : ownerOnly
}
 
async function ownershipStart(deps: UsageChargeDeps, propertyId: string) {
  const { periods } = await deps.ownership.history(propertyId)
  return periods.at(-1)?.validFrom
}
 
/** The flat's tenancies with their occupants. */
export async function occupiedTenancies(
  tenancies: TenancyRepository,
  propertyId: string,
): Promise<OccupiedTenancy[]> {
  const list = await tenancies.forProperty(propertyId)
  const occupants = await tenancies.occupantsFor(list.map((t) => t.id))
  return list.map((t) => ({
    validFrom: t.validFrom,
    validTo: t.validTo,
    occupants: occupants[t.id] ?? [],
  }))
}
 
/** The flat's usage charges and occupants, for everyone with access. */
export async function getUsageCharges(
  deps: UsageChargeDeps,
  propertyId: string,
): Promise<({ ok: true } & UsageChargesView) | typeof notFound> {
  const [role, property, start] = await Promise.all([
    deps.sharing.roleOf(deps.userId, propertyId),
    deps.properties.findForUser(deps.userId, propertyId),
    ownershipStart(deps, propertyId),
  ])
  if (!role || !property || start === undefined) {
    return notFound
  }
  const [charges, tenancies] = await Promise.all([
    deps.charges.forProperty(propertyId),
    occupiedTenancies(deps.tenancies, propertyId),
  ])
  return {
    ok: true,
    role,
    property,
    today: isoDateInFinland(deps.now()),
    ownershipStart: start,
    charges,
    tenancies,
  }
}
 
const validAmount = (amount: number) =>
  Number.isInteger(amount) && amount >= 0 && amount <= MAX_AMOUNT
 
/** Adds a usage charge with its first amount (openspec: "Adding a usage charge"). */
export async function addUsageCharge(
  deps: UsageChargeDeps,
  propertyId: string,
  charge: {
    kind: ChargeKind
    basis: ChargeBasis
    description: string | null
    amount: number
    from: IsoDate
  },
): Promise<
  { ok: true; chargeId: string } | Failure<FieldReason | AlertReason>
> {
  const refused = await roleCheck(deps, propertyId)
  if (refused) {
    return refused
  }
  Iif (!validAmount(charge.amount)) {
    return fail('amount_invalid')
  }
  const description =
    charge.kind === 'other' ? (charge.description?.trim() ?? '') : null
  if (
    description !== null &&
    (description.length === 0 || description.length > MAX_CHARGE_DESCRIPTION)
  ) {
    return fail('description_required')
  }
  const start = await ownershipStart(deps, propertyId)
  Iif (start === undefined) {
    return notFound
  }
  const problem = validateAmountDate(charge.from, {
    ownershipStart: start,
    latestStart: undefined,
  })
  if (problem) {
    return fail(problem)
  }
  const chargeId = await deps.charges.add({
    actorId: deps.userId,
    propertyId,
    kind: charge.kind,
    basis: charge.basis,
    description,
    amount: charge.amount,
    from: charge.from,
  })
  Iif (chargeId === null) {
    return (await roleCheck(deps, propertyId)) ?? notFound
  }
  deps.log.info('usage_charge.added', { propertyId, usageChargeId: chargeId })
  return { ok: true, chargeId }
}
 
async function loadCharge(
  deps: UsageChargeDeps,
  propertyId: string,
  chargeId: string,
) {
  const charges = await deps.charges.forProperty(propertyId)
  return charges.find((c) => c.id === chargeId)
}
 
/** A new amount from a date (openspec: "Changing a usage charge"). */
export async function setUsageCharge(
  deps: UsageChargeDeps,
  propertyId: string,
  chargeId: string,
  change: { amount: number; from: IsoDate },
): Promise<{ ok: true } | Failure<FieldReason | AlertReason>> {
  const refused = await roleCheck(deps, propertyId)
  if (refused) {
    return refused
  }
  const [charge, start] = await Promise.all([
    loadCharge(deps, propertyId, chargeId),
    ownershipStart(deps, propertyId),
  ])
  const latestStart = charge?.periods.at(-1)?.validFrom
  if (!charge || latestStart === undefined || start === undefined) {
    return chargeNotFound
  }
  Iif (!validAmount(change.amount)) {
    return fail('amount_invalid')
  }
  const problem = validateAmountDate(change.from, {
    ownershipStart: start,
    latestStart,
  })
  if (problem) {
    return fail(problem)
  }
  const corrects = change.from === latestStart
  const set = await deps.charges.setAmount({
    actorId: deps.userId,
    propertyId,
    chargeId,
    amount: change.amount,
    from: change.from,
    latestStart,
    corrects,
  })
  Iif (!set) {
    return (await roleCheck(deps, propertyId)) ?? changedMeanwhile
  }
  deps.log.info(
    corrects ? 'usage_charge.amount_corrected' : 'usage_charge.amount_set',
    { propertyId, usageChargeId: chargeId },
  )
  return { ok: true }
}
 
/**
 * Undoes the latest change (openspec: "Undoing the latest usage charge
 * change"); the only amount undone removes the charge.
 */
export async function undoUsageCharge(
  deps: UsageChargeDeps,
  propertyId: string,
  chargeId: string,
): Promise<{ ok: true } | Failure> {
  const refused = await roleCheck(deps, propertyId)
  if (refused) {
    return refused
  }
  const charge = await loadCharge(deps, propertyId, chargeId)
  Iif (!charge) {
    return chargeNotFound
  }
  const latest = charge.periods.at(-1)
  Iif (!latest) {
    return fail('amount_nothing_to_undo')
  }
  const previous = charge.periods.at(-2)
  const undone = await deps.charges.undoLatest({
    actorId: deps.userId,
    propertyId,
    chargeId,
    latestStart: latest.validFrom,
    previousStart: previous?.validFrom ?? null,
  })
  Iif (!undone) {
    return (await roleCheck(deps, propertyId)) ?? changedMeanwhile
  }
  deps.log.info(
    previous ? 'usage_charge.change_undone' : 'usage_charge.removed',
    { propertyId, usageChargeId: chargeId },
  )
  return { ok: true }
}