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

100% Statements 30/30
100% Branches 12/12
100% Functions 14/14
100% Lines 30/30

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                                                                                                                            6x                     431x 59x                           431x 51x                             431x 51x   431x                         44x 44x 44x                                                           44x                           7x 7x                                 7x                 8x                 8x         27x         3x                       32x                                 9x                       16x       16x         20x                     20x         15x                     15x 1x   14x 14x                                      
// A flat's documents (openspec: documents). Writes carry the owner check, the
// storage quota and the lease's tenancy inside their statements, so a role
// lost or a tenancy removed between the read and the write changes nothing.
// The caller stores and deletes the files; this module returns the key it
// removed.
import { and, count, desc, eq, exists, sql } from 'drizzle-orm'
import type { IsoDate } from '#/lib/dates'
import {
  properties,
  propertyAccess,
  propertyDocuments,
  tenancies,
  user,
  type DocumentKind,
} from '#/server/db/schema'
import { fitsStorageQuota, storageUsage } from '#/server/db/storage-usage'
import { type Db } from '#/server/platform/db.server'
 
export interface DocumentFields {
  kind: DocumentKind
  name: string
  date: IsoDate
  /** Only a lease has a tenancy; for another kind it is dropped. */
  tenancyId: string | null
}
 
export interface DocumentFile {
  key: string
  contentType: string
  size: number
  filename: string
}
 
/** A document in a list. */
export interface DocumentRow {
  id: string
  kind: DocumentKind
  name: string
  date: IsoDate
  contentType: string
  size: number
}
 
export interface DocumentDetails extends DocumentRow {
  propertyId: string
  tenancyId: string | null
  /** The tenant of the lease's tenancy. */
  tenantName: string | null
  filename: string
  uploadedBy: { id: string; name: string }
  createdAt: Date
}
 
/** What the file route needs to decide and serve. */
export interface DocumentFileInfo {
  id: string
  propertyId: string
  key: string
  contentType: string
  filename: string
}
 
const rowColumns = {
  id: propertyDocuments.id,
  kind: propertyDocuments.kind,
  name: propertyDocuments.name,
  date: propertyDocuments.date,
  contentType: propertyDocuments.contentType,
  size: propertyDocuments.size,
}
 
