From 163d5a048613d64f92856483d70e32a501cf25cc Mon Sep 17 00:00:00 2001 From: ricky rx Date: Fri, 21 Jun 2024 17:31:54 +0700 Subject: [PATCH] chore: write tvlogs for upsert action --- app/Helper/Frontend/ApiUtilities.php | 2 +- app/Models/NewTvRequest.php | 2 ++ app/Models/Tv.php | 8 ++++++-- app/Models/TvLog.php | 16 +++++++++++----- database/migrations/2024_05_18_033105_tv.php | 4 ++++ 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/app/Helper/Frontend/ApiUtilities.php b/app/Helper/Frontend/ApiUtilities.php index 6f70f42..cc7101c 100644 --- a/app/Helper/Frontend/ApiUtilities.php +++ b/app/Helper/Frontend/ApiUtilities.php @@ -31,7 +31,7 @@ class ApiUtilities { $tvCodes = $request->tvCodes ?? []; $tvs = $request->tvs ?? []; $oValidation = TV::validateExcel($tvs, $tvCodes); - $result = TV::uploadExcel($tvs, $oValidation); + $result = TV::uploadExcel($tvs, $oValidation, $request->user()); return JSONResponse::Success($result); break; case 'exportData': diff --git a/app/Models/NewTvRequest.php b/app/Models/NewTvRequest.php index 92ec5f7..aaf9922 100644 --- a/app/Models/NewTvRequest.php +++ b/app/Models/NewTvRequest.php @@ -175,10 +175,12 @@ class NewTvRequest extends Model { $tv->installed_at = now(); $tv->last_connected_at = now(); $tv->save(); + TVLog::historyCreate($request->user(), $tv->id, $tv); $newTvReq->activated_at = now(); $newTvReq->tv_fk = $tv->id; $newTvReq->save(); + DB::commit(); return JSONResponse::Success(['message'=>'Success to update tv data']); diff --git a/app/Models/Tv.php b/app/Models/Tv.php index 61d8bbf..d97fd2d 100644 --- a/app/Models/Tv.php +++ b/app/Models/Tv.php @@ -117,6 +117,7 @@ class Tv extends Model { try { DB::beginTransaction(); $tv = TV::findOrFail($request->id); + $oldTv = $tv->replicate(); $tv->company_name = $request->company_name; $tv->address = $request->address; $tv->street_address = $request->street_address; @@ -133,6 +134,8 @@ class Tv extends Model { $tv->col10 = $request->col10; $tv->notes = $request->notes; $tv->update(); + + TvLog::historyUpdate($request->user(), $tv->id, $oldTv, $tv); DB::commit(); return JSONResponse::Success(['message'=>'Success to update tv data']); @@ -151,6 +154,7 @@ class Tv extends Model { $request->validate(['id' => 'required|integer|exists:App\Models\TV,id']); try { + DB::beginTransaction(); $tv = Tv::findOrFail($request->id); $tv->is_active = !$tv->is_active; $tv->save(); @@ -303,7 +307,7 @@ class Tv extends Model { } return ['status' => $endStatus, 'results' => $results]; } - public static function uploadExcel($tvRows, $oValidation) { + public static function uploadExcel($tvRows, $oValidation, User $user) { $validationResults = $oValidation['results']; $countUploads = [ self::EXCEL_UPDATE => 0, self::EXCEL_NO_CHANGE => 0, self::EXCEL_FAILED => 0 @@ -334,7 +338,7 @@ class Tv extends Model { $newTv->save(); // save data log - TvLog::saveHistory(TvLog::TYPE_CREATE, $newTv->id, $oldTV, $newTv); + TvLog::historyUpdateExcel($user, $newTv->id, $oldTV, $newTv); DB::commit(); break; diff --git a/app/Models/TvLog.php b/app/Models/TvLog.php index 7fb78bf..2af7207 100644 --- a/app/Models/TvLog.php +++ b/app/Models/TvLog.php @@ -12,22 +12,28 @@ class TvLog extends Model { protected $hidden = ['ik_cust_id', 'ik_address_id']; protected $casts = ['from'=>'object', 'to'=>'object']; - const TYPE_CREATE = 'create'; - const TYPE_UPDATE = 'update'; - const TYPES = ['create', 'update']; - public static function saveHistory(String $type, int $tvFk, ?Tv $oldTv, ?Tv $newTv) { + const TYPES = ['create', 'update', 'update-excel']; + + public static function historyCreate(User $user, int $tvFk, Tv $newTv) { self::saveHistory('create', $user, $tvFk, null, $newTv); } + public static function historyUpdate(User $user, int $tvFk, Tv $oldTv, Tv $newTv) { self::saveHistory('update', $user, $tvFk, $oldTv, $newTv); } + public static function historyUpdateExcel(User $user, int $tvFk, Tv $oldTv, Tv $newTv) { self::saveHistory('update-excel', $user, $tvFk, $oldTv, $newTv); } + + private static function saveHistory(String $type, ?User $user, int $tvFk, ?Tv $oldTv, ?Tv $newTv) { if(!in_array($type, self::TYPES)) throw new \Exception("Type '$type' No Valid"); $tvLog = new TvLog(); $tvLog->tv_fk = $tvFk; $tvLog->type = $type; if($oldTv) { $oldTv = $oldTv->toArray(); + unset($oldTv['id']); $tvLog->from =$oldTv; } if($newTv) { $newTv = $newTv->toArray(); - $tvLog->from =$newTv; + unset($newTv['id']); + $tvLog->to = $newTv; } + if($user) { $tvLog->user_fk = $user->id; } $tvLog->save(); } } diff --git a/database/migrations/2024_05_18_033105_tv.php b/database/migrations/2024_05_18_033105_tv.php index be94026..98f8b12 100644 --- a/database/migrations/2024_05_18_033105_tv.php +++ b/database/migrations/2024_05_18_033105_tv.php @@ -52,7 +52,11 @@ return new class extends Migration { $table->string('type'); $table->json('from')->nullable(); $table->json('to')->nullable(); + $table->foreignId('user_fk')->nullable(); $table->timestampsTz(); + + $table->foreign('tv_fk')->references('id')->on('tvs')->cascadeOnDelete(); + $table->foreign('user_fk')->references('id')->on('users'); }); // Schema::create('tv_sessions', function (Blueprint $table) {