diff --git a/controllers/notification_error.controller.js b/controllers/notification_error.controller.js index 6ef3397..ad9cadd 100644 --- a/controllers/notification_error.controller.js +++ b/controllers/notification_error.controller.js @@ -75,16 +75,13 @@ class NotificationErrorController { } static async resend(req, res) { - try { - const { id } = req.params; - const result = - await NotificationErrorService.resendNotification( - id, - ); - res.status(200).json(result); - } catch (error) { - return error; - } + const { id } = req.params; + const results = await NotificationErrorService.resendNotification(id); + const response = await setResponse( + results, + "Notification Error resend successfully" + ); + res.status(response.statusCode).json(response); } } diff --git a/controllers/notification_error_user.controller.js b/controllers/notification_error_user.controller.js index 3b016d5..1c5fef8 100644 --- a/controllers/notification_error_user.controller.js +++ b/controllers/notification_error_user.controller.js @@ -107,17 +107,16 @@ class NotificationErrorUserController { } static async resendByUser(req, res) { - try { - const { id, contact_phone } = req.params; - const result = - await NotificationErrorUserService.resendNotificationByUser( - id, - contact_phone - ); - res.status(200).json(result); - } catch (error) { - return error; - } + const { id, contact_phone } = req.params; + const results = await NotificationErrorUserService.resendNotificationByUser( + id, + contact_phone + ); + const response = await setResponse( + results, + "Notification Error By User resend successfully" + ); + res.status(response.statusCode).json(response); } } diff --git a/db/notification_wa.db.js b/db/notification_wa.db.js index 42bf77d..bae36a4 100644 --- a/db/notification_wa.db.js +++ b/db/notification_wa.db.js @@ -43,9 +43,6 @@ const sendNotifikasi = async (phone, message) => { const endPointWhatsapp = process.env.ENDPOINT_WHATSAPP; - const response = await axios.post(endPointWhatsapp, payload, { httpsAgent }); - // console.log('response', response); - try { const response = await axios.post(endPointWhatsapp, payload, { httpsAgent }); // console.log(response.data); diff --git a/services/notification_error.service.js b/services/notification_error.service.js index afe9786..966984e 100644 --- a/services/notification_error.service.js +++ b/services/notification_error.service.js @@ -64,7 +64,7 @@ class NotificationService { return result; } catch (error) { - return error + return error; } } @@ -150,9 +150,8 @@ class NotificationService { const notification = Array.isArray(dataExist) ? dataExist[0] : dataExist; if (notification.is_read === true) { - return {success: true, message:"Notification has already been read"}; - } - + return { success: true, message: "Notification has already been read" }; + } if (!notification.is_read) { const updateStatus = await updateNotificationErrorDb( @@ -172,83 +171,72 @@ class NotificationService { } static async resendNotification(id) { - try { - const deviceNotification = await getNotificationByIdDb(id); - if (!deviceNotification) { - throw new ErrorHandler(404, "Notification Data not found"); - } + 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 errorCode = await getErrorCodeByIdDb( + deviceNotification.error_code_id + ); + const dataExist = await getUsersNotificationErrorDb(id); - const dataExist = await getUsersNotificationErrorDb(id); - const activeUsers = - dataExist?.filter((user) => user.is_active === true) || []; + 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 (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 results = []; - 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 err; - } - }) + for (const user of activeUsers) { + const tokenRedirect = await generateTokenRedirect( + user.contact_phone, + user.contact_name, + id ); - 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 (err) { - return err; + 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, + }); + + results.push({ + contact_name: user.contact_name, + contact_phone: user.contact_phone, + is_send: isSuccess, + is_delivered: isSuccess, + }); } + + return { + notification_error_id: id, + device_name: deviceNotification.device_name, + error_code: errorCode?.error_code, + error_code_name: errorCode?.error_code_name, + users: results, + }; } } diff --git a/services/notification_error_user.service.js b/services/notification_error_user.service.js index b001377..78b618e 100644 --- a/services/notification_error_user.service.js +++ b/services/notification_error_user.service.js @@ -92,80 +92,103 @@ 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"); - } + const results = []; - const data = dataExist[0]; + const idUser = Array.isArray(id) ? id : [id]; - if (data.contact_phone !== contact_phone) { - throw new ErrorHandler( - 404, - `Contact Phone with this phone ${contact_phone} not found.` + for (const id of idUser) { + try { + const dataExist = await getNotificationErrorUserByIdDb(id); + + if (!dataExist || dataExist.length < 1) { + results.push({ + id, + status: "failed", + message: "Data Notification Error User not found", + }); + continue; + } + + const data = dataExist[0]; + + if (data.contact_phone !== contact_phone) { + results.push({ + id, + status: "failed", + message: `Phone ${contact_phone} not found.`, + }); + continue; + } + + if (data.contact_is_active === false) { + results.push({ + id, + status: "failed", + message: `Contact ${data.contact_phone} not active.`, + }); + continue; + } + + const tokenRedirect = await generateTokenRedirect( + data.contact_phone, + data.contact_name, + data.notification_error_id ); - } - if (data.contact_is_active === false) { - throw new ErrorHandler( - 400, - `Contact Phone with this phone ${data.contact_phone} not active.` + const encodedToken = encodeURIComponent(tokenRedirect); + const shortUrl = await shortUrltiny(encodedToken); + + 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 ); + const isSuccess = resultSend?.error ? false : true; + + const updateData = { + is_send: isSuccess, + message_error_issue: bodyWithUrl, + }; + + await updateNotificationErrorUserDb(id, updateData); + + if (!isSuccess) { + results.push({ + id, + status: "failed", + message: `WhatsApp API Gagal: ${ + resultSend?.message || "Unknown Error" + }`, + }); + } else { + results.push({ + status: 200, + notification_error_user_id: id, + notification_error_id: data.notification_error_id, + device_name: data.device_name, + error_code: data?.error_code, + error_code_name: data?.error_code_name, + users: { + contact_name: data.contact_name, + contact_phone: data.contact_phone, + is_send: isSuccess, + }, + }); + } + } catch (err) { + results.push({ id, status: "error", message: err.message }); } - - const tokenRedirect = await generateTokenRedirect( - data.contact_phone, - data.contact_name, - data.notification_error_id - ); - - const encodedToken = encodeURIComponent(tokenRedirect); - const shortUrl = await shortUrltiny(encodedToken); - - 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); - - const isSuccess = resultSend?.error ? false : true; - - const updateData = { - is_send: isSuccess, - message_error_issue: bodyWithUrl, - }; - - await updateNotificationErrorUserDb(id, updateData); - - if (!isSuccess) { - throw new ErrorHandler( - 500, - `WhatsApp API Gagal mengirim pesan: ${ - resultSend?.message || "Unknown Error" - }` - ); - } - - 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 (err) { - return err } + return results; } }