repair: service & controller notif and notif user #27

Merged
bragaz_rexita merged 1 commits from wisdom into main 2026-01-07 03:10:34 +00:00
5 changed files with 168 additions and 164 deletions

View File

@@ -75,16 +75,13 @@ class NotificationErrorController {
} }
static async resend(req, res) { static async resend(req, res) {
try {
const { id } = req.params; const { id } = req.params;
const result = const results = await NotificationErrorService.resendNotification(id);
await NotificationErrorService.resendNotification( const response = await setResponse(
id, results,
"Notification Error resend successfully"
); );
res.status(200).json(result); res.status(response.statusCode).json(response);
} catch (error) {
return error;
}
} }
} }

View File

@@ -107,17 +107,16 @@ class NotificationErrorUserController {
} }
static async resendByUser(req, res) { static async resendByUser(req, res) {
try {
const { id, contact_phone } = req.params; const { id, contact_phone } = req.params;
const result = const results = await NotificationErrorUserService.resendNotificationByUser(
await NotificationErrorUserService.resendNotificationByUser(
id, id,
contact_phone contact_phone
); );
res.status(200).json(result); const response = await setResponse(
} catch (error) { results,
return error; "Notification Error By User resend successfully"
} );
res.status(response.statusCode).json(response);
} }
} }

View File

@@ -43,9 +43,6 @@ const sendNotifikasi = async (phone, message) => {
const endPointWhatsapp = process.env.ENDPOINT_WHATSAPP; const endPointWhatsapp = process.env.ENDPOINT_WHATSAPP;
const response = await axios.post(endPointWhatsapp, payload, { httpsAgent });
// console.log('response', response);
try { try {
const response = await axios.post(endPointWhatsapp, payload, { httpsAgent }); const response = await axios.post(endPointWhatsapp, payload, { httpsAgent });
// console.log(response.data); // console.log(response.data);

View File

@@ -64,7 +64,7 @@ class NotificationService {
return result; return result;
} catch (error) { } catch (error) {
return error return error;
} }
} }
@@ -150,10 +150,9 @@ class NotificationService {
const notification = Array.isArray(dataExist) ? dataExist[0] : dataExist; const notification = Array.isArray(dataExist) ? dataExist[0] : dataExist;
if (notification.is_read === true) { 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) { if (!notification.is_read) {
const updateStatus = await updateNotificationErrorDb( const updateStatus = await updateNotificationErrorDb(
notification_error_id, notification_error_id,
@@ -172,16 +171,16 @@ class NotificationService {
} }
static async resendNotification(id) { static async resendNotification(id) {
try {
const deviceNotification = await getNotificationByIdDb(id); const deviceNotification = await getNotificationByIdDb(id);
if (!deviceNotification) { if (!deviceNotification) {
throw new ErrorHandler(404, "Notification Data not found"); throw new ErrorHandler(404, "Notification Data not found");
} }
const errorCodeId = deviceNotification.error_code_id; const errorCode = await getErrorCodeByIdDb(
const errorCode = await getErrorCodeByIdDb(errorCodeId); deviceNotification.error_code_id
);
const dataExist = await getUsersNotificationErrorDb(id); const dataExist = await getUsersNotificationErrorDb(id);
const activeUsers = const activeUsers =
dataExist?.filter((user) => user.is_active === true) || []; dataExist?.filter((user) => user.is_active === true) || [];
@@ -192,13 +191,13 @@ class NotificationService {
); );
} }
const results = await Promise.all( const results = [];
activeUsers.map(async (user) => {
try { for (const user of activeUsers) {
const tokenRedirect = await generateTokenRedirect( const tokenRedirect = await generateTokenRedirect(
user.contact_phone, user.contact_phone,
user.contact_name, user.contact_name,
user.notification_error_id id
); );
const encodedToken = encodeURIComponent(tokenRedirect); const encodedToken = encodeURIComponent(tokenRedirect);
@@ -207,17 +206,14 @@ class NotificationService {
const bodyWithUrl = const bodyWithUrl =
`Hai ${user.contact_name || "-"}\n` + `Hai ${user.contact_name || "-"}\n` +
`Terjadi peringatan dengan kode error ${ `Terjadi peringatan dengan kode error ${
errorCode.error_code || "-" errorCode?.error_code || "-"
} - ${errorCode.error_code_name || "-"} ` + } - ${errorCode?.error_code_name || "-"} ` +
`pada device ${ `pada device ${
deviceNotification.device_name || "-" deviceNotification.device_name || "-"
}, silahkan cek detail pada link berikut:\n` + }, silahkan cek detail pada link berikut:\n` +
`${shortUrl}`; `${shortUrl}`;
const resultSend = await sendNotifikasi( const resultSend = await sendNotifikasi(user.contact_phone, bodyWithUrl);
user.contact_phone,
bodyWithUrl
);
const isSuccess = !resultSend?.error; const isSuccess = !resultSend?.error;
@@ -226,29 +222,21 @@ class NotificationService {
is_delivered: isSuccess, is_delivered: isSuccess,
}); });
return { results.push({
contact_name: user.contact_name, contact_name: user.contact_name,
contact_phone: user.contact_phone, contact_phone: user.contact_phone,
is_send: isSuccess, is_send: isSuccess,
is_delivered: isSuccess, is_delivered: isSuccess,
}; });
} catch (err) {
return err;
} }
})
);
return { return {
notification_error_id: id, notification_error_id: id,
device_name: deviceNotification.device_name, device_name: deviceNotification.device_name,
error_code: errorCode.error_code, error_code: errorCode?.error_code,
error_code_name: errorCode.error_code_name, error_code_name: errorCode?.error_code_name,
users: results, users: results,
message: "Berhasil mengirim ulang notifikasi",
}; };
} catch (err) {
return err;
}
} }
} }

