Skip to content

Commit

Permalink
BE-012: Fix null violation DB transact issue
Browse files Browse the repository at this point in the history
Error Info :
Missing foreign key (processId) with relation notifyLogs → appLogs, that causing issue failed to transact DB on notificationhelper module run

Payload :
Foreign key violation: 7 ERROR:  insert or update on table "notifyLogs" violates foreign key constraint "notifylogs_processid_foreign"

Environment :
Production

Root cause :
foreign key relation are not exist between notifylogs → applogs when insert transact happens

Resolution :
check and validate if processid between notifylogs → applogs is exists or not, if exists then do nothing. But if not exists then insert it before insert into notifylogs

Related Changes :
- app\Helpers\NotificationHelper.php

Signed-off-by: Dicky Herlambang (花) <herlambangdicky5@gmail.com>
  • Loading branch information
Nicklas373 committed Jul 7, 2024
1 parent 7693b19 commit 10a8b79
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions app/Helpers/NotificationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function sendRouteErrNotify($processId, $status, $errReason, $errRoute, $errCode
"\n\nError Reason: <b>".$errReason.
"</b>\nError Log: <pre><code>".$errCode.
"</code></pre>";
$checkValidate = DB::table('appLogs')::where('processId', '=', $processId)->count();
try {
$response = Telegram::sendMessage([
'chat_id' => env('TELEGRAM_CHAT_ID'),
Expand All @@ -36,11 +37,19 @@ function sendRouteErrNotify($processId, $status, $errReason, $errRoute, $errCode
$messageId = $response->getMessageId();

try {
DB::table('appLogs')->insert([
'processId' => $processId,
'errReason' => null,
'errStatus' => null,
]);
if ($checkValidate = 0) {
DB::table('appLogs')->insert([
'processId' => $processId,
'errReason' => $errReason,
'errStatus' => null
]);
} else if ($checkValidate > 0) {
DB::table('appLogs')->where('processId', '=', $processId)
->update([
'errReason' => $errReason,
'errStatus' => null
]);
}
DB::table('notifyLogs')->insert([
'processId' => $processId,
'notifyName' => 'Telegram SDK',
Expand All @@ -58,11 +67,19 @@ function sendRouteErrNotify($processId, $status, $errReason, $errRoute, $errCode
} else {
$httpStatus = $e->getHttpStatusCode();
}
DB::table('appLogs')->insert([
'processId' => $processId,
'errReason' => 'TelegramResponseException',
'errStatus' => $e->getMessage(),
]);
if ($checkValidate = 0) {
DB::table('appLogs')->insert([
'processId' => $processId,
'errReason' => 'TelegramResponseException',
'errStatus' => $e->getMessage()
]);
} else if ($checkValidate > 0) {
DB::table('appLogs')->where('processId', '=', $processId)
->update([
'errReason' => 'TelegramResponseException',
'errStatus' => $e->getMessage()
]);
}
DB::table('notifyLogs')->insert([
'processId' => $processId,
'notifyName' => 'Telegram SDK',
Expand All @@ -75,11 +92,19 @@ function sendRouteErrNotify($processId, $status, $errReason, $errRoute, $errCode
}
} catch (\Exception $e) {
try {
DB::table('appLogs')->insert([
'processId' => $processId,
'errReason' => 'Unexpected handling exception !',
'errStatus' => $e->getMessage(),
]);
if ($checkValidate = 0) {
DB::table('appLogs')->insert([
'processId' => $processId,
'errReason' => 'Unexpected handling exception !',
'errStatus' => $e->getMessage()
]);
} else if ($checkValidate > 0) {
DB::table('appLogs')->where('processId', '=', $processId)
->update([
'errReason' => 'Unexpected handling exception !',
'errStatus' => $e->getMessage()
]);
}
DB::table('notifyLogs')->insert([
'processId' => $processId,
'notifyName' => 'Telegram SDK',
Expand Down

0 comments on commit 10a8b79

Please sign in to comment.