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

@@ -25,6 +25,36 @@ class NotificationErrorController {
res.status(response.statusCode).json(response); 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) { static async getById(req, res) {
const { id } = req.params; const { id } = req.params;

View File

@@ -144,15 +144,13 @@ const getReaderNotificationErrorDb = async (id) => {
a.contact_phone, a.contact_phone,
a.contact_name, a.contact_name,
a.is_send, a.is_send,
b.notification_error_id, b.message_error_issue
b.error_code_id
FROM notification_error_user a FROM notification_error_user a
LEFT JOIN notification_error b ON a.notification_error_id = b.notification_error_id LEFT JOIN notification_error b ON a.notification_error_id = b.notification_error_id
WHERE a.notification_error_id = $1 WHERE a.notification_error_id = $1
AND a.is_send = 1
AND a.deleted_at IS NULL AND a.deleted_at IS NULL
`; `;
@@ -160,11 +158,197 @@ const getReaderNotificationErrorDb = async (id) => {
return result.recordset; 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 = { module.exports = {
getNotificationByIdDb, getNotificationByIdDb,
getAllNotificationDb, getAllNotificationDb,
getAllNotificationIsReadDb,
getAllNotificationIsNotReadDb,
InsertNotificationErrorDb, InsertNotificationErrorDb,
updateNotificationErrorDb, updateNotificationErrorDb,
getReaderNotificationErrorDb getReaderNotificationErrorDb
}; };

View File

@@ -9,6 +9,14 @@ router
.route('/') .route('/')
.get(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.getAll) .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 router
.route('/') .route('/')
.post(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.create) .post(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.create)

View File

@@ -1,5 +1,7 @@
const { const {
getAllNotificationDb, getAllNotificationDb,
getAllNotificationIsReadDb,
getAllNotificationIsNotReadDb,
getNotificationByIdDb, getNotificationByIdDb,
InsertNotificationErrorDb, InsertNotificationErrorDb,
getReaderNotificationErrorDb, getReaderNotificationErrorDb,
@@ -43,6 +45,32 @@ 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) { static async createNotificationError(data) {
try { try {
if (!data || typeof data !== 'object') data = {}; if (!data || typeof data !== 'object') data = {};

View File

@@ -40,10 +40,6 @@ const insertNotificationSchema = Joi.object({
// Update Notification Schema // Update Notification Schema
// ======================== // ========================
const updateNotificationSchema = Joi.object({ const updateNotificationSchema = Joi.object({
error_code_id: Joi.number().optional().messages({
"number.base": "error_code_id must be a number",
}),
is_send: Joi.boolean().optional().messages({ is_send: Joi.boolean().optional().messages({
"boolean.base": "is_send must be a boolean", "boolean.base": "is_send must be a boolean",
}), }),