Files
cod-api/db/notification_error_log.db.js
2026-01-08 14:17:11 +07:00

94 lines
2.8 KiB
JavaScript

const pool = require("../config");
const getAllNotificationErrorLogDb = async () => {
const queryText = `
SELECT
a.*,
b.contact_name,
b.contact_type
FROM notification_error_log a
LEFT JOIN contact b ON a.contact_phone = b.contact_phone
WHERE a.deleted_at IS NULL
ORDER BY a.notification_error_log_id DESC
`;
const result = await pool.query(queryText);
return result.recordset;
};
const getNotificationErrorLogByIdDb = async (id) => {
const queryText = `
SELECT
a.*,
b.contact_name,
b.contact_type
FROM notification_error_log a
LEFT JOIN contact b ON a.contact_phone = b.contact_phone
WHERE a.notification_error_log_id = $1 AND a.deleted_at IS NULL
`;
const result = await pool.query(queryText, [id]);
return result.recordset[0];
};
const getNotificationErrorLogByNotificationErrorIdDb = async (notificationErrorId) => {
const queryText = `
SELECT
a.notification_error_log_description,
a.created_at,
b.contact_type,
c.user_fullname as created_by_name,
case when a.created_by is not null then c.user_fullname else b.contact_name end as contact_name,
case when a.created_by is not null then c.user_phone else a.contact_phone end as contact_phone
FROM notification_error_log a
LEFT JOIN contact b ON a.contact_phone = b.contact_phone
LEFT JOIN m_users c ON a.created_by = c.user_id
WHERE a.notification_error_id = $1 AND a.deleted_at IS NULL
ORDER BY a.created_at DESC
`;
const result = await pool.query(queryText, [notificationErrorId]);
return result.recordset;
};
const createNotificationErrorLogDb = async (store) => {
const queryText = `
INSERT INTO notification_error_log (
notification_error_id,
contact_phone,
notification_error_log_description,
created_by,
updated_by,
created_at,
updated_at
)
VALUES ($1, $2, $3, $4, $4, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
SELECT SCOPE_IDENTITY() as inserted_id;
`;
const values = [
store.notification_error_id,
store.contact_phone,
store.notification_error_log_description,
store.created_by
];
const result = await pool.query(queryText, values);
const insertedId = result.recordset[0]?.inserted_id;
return insertedId ? await getNotificationErrorLogByIdDb(insertedId) : null;
};
const deleteNotificationErrorLogDb = async (id, deletedBy) => {
const queryText = `
UPDATE notification_error_log
SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1
WHERE notification_error_log_id = $2 AND deleted_at IS NULL
`;
await pool.query(queryText, [deletedBy, id]);
return true;
};
module.exports = {
getAllNotificationErrorLogDb,
getNotificationErrorLogByIdDb,
getNotificationErrorLogByNotificationErrorIdDb,
createNotificationErrorLogDb,
deleteNotificationErrorLogDb,
};