repair: update is read in notification error
This commit is contained in:
@@ -25,7 +25,6 @@ class NotificationErrorController {
|
|||||||
res.status(response.statusCode).json(response);
|
res.status(response.statusCode).json(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static async getById(req, res) {
|
static async getById(req, res) {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|
||||||
@@ -50,7 +49,10 @@ class NotificationErrorController {
|
|||||||
const results = await NotificationErrorService.createNotificationError(
|
const results = await NotificationErrorService.createNotificationError(
|
||||||
value
|
value
|
||||||
);
|
);
|
||||||
const response = await setResponse(results, "Notification created successfully");
|
const response = await setResponse(
|
||||||
|
results,
|
||||||
|
"Notification created successfully"
|
||||||
|
);
|
||||||
|
|
||||||
return res.status(response.statusCode).json(response);
|
return res.status(response.statusCode).json(response);
|
||||||
}
|
}
|
||||||
@@ -58,34 +60,35 @@ class NotificationErrorController {
|
|||||||
static async update(req, res) {
|
static async update(req, res) {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|
||||||
const { error, value } = checkValidate(updateNotificationSchema, req)
|
const userId = req.user.user_id;
|
||||||
|
|
||||||
if (error) {
|
const results = await NotificationErrorService.updateNotificationError(
|
||||||
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
id,
|
||||||
}
|
userId
|
||||||
|
);
|
||||||
value.userId = req.user.user_id
|
const response = await setResponse(
|
||||||
|
results,
|
||||||
const results = await NotificationErrorService.updateNotificationError(id, value);
|
"Notification Error updated successfully"
|
||||||
const response = await setResponse(results, 'Notification Error User updated successfully')
|
);
|
||||||
|
|
||||||
res.status(response.statusCode).json(response);
|
res.status(response.statusCode).json(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
static async resend(req, res) {
|
static async resend(req, res) {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|
||||||
const results = await NotificationErrorService.resendNotification(id);
|
const results = await NotificationErrorService.resendNotification(id);
|
||||||
|
|
||||||
const response = await setResponse(
|
const response = await setResponse(results, results.message);
|
||||||
results,
|
|
||||||
results.message,
|
|
||||||
);
|
|
||||||
|
|
||||||
res.status(response.statusCode).json(response);
|
res.status(response.statusCode).json(response);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const response = setResponse(null, error.message, error.statusCode || 500);
|
const response = setResponse(
|
||||||
|
null,
|
||||||
|
error.message,
|
||||||
|
error.statusCode || 500
|
||||||
|
);
|
||||||
res.status(response.statusCode).json(response);
|
res.status(response.statusCode).json(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,10 +10,7 @@ const getAllNotificationErrorUserDb = async (searchParams = {}) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike(
|
const { whereOrConditions, whereParamOr } = pool.buildStringOrIlike(
|
||||||
[
|
["a.notification_error_id", "a.contact_id"],
|
||||||
"a.notification_error_id",
|
|
||||||
"a.contact_id",
|
|
||||||
],
|
|
||||||
searchParams.criteria,
|
searchParams.criteria,
|
||||||
queryParams
|
queryParams
|
||||||
);
|
);
|
||||||
@@ -22,7 +19,11 @@ const getAllNotificationErrorUserDb = async (searchParams = {}) => {
|
|||||||
|
|
||||||
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
|
const { whereConditions, whereParamAnd } = pool.buildFilterQuery(
|
||||||
[
|
[
|
||||||
{ column: "a.notification_error_id", param: searchParams.name, type: "int" },
|
{
|
||||||
|
column: "a.notification_error_id",
|
||||||
|
param: searchParams.name,
|
||||||
|
type: "int",
|
||||||
|
},
|
||||||
{ column: "a.contact_id", param: searchParams.code, type: "int" },
|
{ column: "a.contact_id", param: searchParams.code, type: "int" },
|
||||||
],
|
],
|
||||||
queryParams
|
queryParams
|
||||||
@@ -36,10 +37,14 @@ const getAllNotificationErrorUserDb = async (searchParams = {}) => {
|
|||||||
a.*
|
a.*
|
||||||
FROM notification_error_user a
|
FROM notification_error_user a
|
||||||
WHERE a.deleted_at IS NULL
|
WHERE a.deleted_at IS NULL
|
||||||
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""}
|
${
|
||||||
|
whereConditions.length > 0
|
||||||
|
? ` AND ${whereConditions.join(" AND ")}`
|
||||||
|
: ""
|
||||||
|
}
|
||||||
${whereOrConditions ? ` ${whereOrConditions}` : ""}
|
${whereOrConditions ? ` ${whereOrConditions}` : ""}
|
||||||
ORDER BY a.notification_error_user_id ASC
|
ORDER BY a.notification_error_user_id ASC
|
||||||
${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ''}
|
${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ""}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const result = await pool.query(queryText, queryParams);
|
const result = await pool.query(queryText, queryParams);
|
||||||
@@ -66,8 +71,27 @@ const getNotificationErrorUserByIdDb = async (id) => {
|
|||||||
return result.recordset;
|
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 createNotificationErrorUserDb = async (store) => {
|
||||||
const { query: queryText, values } = pool.buildDynamicInsert("notification_error_user", store);
|
const { query: queryText, values } = pool.buildDynamicInsert(
|
||||||
|
"notification_error_user",
|
||||||
|
store
|
||||||
|
);
|
||||||
const result = await pool.query(queryText, values);
|
const result = await pool.query(queryText, values);
|
||||||
const insertedId = result.recordset?.[0]?.inserted_id;
|
const insertedId = result.recordset?.[0]?.inserted_id;
|
||||||
|
|
||||||
@@ -85,7 +109,7 @@ const updateNotificationErrorUserDb = async (id, data) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
await pool.query(`${queryText} AND deleted_at IS NULL`, values);
|
await pool.query(`${queryText} AND deleted_at IS NULL`, values);
|
||||||
return getNotificationErrorUserByIdDb(id);
|
return getNotificationErrorUserByIdDb(id);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Soft delete tag
|
// Soft delete tag
|
||||||
@@ -102,6 +126,7 @@ const deleteNotificationErrorUserDb = async (id, deletedBy) => {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
getAllNotificationErrorUserDb,
|
getAllNotificationErrorUserDb,
|
||||||
getNotificationErrorUserByIdDb,
|
getNotificationErrorUserByIdDb,
|
||||||
|
getNotificationErrorByIdDb,
|
||||||
createNotificationErrorUserDb,
|
createNotificationErrorUserDb,
|
||||||
updateNotificationErrorUserDb,
|
updateNotificationErrorUserDb,
|
||||||
deleteNotificationErrorUserDb,
|
deleteNotificationErrorUserDb,
|
||||||
|
|||||||
@@ -17,6 +17,10 @@ const {
|
|||||||
|
|
||||||
const { getSparepartsByErrorCodeIdDb } = require("../db/brand_sparepart.db");
|
const { getSparepartsByErrorCodeIdDb } = require("../db/brand_sparepart.db");
|
||||||
|
|
||||||
|
const {
|
||||||
|
getNotificationErrorByIdDb,
|
||||||
|
} = require("../db/notification_error_user.db");
|
||||||
|
|
||||||
const { getFileUploadByPathDb } = require("../db/file_uploads.db");
|
const { getFileUploadByPathDb } = require("../db/file_uploads.db");
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -135,21 +139,34 @@ class NotificationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async updateNotificationError(id, data) {
|
static async updateNotificationError(notification_error_id, data) {
|
||||||
try {
|
try {
|
||||||
if (!data || typeof data !== "object") data = {};
|
const dataExist = await getNotificationErrorByIdDb(notification_error_id);
|
||||||
|
|
||||||
const dataExist = await getNotificationByIdDb(id);
|
if (!dataExist || (Array.isArray(dataExist) && dataExist.length < 1)) {
|
||||||
|
throw new ErrorHandler(404, "Notification Error User not found");
|
||||||
if (dataExist.length < 1) {
|
|
||||||
throw new ErrorHandler(404, "NotificationErrorUser not found");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await updateNotificationErrorDb(id, data);
|
const notification = Array.isArray(dataExist) ? dataExist[0] : dataExist;
|
||||||
|
|
||||||
return result;
|
if (notification.is_read === true) {
|
||||||
|
throw new ErrorHandler(400, "Notification has already been read");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!notification.is_read) {
|
||||||
|
const updateStatus = await updateNotificationErrorDb(
|
||||||
|
notification_error_id,
|
||||||
|
{ is_read: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!updateStatus) {
|
||||||
|
throw new ErrorHandler(500, "Failed to update notification");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { success: true, message: "Notification marked as read" };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new ErrorHandler(error.statusCode, error.message);
|
throw new ErrorHandler(error.statusCode || 500, error.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user