211 lines
5.5 KiB
JavaScript
211 lines
5.5 KiB
JavaScript
const pool = require("../config");
|
|
|
|
const InsertNotificationErrorDb = 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 getNotificationByIdDb = async (id) => {
|
|
const queryText = `
|
|
SELECT
|
|
a.*,
|
|
b.plant_sub_section_id,
|
|
c.plant_sub_section_name,
|
|
d.device_code,
|
|
d.device_name,
|
|
d.device_location,
|
|
d.listen_channel,
|
|
e.brand_name
|
|
|
|
FROM notification_error a
|
|
|
|
LEFT JOIN m_tags b
|
|
ON a.error_chanel = b.tag_number
|
|
|
|
LEFT JOIN m_plant_sub_section c
|
|
ON b.plant_sub_section_id = c.plant_sub_section_id
|
|
|
|
LEFT JOIN m_device d
|
|
ON a.error_chanel = d.listen_channel AND d.deleted_at IS NULL
|
|
|
|
LEFT JOIN m_brands e
|
|
ON d.brand_id = e.brand_id AND d.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];
|
|
};
|
|
|
|
const getDeviceNotificationByIdDb = async (chanel_id) => {
|
|
const queryText = `
|
|
SELECT
|
|
device_code,
|
|
device_name,
|
|
device_location,
|
|
listen_channel
|
|
|
|
FROM m_device
|
|
WHERE listen_channel = $1
|
|
AND deleted_at IS NULL
|
|
`;
|
|
const result = await pool.query(queryText, [chanel_id]);
|
|
return result.recordset[0];
|
|
};
|
|
|
|
|
|
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.message_error_issue",
|
|
"a.is_send",
|
|
"a.is_delivered",
|
|
"a.is_read",
|
|
"a.is_active",
|
|
"b.error_code",
|
|
"b.error_code_name",
|
|
],
|
|
searchParams.criteria,
|
|
queryParams
|
|
);
|
|
if (whereParamOr) queryParams = whereParamOr;
|
|
|
|
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
|
|
[
|
|
{ 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
|
|
);
|
|
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_code,
|
|
d.device_name,
|
|
d.device_location,
|
|
d.listen_channel,
|
|
e.brand_name,
|
|
|
|
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 a.error_chanel = d.listen_channel AND d.deleted_at IS NULL
|
|
|
|
LEFT JOIN m_brands e
|
|
ON d.brand_id = e.brand_id AND d.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 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, 10)
|
|
: 0;
|
|
|
|
return { data: result.recordset, total };
|
|
};
|
|
|
|
const updateNotificationErrorDb = 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 getUsersNotificationErrorDb = async (id) => {
|
|
const queryText = `
|
|
SELECT
|
|
b.notification_error_id,
|
|
a.notification_error_user_id,
|
|
a.contact_phone,
|
|
a.contact_name,
|
|
a.is_send,
|
|
a.updated_at,
|
|
c.is_active
|
|
|
|
FROM notification_error_user a
|
|
|
|
LEFT JOIN notification_error b ON a.notification_error_id = b.notification_error_id
|
|
|
|
LEFT JOIN contact c ON a.contact_phone = c.contact_phone
|
|
|
|
WHERE a.notification_error_id = $1
|
|
AND a.deleted_at IS NULL
|
|
`;
|
|
|
|
const result = await pool.query(queryText, [id]);
|
|
return result.recordset;
|
|
};
|
|
|
|
module.exports = {
|
|
getNotificationByIdDb,
|
|
getDeviceNotificationByIdDb,
|
|
getAllNotificationDb,
|
|
InsertNotificationErrorDb,
|
|
updateNotificationErrorDb,
|
|
getUsersNotificationErrorDb
|
|
|
|
};
|