View File

@@ -92,26 +92,41 @@ class NotificationErrorUserService {
} }
static async resendNotificationByUser(id, contact_phone) { static async resendNotificationByUser(id, contact_phone) {
const results = [];
const idUser = Array.isArray(id) ? id : [id];
for (const id of idUser) {
try { try {
const dataExist = await getNotificationErrorUserByIdDb(id); const dataExist = await getNotificationErrorUserByIdDb(id);
if (!dataExist || dataExist.length < 1) { if (!dataExist || dataExist.length < 1) {
throw new ErrorHandler(404, "Data Notification Error User not found"); results.push({
id,
status: "failed",
message: "Data Notification Error User not found",
});
continue;
} }
const data = dataExist[0]; const data = dataExist[0];
if (data.contact_phone !== contact_phone) { if (data.contact_phone !== contact_phone) {
throw new ErrorHandler( results.push({
404, id,
`Contact Phone with this phone ${contact_phone} not found.` status: "failed",
); message: `Phone ${contact_phone} not found.`,
});
continue;
} }
if (data.contact_is_active === false) { if (data.contact_is_active === false) {
throw new ErrorHandler( results.push({
400, id,
`Contact Phone with this phone ${data.contact_phone} not active.` status: "failed",
); message: `Contact ${data.contact_phone} not active.`,
});
continue;
} }
const tokenRedirect = await generateTokenRedirect( const tokenRedirect = await generateTokenRedirect(
@@ -133,8 +148,10 @@ class NotificationErrorUserService {
}, silahkan cek detail pada link berikut:\n` + }, silahkan cek detail pada link berikut:\n` +
`${shortUrl}`; `${shortUrl}`;
const resultSend = await sendNotifikasi(data.contact_phone, bodyWithUrl); const resultSend = await sendNotifikasi(
data.contact_phone,
bodyWithUrl
);
const isSuccess = resultSend?.error ? false : true; const isSuccess = resultSend?.error ? false : true;
const updateData = { const updateData = {
@@ -145,27 +162,33 @@ class NotificationErrorUserService {
await updateNotificationErrorUserDb(id, updateData); await updateNotificationErrorUserDb(id, updateData);
if (!isSuccess) { if (!isSuccess) {
throw new ErrorHandler( results.push({
500, id,
`WhatsApp API Gagal mengirim pesan: ${ status: "failed",
message: `WhatsApp API Gagal: ${
resultSend?.message || "Unknown Error" resultSend?.message || "Unknown Error"
}` }`,
); });
} } else {
results.push({
return { status: 200,
notification_error_user_id: id, notification_error_user_id: id,
notification_error_id: data.notification_error_id, notification_error_id: data.notification_error_id,
is_send: isSuccess,
short_url: shortUrl,
device_name: data.device_name, device_name: data.device_name,
error_code: data.error_code, error_code: data?.error_code,
error_code_name: data.error_code_name, error_code_name: data?.error_code_name,
message: "Berhasil mengirim ulang notifikasi", users: {
}; contact_name: data.contact_name,
} catch (err) { contact_phone: data.contact_phone,
return err is_send: isSuccess,
},
});
} }
} catch (err) {
results.push({ id, status: "error", message: err.message });
}
}
return results;
} }
} }