xerexjs
Version:
A simple notification system for NextJS/ReactJS with real-time capabilities
52 lines (51 loc) • 2.42 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAllNotifications = exports.addNotification = void 0;
const server_1 = require("./_generated/server");
const values_1 = require("convex/values");
exports.addNotification = (0, server_1.mutation)({
args: {
content: values_1.v.string(),
buttonText: values_1.v.string(),
buttonUrl: values_1.v.string(),
recipients: values_1.v.array(values_1.v.id("members")),
},
handler: (ctx, args) => __awaiter(void 0, void 0, void 0, function* () {
const notificationId = yield ctx.db.insert("notifications", {
content: args.content,
buttonText: args.buttonText,
buttonUrl: args.buttonUrl,
recipients: args.recipients
});
for (const userId of args.recipients) {
const user = yield ctx.db.get(userId);
const currentNotifications = (user === null || user === void 0 ? void 0 : user.notifications) || [];
yield ctx.db.patch(userId, {
notifications: [...currentNotifications, notificationId],
});
}
return notificationId;
}),
});
exports.getAllNotifications = (0, server_1.query)({
args: { userId: values_1.v.id("members") },
handler: (ctx, args) => __awaiter(void 0, void 0, void 0, function* () {
// Fetch all notifications
const notifications = yield ctx.db
.query("notifications")
.order("desc")
.collect();
// Filter in memory
const filteredNotifications = notifications.filter((notification) => notification.recipients.includes(args.userId));
return filteredNotifications;
}),
});