repair: service & controller notif and notif user
This commit is contained in:
@@ -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 results = await NotificationErrorService.resendNotification(id);
|
||||||
const result =
|
const response = await setResponse(
|
||||||
await NotificationErrorService.resendNotification(
|
results,
|
||||||
id,
|
"Notification Error resend successfully"
|
||||||
);
|
);
|
||||||
res.status(200).json(result);
|
res.status(response.statusCode).json(response);
|
||||||
} catch (error) {
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 results = await NotificationErrorUserService.resendNotificationByUser(
|
||||||
const result =
|
id,
|
||||||
await NotificationErrorUserService.resendNotificationByUser(
|
contact_phone
|
||||||
id,
|
);
|
||||||
contact_phone
|
const response = await setResponse(
|
||||||
);
|
results,
|
||||||
res.status(200).json(result);
|
"Notification Error By User resend successfully"
|
||||||
} catch (error) {
|
);
|
||||||
return error;
|
res.status(response.statusCode).json(response);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ class NotificationService {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return error
|
return error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,9 +150,8 @@ 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(
|
||||||
@@ -172,83 +171,72 @@ 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) || [];
|
|
||||||
|
|
||||||
if (activeUsers.length < 1) {
|
if (activeUsers.length < 1) {
|
||||||
throw new ErrorHandler(
|
throw new ErrorHandler(
|
||||||
404,
|
404,
|
||||||
"No active contacts found for this notification"
|
"No active contacts found for this notification"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const results = await Promise.all(
|
const results = [];
|
||||||
activeUsers.map(async (user) => {
|
|
||||||
try {
|
|
||||||
const tokenRedirect = await generateTokenRedirect(
|
|
||||||
user.contact_phone,
|
|
||||||
user.contact_name,
|
|
||||||
user.notification_error_id
|
|
||||||
);
|
|
||||||
|
|
||||||
const encodedToken = encodeURIComponent(tokenRedirect);
|
for (const user of activeUsers) {
|
||||||
const shortUrl = await shortUrltiny(encodedToken);
|
const tokenRedirect = await generateTokenRedirect(
|
||||||
|
user.contact_phone,
|
||||||
const bodyWithUrl =
|
user.contact_name,
|
||||||
`Hai ${user.contact_name || "-"}\n` +
|
id
|
||||||
`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;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
const encodedToken = encodeURIComponent(tokenRedirect);
|
||||||
notification_error_id: id,
|
const shortUrl = await shortUrltiny(encodedToken);
|
||||||
device_name: deviceNotification.device_name,
|
|
||||||
error_code: errorCode.error_code,
|
const bodyWithUrl =
|
||||||
error_code_name: errorCode.error_code_name,
|
`Hai ${user.contact_name || "-"}\n` +
|
||||||
users: results,
|
`Terjadi peringatan dengan kode error ${
|
||||||
message: "Berhasil mengirim ulang notifikasi",
|
errorCode?.error_code || "-"
|
||||||
};
|
} - ${errorCode?.error_code_name || "-"} ` +
|
||||||
} catch (err) {
|
`pada device ${
|
||||||
return err;
|
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,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -92,80 +92,103 @@ class NotificationErrorUserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async resendNotificationByUser(id, contact_phone) {
|
static async resendNotificationByUser(id, contact_phone) {
|
||||||
try {
|
const results = [];
|
||||||
const dataExist = await getNotificationErrorUserByIdDb(id);
|
|
||||||
if (!dataExist || dataExist.length < 1) {
|
|
||||||
throw new ErrorHandler(404, "Data Notification Error User not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = dataExist[0];
|
const idUser = Array.isArray(id) ? id : [id];
|
||||||
|
|
||||||
if (data.contact_phone !== contact_phone) {
|
for (const id of idUser) {
|
||||||
throw new ErrorHandler(
|
try {
|
||||||
404,
|
const dataExist = await getNotificationErrorUserByIdDb(id);
|
||||||
`Contact Phone with this phone ${contact_phone} not found.`
|
|
||||||
|
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) {
|
const encodedToken = encodeURIComponent(tokenRedirect);
|
||||||
throw new ErrorHandler(
|
const shortUrl = await shortUrltiny(encodedToken);
|
||||||
400,
|
|
||||||
`Contact Phone with this phone ${data.contact_phone} not active.`
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user