Merge pull request 'repair: change message wa notification' (#24) from wisdom into main
Reviewed-on: #24
This commit is contained in:
@@ -61,10 +61,24 @@ const getNotificationErrorUserByIdDb = async (id) => {
|
|||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT
|
SELECT
|
||||||
a.*,
|
a.*,
|
||||||
b. is_active as contact_is_active
|
|
||||||
|
b. is_active as contact_is_active,
|
||||||
|
|
||||||
|
d.error_code,
|
||||||
|
d.error_code_name,
|
||||||
|
|
||||||
|
e.device_name
|
||||||
|
|
||||||
FROM notification_error_user a
|
FROM notification_error_user a
|
||||||
|
|
||||||
LEFT JOIN contact b ON a.contact_phone = b.contact_phone
|
LEFT JOIN contact b ON a.contact_phone = b.contact_phone
|
||||||
|
|
||||||
|
LEFT JOIN notification_error c ON a.notification_error_id = c.notification_error_id
|
||||||
|
|
||||||
|
LEFT JOIN brand_code d ON d.error_code_id = c.error_code_id
|
||||||
|
|
||||||
|
LEFT JOIN m_device e ON c.error_chanel = e.listen_channel
|
||||||
|
|
||||||
WHERE a.notification_error_user_id = $1 AND a.deleted_at IS NULL
|
WHERE a.notification_error_user_id = $1 AND a.deleted_at IS NULL
|
||||||
`;
|
`;
|
||||||
const result = await pool.query(queryText, [id]);
|
const result = await pool.query(queryText, [id]);
|
||||||
|
|||||||
@@ -171,69 +171,76 @@ class NotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async resendNotification(id) {
|
static async resendNotification(id) {
|
||||||
try {
|
try {
|
||||||
const dataExist = await getUsersNotificationErrorDb(id);
|
const deviceNotification = await getNotificationByIdDb(id);
|
||||||
|
if (!deviceNotification) {
|
||||||
const activeUsers =
|
throw new ErrorHandler(404, "Notification Data not found");
|
||||||
dataExist?.filter((user) => user.is_active === true) || [];
|
|
||||||
|
|
||||||
if (activeUsers.length < 1) {
|
|
||||||
throw new ErrorHandler(
|
|
||||||
404,
|
|
||||||
"No active contacts found for this notification"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!dataExist || dataExist.length < 1) {
|
|
||||||
throw new ErrorHandler(404, "Data Notification Error not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
const results = await Promise.all(
|
|
||||||
dataExist &&
|
|
||||||
activeUsers.map(async (user) => {
|
|
||||||
const tokenRedirect = await generateTokenRedirect(
|
|
||||||
user.contact_phone,
|
|
||||||
user.contact_name,
|
|
||||||
user.notification_error_id
|
|
||||||
);
|
|
||||||
|
|
||||||
const encodedToken = encodeURIComponent(tokenRedirect);
|
|
||||||
const shortUrl = await shortUrltiny(encodedToken);
|
|
||||||
|
|
||||||
const bodyBase =
|
|
||||||
`Hai Operator\n` +
|
|
||||||
`Terjadi peringatan pada device, silahkan cek detail pada link berikut :\n`;
|
|
||||||
const bodyWithUrl = `${bodyBase}\n🔗 ${shortUrl}`;
|
|
||||||
|
|
||||||
const resultSend = await sendNotifikasi(
|
|
||||||
user.contact_phone,
|
|
||||||
bodyWithUrl
|
|
||||||
);
|
|
||||||
const isSuccess = resultSend?.error ? false : true;
|
|
||||||
|
|
||||||
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,
|
|
||||||
};
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
notification_error_id: id,
|
|
||||||
user: results,
|
|
||||||
message: "Berhasil mengirim ulang notifikasi",
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
throw new ErrorHandler(error.statusCode || 500, error.message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const errorCodeId = deviceNotification.error_code_id;
|
||||||
|
const errorCode = await getErrorCodeByIdDb(errorCodeId);
|
||||||
|
|
||||||
|
const dataExist = await getUsersNotificationErrorDb(id);
|
||||||
|
const activeUsers = dataExist?.filter((user) => user.is_active === true) || [];
|
||||||
|
|
||||||
|
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 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 {
|
||||||
|
contact_name: user.contact_name,
|
||||||
|
error: err.message,
|
||||||
|
is_send: false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
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 (error) {
|
||||||
|
throw new ErrorHandler(error.statusCode || 500, error.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = NotificationService;
|
module.exports = NotificationService;
|
||||||
|
|||||||
@@ -92,14 +92,10 @@ class NotificationErrorUserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async resendNotificationByUser(id, contact_phone) {
|
static async resendNotificationByUser(id, contact_phone) {
|
||||||
|
|
||||||
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(
|
throw new ErrorHandler(404, "Data Notification Error User not found");
|
||||||
404,
|
|
||||||
"Data Notification Error User not found"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = dataExist[0];
|
const data = dataExist[0];
|
||||||
@@ -127,11 +123,15 @@ class NotificationErrorUserService {
|
|||||||
const encodedToken = encodeURIComponent(tokenRedirect);
|
const encodedToken = encodeURIComponent(tokenRedirect);
|
||||||
const shortUrl = await shortUrltiny(encodedToken);
|
const shortUrl = await shortUrltiny(encodedToken);
|
||||||
|
|
||||||
const bodyBase =
|
const bodyWithUrl =
|
||||||
`Hai Operator\n` +
|
`Hai ${data.contact_name || "-"}\n` +
|
||||||
`Terjadi peringatan pada device, silahkan cek detail pada link berikut :\n`;
|
`Terjadi peringatan dengan kode error ${data.error_code || "-"} - ${
|
||||||
|
data.error_code_name || "-"
|
||||||
const bodyWithUrl = `${bodyBase}\n🔗 ${shortUrl}`;
|
} ` +
|
||||||
|
`pada device ${
|
||||||
|
data.device_name || "-"
|
||||||
|
}, silahkan cek detail pada link berikut:\n` +
|
||||||
|
`${shortUrl}`;
|
||||||
|
|
||||||
const resultSend = await sendNotifikasi(data.contact_phone, bodyWithUrl);
|
const resultSend = await sendNotifikasi(data.contact_phone, bodyWithUrl);
|
||||||
|
|
||||||
@@ -155,8 +155,12 @@ class NotificationErrorUserService {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
notification_error_user_id: id,
|
notification_error_user_id: id,
|
||||||
|
notification_error_id: data.notification_error_id,
|
||||||
is_send: isSuccess,
|
is_send: isSuccess,
|
||||||
short_url: shortUrl,
|
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",
|
message: "Berhasil mengirim ulang notifikasi",
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user