108 lines
3.5 KiB
JavaScript
108 lines
3.5 KiB
JavaScript
const pool = require("../config");
|
|
|
|
const getAllNotificationDb = 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.is_send", "a.is_delivered", "a.is_read", "a.is_active"],
|
|
searchParams.criteria,
|
|
queryParams
|
|
);
|
|
if (whereParamOr) queryParams = whereParamOr;
|
|
|
|
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
|
|
[
|
|
{ column: "a.is_delivered", param: searchParams.is_delivered, type: "int" },
|
|
{ column: "a.is_send", param: searchParams.is_send, type: "int" },
|
|
{ column: "a.is_read", param: searchParams.is_read, type: "int" },
|
|
{ column: "a.is_active", param: searchParams.is_active, type: "int" },
|
|
],
|
|
queryParams
|
|
);
|
|
if (whereParamAnd) queryParams = whereParamAnd;
|
|
|
|
const queryText = `
|
|
SELECT
|
|
COUNT(*) OVER() AS total_data,
|
|
a.*,
|
|
b.error_code,
|
|
b.error_code_name,
|
|
b.error_code_description,
|
|
c.solution_name,
|
|
c.type_solution,
|
|
c.path_solution
|
|
FROM notification_error a
|
|
LEFT JOIN brand_code b ON a.error_code_id = b.error_code_id AND b.deleted_at IS NULL
|
|
LEFT JOIN brand_code_solution c ON a.error_code_id = c.error_code_id AND c.deleted_at IS NULL
|
|
WHERE a.deleted_at IS NULL
|
|
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""}
|
|
${whereOrConditions ? ` ${whereOrConditions}` : ""}
|
|
ORDER BY a.notification_error_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 getNotificationByIdDb = async (id) => {
|
|
const queryText = `
|
|
SELECT a.*,
|
|
b.error_code,
|
|
b.error_code_name,
|
|
b.error_code_description,
|
|
c.solution_name,
|
|
c.type_solution,
|
|
c.path_solution
|
|
FROM notification_error a
|
|
LEFT JOIN brand_code b ON a.error_code_id = b.error_code_id AND b.deleted_at IS NULL
|
|
LEFT JOIN brand_code_solution c ON a.error_code_id = c.error_code_id AND c.deleted_at IS NULL
|
|
WHERE a.notification_error_id = $1 AND a.deleted_at IS NULL
|
|
`;
|
|
const result = await pool.query(queryText, [id]);
|
|
return result.recordset?.[0] || null;
|
|
};
|
|
|
|
const insertNotificationDb = async (store) => {
|
|
const { query: queryText, values } = pool.buildDynamicInsert("notification_error", store);
|
|
const result = await pool.query(queryText, values);
|
|
const insertedId = result.recordset?.[0]?.inserted_id;
|
|
return insertedId ? await getNotificationByIdDb(insertedId) : null;
|
|
};
|
|
|
|
const updateNotificationDb = async (id, data) => {
|
|
const store = { ...data };
|
|
const whereData = { notification_error_id: id };
|
|
const { query: queryText, values } = pool.buildDynamicUpdate("notification_error", store, whereData);
|
|
await pool.query(`${queryText} AND deleted_at IS NULL`, values);
|
|
return getNotificationByIdDb(id);
|
|
};
|
|
|
|
const deleteNotificationDb = async (id, deletedBy) => {
|
|
const queryText = `
|
|
UPDATE notification_error
|
|
SET deleted_at = CURRENT_TIMESTAMP, deleted_by = $1
|
|
WHERE notification_error_id = $2 AND deleted_at IS NULL
|
|
`;
|
|
await pool.query(queryText, [deletedBy, id]);
|
|
return true;
|
|
};
|
|
|
|
module.exports = {
|
|
getAllNotificationDb,
|
|
getNotificationByIdDb,
|
|
insertNotificationDb,
|
|
updateNotificationDb,
|
|
deleteNotificationDb,
|
|
};
|