repair: service & controller notif and notif user
This commit is contained in:
@@ -75,16 +75,13 @@ class NotificationErrorController {
|
||||
}
|
||||
|
||||
static async resend(req, res) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const result =
|
||||
await NotificationErrorService.resendNotification(
|
||||
id,
|
||||
const results = await NotificationErrorService.resendNotification(id);
|
||||
const response = await setResponse(
|
||||
results,
|
||||
"Notification Error resend successfully"
|
||||
);
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
res.status(response.statusCode).json(response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -107,17 +107,16 @@ class NotificationErrorUserController {
|
||||
}
|
||||
|
||||
static async resendByUser(req, res) {
|
||||
try {
|
||||
const { id, contact_phone } = req.params;
|
||||
const result =
|
||||
await NotificationErrorUserService.resendNotificationByUser(
|
||||
const results = await NotificationErrorUserService.resendNotificationByUser(
|
||||
id,
|
||||
contact_phone
|
||||
);
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
return error;
|
||||
}
|
||||
const response = await setResponse(
|
||||
results,
|
||||
"Notification Error By User resend successfully"
|
||||
);
|
||||
res.status(response.statusCode).json(response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -64,7 +64,7 @@ class NotificationService {
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
return error
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,10 +150,9 @@ 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(
|
||||
notification_error_id,
|
||||
@@ -172,16 +171,16 @@ class NotificationService {
|
||||
}
|
||||
|
||||
static async resendNotification(id) {
|
||||
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 errorCode = await getErrorCodeByIdDb(
|
||||
deviceNotification.error_code_id
|
||||
);
|
||||
const dataExist = await getUsersNotificationErrorDb(id);
|
||||
|
||||
const activeUsers =
|
||||
dataExist?.filter((user) => user.is_active === true) || [];
|
||||
|
||||
@@ -192,13 +191,13 @@ class NotificationService {
|
||||
);
|
||||
}
|
||||
|
||||
const results = await Promise.all(
|
||||
activeUsers.map(async (user) => {
|
||||
try {
|
||||
const results = [];
|
||||
|
||||
for (const user of activeUsers) {
|
||||
const tokenRedirect = await generateTokenRedirect(
|
||||
user.contact_phone,
|
||||
user.contact_name,
|
||||
user.notification_error_id
|
||||
id
|
||||
);
|
||||
|
||||
const encodedToken = encodeURIComponent(tokenRedirect);
|
||||
@@ -207,17 +206,14 @@ class NotificationService {
|
||||
const bodyWithUrl =
|
||||
`Hai ${user.contact_name || "-"}\n` +
|
||||
`Terjadi peringatan dengan kode error ${
|
||||
errorCode.error_code || "-"
|
||||
} - ${errorCode.error_code_name || "-"} ` +
|
||||
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 resultSend = await sendNotifikasi(user.contact_phone, bodyWithUrl);
|
||||
|
||||
const isSuccess = !resultSend?.error;
|
||||
|
||||
@@ -226,29 +222,21 @@ class NotificationService {
|
||||
is_delivered: isSuccess,
|
||||
});
|
||||
|
||||
return {
|
||||
results.push({
|
||||
contact_name: user.contact_name,
|
||||
contact_phone: user.contact_phone,
|
||||
is_send: isSuccess,
|
||||
is_delivered: isSuccess,
|
||||
};
|
||||
} catch (err) {
|
||||
return err;
|
||||
});
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
notification_error_id: id,
|
||||
device_name: deviceNotification.device_name,
|
||||
error_code: errorCode.error_code,
|
||||
error_code_name: errorCode.error_code_name,
|
||||
error_code: errorCode?.error_code,
|
||||
error_code_name: errorCode?.error_code_name,
|
||||
users: results,
|
||||
message: "Berhasil mengirim ulang notifikasi",
|
||||
};
|
||||
} catch (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -92,26 +92,41 @@ class NotificationErrorUserService {
|
||||
}
|
||||
|
||||
static async resendNotificationByUser(id, contact_phone) {
|
||||
const results = [];
|
||||
|
||||
const idUser = Array.isArray(id) ? id : [id];
|
||||
|
||||
for (const id of idUser) {
|
||||
try {
|
||||
const dataExist = await getNotificationErrorUserByIdDb(id);
|
||||
|
||||
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];
|
||||
|
||||
if (data.contact_phone !== contact_phone) {
|
||||
throw new ErrorHandler(
|
||||
404,
|
||||
`Contact Phone with this phone ${contact_phone} not found.`
|
||||
);
|
||||
results.push({
|
||||
id,
|
||||
status: "failed",
|
||||
message: `Phone ${contact_phone} not found.`,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
if (data.contact_is_active === false) {
|
||||
throw new ErrorHandler(
|
||||
400,
|
||||
`Contact Phone with this phone ${data.contact_phone} not active.`
|
||||
);
|
||||
results.push({
|
||||
id,
|
||||
status: "failed",
|
||||
message: `Contact ${data.contact_phone} not active.`,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
const tokenRedirect = await generateTokenRedirect(
|
||||
@@ -133,8 +148,10 @@ class NotificationErrorUserService {
|
||||
}, silahkan cek detail pada link berikut:\n` +
|
||||
`${shortUrl}`;
|
||||
|
||||
const resultSend = await sendNotifikasi(data.contact_phone, bodyWithUrl);
|
||||
|
||||
const resultSend = await sendNotifikasi(
|
||||
data.contact_phone,
|
||||
bodyWithUrl
|
||||
);
|
||||
const isSuccess = resultSend?.error ? false : true;
|
||||
|
||||
const updateData = {
|
||||
@@ -145,27 +162,33 @@ class NotificationErrorUserService {
|
||||
await updateNotificationErrorUserDb(id, updateData);
|
||||
|
||||
if (!isSuccess) {
|
||||
throw new ErrorHandler(
|
||||
500,
|
||||
`WhatsApp API Gagal mengirim pesan: ${
|
||||
results.push({
|
||||
id,
|
||||
status: "failed",
|
||||
message: `WhatsApp API Gagal: ${
|
||||
resultSend?.message || "Unknown Error"
|
||||
}`
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
}`,
|
||||
});
|
||||
} else {
|
||||
results.push({
|
||||
status: 200,
|
||||
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
|
||||
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 });
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user