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:
@@ -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);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user