All files / src/server/db periods.ts

100% Statements 17/17
100% Branches 6/6
100% Functions 2/2
100% Lines 17/17

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                                                                                            18x                     18x                     18x                                                                           18x                       373x 373x 373x 7x                                   366x                                                 366x                             366x 335x   31x                                                       8x 8x 8x         8x                                                          
// The statements that change a sequence of period rows (a value with
// `valid_from` / `valid_to`, the latest one open): occupants, usage charge
// amounts and tenant fee amounts. Each statement carries the caller's owner
// check and the expected latest start inside it, so a role lost or a period
// changed between the read and the write applies nothing; the server checks
// the same rules first to give the reason. The older builders for
// recurring_amounts and tenancy_rents stay in their repositories.
import {
  and,
  eq,
  exists,
  gte,
  isNull,
  lt,
  notExists,
  sql,
  type SQL,
} from 'drizzle-orm'
import type { BatchItem } from 'drizzle-orm/batch'
import type { SQLiteColumn, SQLiteTable } from 'drizzle-orm/sqlite-core'
import { dayBefore, type IsoDate } from '#/lib/dates'
import type { Db } from '#/server/platform/db.server'
import {
  tenancies,
  tenancyOccupants,
  tenantFeeAmounts,
  tenantFees,
  usageChargeAmounts,
  usageCharges,
} from './schema'
 
/** A period table and the parent row it belongs to. */
export interface PeriodTable {
  table: SQLiteTable
  /** The period rows' reference to the parent. */
  parentKey: SQLiteColumn
  validFrom: SQLiteColumn
  validTo: SQLiteColumn
  /** The period's value: persons or an amount in cents. */
  value: SQLiteColumn
  parent: SQLiteTable
  parentId: SQLiteColumn
  /** The property names of the table's columns, for the inserted row. */
  keys: { id: string; parent: string; value: string }
}
 
export const occupantsPeriods: PeriodTable = {
  table: tenancyOccupants,
  parentKey: tenancyOccupants.tenancyId,
  validFrom: tenancyOccupants.validFrom,
  validTo: tenancyOccupants.validTo,
  value: tenancyOccupants.persons,
  parent: tenancies,
  parentId: tenancies.id,
  keys: { id: 'id', parent: 'tenancyId', value: 'persons' },
}
 
export const usageChargePeriods: PeriodTable = {
  table: usageChargeAmounts,
  parentKey: usageChargeAmounts.usageChargeId,
  validFrom: usageChargeAmounts.validFrom,
  validTo: usageChargeAmounts.validTo,
  value: usageChargeAmounts.amount,
  parent: usageCharges,
  parentId: usageCharges.id,
  keys: { id: 'id', parent: 'usageChargeId', value: 'amount' },
}
 
export const tenantFeePeriods: PeriodTable = {
  table: tenantFeeAmounts,
  parentKey: tenantFeeAmounts.tenantFeeId,
  validFrom: tenantFeeAmounts.validFrom,
  validTo: tenantFeeAmounts.validTo,
  value: tenantFeeAmounts.amount,
  parent: tenantFees,
  parentId: tenantFees.id,
  keys: { id: 'id', parent: 'tenantFeeId', value: 'amount' },
}
 
/** What a write expects of the sequence it changes. */
export interface PeriodWrite {
  parentId: string
  value: number
  /** The new period's start. */
  from: IsoDate
  /** The latest period's start as read, or none for a first period. */
  latestStart: IsoDate | null
  /**
   * True when `from` is the latest period's start: the value of that
   * period is corrected, while it still starts there.
   */
  corrects?: boolean
  /** The caller's check that the actor may change the parent. */
  owned: SQL | undefined
  /** Another condition every statement carries. */
  onlyIf?: SQL
}
 
/** An undo's expectation: the latest and previous periods' starts. */
export interface PeriodUndo {
  parentId: string
  latestStart: IsoDate
  previousStart: IsoDate
  owned: SQL | undefined
}
 
const one = sql`1`
 
/**
 * Adds a period from `write.from`, closing the open one the day before, or
 * corrects the latest period's value. The last statement returns the row's
 * id exactly when the write applied.
 */
export function periodWriteStatements(
  db: Db,
  periods: PeriodTable,
  write: PeriodWrite,
): [BatchItem<'sqlite'>, ...BatchItem<'sqlite'>[]] {
  const { parentId, value, from, latestStart, owned, onlyIf } = write
  const scope = eq(periods.parentKey, parentId)
  if (write.corrects === true) {
    return [
      db
        .update(periods.table)
        .set({ [periods.keys.value]: value })
        .where(
          and(
            scope,
            eq(periods.validFrom, from),
            isNull(periods.validTo),
            owned,
            onlyIf,
          ),
        )
        .returning({ id: periods.parentKey }),
    ]
  }
  // The latest period is closed by this write (or there was none), and
  // nothing starts on or after `from`.
  const ready = and(
    eq(periods.parentId, parentId),
    owned,
    onlyIf,
    notExists(
      db
        .select({ one })
        .from(periods.table)
        .where(and(scope, gte(periods.validFrom, from))),
    ),
    latestStart === null
      ? notExists(db.select({ one }).from(periods.table).where(scope))
      : exists(
          db
            .select({ one })
            .from(periods.table)
            .where(
              and(
                scope,
                eq(periods.validFrom, latestStart),
                eq(periods.validTo, dayBefore(from)),
              ),
            ),
        ),
  )
  const insert = db
    .insert(periods.table)
    .select(
      db
        .select({
          [periods.keys.id]: sql<string>`${crypto.randomUUID()}`.as('id'),
          [periods.keys.parent]: periods.parentId,
          [periods.keys.value]: sql<number>`${value}`.as('value'),
          validFrom: sql<string>`${from}`.as('valid_from'),
          validTo: sql<null>`null`.as('valid_to'),
        })
        .from(periods.parent)
        .where(ready) as never,
    )
    .returning({ id: periods.parentKey })
  if (latestStart === null) {
    return [insert]
  }
  return [
    db
      .update(periods.table)
      .set({ validTo: dayBefore(from) })
      .where(
        and(
          scope,
          eq(periods.validFrom, latestStart),
          lt(periods.validFrom, from),
          isNull(periods.validTo),
          owned,
          onlyIf,
        ),
      ),
    insert,
  ]
}
 
/**
 * Deletes the latest period and reopens the previous one, while the latest
 * still starts on `undo.latestStart` and the previous one ends the day
 * before. The first statement returns a row exactly when the undo applied.
 */
export function periodUndoStatements(
  db: Db,
  periods: PeriodTable,
  undo: PeriodUndo,
): [BatchItem<'sqlite'>, BatchItem<'sqlite'>] {
  const { parentId, latestStart, previousStart, owned } = undo
  const scope = eq(periods.parentKey, parentId)
  const previous = and(
    scope,
    eq(periods.validFrom, previousStart),
    eq(periods.validTo, dayBefore(latestStart)),
  )
  return [
    db
      .delete(periods.table)
      .where(
        and(
          scope,
          eq(periods.validFrom, latestStart),
          isNull(periods.validTo),
          owned,
          exists(db.select({ one }).from(periods.table).where(previous)),
        ),
      )
      .returning({ id: periods.parentKey }),
    db
      .update(periods.table)
      .set({ validTo: null })
      .where(
        and(
          previous,
          notExists(
            db
              .select({ one })
              .from(periods.table)
              .where(and(scope, eq(periods.validFrom, latestStart))),
          ),
        ),
      ),
  ]
}