chore: catch all tv upsert action

ricky rx 1 year ago
parent 56384c1fa9
commit f54f113acf

@ -31,7 +31,7 @@ class ApiUtilities {
$tvCodes = $request->tvCodes ?? []; $tvCodes = $request->tvCodes ?? [];
$tvs = $request->tvs ?? []; $tvs = $request->tvs ?? [];
$oValidation = TV::validateExcel($tvs, $tvCodes); $oValidation = TV::validateExcel($tvs, $tvCodes);
$result = TV::uploadExcel($tvs, $oValidation); $result = TV::uploadExcel($tvs, $oValidation, $request->user());
return JSONResponse::Success($result); return JSONResponse::Success($result);
break; break;
case 'exportData': case 'exportData':

@ -175,10 +175,12 @@ class NewTvRequest extends Model {
$tv->installed_at = now(); $tv->installed_at = now();
$tv->last_connected_at = now(); $tv->last_connected_at = now();
$tv->save(); $tv->save();
TVLog::historyCreate($request->user(), $tv->id, $tv);
$newTvReq->activated_at = now(); $newTvReq->activated_at = now();
$newTvReq->tv_fk = $tv->id; $newTvReq->tv_fk = $tv->id;
$newTvReq->save(); $newTvReq->save();
DB::commit(); DB::commit();
return JSONResponse::Success(['message'=>'Success to update tv data']); return JSONResponse::Success(['message'=>'Success to update tv data']);

@ -117,6 +117,7 @@ class Tv extends Model {
try { try {
DB::beginTransaction(); DB::beginTransaction();
$tv = TV::findOrFail($request->id); $tv = TV::findOrFail($request->id);
$oldTv = $tv->replicate();
$tv->company_name = $request->company_name; $tv->company_name = $request->company_name;
$tv->address = $request->address; $tv->address = $request->address;
$tv->street_address = $request->street_address; $tv->street_address = $request->street_address;
@ -133,6 +134,8 @@ class Tv extends Model {
$tv->col10 = $request->col10; $tv->col10 = $request->col10;
$tv->notes = $request->notes; $tv->notes = $request->notes;
$tv->update(); $tv->update();
TvLog::historyUpdate($request->user(), $tv->id, $oldTv, $tv);
DB::commit(); DB::commit();
return JSONResponse::Success(['message'=>'Success to update tv data']); 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']); $request->validate(['id' => 'required|integer|exists:App\Models\TV,id']);
try { try {
DB::beginTransaction();
$tv = Tv::findOrFail($request->id); $tv = Tv::findOrFail($request->id);
$tv->is_active = !$tv->is_active; $tv->is_active = !$tv->is_active;
$tv->save(); $tv->save();
@ -303,7 +307,7 @@ class Tv extends Model {
} }
return ['status' => $endStatus, 'results' => $results]; return ['status' => $endStatus, 'results' => $results];
} }
public static function uploadExcel($tvRows, $oValidation) { public static function uploadExcel($tvRows, $oValidation, User $user) {
$validationResults = $oValidation['results']; $validationResults = $oValidation['results'];
$countUploads = [ $countUploads = [
self::EXCEL_UPDATE => 0, self::EXCEL_NO_CHANGE => 0, self::EXCEL_FAILED => 0 self::EXCEL_UPDATE => 0, self::EXCEL_NO_CHANGE => 0, self::EXCEL_FAILED => 0
@ -334,7 +338,7 @@ class Tv extends Model {
$newTv->save(); $newTv->save();
// save data log // save data log
TvLog::saveHistory(TvLog::TYPE_CREATE, $newTv->id, $oldTV, $newTv); TvLog::historyUpdateExcel($user, $newTv->id, $oldTV, $newTv);
DB::commit(); DB::commit();
break; break;

@ -12,22 +12,28 @@ class TvLog extends Model {
protected $hidden = ['ik_cust_id', 'ik_address_id']; protected $hidden = ['ik_cust_id', 'ik_address_id'];
protected $casts = ['from'=>'object', 'to'=>'object']; protected $casts = ['from'=>'object', 'to'=>'object'];
const TYPE_CREATE = 'create'; const TYPES = ['create', 'update', 'update-excel'];
const TYPE_UPDATE = 'update';
const TYPES = ['create', 'update']; public static function historyCreate(User $user, int $tvFk, Tv $newTv) { self::saveHistory('create', $user, $tvFk, null, $newTv); }
public static function saveHistory(String $type, int $tvFk, ?Tv $oldTv, ?Tv $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"); if(!in_array($type, self::TYPES)) throw new \Exception("Type '$type' No Valid");
$tvLog = new TvLog(); $tvLog = new TvLog();
$tvLog->tv_fk = $tvFk; $tvLog->tv_fk = $tvFk;
$tvLog->type = $type; $tvLog->type = $type;
if($oldTv) { if($oldTv) {
$oldTv = $oldTv->toArray(); $oldTv = $oldTv->toArray();
unset($oldTv['id']);
$tvLog->from =$oldTv; $tvLog->from =$oldTv;
} }
if($newTv) { if($newTv) {
$newTv = $newTv->toArray(); $newTv = $newTv->toArray();
$tvLog->from =$newTv; unset($newTv['id']);
$tvLog->to = $newTv;
} }
if($user) { $tvLog->user_fk = $user->id; }
$tvLog->save(); $tvLog->save();
} }
} }

@ -52,7 +52,11 @@ return new class extends Migration {
$table->string('type'); $table->string('type');
$table->json('from')->nullable(); $table->json('from')->nullable();
$table->json('to')->nullable(); $table->json('to')->nullable();
$table->foreignId('user_fk')->nullable();
$table->timestampsTz(); $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) { // Schema::create('tv_sessions', function (Blueprint $table) {

Loading…
Cancel
Save