All files / dal ho-mood-board-members.dal.ts

100% Statements 16/16
100% Branches 4/4
100% Functions 10/10
100% Lines 15/15

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                      19x     1x 1x                                     3x 3x                       3x                   2x                         2x       1x                       1x                       1x             2x                   2x       1x                       1x                      
import { and, eq, ne } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import type * as schema from "../db/schema";
import {
	hoMoodBoardMembers,
	hoUsers,
	type HoMoodBoardInvitedVia,
	type HoMoodBoardRole,
} from "../db/schema";
 
export class HoMoodBoardMembersDal {
	constructor(private db: DrizzleD1Database<typeof schema>) {}
 
	async createOwner(input: { boardId: string; userId: string }) {
		const now = new Date();
		return this.db
			.insert(hoMoodBoardMembers)
			.values({
				boardId: input.boardId,
				userId: input.userId,
				role: "owner",
				invitedVia: "creator",
				acceptedAt: now,
			})
			.onConflictDoNothing()
			.returning()
			.get();
	}
 
	async createCoEditor(input: {
		boardId: string;
		userId: string;
		invitedVia: HoMoodBoardInvitedVia;
	}) {
		const now = new Date();
		const row = await this.db
			.insert(hoMoodBoardMembers)
			.values({
				boardId: input.boardId,
				userId: input.userId,
				role: "co_editor",
				invitedVia: input.invitedVia,
				acceptedAt: now,
			})
			.onConflictDoNothing()
			.returning()
			.get();
		return row ?? null;
	}
 
	async findRole(
		boardId: string,
		userId: string,
	): Promise<{
		role: HoMoodBoardRole;
		invitedVia: HoMoodBoardInvitedVia;
	} | null> {
		const row = await this.db
			.select({
				role: hoMoodBoardMembers.role,
				invitedVia: hoMoodBoardMembers.invitedVia,
			})
			.from(hoMoodBoardMembers)
			.where(
				and(
					eq(hoMoodBoardMembers.boardId, boardId),
					eq(hoMoodBoardMembers.userId, userId),
				),
			)
			.get();
		return row ?? null;
	}
 
	async listByBoardId(boardId: string) {
		return this.db
			.select()
			.from(hoMoodBoardMembers)
			.where(eq(hoMoodBoardMembers.boardId, boardId))
			.orderBy(hoMoodBoardMembers.createdAt)
			.all();
	}
 
	// Used by the CollaboratorStrip UI to render who's on a board.
	// Joins ho_users so the route layer can produce a PII-reduced
	// first-name projection without needing a second round-trip.
	async listMembersWithNames(boardId: string) {
		const rows = await this.db
			.select({
				role: hoMoodBoardMembers.role,
				userName: hoUsers.name,
				acceptedAt: hoMoodBoardMembers.acceptedAt,
				createdAt: hoMoodBoardMembers.createdAt,
			})
			.from(hoMoodBoardMembers)
			.innerJoin(hoUsers, eq(hoUsers.id, hoMoodBoardMembers.userId))
			.where(eq(hoMoodBoardMembers.boardId, boardId))
			.orderBy(hoMoodBoardMembers.createdAt)
			.all();
		return rows;
	}
 
	async listBoardIdsByUser(
		userId: string,
		role: HoMoodBoardRole,
	): Promise<string[]> {
		const rows = await this.db
			.select({ boardId: hoMoodBoardMembers.boardId })
			.from(hoMoodBoardMembers)
			.where(
				and(
					eq(hoMoodBoardMembers.userId, userId),
					eq(hoMoodBoardMembers.role, role),
				),
			)
			.all();
		return rows.map((r) => r.boardId);
	}
 
	async deleteNonOwners(boardId: string) {
		return this.db
			.delete(hoMoodBoardMembers)
			.where(
				and(
					eq(hoMoodBoardMembers.boardId, boardId),
					ne(hoMoodBoardMembers.role, "owner"),
				),
			)
			.run();
	}
 
	async deleteMember(boardId: string, userId: string) {
		return this.db
			.delete(hoMoodBoardMembers)
			.where(
				and(
					eq(hoMoodBoardMembers.boardId, boardId),
					eq(hoMoodBoardMembers.userId, userId),
				),
			)
			.run();
	}
}