delete: api is ready & not read
This commit is contained in:
@@ -26,15 +26,6 @@ const getNotificationByIdDb = async (id) => {
|
||||
const getAllNotificationDb = async (searchParams = {}) => {
|
||||
let queryParams = [];
|
||||
|
||||
const boolFields = ["is_send", "is_delivered", "is_read", "is_active"];
|
||||
|
||||
boolFields.forEach((f) => {
|
||||
if (searchParams[f] !== undefined && searchParams[f] !== null && searchParams[f] !== "") {
|
||||
const v = searchParams[f];
|
||||
searchParams[f] = v == "1" ? 1 : v == "0" ? 0 : null;
|
||||
}
|
||||
});
|
||||
|
||||
if (searchParams.limit) {
|
||||
const page = Number(searchParams.page ?? 1) - 1;
|
||||
queryParams = [Number(searchParams.limit ?? 10), page];
|
||||
@@ -42,13 +33,13 @@ const getAllNotificationDb = async (searchParams = {}) => {
|
||||
|
||||
const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike(
|
||||
[
|
||||
"a.message_error_issue",
|
||||
"a.is_send",
|
||||
"a.is_delivered",
|
||||
"a.is_read",
|
||||
"a.is_active",
|
||||
"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
|
||||
@@ -57,10 +48,13 @@ const getAllNotificationDb = async (searchParams = {}) => {
|
||||
|
||||
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" },
|
||||
{ column: "a.message_error_issue", param: searchParams.message_error_issue, type: "string" },
|
||||
{ column: "a.is_send", param: searchParams.is_send, type: "number" },
|
||||
{ column: "a.is_delivered", param: searchParams.is_delivered, type: "number" },
|
||||
{ column: "a.is_read", param: searchParams.is_read, type: "number" },
|
||||
{ column: "a.is_active", param: searchParams.is_active, type: "number" },
|
||||
{ column: "b.error_code", param: searchParams.error_code, type: "string" },
|
||||
{ column: "b.error_code_name", param: searchParams.error_code_name, type: "string" },
|
||||
],
|
||||
queryParams
|
||||
);
|
||||
@@ -158,195 +152,9 @@ 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
|
||||
|
||||
Reference in New Issue
Block a user