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 | 19x 2x 2x 2x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x | // Data Access Layer for Lead Reminders
import { eq, and, sql, asc, lt, gte } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import * as schema from "../db/schema";
import type { LeadReminder, NewLeadReminder } from "../db/schema";
export class LeadRemindersDal {
constructor(private db: DrizzleD1Database<typeof schema>) {}
async findByLeadId(leadId: number): Promise<LeadReminder[]> {
return this.db
.select()
.from(schema.leadReminders)
.where(eq(schema.leadReminders.leadId, leadId))
.orderBy(asc(schema.leadReminders.dueAt));
}
async findById(id: number): Promise<LeadReminder | undefined> {
const result = await this.db
.select()
.from(schema.leadReminders)
.where(eq(schema.leadReminders.id, id))
.limit(1);
return result[0];
}
async create(data: NewLeadReminder): Promise<LeadReminder> {
const result = await this.db
.insert(schema.leadReminders)
.values(data)
.returning();
return result[0];
}
async update(
id: number,
data: Partial<Omit<LeadReminder, "id" | "dateCreated">>,
): Promise<LeadReminder | undefined> {
const result = await this.db
.update(schema.leadReminders)
.set(data)
.where(eq(schema.leadReminders.id, id))
.returning();
return result[0];
}
async countActiveByLeadId(leadId: number): Promise<number> {
const result = await this.db
.select({ count: sql<number>`count(*)` })
.from(schema.leadReminders)
.where(
and(
eq(schema.leadReminders.leadId, leadId),
sql`${schema.leadReminders.status} IN ('upcoming', 'overdue')`,
),
);
return result[0]?.count ?? 0;
}
async countOverdueByProId(proId: string): Promise<number> {
const result = await this.db
.select({ count: sql<number>`count(*)` })
.from(schema.leadReminders)
.innerJoin(schema.leads, eq(schema.leadReminders.leadId, schema.leads.id))
.where(
and(
eq(schema.leads.proId, proId),
eq(schema.leadReminders.status, "upcoming"),
lt(schema.leadReminders.dueAt, new Date()),
),
);
return result[0]?.count ?? 0;
}
async findDueReminders(): Promise<
{
reminder: LeadReminder;
leadId: number;
proId: string;
customerName: string;
}[]
> {
const rows = await this.db
.select({
reminder: schema.leadReminders,
leadId: schema.leads.id,
proId: schema.leads.proId,
customerName: schema.leads.customerName,
})
.from(schema.leadReminders)
.innerJoin(schema.leads, eq(schema.leadReminders.leadId, schema.leads.id))
.where(
and(
eq(schema.leadReminders.status, "upcoming"),
lt(schema.leadReminders.dueAt, new Date()),
),
)
.orderBy(asc(schema.leadReminders.dueAt))
.limit(100);
return rows;
}
async countUpcomingByProId(proId: string): Promise<number> {
const result = await this.db
.select({ count: sql<number>`count(*)` })
.from(schema.leadReminders)
.innerJoin(schema.leads, eq(schema.leadReminders.leadId, schema.leads.id))
.where(
and(
eq(schema.leads.proId, proId),
eq(schema.leadReminders.status, "upcoming"),
gte(schema.leadReminders.dueAt, new Date()),
),
);
return result[0]?.count ?? 0;
}
}
|