add: crud notif error user

This commit is contained in:
2025-12-09 16:30:19 +07:00
parent fb3061e0d1
commit 2b93baa648
6 changed files with 327 additions and 0 deletions

View File

@@ -0,0 +1,105 @@
const pool = require("../config");
// Get all Notification
const getAllNotificationErrorUserDb = async (searchParams = {}) => {
let queryParams = [];
if (searchParams.limit) {
const page = Number(searchParams.page ?? 1) - 1;
queryParams = [Number(searchParams.limit ?? 10), page];
}
const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike(
[
"a.notification_error_id",
"a.contact_id",
],
searchParams.criteria,
queryParams
);
if (whereParamOr) queryParams = whereParamOr;
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
[
{ column: "a.notification_error_id", param: searchParams.name, type: "int" },
{ column: "a.contact_id", param: searchParams.code, type: "int" },
],
queryParams
);
if (whereParamAnd) queryParams = whereParamAnd;
const queryText = `
SELECT
COUNT(*) OVER() AS total_data,
a.*
FROM notification_error_user a
WHERE a.deleted_at IS NULL
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""}
${whereOrConditions ? ` ${whereOrConditions}` : ""}
ORDER BY a.notification_error_user_id ASC
${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ''}
`;
const result = await pool.query(queryText, queryParams);
const total =
result?.recordset?.length > 0
? parseInt(result.recordset[0].total_data, 10)
: 0;
return { data: result.recordset, total };
};
const getNotificationErrorUserByIdDb = async (id) => {
const queryText = `
SELECT
a.*
FROM notification_error_user a
WHERE a.notification_error_user_id = $1 AND a.deleted_at IS NULL
`;
const result = await pool.query(queryText, [id]);
return result.recordset;
};
const createNotificationErrorUserDb = async (store) => {
const { query: queryText, values } = pool.buildDynamicInsert("notification_error_user", store);
const result = await pool.query(queryText, values);
const insertedId = result.recordset?.[0]?.inserted_id;
return insertedId ? await getNotificationErrorUserByIdDb(insertedId) : null;
};
const updateNotificationErrorUserDb = async (id, data) => {
const store = { ...data };
const whereData = { notification_error_user_id: id };
const { query: queryText, values } = pool.buildDynamicUpdate(
"notification_error_user",
store,
whereData
);
await pool.query(`${queryText} AND deleted_at IS NULL`, values);
return getNotificationErrorUserByIdDb(id);
};
// Soft delete tag
const deleteNotificationErrorUserDb = async (id, deletedBy) => {
const queryText = `
UPDATE notification_error_user
SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1
WHERE notification_error_user_id = $2 AND deleted_at IS NULL
`;
await pool.query(queryText, [deletedBy, id]);
return true;
};
module.exports = {
getAllNotificationErrorUserDb,
getNotificationErrorUserByIdDb,
createNotificationErrorUserDb,
updateNotificationErrorUserDb,
deleteNotificationErrorUserDb,
};