All files / dal notification-preferences.dal.ts

100% Statements 14/14
100% Branches 4/4
100% Functions 6/6
100% Lines 12/12

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                          23x             2x                                     2x                                   4x                         4x 1x         1x     3x                   3x               3x   3x               2x                                    
import { eq, and, sql } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import * as schema from "../db/schema";
import type {
	NotificationPreference,
	NOTIFICATION_EVENT_TYPES,
	NOTIFICATION_CHANNELS,
} from "../db/schema";
 
type EventType = (typeof NOTIFICATION_EVENT_TYPES)[number];
type Channel = (typeof NOTIFICATION_CHANNELS)[number];
 
export class NotificationPreferencesDal {
	constructor(private db: DrizzleD1Database<typeof schema>) {}
 
	async findByUserProEvent(
		userId: string,
		proId: string,
		eventType: EventType,
	): Promise<{ channel: string; enabled: boolean }[]> {
		return this.db
			.select({
				channel: schema.notificationPreferences.channel,
				enabled: schema.notificationPreferences.enabled,
			})
			.from(schema.notificationPreferences)
			.where(
				and(
					eq(schema.notificationPreferences.userId, userId),
					eq(schema.notificationPreferences.proId, proId),
					eq(schema.notificationPreferences.eventType, eventType),
				),
			);
	}
 
	async findByUserPro(
		userId: string,
		proId: string,
	): Promise<NotificationPreference[]> {
		return this.db
			.select()
			.from(schema.notificationPreferences)
			.where(
				and(
					eq(schema.notificationPreferences.userId, userId),
					eq(schema.notificationPreferences.proId, proId),
				),
			);
	}
 
	async upsert(
		userId: string,
		proId: string,
		eventType: EventType,
		channel: Channel,
		enabled: boolean,
	): Promise<NotificationPreference> {
		const existing = await this.db
			.select()
			.from(schema.notificationPreferences)
			.where(
				and(
					eq(schema.notificationPreferences.userId, userId),
					eq(schema.notificationPreferences.proId, proId),
					eq(schema.notificationPreferences.eventType, eventType),
					eq(schema.notificationPreferences.channel, channel),
				),
			)
			.limit(1);
 
		if (existing[0]) {
			const result = await this.db
				.update(schema.notificationPreferences)
				.set({ enabled, dateUpdated: new Date() })
				.where(eq(schema.notificationPreferences.id, existing[0].id))
				.returning();
			return result[0];
		}
 
		const result = await this.db
			.insert(schema.notificationPreferences)
			.values({
				userId,
				proId,
				eventType,
				channel,
				enabled,
			})
			.returning();
		return result[0];
	}
 
	async bulkUpsert(
		userId: string,
		proId: string,
		prefs: { eventType: EventType; channel: Channel; enabled: boolean }[],
	): Promise<NotificationPreference[]> {
		if (prefs.length === 0) return [];
 
		const values = prefs.map((pref) => ({
			userId,
			proId,
			eventType: pref.eventType,
			channel: pref.channel,
			enabled: pref.enabled,
		}));
 
		return this.db
			.insert(schema.notificationPreferences)
			.values(values)
			.onConflictDoUpdate({
				target: [
					schema.notificationPreferences.userId,
					schema.notificationPreferences.proId,
					schema.notificationPreferences.eventType,
					schema.notificationPreferences.channel,
				],
				set: {
					enabled: sql`excluded.enabled`,
					dateUpdated: new Date(),
				},
			})
			.returning();
	}
}