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 | 11x 64x 2x 1x 2x 2x 3x 2x 3x 3x 3x 3x 3x 3x 3x 3x 2x | // Free consultation bookings — not attached to a pro (see db/schema/consultation-bookings.ts).
import { and, desc, eq, inArray, sql } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import type { ConsultationBooking, NewConsultationBooking } from "../db/schema";
import * as schema from "../db/schema";
// A booking is "active" (blocks a second request from the same phone) while it
// is still awaiting or in ops coordination.
const ACTIVE_STATUSES = ["pending", "contacted"] as const;
export type ConsultationBookingStatus = ConsultationBooking["status"];
export class ConsultationBookingsDal {
constructor(private db: DrizzleD1Database<typeof schema>) {}
async create(data: NewConsultationBooking): Promise<ConsultationBooking> {
const result = await this.db
.insert(schema.consultationBookings)
.values(data)
.returning();
return result[0];
}
// Count of active bookings for a phone — used to enforce the
// one-active-consultation-per-phone rule.
async countActiveByPhone(phone: string): Promise<number> {
const result = await this.db
.select({ count: sql<number>`count(*)` })
.from(schema.consultationBookings)
.where(
and(
eq(schema.consultationBookings.phone, phone),
inArray(schema.consultationBookings.status, [...ACTIVE_STATUSES]),
),
);
return Number(result[0]?.count ?? 0);
}
// Attach any anonymous bookings made with this phone (before the homeowner
// had an account) to the account now that it exists. Only claims rows still
// unlinked — never overwrites a booking already attached to a different
// homeowner. Returns the number of rows linked.
async linkToHomeowner(phone: string, homeownerId: string): Promise<number> {
const result = await this.db
.update(schema.consultationBookings)
.set({ homeownerId, dateUpdated: new Date() })
.where(
and(
eq(schema.consultationBookings.phone, phone),
sql`${schema.consultationBookings.homeownerId} IS NULL`,
),
)
.returning({ id: schema.consultationBookings.id });
return result.length;
}
// Paginated ops list, newest first, optionally filtered by status.
async list(params: {
limit: number;
offset: number;
status?: ConsultationBookingStatus;
}): Promise<{ items: ConsultationBooking[]; total: number }> {
const statusFilter = params.status
? eq(schema.consultationBookings.status, params.status)
: undefined;
const baseSelect = this.db.select().from(schema.consultationBookings);
const itemsQuery = statusFilter
? baseSelect
.where(statusFilter)
.orderBy(desc(schema.consultationBookings.dateCreated))
.limit(params.limit)
.offset(params.offset)
: baseSelect
.orderBy(desc(schema.consultationBookings.dateCreated))
.limit(params.limit)
.offset(params.offset);
const baseCount = this.db
.select({ count: sql<number>`count(*)` })
.from(schema.consultationBookings);
const countQuery = statusFilter ? baseCount.where(statusFilter) : baseCount;
const [items, countResult] = await Promise.all([itemsQuery, countQuery]);
return { items, total: Number(countResult[0]?.count ?? 0) };
}
// Ops status transition (pending -> contacted -> completed, or cancelled).
// Returns null when the id doesn't exist so the route can 404.
async updateStatus(
id: number,
status: ConsultationBookingStatus,
): Promise<ConsultationBooking | null> {
const result = await this.db
.update(schema.consultationBookings)
.set({ status, dateUpdated: new Date() })
.where(eq(schema.consultationBookings.id, id))
.returning();
return result[0] ?? null;
}
}
|