All files / src/features/documents kinds.ts

83.33% Statements 15/18
100% Branches 3/3
80% Functions 12/15
82.35% Lines 14/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                5x 41x 63x 40x 46x 40x       5x   16x   18x           28x 28x             35x   90x 35x    
// The kinds of a flat's document, with their words (openspec: documents).
// The order is the documents page's group order.
import { m } from '#/paraglide/messages.js'
import { DOCUMENT_KINDS, type DocumentKind } from '#/server/db/schema'
 
export { DOCUMENT_KINDS }
 
/** A kind's name for one document, e.g. in the form and on details. */
export const kindLabel: Record<DocumentKind, () => string> = {
  lease: () => m.documents_kind_lease(),
  financial_statement: () => m.documents_kind_financial_statement(),
  charge_notice: () => m.documents_kind_charge_notice(),
  minutes: () => m.documents_kind_minutes(),
  other: () => m.documents_kind_other(),
}
 
/** A kind's name for a group of documents: the heading and the filter. */
export const kindHeading: Record<DocumentKind, () => string> = {
  lease: () => m.documents_heading_lease(),
  financial_statement: () => m.documents_heading_financial_statement(),
  charge_notice: () => m.documents_heading_charge_notice(),
  minutes: () => m.documents_heading_minutes(),
  other: () => m.documents_heading_other(),
}
 
/** Where a document's file is served; `download` sends it as an attachment. */
export function documentUrl(documentId: string, download = false) {
  const path = `/api/documents/${encodeURIComponent(documentId)}`
  return download ? `${path}?download=1` : path
}
 
/** The documents grouped by kind in the fixed order, keeping their order. */
export function groupByKind<T extends { kind: DocumentKind }>(
  documents: readonly T[],
): { kind: DocumentKind; documents: T[] }[] {
  return DOCUMENT_KINDS.map((kind) => ({
    kind,
    documents: documents.filter((document) => document.kind === kind),
  })).filter((group) => group.documents.length > 0)
}