chore: catch all tv upsert action

ricky rx 1 year ago
parent 56384c1fa9
commit f54f113acf

@ -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':

@ -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']);

@ -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;

@ -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();
}
}

@ -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) {

Loading…
Cancel
Save