wisdom #19

Merged
bragaz_rexita merged 38 commits from wisdom into main 2025-12-22 09:18:18 +00:00
4 changed files with 12 additions and 269 deletions
Showing only changes of commit 5e7de6d144 - Show all commits

View File

@@ -25,35 +25,6 @@ class NotificationErrorController {
res.status(response.statusCode).json(response);
}
static async getAllIsRead(req, res) {
const queryParams = req.query;
const results = await NotificationErrorService.getAllNotificationIsRead(
queryParams
);
const response = await setResponsePaging(
queryParams,
results,
"Notification found"
);
res.status(response.statusCode).json(response);
}
static async getAllIsNotRead(req, res) {
const queryParams = req.query;
const results = await NotificationErrorService.getAllNotificationIsNotRead(
queryParams
);
const response = await setResponsePaging(
queryParams,
results,
"Notification found"
);
res.status(response.statusCode).json(response);
}
static async getById(req, res) {
const { id } = req.params;

View File

@@ -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

View File

@@ -9,14 +9,6 @@ router
.route('/')
.get(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.getAll)
router
.route('/read')
.get(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.getAllIsRead)
router
.route('/not-ready')
.get(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.getAllIsNotRead)
router
.route('/')
.post(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.create)

View File

@@ -1,7 +1,5 @@
const {
getAllNotificationDb,
getAllNotificationIsReadDb,
getAllNotificationIsNotReadDb,
getNotificationByIdDb,
InsertNotificationErrorDb,
getReaderNotificationErrorDb,
@@ -45,32 +43,6 @@ class NotificationService {
}
}
static async getAllNotificationIsRead(param) {
try {
const results = await getAllNotificationIsReadDb(param);
results.data.map(element => {
});
return results;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
static async getAllNotificationIsNotRead(param) {
try {
const results = await getAllNotificationIsNotReadDb(param);
results.data.map(element => {
});
return results;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
static async createNotificationError(data) {
try {
if (!data || typeof data !== 'object') data = {};