All files / src/features/documents documents.server.ts

94.68% Statements 89/94
87.5% Branches 56/64
100% Functions 16/16
94.56% Lines 87/92

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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378                                                                                          1x       1x       1x       1x                 48x 48x 2x   46x                 34x 30x   4x 4x                 4x 4x                             28x 28x   2x     2x 2x                 26x 26x 2x   24x 24x 24x                                                       3x       3x 1x   2x       2x                                 3x 3x 3x 4x   4x                                         3x       3x 2x   1x     1x 1x                                                   5x         5x     5x 2x   3x   3x 3x                                       31x   31x 3x   28x 28x 4x   24x 24x       24x 23x   23x   2x   2x   21x         21x                 5x 5x 1x   4x 4x 1x   3x 3x 1x   2x           2x         2x         2x               7x 7x 1x   6x 6x 1x   5x 5x     5x           5x 5x    
// A flat's documents: the logic behind the server functions in functions.ts,
// with its dependencies passed in so Workers tests can drive it (openspec:
// documents). A file is checked and stored before the database write and
// deleted again if that write doesn't apply, so a refused or failed save
// leaves no file behind.
import type { Property } from '#/features/properties/property'
import type { PropertyRepository } from '#/features/properties/property.repository.server'
import type { SharingRepository } from '#/features/sharing/sharing.repository.server'
import type { TenancyRepository } from '#/features/tenancies/tenancy.repository.server'
import { currentTenancy } from '#/features/tenancies/status'
import type { Upload } from '#/features/transactions/transactions.server'
import { isoDateInFinland, type IsoDate } from '#/lib/dates'
import type { AlertReason, FieldReason } from '#/lib/reasons'
import type { PropertyRole } from '#/server/db/schema'
import { deleteStoredFiles } from '#/server/files/delete'
import { fitsQuota, type StorageUsage } from '#/server/files/quota'
import { uploadFilename, withStoredFile } from '#/server/files/stored'
import { newFileKey, type FileStorage } from '#/server/files/storage'
import { checkUpload, UploadError } from '#/server/files/upload'
import type { Logger } from '#/server/log/log'
import type {
  DocumentDetails,
  DocumentFields,
  DocumentFile,
  DocumentRepository,
  DocumentRow,
} from './document.repository.server'
 
