add: api is read & not read in notification

This commit is contained in:
2025-12-18 12:43:44 +07:00
parent 1aa7b1bc08
commit dd5e1cc713
5 changed files with 253 additions and 7 deletions

View File

@@ -144,15 +144,13 @@ const getReaderNotificationErrorDb = async (id) => {
a.contact_phone,
a.contact_name,
a.is_send,
b.notification_error_id,
b.error_code_id
b.message_error_issue
FROM notification_error_user a
LEFT JOIN notification_error b ON a.notification_error_id = b.notification_error_id
WHERE a.notification_error_id = $1
AND a.is_send = 1
AND a.deleted_at IS NULL
`;
@@ -160,11 +158,197 @@ const getReaderNotificationErrorDb = async (id) => {
return result.recordset;
};
const getAllNotificationIsReadDb = 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(
[
"b.error_code",
"b.error_code_name",
"c.solution_name",
"COALESCE(a.is_send, 0)",
"COALESCE(a.is_delivered, 0)",
"COALESCE(a.is_read, 0)",
"COALESCE(a.is_active, 0)",
],
searchParams.criteria,
queryParams
);
if (whereParamOr) queryParams = whereParamOr;
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
[
{ column: "COALESCE(a.is_send, 0)", param: searchParams.is_send, type: "number" },
{ column: "COALESCE(a.is_delivered, 0)", param: searchParams.is_delivered, type: "number" },
{ column: "COALESCE(a.is_read, 0)", param: searchParams.is_read, type: "number" },
{ column: "COALESCE(a.is_active, 0)", param: searchParams.is_active, type: "number" },
],
queryParams
);
if (whereParamAnd) queryParams = whereParamAnd;
const queryText = `
SELECT
COUNT(*) OVER() AS total_data,
a.notification_error_id,
a.error_code_id,
a.message_error_issue,
a.is_send,
a.is_delivered,
a.is_read,
a.is_active,
b.error_code,
b.error_code_name,
b.error_code_color,
b.path_icon,
b.created_at,
c.solution_name,
c.type_solution,
c.path_solution,
d.device_name,
d.device_location,
COALESCE(d.device_name, '') + ' - ' + COALESCE(b.error_code_name, '') AS device_name_error
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 b.error_code_id = c.error_code_id AND c.deleted_at IS NULL
LEFT JOIN m_device d
ON b.brand_id = d.brand_id AND d.deleted_at IS NULL
WHERE a.deleted_at IS NULL
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""}
${whereOrConditions ? ` ${whereOrConditions}` : ""}
AND a.is_read = 1
ORDER BY a.notification_error_id DESC
${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, 5)
: 0;
return { data: result.recordset, total };
};
const getAllNotificationIsNotReadDb = 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(
[
"b.error_code",
"b.error_code_name",
"c.solution_name",
"COALESCE(a.is_send, 0)",
"COALESCE(a.is_delivered, 0)",
"COALESCE(a.is_read, 0)",
"COALESCE(a.is_active, 0)",
],
searchParams.criteria,
queryParams
);
if (whereParamOr) queryParams = whereParamOr;
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
[
{ column: "COALESCE(a.is_send, 0)", param: searchParams.is_send, type: "number" },
{ column: "COALESCE(a.is_delivered, 0)", param: searchParams.is_delivered, type: "number" },
{ column: "COALESCE(a.is_read, 0)", param: searchParams.is_read, type: "number" },
{ column: "COALESCE(a.is_active, 0)", param: searchParams.is_active, type: "number" },
],
queryParams
);
if (whereParamAnd) queryParams = whereParamAnd;
const queryText = `
SELECT
COUNT(*) OVER() AS total_data,
a.notification_error_id,
a.error_code_id,
a.message_error_issue,
a.is_send,
a.is_delivered,
a.is_read,
a.is_active,
b.error_code,
b.error_code_name,
b.error_code_color,
b.path_icon,
b.created_at,
c.solution_name,
c.type_solution,
c.path_solution,
d.device_name,
d.device_location,
COALESCE(d.device_name, '') + ' - ' + COALESCE(b.error_code_name, '') AS device_name_error
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 b.error_code_id = c.error_code_id AND c.deleted_at IS NULL
LEFT JOIN m_device d
ON b.brand_id = d.brand_id AND d.deleted_at IS NULL
WHERE a.deleted_at IS NULL
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""}
${whereOrConditions ? ` ${whereOrConditions}` : ""}
AND a.is_read = 0
ORDER BY a.notification_error_id DESC
${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, 5)
: 0;
return { data: result.recordset, total };
};
module.exports = {
getNotificationByIdDb,
getAllNotificationDb,
getAllNotificationIsReadDb,
getAllNotificationIsNotReadDb,
InsertNotificationErrorDb,
updateNotificationErrorDb,
getReaderNotificationErrorDb
};