diff --git a/db/notification_error_user.db.js b/db/notification_error_user.db.js index 8312a4f..e4b3ae5 100644 --- a/db/notification_error_user.db.js +++ b/db/notification_error_user.db.js @@ -61,10 +61,24 @@ const getNotificationErrorUserByIdDb = async (id) => { const queryText = ` SELECT a.*, - b. is_active as contact_is_active + + 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]); diff --git a/services/notification_error.service.js b/services/notification_error.service.js index 42f5644..eca2e34 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -171,69 +171,76 @@ class NotificationService { } static async resendNotification(id) { - try { - const dataExist = await getUsersNotificationErrorDb(id); - - const activeUsers = - dataExist?.filter((user) => user.is_active === true) || []; - - if (activeUsers.length < 1) { - throw new ErrorHandler( - 404, - "No active contacts found for this notification" - ); - } - - if (!dataExist || dataExist.length < 1) { - throw new ErrorHandler(404, "Data Notification Error not found"); - } - - const results = await Promise.all( - dataExist && - activeUsers.map(async (user) => { - const tokenRedirect = await generateTokenRedirect( - user.contact_phone, - user.contact_name, - user.notification_error_id - ); - - const encodedToken = encodeURIComponent(tokenRedirect); - const shortUrl = await shortUrltiny(encodedToken); - - const bodyBase = - `Hai Operator\n` + - `Terjadi peringatan pada device, silahkan cek detail pada link berikut :\n`; - const bodyWithUrl = `${bodyBase}\nšŸ”— ${shortUrl}`; - - const resultSend = await sendNotifikasi( - user.contact_phone, - bodyWithUrl - ); - const isSuccess = resultSend?.error ? false : true; - - await updateNotificationErrorDb(user.notification_error_id, { - is_send: isSuccess, - is_delivered: isSuccess, - }); - - return { - contact_name: user.contact_name, - contact_phone: user.contact_phone, - is_send: isSuccess, - is_delivered: isSuccess, - }; - }) - ); - - return { - notification_error_id: id, - user: results, - message: "Berhasil mengirim ulang notifikasi", - }; - } catch (error) { - throw new ErrorHandler(error.statusCode || 500, error.message); + try { + const deviceNotification = await getNotificationByIdDb(id); + if (!deviceNotification) { + throw new ErrorHandler(404, "Notification Data not found"); } + + const errorCodeId = deviceNotification.error_code_id; + const errorCode = await getErrorCodeByIdDb(errorCodeId); + + const dataExist = await getUsersNotificationErrorDb(id); + const activeUsers = dataExist?.filter((user) => user.is_active === true) || []; + + if (activeUsers.length < 1) { + throw new ErrorHandler(404, "No active contacts found for this notification"); + } + + const results = await Promise.all( + activeUsers.map(async (user) => { + try { + const tokenRedirect = await generateTokenRedirect( + user.contact_phone, + user.contact_name, + user.notification_error_id + ); + + const encodedToken = encodeURIComponent(tokenRedirect); + const shortUrl = await shortUrltiny(encodedToken); + + const bodyWithUrl = `Hai ${user.contact_name || "-"}\n` + + `Terjadi peringatan dengan kode error ${errorCode.error_code || "-"} - ${errorCode.error_code_name || "-"} ` + + `pada device ${deviceNotification.device_name || "-"}, silahkan cek detail pada link berikut:\n` + + `${shortUrl}`; + + const resultSend = await sendNotifikasi(user.contact_phone, bodyWithUrl); + + const isSuccess = !resultSend?.error; + + await updateNotificationErrorDb(user.notification_error_id, { + is_send: isSuccess, + is_delivered: isSuccess, + }); + + return { + contact_name: user.contact_name, + contact_phone: user.contact_phone, + is_send: isSuccess, + is_delivered: isSuccess, + }; + } catch (err) { + return { + contact_name: user.contact_name, + error: err.message, + is_send: false + }; + } + }) + ); + + return { + notification_error_id: id, + device_name: deviceNotification.device_name, + error_code: errorCode.error_code, + error_code_name:errorCode.error_code_name, + users: results, + message: "Berhasil mengirim ulang notifikasi", + }; + } catch (error) { + throw new ErrorHandler(error.statusCode || 500, error.message); } } +} module.exports = NotificationService; diff --git a/services/notification_error_user.service.js b/services/notification_error_user.service.js index 5b37bff..ec91362 100644 --- a/services/notification_error_user.service.js +++ b/services/notification_error_user.service.js @@ -92,14 +92,10 @@ class NotificationErrorUserService { } static async resendNotificationByUser(id, contact_phone) { - try { const dataExist = await getNotificationErrorUserByIdDb(id); if (!dataExist || dataExist.length < 1) { - throw new ErrorHandler( - 404, - "Data Notification Error User not found" - ); + throw new ErrorHandler(404, "Data Notification Error User not found"); } const data = dataExist[0]; @@ -127,11 +123,15 @@ class NotificationErrorUserService { const encodedToken = encodeURIComponent(tokenRedirect); const shortUrl = await shortUrltiny(encodedToken); - const bodyBase = - `Hai Operator\n` + - `Terjadi peringatan pada device, silahkan cek detail pada link berikut :\n`; - - const bodyWithUrl = `${bodyBase}\nšŸ”— ${shortUrl}`; + const bodyWithUrl = + `Hai ${data.contact_name || "-"}\n` + + `Terjadi peringatan dengan kode error ${data.error_code || "-"} - ${ + data.error_code_name || "-" + } ` + + `pada device ${ + data.device_name || "-" + }, silahkan cek detail pada link berikut:\n` + + `${shortUrl}`; const resultSend = await sendNotifikasi(data.contact_phone, bodyWithUrl); @@ -155,8 +155,12 @@ class NotificationErrorUserService { return { notification_error_user_id: id, + notification_error_id: data.notification_error_id, is_send: isSuccess, short_url: shortUrl, + device_name: data.device_name, + error_code: data.error_code, + error_code_name: data.error_code_name, message: "Berhasil mengirim ulang notifikasi", }; } catch (error) {