mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
add: more commits
Signed-off-by: Kenneth Obsequio <k80308392@gmail.com>
This commit is contained in:
@@ -12,7 +12,7 @@ const { restoreOne, restoreMany } = require("../../utils/courses/restore.util");
|
||||
const { getFieldValues } = require("../../utils/fieldValues.util");
|
||||
const logActivity = require('../../utils/logActivity.util');
|
||||
const UserNotification = require('../../models/notifications/user_notification.mdl');
|
||||
const { NOTIFICATION_REGISTRY } = require('../../data/notifications.data');
|
||||
const { renderNotification } = require('../../services/notificationTemplate.service');
|
||||
|
||||
// ── Models ────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -1475,11 +1475,11 @@ exports.updateAssessment = async (req, res) => {
|
||||
where: { course_id: courseId },
|
||||
attributes: ['title', 'uuid'],
|
||||
});
|
||||
const notify = NOTIFICATION_REGISTRY.assessment_updated.build({
|
||||
const notify = await renderNotification({ type: 'assessment_updated', data: {
|
||||
assessmentTitle: assessment.title,
|
||||
courseTitle: course?.title ?? null,
|
||||
courseUuid: course?.uuid ?? null,
|
||||
});
|
||||
} });
|
||||
const now = new Date();
|
||||
await UserNotification.bulkCreate(
|
||||
inProgressSessions.map(({ user_id }) => ({
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
'use strict';
|
||||
|
||||
const mdl_NotificationTemplate = require('../../models/notifications/notification_template.mdl');
|
||||
const R = require('../../utils/response.util');
|
||||
const logActivity = require('../../utils/logActivity.util');
|
||||
|
||||
// ─── GET /admin/notification-templates ─────────────────────────────────────────
|
||||
|
||||
exports.getNotificationTemplates = async (req, res) => {
|
||||
try {
|
||||
const templates = await mdl_NotificationTemplate.findAll({
|
||||
order: [['notify_type', 'ASC'], ['type', 'ASC']],
|
||||
});
|
||||
return R.success(res, 'Notification templates retrieved.', templates);
|
||||
} catch (err) {
|
||||
console.error('[ADMIN][GET NOTIFICATION TEMPLATES]', err);
|
||||
return R.error(res, 'Could not retrieve notification templates.', 500);
|
||||
}
|
||||
};
|
||||
|
||||
// ─── GET /admin/notification-templates/:id ─────────────────────────────────────
|
||||
|
||||
exports.getNotificationTemplate = async (req, res) => {
|
||||
try {
|
||||
const template = await mdl_NotificationTemplate.findByPk(req.params.id);
|
||||
if (!template) return R.error(res, 'Notification template not found.', 404);
|
||||
return R.success(res, 'Notification template retrieved.', template);
|
||||
} catch (err) {
|
||||
console.error('[ADMIN][GET NOTIFICATION TEMPLATE]', err);
|
||||
return R.error(res, 'Could not retrieve notification template.', 500);
|
||||
}
|
||||
};
|
||||
|
||||
// ─── PUT /admin/notification-templates/:id ─────────────────────────────────────
|
||||
// No create/delete endpoints — every row is is_system by definition (a new
|
||||
// type needs a code call site before it means anything), so there is nothing
|
||||
// valid to create or delete through this UI.
|
||||
|
||||
exports.updateNotificationTemplate = async (req, res) => {
|
||||
try {
|
||||
const template = await mdl_NotificationTemplate.findByPk(req.params.id);
|
||||
if (!template) return R.error(res, 'Notification template not found.', 404);
|
||||
|
||||
const { label, title, message, publish } = req.body;
|
||||
|
||||
if (title !== undefined && !title.trim()) return R.error(res, 'title cannot be empty.', 400);
|
||||
if (message !== undefined && !message.trim()) return R.error(res, 'message cannot be empty.', 400);
|
||||
|
||||
// "Publish" writes title/message straight to the live columns
|
||||
// renderNotification() reads and clears any pending draft. A plain save
|
||||
// (no publish flag) writes into draft_title/draft_message instead, so
|
||||
// real notifications keep using the last-published content until an
|
||||
// admin comes back and explicitly publishes again.
|
||||
const isPublishing = publish === true || publish === 'true';
|
||||
const nextTitle = title ?? template.draft_title ?? template.title;
|
||||
const nextMessage = message ?? template.draft_message ?? template.message;
|
||||
|
||||
await template.update({
|
||||
label: label ?? template.label,
|
||||
...(isPublishing
|
||||
? {
|
||||
status: 'sent',
|
||||
title: nextTitle,
|
||||
message: nextMessage,
|
||||
draft_title: null,
|
||||
draft_message: null,
|
||||
last_sent_at: new Date(),
|
||||
}
|
||||
: {
|
||||
draft_title: nextTitle,
|
||||
draft_message: nextMessage,
|
||||
}),
|
||||
});
|
||||
|
||||
logActivity(req.user?.user_id, 'update_notification_template', { entityType: 'notification_template', entityId: template.notification_template_id, details: { type: template.type, published: isPublishing } });
|
||||
|
||||
return R.success(res, 'Notification template updated.', template);
|
||||
} catch (err) {
|
||||
console.error('[ADMIN][UPDATE NOTIFICATION TEMPLATE]', err);
|
||||
return R.error(res, 'Could not update notification template.', 500);
|
||||
}
|
||||
};
|
||||
@@ -14,7 +14,7 @@ const { Task, TaskList, TaskRequirement, TaskListGroup, mdl_UserGroups } = requi
|
||||
const { mdl_UserGroupMembers } = require('../../models/users/user_groups.mdl');
|
||||
const mdl_Users = require('../../models/users/users.mdl');
|
||||
const UserNotification = require('../../models/notifications/user_notification.mdl');
|
||||
const { NOTIFICATION_REGISTRY } = require('../../data/notifications.data');
|
||||
const { renderNotification } = require('../../services/notificationTemplate.service');
|
||||
const { adminExclude, jsonbSchemas, computedAttributes } = require('../../models/task/task.attributes');
|
||||
|
||||
const R = require('../../utils/response.util');
|
||||
@@ -652,14 +652,18 @@ exports.updateTask = async (req, res) => {
|
||||
|
||||
if (members.length) {
|
||||
const now = new Date();
|
||||
// Title/message are identical for every member — render once,
|
||||
// then vary only the per-member groupId in the data payload.
|
||||
const notify = await renderNotification({ type: 'task_requirements_updated', data: {
|
||||
taskName: full.name,
|
||||
taskListId: task.task_list_id,
|
||||
groupId: null,
|
||||
} });
|
||||
await UserNotification.bulkCreate(
|
||||
members.map(({ user_id, group_id }) => ({
|
||||
user_id,
|
||||
...NOTIFICATION_REGISTRY.task_requirements_updated.build({
|
||||
taskName: full.name,
|
||||
taskListId: task.task_list_id,
|
||||
groupId: group_id,
|
||||
}),
|
||||
...notify,
|
||||
data: { ...notify.data, groupId: group_id },
|
||||
seen: false,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
|
||||
Reference in New Issue
Block a user