Merge pull request 'repair: notification error log created_by' (#21) from wisdom into main
Reviewed-on: #21
This commit is contained in:
@@ -55,20 +55,7 @@ class NotificationErrorLogController {
|
|||||||
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
||||||
}
|
}
|
||||||
|
|
||||||
let createdBy, contactPhone;
|
const results = await NotificationErrorLogService.createNotificationErrorLog(value, req.user.user_id);
|
||||||
|
|
||||||
if (!isNaN(req.user.userId) && Number(req.user.userId) > 0) {
|
|
||||||
createdBy = Number(req.user.userId);
|
|
||||||
contactPhone = value.contact_phone || null;
|
|
||||||
} else {
|
|
||||||
createdBy = null;
|
|
||||||
contactPhone = req.user.userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
value.created_by = createdBy;
|
|
||||||
value.contact_phone = contactPhone;
|
|
||||||
|
|
||||||
const results = await NotificationErrorLogService.createNotificationErrorLog(value);
|
|
||||||
const response = await setResponse(results, 'Notification Error Log created successfully')
|
const response = await setResponse(results, 'Notification Error Log created successfully')
|
||||||
|
|
||||||
return res.status(response.statusCode).json(response);
|
return res.status(response.statusCode).json(response);
|
||||||
|
|||||||
@@ -45,7 +45,27 @@ const getNotificationErrorLogByNotificationErrorIdDb = async (notificationErrorI
|
|||||||
};
|
};
|
||||||
|
|
||||||
const createNotificationErrorLogDb = async (store) => {
|
const createNotificationErrorLogDb = async (store) => {
|
||||||
const { query: queryText, values } = pool.buildDynamicInsert("notification_error_log", store);
|
const queryText = `
|
||||||
|
INSERT INTO notification_error_log (
|
||||||
|
notification_error_id,
|
||||||
|
contact_phone,
|
||||||
|
notification_error_log_description,
|
||||||
|
created_by,
|
||||||
|
updated_by,
|
||||||
|
created_at,
|
||||||
|
updated_at
|
||||||
|
)
|
||||||
|
VALUES ($1, $2, $3, $4, $4, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
|
||||||
|
SELECT SCOPE_IDENTITY() as inserted_id;
|
||||||
|
`;
|
||||||
|
|
||||||
|
const values = [
|
||||||
|
store.notification_error_id,
|
||||||
|
store.contact_phone,
|
||||||
|
store.notification_error_log_description,
|
||||||
|
store.created_by
|
||||||
|
];
|
||||||
|
|
||||||
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;
|
||||||
return insertedId ? await getNotificationErrorLogByIdDb(insertedId) : null;
|
return insertedId ? await getNotificationErrorLogByIdDb(insertedId) : null;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ const {
|
|||||||
deleteNotificationErrorLogDb
|
deleteNotificationErrorLogDb
|
||||||
} = require('../db/notification_error_log.db');
|
} = require('../db/notification_error_log.db');
|
||||||
|
|
||||||
|
const { getUserByIdDb } = require('../db/user.db');
|
||||||
const { ErrorHandler } = require('../helpers/error');
|
const { ErrorHandler } = require('../helpers/error');
|
||||||
|
|
||||||
class NotificationErrorLogService {
|
class NotificationErrorLogService {
|
||||||
@@ -40,15 +41,34 @@ class NotificationErrorLogService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create Notification Error Log
|
// Create Notification Error Log
|
||||||
static async createNotificationErrorLog(data) {
|
static async createNotificationErrorLog(data, userId) {
|
||||||
try {
|
try {
|
||||||
if (!data || typeof data !== 'object') data = {};
|
if (!data || typeof data !== 'object') data = {};
|
||||||
|
|
||||||
|
let createdBy = null;
|
||||||
|
let contactPhone = data.contact_phone || null;
|
||||||
|
|
||||||
|
if (userId) {
|
||||||
|
try {
|
||||||
|
const user = await getUserByIdDb(userId);
|
||||||
|
|
||||||
|
if (user && user.user_id) {
|
||||||
|
createdBy = Number(user.user_id);
|
||||||
|
} else {
|
||||||
|
createdBy = null;
|
||||||
|
contactPhone = userId;
|
||||||
|
}
|
||||||
|
} catch (dbError) {
|
||||||
|
createdBy = null;
|
||||||
|
contactPhone = userId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const store = {
|
const store = {
|
||||||
notification_error_id: data.notification_error_id,
|
notification_error_id: data.notification_error_id,
|
||||||
contact_phone: data.contact_phone,
|
contact_phone: contactPhone,
|
||||||
notification_error_log_description: data.notification_error_log_description,
|
notification_error_log_description: data.notification_error_log_description,
|
||||||
created_by: data.created_by
|
created_by: createdBy
|
||||||
};
|
};
|
||||||
|
|
||||||
const result = await createNotificationErrorLogDb(store);
|
const result = await createNotificationErrorLogDb(store);
|
||||||
|
|||||||
Reference in New Issue
Block a user