export function documentRepository(db: Db) {
  /** The actor owns the flat: the check inside each write. */
  const isOwner = (actorId: string, propertyId: unknown) =>
    exists(
      db
        .select({ one: sql`1` })
        .from(propertyAccess)
        .where(
          and(
            sql`${propertyAccess.propertyId} = ${propertyId}`,
            eq(propertyAccess.userId, actorId),
            eq(propertyAccess.role, 'owner'),
          ),
        ),
    )
 
  /** The tenancy, if any, is one of the flat. */
  const tenancyOfFlat = (tenancyId: string | null, propertyId: unknown) =>
    tenancyId === null
      ? undefined
      : exists(
          db
            .select({ one: sql`1` })
            .from(tenancies)
            .where(
              and(
                eq(tenancies.id, tenancyId),
                sql`${tenancies.propertyId} = ${propertyId}`,
              ),
            ),
        )
 
  /** Only a lease keeps a tenancy. */
  const leaseTenancy = (fields: DocumentFields) =>
    fields.kind === 'lease' ? fields.tenancyId : null
 
  return {
    /**
     * Adds a document to a flat the actor owns. Null when they aren't an
     * owner, the tenancy isn't the flat's or the file doesn't fit their
     * storage quota; nothing is written then.
     */
    async add(
      actorId: string,
      propertyId: string,
      fields: DocumentFields,
      file: DocumentFile,
      now: Date,
    ): Promise<string | null> {
      const id = crypto.randomUUID()
      const tenancyId = leaseTenancy(fields)
      const inserted = await db
        .insert(propertyDocuments)
        .select(
          db
            .select({
              id: sql<string>`${id}`.as('id'),
              propertyId: properties.id,
              tenancyId: sql<string | null>`${tenancyId}`.as('tenancy_id'),
              kind: sql<DocumentKind>`${fields.kind}`.as('kind'),
              name: sql<string>`${fields.name}`.as('name'),
              date: sql<string>`${fields.date}`.as('date'),
              key: sql<string>`${file.key}`.as('key'),
              contentType: sql<string>`${file.contentType}`.as('content_type'),
              size: sql<number>`${file.size}`.as('size'),
              filename: sql<string>`${file.filename}`.as('filename'),
              uploadedBy: sql<string>`${actorId}`.as('uploaded_by'),
              createdAt: sql<number>`${now.getTime()}`.as('created_at'),
              updatedAt: sql<number>`${now.getTime()}`.as('updated_at'),
            })
            .from(properties)
            .where(
              and(
                eq(properties.id, propertyId),
                isOwner(actorId, properties.id),
                tenancyOfFlat(tenancyId, properties.id),
                fitsStorageQuota(db, actorId, file.size),
              ),
            ),
        )
        .returning({ id: propertyDocuments.id })
      return inserted.length > 0 ? id : null
    },
 
    /**
     * Changes a document's kind, name, date and tenancy, never its file.
     * Returns whether it applied: false when the document is gone, the actor
     * doesn't own its flat or the tenancy isn't the flat's.
     */
    async update(
      actorId: string,
      documentId: string,
      fields: DocumentFields,
      now: Date,
    ): Promise<boolean> {
      const tenancyId = leaseTenancy(fields)
      const updated = await db
        .update(propertyDocuments)
        .set({
          kind: fields.kind,
          name: fields.name,
          date: fields.date,
          tenancyId,
          updatedAt: now,
        })
        .where(
          and(
            eq(propertyDocuments.id, documentId),
            isOwner(actorId, propertyDocuments.propertyId),
            tenancyOfFlat(tenancyId, propertyDocuments.propertyId),
          ),
        )
        .returning({ id: propertyDocuments.id })
      return updated.length > 0
    },
 
    /**
     * Deletes a document of a flat the actor owns. Returns the key of its
     * file, which the caller deletes from storage, or null when nothing was
     * deleted.
     */
    async remove(actorId: string, documentId: string): Promise<string | null> {
      const removed = await db
        .delete(propertyDocuments)
        .where(
          and(
            eq(propertyDocuments.id, documentId),
            isOwner(actorId, propertyDocuments.propertyId),
          ),
        )
        .returning({ key: propertyDocuments.key })
      return removed.length > 0 ? removed[0].key : null
    },
 
    /** The bytes and number of files the user has uploaded, of any kind. */
    storageUsage(userId: string) {
      return storageUsage(db, userId)
    },
 
    /** The flat's documents, newest first. */
    listForProperty(propertyId: string): Promise<DocumentRow[]> {
      return db
        .select(rowColumns)
        .from(propertyDocuments)
        .where(eq(propertyDocuments.propertyId, propertyId))
        .orderBy(
          desc(propertyDocuments.date),
          desc(propertyDocuments.createdAt),
        )
    },
 
    /** The leases naming the tenancy, newest first. */
    leasesOfTenancy(tenancyId: string): Promise<DocumentRow[]> {
      return db
        .select(rowColumns)
        .from(propertyDocuments)
        .where(
          and(
            eq(propertyDocuments.tenancyId, tenancyId),
            eq(propertyDocuments.kind, 'lease'),
          ),
        )
        .orderBy(
          desc(propertyDocuments.date),
          desc(propertyDocuments.createdAt),
        )
    },
 
    /** The flat's latest documents by date, newest first. */
    latestForProperty(propertyId: string, limit: number) {
      return db
        .select(rowColumns)
        .from(propertyDocuments)
        .where(eq(propertyDocuments.propertyId, propertyId))
        .orderBy(
          desc(propertyDocuments.date),
          desc(propertyDocuments.createdAt),
        )
        .limit(limit)
    },
 
    async countForProperty(propertyId: string): Promise<number> {
      const [row] = await db
        .select({ count: count() })
        .from(propertyDocuments)
        .where(eq(propertyDocuments.propertyId, propertyId))
      return row.count
    },
 
    /** Where a document's file is, for the access check and serving. */
    async findForAccess(documentId: string): Promise<DocumentFileInfo | null> {
      const row = await db
        .select({
          id: propertyDocuments.id,
          propertyId: propertyDocuments.propertyId,
          key: propertyDocuments.key,
          contentType: propertyDocuments.contentType,
          filename: propertyDocuments.filename,
        })
        .from(propertyDocuments)
        .where(eq(propertyDocuments.id, documentId))
        .get()
      return row ?? null
    },
 
    /** A document with who added it and its lease's tenant, or null. */
    async findDetails(documentId: string): Promise<DocumentDetails | null> {
      const row = await db
        .select({
          document: propertyDocuments,
          uploaderName: user.name,
          tenantName: tenancies.tenantName,
        })
        .from(propertyDocuments)
        .innerJoin(user, eq(user.id, propertyDocuments.uploadedBy))
        .leftJoin(tenancies, eq(tenancies.id, propertyDocuments.tenancyId))
        .where(eq(propertyDocuments.id, documentId))
        .get()
      if (!row) {
        return null
      }
      const { document } = row
      return {
        id: document.id,
        propertyId: document.propertyId,
        kind: document.kind,
        name: document.name,
        date: document.date,
        contentType: document.contentType,
        size: document.size,
        tenancyId: document.tenancyId,
        tenantName: row.tenantName,
        filename: document.filename,
        uploadedBy: { id: document.uploadedBy, name: row.uploaderName },
        createdAt: document.createdAt,
      }
    },
  }
}
 
export type DocumentRepository = ReturnType<typeof documentRepository>