Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

874 B
Raw Blame
"use server";

import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { notifications } from "@cap/database/schema";
import { and, eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";

export const markAsRead = async (notificationId?: string) => {
	const currentUser = await getCurrentUser();
	if (!currentUser) {
		throw new Error("User not found");
	}

	try {
		const now = new Date();
		await db()
			.update(notifications)
			.set({ readAt: now })
			.where(
				and(
					eq(notifications.recipientId, currentUser.id),
					notificationId
						? eq(notifications.id, notificationId)
						: eq(notifications.orgId, currentUser.activeOrganizationId),
				),
			);
	} catch (error) {
		console.log(error);
		throw new Error("Error marking notification(s) as read");
	}

	revalidatePath("/dashboard");
};