All files / dal notification-log.dal.ts

100% Statements 9/9
100% Branches 6/6
100% Functions 3/3
100% Lines 9/9

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                            19x     2x       2x             7x   7x 2x       7x 2x     7x                
import { eq, and, desc } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import * as schema from "../db/schema";
import type {
	NotificationLogEntry,
	NewNotificationLogEntry,
	NOTIFICATION_LOG_STATUSES,
	NOTIFICATION_EVENT_TYPES,
} from "../db/schema";
 
type LogStatus = (typeof NOTIFICATION_LOG_STATUSES)[number];
type EventType = (typeof NOTIFICATION_EVENT_TYPES)[number];
 
export class NotificationLogDal {
	constructor(private db: DrizzleD1Database<typeof schema>) {}
 
	async insert(data: NewNotificationLogEntry): Promise<NotificationLogEntry> {
		const result = await this.db
			.insert(schema.notificationLog)
			.values(data)
			.returning();
		return result[0];
	}
 
	async findByPro(
		proId: string,
		filters?: { eventType?: EventType; status?: LogStatus; limit?: number },
	): Promise<NotificationLogEntry[]> {
		const conditions = [eq(schema.notificationLog.proId, proId)];
 
		if (filters?.eventType) {
			conditions.push(
				eq(schema.notificationLog.eventType, filters.eventType),
			);
		}
		if (filters?.status) {
			conditions.push(eq(schema.notificationLog.status, filters.status));
		}
 
		return this.db
			.select()
			.from(schema.notificationLog)
			.where(and(...conditions))
			.orderBy(desc(schema.notificationLog.dateCreated))
			.limit(filters?.limit ?? 100);
	}
}