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 | 61x 1x 1x 2x 2x 56x 1x 1x 4x 3x 3x 3x 4x 4x 4x 3x 56x 2x 1x 4x 4x 56x 1x 1x 2x 2x 2x | // Data Access Layer — Communication History Log (append-only event timeline,
// per-inquiry message thread, annotations, consent records).
import { and, asc, eq, inArray, isNull } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import type {
CommunicationLog,
CommunicationLogAnnotation,
ConsentRecord,
InquiryMessage,
NewCommunicationLog,
NewCommunicationLogAnnotation,
NewConsentRecord,
NewInquiryMessage,
} from "../db/schema";
import * as schema from "../db/schema";
export class CommunicationLogsDal {
constructor(private db: DrizzleD1Database<typeof schema>) {}
// Append-only: only inserts and reads are exposed. No update/delete path.
async append(entry: NewCommunicationLog): Promise<CommunicationLog> {
const rows = await this.db
.insert(schema.communicationLogs)
.values(entry)
.returning();
return rows[0];
}
async listByInquiry(inquiryId: number): Promise<CommunicationLog[]> {
return this.db
.select()
.from(schema.communicationLogs)
.where(eq(schema.communicationLogs.inquiryId, inquiryId))
.orderBy(
asc(schema.communicationLogs.createdAt),
asc(schema.communicationLogs.id),
);
}
// Ops/legal export (spec: communication_history_log P0 — CSV export).
// Includes ALL entries regardless of visibility. Capped to bound D1 reads.
async listAll(limit = 10000): Promise<CommunicationLog[]> {
return this.db
.select()
.from(schema.communicationLogs)
.orderBy(
asc(schema.communicationLogs.createdAt),
asc(schema.communicationLogs.id),
)
.limit(limit);
}
}
export class CommunicationLogAnnotationsDal {
constructor(private db: DrizzleD1Database<typeof schema>) {}
async create(
data: NewCommunicationLogAnnotation,
): Promise<CommunicationLogAnnotation> {
const rows = await this.db
.insert(schema.communicationLogAnnotations)
.values(data)
.returning();
return rows[0];
}
async listByLogIds(logIds: number[]): Promise<CommunicationLogAnnotation[]> {
if (logIds.length === 0) return [];
// Scope the query to the requested log ids via the indexed column. The
// previous "select all, filter in memory" scanned the entire annotations
// table (never using communication_log_annotations_log_idx) and grew with
// total platform volume. Chunk to stay under D1's ~100-variable bind cap.
const CHUNK = 100;
const out: CommunicationLogAnnotation[] = [];
for (let i = 0; i < logIds.length; i += CHUNK) {
const chunk = logIds.slice(i, i + CHUNK);
const rows = await this.db
.select()
.from(schema.communicationLogAnnotations)
.where(inArray(schema.communicationLogAnnotations.logId, chunk))
.orderBy(asc(schema.communicationLogAnnotations.createdAt));
out.push(...rows);
}
return out;
}
}
export class ConsentRecordsDal {
constructor(private db: DrizzleD1Database<typeof schema>) {}
async record(data: NewConsentRecord): Promise<ConsentRecord> {
const rows = await this.db
.insert(schema.consentRecords)
.values(data)
.returning();
return rows[0];
}
async has(
userId: string,
userType: ConsentRecord["userType"],
consentType: ConsentRecord["consentType"] = "communication_logging",
): Promise<boolean> {
const rows = await this.db
.select({ id: schema.consentRecords.id })
.from(schema.consentRecords)
.where(
and(
eq(schema.consentRecords.userId, userId),
eq(schema.consentRecords.userType, userType),
eq(schema.consentRecords.consentType, consentType),
),
)
.limit(1);
return rows.length > 0;
}
}
export class InquiryMessagesDal {
constructor(private db: DrizzleD1Database<typeof schema>) {}
async create(data: NewInquiryMessage): Promise<InquiryMessage> {
const rows = await this.db
.insert(schema.inquiryMessages)
.values(data)
.returning();
return rows[0];
}
async listByInquiry(inquiryId: number): Promise<InquiryMessage[]> {
return this.db
.select()
.from(schema.inquiryMessages)
.where(eq(schema.inquiryMessages.inquiryId, inquiryId))
.orderBy(
asc(schema.inquiryMessages.createdAt),
asc(schema.inquiryMessages.id),
);
}
// Mark the other party's UNREAD messages as read when a viewer opens the
// thread. The `isNull(readAt)` guard is load-bearing: without it every thread
// open re-stamped read_at on all of the other party's messages (write-on-read
// amplification against D1's single writer — made frequent by the portal's
// 30s staleTime + refetch-on-focus) and clobbered the original first-read
// timestamp, which matters for the accountability log.
async markRead(
inquiryId: number,
readerType: InquiryMessage["senderType"],
): Promise<void> {
// Reader marks messages NOT sent by themselves as read.
const otherType = readerType === "homeowner" ? "pro" : "homeowner";
await this.db
.update(schema.inquiryMessages)
.set({ readAt: new Date() })
.where(
and(
eq(schema.inquiryMessages.inquiryId, inquiryId),
eq(schema.inquiryMessages.senderType, otherType),
isNull(schema.inquiryMessages.readAt),
),
);
}
}
|