148 lines
4.0 KiB
JavaScript
148 lines
4.0 KiB
JavaScript
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.*,
|
|
|
|
b. is_active as contact_is_active,
|
|
|
|
d.error_code,
|
|
d.error_code_name,
|
|
|
|
e.device_name
|
|
|
|
FROM notification_error_user a
|
|
|
|
LEFT JOIN contact b ON a.contact_phone = b.contact_phone
|
|
|
|
LEFT JOIN notification_error c ON a.notification_error_id = c.notification_error_id
|
|
|
|
LEFT JOIN brand_code d ON d.error_code_id = c.error_code_id
|
|
|
|
LEFT JOIN m_device e ON c.error_chanel = e.listen_channel
|
|
|
|
WHERE a.notification_error_user_id = $1 AND a.deleted_at IS NULL
|
|
`;
|
|
const result = await pool.query(queryText, [id]);
|
|
return result.recordset;
|
|
};
|
|
|
|
const getNotificationErrorByIdDb = async (notification_error_id) => {
|
|
const queryText = `
|
|
SELECT
|
|
a.*,
|
|
b.is_active as contact_is_active,
|
|
c.is_read
|
|
|
|
FROM notification_error_user a
|
|
LEFT JOIN contact b ON a.contact_phone = b.contact_phone
|
|
LEFT JOIN notification_error c ON a.notification_error_id = c.notification_error_id
|
|
WHERE a.notification_error_id = $1 AND a.deleted_at IS NULL
|
|
`;
|
|
const result = await pool.query(queryText, [notification_error_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,
|
|
getNotificationErrorByIdDb,
|
|
createNotificationErrorUserDb,
|
|
updateNotificationErrorUserDb,
|
|
deleteNotificationErrorUserDb,
|
|
};
|