repair: service & controller notif and notif user

This commit is contained in:
2026-01-07 09:34:59 +07:00
parent f3abaeac39
commit aa7cd2fbaa
5 changed files with 168 additions and 164 deletions

View File

@@ -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;
}
}