Files
alrahma_sunday_school_api/apps/api/dist/modules/notifications/notification.repo.js
T
2026-06-11 03:22:12 -04:00

78 lines
3.8 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.findCompany = findCompany;
exports.countUnread = countUnread;
exports.markRead = markRead;
exports.markAllRead = markAllRead;
exports.findEmployeePreferences = findEmployeePreferences;
exports.upsertEmployeePreferences = upsertEmployeePreferences;
exports.findCompanyHistory = findCompanyHistory;
exports.findRenter = findRenter;
exports.markRenterRead = markRenterRead;
exports.markAllRenterRead = markAllRenterRead;
exports.findRenterPreferences = findRenterPreferences;
exports.upsertRenterPreferences = upsertRenterPreferences;
const prisma_1 = require("../../lib/prisma");
// ─── Company notifications ────────────────────────────────────
function findCompany(companyId, unread) {
const where = { companyId, channel: 'IN_APP' };
if (unread === 'true')
where.readAt = null;
return prisma_1.prisma.notification.findMany({ where, orderBy: { createdAt: 'desc' }, take: 50 });
}
function countUnread(companyId) {
return prisma_1.prisma.notification.count({ where: { companyId, channel: 'IN_APP', readAt: null } });
}
function markRead(id, companyId) {
return prisma_1.prisma.notification.updateMany({ where: { id, companyId }, data: { readAt: new Date(), status: 'READ' } });
}
function markAllRead(companyId) {
return prisma_1.prisma.notification.updateMany({ where: { companyId, readAt: null }, data: { readAt: new Date(), status: 'READ' } });
}
function findEmployeePreferences(employeeId) {
return prisma_1.prisma.notificationPreference.findMany({ where: { employeeId } });
}
async function upsertEmployeePreferences(employeeId, prefs) {
for (const pref of prefs) {
await prisma_1.prisma.notificationPreference.upsert({
where: { employeeId_notificationType_channel: { employeeId, notificationType: pref.notificationType, channel: pref.channel } },
create: { employeeId, notificationType: pref.notificationType, channel: pref.channel, enabled: pref.enabled },
update: { enabled: pref.enabled },
});
}
}
function findCompanyHistory(companyId, opts) {
const where = { companyId };
if (opts?.channel)
where.channel = opts.channel;
if (opts?.status)
where.status = opts.status;
return prisma_1.prisma.notification.findMany({
where,
orderBy: { createdAt: 'desc' },
take: opts?.limit ?? 200,
});
}
// ─── Renter notifications ─────────────────────────────────────
function findRenter(renterId) {
return prisma_1.prisma.notification.findMany({ where: { renterId, channel: 'IN_APP' }, orderBy: { createdAt: 'desc' }, take: 50 });
}
function markRenterRead(id, renterId) {
return prisma_1.prisma.notification.updateMany({ where: { id, renterId }, data: { readAt: new Date(), status: 'READ' } });
}
function markAllRenterRead(renterId) {
return prisma_1.prisma.notification.updateMany({ where: { renterId, channel: 'IN_APP', readAt: null }, data: { readAt: new Date(), status: 'READ' } });
}
function findRenterPreferences(renterId) {
return prisma_1.prisma.notificationPreference.findMany({ where: { renterId } });
}
async function upsertRenterPreferences(renterId, prefs) {
for (const pref of prefs) {
await prisma_1.prisma.notificationPreference.upsert({
where: { renterId_notificationType_channel: { renterId, notificationType: pref.notificationType, channel: pref.channel } },
create: { renterId, notificationType: pref.notificationType, channel: pref.channel, enabled: pref.enabled },
update: { enabled: pref.enabled },
});
}
}
//# sourceMappingURL=notification.repo.js.map