export interface DocumentDeps {
  /** The signed-in user. */
  userId: string
  documents: DocumentRepository
  properties: PropertyRepository
  sharing: SharingRepository
  tenancies: TenancyRepository
  storage: FileStorage
  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 satisfies Failure
const ownerOnly = {
  ok: false,
  reason: 'sharing_owner_only',
} as const satisfies Failure
const documentNotFound = {
  ok: false,
  reason: 'document_not_found',
} as const satisfies Failure
const tenancyNotFound = {
  ok: false,
  reason: 'tenancy_not_found',
} as const satisfies Failure
 
async function roleCheck(
  deps: DocumentDeps,
  propertyId: string,
): Promise<typeof notFound | typeof ownerOnly | undefined> {
  const role = await deps.sharing.roleOf(deps.userId, propertyId)
  if (!role) {
    return notFound
  }
  return role === 'owner' ? undefined : ownerOnly
}
 
/** The lease's tenancy must be one of the flat's. */
async function tenancyProblem(
  deps: DocumentDeps,
  propertyId: string,
  fields: DocumentFields,
): Promise<typeof tenancyNotFound | undefined> {
  if (fields.kind !== 'lease' || fields.tenancyId === null) {
    return undefined
  }
  const flatsTenancies = await deps.tenancies.forProperty(propertyId)
  return flatsTenancies.some((tenancy) => tenancy.id === fields.tenancyId)
    ? undefined
    : tenancyNotFound
}
 
function quotaFull(
  deps: DocumentDeps,
  propertyId: string,
): Failure<'storage_quota_full'> {
  deps.log.warn('document.quota_exceeded', { propertyId })
  return { ok: false, reason: 'storage_quota_full' }
}
 
/**
 * Checks and stores a document's file. Returns its fields, or why it was
 * refused; nothing is stored then.
 */
async function storeFile(
  deps: DocumentDeps,
  propertyId: string,
  upload: Upload,
): Promise<
  { ok: true; file: DocumentFile } | Failure<FieldReason | AlertReason>
> {
  let checked: ReturnType<typeof checkUpload>
  try {
    checked = checkUpload(upload.bytes)
  } catch (error) {
    Iif (!(error instanceof UploadError)) {
      throw error
    }
    deps.log.warn('document.refused', { propertyId, problem: error.problem })
    return {
      ok: false,
      reason:
        error.problem === 'too_large'
          ? 'document_too_large'
          : 'document_unsupported_type',
    }
  }
  // A first check, so a full quota stores no file; the write checks again.
  const usage = await deps.documents.storageUsage(deps.userId)
  if (!fitsQuota(usage, checked.size)) {
    return quotaFull(deps, propertyId)
  }
  const key = newFileKey('attachments')
  await deps.storage.put(key, upload.bytes, checked.contentType)
  return {
    ok: true,
    file: {
      key,
      contentType: checked.contentType,
      size: checked.size,
      filename: uploadFilename(
        upload.filename,
        checked.contentType,
        'document',
      ),
    },
  }
}
 
export interface DocumentsView {
  role: PropertyRole
  property: Property
  /** Newest first. */
  documents: DocumentRow[]
  /** Owners only: what their uploads take of the quota. */
  usage: StorageUsage | null
}
 
export async function getDocuments(
  deps: DocumentDeps,
  propertyId: string,
): Promise<({ ok: true } & DocumentsView) | typeof notFound> {
  const [role, property] = await Promise.all([
    deps.sharing.roleOf(deps.userId, propertyId),
    deps.properties.findForUser(deps.userId, propertyId),
  ])
  if (!role || !property) {
    return notFound
  }
  const [documents, usage] = await Promise.all([
    deps.documents.listForProperty(propertyId),
    role === 'owner' ? deps.documents.storageUsage(deps.userId) : null,
  ])
  return { ok: true, role, property, documents, usage }
}
 
/** A tenancy a lease can name. */
export interface TenancyChoice {
  id: string
  tenantName: string
  validFrom: IsoDate
  validTo: IsoDate | null
}
 
/** The flat's tenancies a lease can name: the current one first. */
async function tenancyChoices(
  deps: DocumentDeps,
  propertyId: string,
  today: IsoDate,
): Promise<TenancyChoice[]> {
  const all = await deps.tenancies.forProperty(propertyId)
  const current = currentTenancy(all, today)
  const ordered = current
    ? [current, ...all.filter((tenancy) => tenancy.id !== current.id)]
    : all
  return ordered.map((tenancy) => ({
    id: tenancy.id,
    tenantName: tenancy.tenantName,
    validFrom: tenancy.validFrom,
    validTo: tenancy.validTo,
  }))
}
 
export interface NewDocumentView {
  property: Property
  tenancies: TenancyChoice[]
  today: IsoDate
}
 
/** What the add form needs. Owners only. */
export async function getNewDocument(
  deps: DocumentDeps,
  propertyId: string,
): Promise<
  ({ ok: true } & NewDocumentView) | typeof notFound | typeof ownerOnly
> {
  const [refused, property] = await Promise.all([
    roleCheck(deps, propertyId),
    deps.properties.findForUser(deps.userId, propertyId),
  ])
  if (refused) {
    return refused
  }
  Iif (!property) {
    return notFound
  }
  const today = isoDateInFinland(deps.now())
  return {
    ok: true,
    property,
    tenancies: await tenancyChoices(deps, propertyId, today),
    today,
  }
}
 
export interface DocumentView {
  role: PropertyRole
  property: Property
  document: DocumentDetails
  /** Whether the signed-in user added it. */
  addedByYou: boolean
  /** Owners only: the current tenancy first, then the others newest first. */
  tenancies: TenancyChoice[]
  today: IsoDate
}
 
export async function getDocument(
  deps: DocumentDeps,
  propertyId: string,
  documentId: string,
): Promise<
  ({ ok: true } & DocumentView) | typeof notFound | typeof documentNotFound
> {
  const [role, property, document] = await Promise.all([
    deps.sharing.roleOf(deps.userId, propertyId),
    deps.properties.findForUser(deps.userId, propertyId),
    deps.documents.findDetails(documentId),
  ])
  Iif (!role || !property) {
    return notFound
  }
  if (document?.propertyId !== propertyId) {
    return documentNotFound
  }
  const today = isoDateInFinland(deps.now())
  const tenancies =
    role === 'owner' ? await tenancyChoices(deps, propertyId, today) : []
  return {
    ok: true,
    role,
    property,
    document,
    addedByYou: document.uploadedBy.id === deps.userId,
    tenancies,
    today,
  }
}
 
export async function addDocument(
  deps: DocumentDeps,
  propertyId: string,
  fields: DocumentFields,
  upload: Upload,
): Promise<
  { ok: true; documentId: string } | Failure<FieldReason | AlertReason>
> {
  const refused =
    (await roleCheck(deps, propertyId)) ??
    (await tenancyProblem(deps, propertyId, fields))
  if (refused) {
    return refused
  }
  const stored = await storeFile(deps, propertyId, upload)
  if (!stored.ok) {
    return stored
  }
  const { file } = stored
  const id = await withStoredFile(
    deps,
    'document.file_delete_failed',
    file.key,
    () => deps.documents.add(deps.userId, propertyId, fields, file, deps.now()),
    (added) => added !== null,
  )
  if (id === null) {
    const again =
      (await roleCheck(deps, propertyId)) ??
      (await tenancyProblem(deps, propertyId, fields))
    return again ?? quotaFull(deps, propertyId)
  }
  deps.log.info('document.added', {
    propertyId,
    documentId: id,
    kind: fields.kind,
  })
  return { ok: true, documentId: id }
}
 
export async function updateDocument(
  deps: DocumentDeps,
  propertyId: string,
  documentId: string,
  fields: DocumentFields,
): Promise<{ ok: true } | Failure<FieldReason | AlertReason>> {
  const refused = await roleCheck(deps, propertyId)
  if (refused) {
    return refused
  }
  const existing = await deps.documents.findForAccess(documentId)
  if (existing?.propertyId !== propertyId) {
    return documentNotFound
  }
  const problem = await tenancyProblem(deps, propertyId, fields)
  if (problem) {
    return problem
  }
  const applied = await deps.documents.update(
    deps.userId,
    documentId,
    fields,
    deps.now(),
  )
  Iif (!applied) {
    return (await deps.documents.findForAccess(documentId))
      ? tenancyNotFound
      : documentNotFound
  }
  deps.log.info('document.changed', {
    propertyId,
    documentId,
    kind: fields.kind,
  })
  return { ok: true }
}
 
export async function deleteDocument(
  deps: DocumentDeps,
  propertyId: string,
  documentId: string,
): Promise<{ ok: true } | Failure> {
  const refused = await roleCheck(deps, propertyId)
  if (refused) {
    return refused
  }
  const existing = await deps.documents.findForAccess(documentId)
  if (existing?.propertyId !== propertyId) {
    return documentNotFound
  }
  const key = await deps.documents.remove(deps.userId, documentId)
  Iif (key === null) {
    return documentNotFound
  }
  await deleteStoredFiles(
    deps.storage,
    deps.log,
    [key],
    'document.file_delete_failed',
  )
  deps.log.info('document.deleted', { propertyId, documentId })
  return { ok: true }
}