Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
w1lldone committed May 6, 2021
2 parents adafad0 + 3c4da73 commit 414adbd
Show file tree
Hide file tree
Showing 55 changed files with 1,964 additions and 129 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/StationController.php
Expand Up @@ -17,7 +17,7 @@ public function index(Request $request)

$station = $this->stationQuery($station, $request);

$stations = $station->with('schedules')->paginate();
$stations = $station->with('schedules')->paginate(30);

return $stations;
}
Expand Down
6 changes: 4 additions & 2 deletions app/Http/Controllers/Auth/GoogleAuthenticationController.php
Expand Up @@ -25,13 +25,15 @@ public function callback()
return redirect('/login');
}

$password = Hash::make($user->token);

$user = User::firstOrCreate([
'email' => $user->getEmail(),
],[
'name' => $user->getName(),
'google_id' => $user->getId(),
'password' => Hash::make($user->token),
'default_password' => Hash::make($user->token),
'password' => $password,
'default_password' => $password,
'email_verified_at' => now()
]);

Expand Down
140 changes: 140 additions & 0 deletions app/Http/Controllers/OperationController.php
@@ -0,0 +1,140 @@
<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Models\Dropbox;
use App\Models\DropboxLog;
use App\Models\Station;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class OperationController extends Controller
{
use StationQueryTrait;

public function index(Request $request)
{
$this->authorize('viewAny', Station::class);

$station = new Station;

$station = $this->stationQuery($station, $request);

$stations = $station->with('dropboxes.activeLog')->paginate(5);

return view('operation.index', compact('stations'));
}

public function replace(Station $station, Request $request)
{
// Temporary
$this->authorize('update', $station);

$request->validate([
'dropbox_id' => ['required', Rule::exists('dropboxes', 'id')->where('station_id', $station->id)],
'empty_weight' => 'numeric|required',
'filled_weight' => 'numeric|nullable',
'timestamp' => 'date|required',
]);

$dropbox = $station->dropboxes()->find($request->dropbox_id);

if ($request->filled('filled_weight') && $dropbox->active_log_id) {
$this->createInspection($dropbox, $request->filled_weight, $request->timestamp, $request->user());
}

// Create a replacement dropbox log
// When a dropboxLog created, the related dropbox active_log_id will be automatically updated
$log = $dropbox->dropboxLogs()->create([
'activity' => 'replacement',
'weight' => $request->empty_weight,
'starts_at' => $request->timestamp,
'user_id' => $request->user()->id
]);

// Update station last_operation_at to now
$dropbox->station->update(['last_operation_at' => $request->timestamp]);

return $log;
}

public function show(Station $station)
{
$this->authorize('view', $station);

$dropboxes = $station->dropboxes()->with(['dropboxLogs' => function ($query) {
$query->orderBy('starts_at')->whereNull('parent_id')->with(['user', 'children' => function ($children)
{
$children->with('user')->orderBy('ends_at', 'asc');
}]);
}])->get();

// Uncomment line below to see dropboxes data structure
// return $dropboxes;

return view('operation.show', compact('dropboxes', 'station'));
}

public function inspect(Station $station, Request $request)
{
// Temporary
$this->authorize('update', $station);

$request->validate([
'dropbox_id' => ['required', Rule::exists('dropboxes', 'id')->where('station_id', $station->id)->whereNotNull('active_log_id')],
'filled_weight' => 'numeric|required',
'timestamp' => 'date|required',
]);

$dropbox = $station->dropboxes()->find($request->dropbox_id);

$log = $this->createInspection($dropbox, $request->filled_weight, $request->timestamp, $request->user());

// Update station last_operation_at to now
$dropbox->station->update(['last_operation_at' => $request->timestamp]);

return $log;
}

public function destroy(DropboxLog $dropboxLog)
{
$dropboxLog->load('dropbox.station');

$this->authorize('update', $dropboxLog->dropbox->station);

$dropboxLog->delete();

return response()->noContent();
}

/**
* Create a inspection typed log
*
* @param Dropbox $dropbox
* @param $weight
* @param $timestamp
* @return DropboxLog
*/
protected function createInspection(Dropbox $dropbox, $weight, $timestamp, User $user)
{
// Create a new dropbox log with parent_id = $dropbox->active_log_id, type = inspection
$log = $dropbox->dropboxLogs()->create([
'activity' => 'inspection',
'final_weight' => $weight,
'ends_at' => $timestamp,
'parent_id' => $dropbox->active_log_id,
'user_id' => $user->id
]);

$latestLog = $log->parent->children()->orderBy('ends_at', 'desc')->first();

// Update parent final_weight, ends_at
$log->parent->update([
'final_weight' => $latestLog->final_weight,
'ends_at' => $latestLog->ends_at,
]);

return $log;
}
}
63 changes: 63 additions & 0 deletions app/Http/Controllers/ProfileController.php
@@ -0,0 +1,63 @@
<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rule;

class ProfileController extends Controller
{
public function index(Request $request)
{
/** @var User */
$user = $request->user();

// Appending $user->has_no_password attribute
$user->append('has_no_password');

return view('auth.profile', compact('user'));;
}

public function update(Request $request)
{
/** @var User */
$user = $request->user();

$data = $request->validate([
'name' => 'string|max:255|min:3',
]);

$user->update($data);

return $user->fresh();
}

public function updatePassword(Request $request)
{
/** @var User */
$user = $request->user();

$request->validate([
'old_password' => [
Rule::requiredIf($user->has_no_password == false),
function ($attribute, $value, $fail) use ($user)
{
if (Hash::check($value, $user->password) == false) {
$fail('The ' . $attribute . ' is not match.');
}
}
],
'password' => 'required|string|min:8|confirmed'
]);

$user->update([
'password' => Hash::make($request->password)
]);

return response([
'success' => true
]);
}
}
16 changes: 16 additions & 0 deletions app/Http/Controllers/StaticPageController.php
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class StaticPageController extends Controller
{
public function about($lang = 'id')
{
App::setLocale($lang);

return view('about', compact('lang'));
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/StationDropboxController.php
Expand Up @@ -27,7 +27,7 @@ public function update(Request $request, Station $station, $dropbox)
{
$this->authorize('update', $station);
$dropbox = $station->dropboxes()->findOrFail($dropbox);

$data = $request->validate([
'model' => ['string', Rule::in(Dropbox::$availableModels)],
'color' => ['string', Rule::in(Dropbox::$availableColors)]
Expand Down
27 changes: 27 additions & 0 deletions app/Models/Dropbox.php
Expand Up @@ -10,6 +10,12 @@ class Dropbox extends Model
use HasFactory;

protected $guarded = ['id'];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['image_url'];

static public $availableModels = ['lubang bulat', 'lubang kotak'];
static public $availableColors = ['kuning', 'hijau'];
Expand All @@ -23,4 +29,25 @@ public function dropboxLogs()
{
return $this->hasMany('App\Models\DropboxLog');
}

public function activeLog()
{
return $this->belongsTo('App\Models\DropboxLog', 'active_log_id');
}

/**
* Generate Image Url from dropbox images resources
*
* @return string
*/
public function getImageUrlAttribute()
{
try {
$fileName = mix("dropboxes/{$this->color}_{$this->model}.jpg")->toHtml();
} catch (\Throwable $th) {
$fileName = null;
}

return $fileName;
}
}
38 changes: 36 additions & 2 deletions app/Models/DropboxLog.php
Expand Up @@ -10,9 +10,38 @@ class DropboxLog extends Model
use HasFactory;

protected $guarded = ['id'];
static public $availableActivities = ['replacement', 'deployment', 'inspection'];
protected $dates = ['starts_at', 'ends_at'];
protected $casts = [
'starts_at' => 'datetime:Y-m-d\TH:i:sP',
'ends_at' => 'datetime:Y-m-d\TH:i:sP',
];
static public $availableActivities = ['replacement', 'inspection'];

public function station()
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
// Listen to the created event
static::created(function ($dropboxLog) {
// Automatically update active_log_id on related Dropbox Model
if ($dropboxLog->activity == 'replacement') {
$dropboxLog->dropbox->update(['active_log_id' => $dropboxLog->id]);
}
});

// Listen to the deleted event
static::deleted(function ($dropboxLog) {
// If the parent gets deleted, the childern will be deleted too
if ($dropboxLog->parent_id == null) {
$dropboxLog->children()->delete();
}
});
}

public function dropbox()
{
return $this->belongsTo('App\Models\Dropbox');
}
Expand All @@ -26,4 +55,9 @@ public function parent()
{
return $this->belongsTo('App\Models\DropboxLog', 'parent_id');
}

public function user()
{
return $this->belongsTo('App\Models\User');
}
}
22 changes: 21 additions & 1 deletion app/Models/User.php
Expand Up @@ -26,6 +26,7 @@ class User extends Authenticatable
protected $hidden = [
'password',
'remember_token',
'default_password'
];

/**
Expand All @@ -40,7 +41,8 @@ class User extends Authenticatable

public static $permissions = [
'manage users',
'manage stations'
'manage stations',
'operate stations'
];

public function hasPermission($permission)
Expand All @@ -51,4 +53,22 @@ public function hasPermission($permission)

return in_array($permission, $this->permissions);
}

/**
* Users does not have password when they registered via Google
*
* @return bool
*/
public function getHasNoPasswordAttribute()
{
if ($this->google_id == null) {
return false;
}

if ($this->password == $this->default_password) {
return true;
}

return false;
}
}
Expand Up @@ -27,7 +27,7 @@ public function up()
public function down()
{
Schema::table('users', function (Blueprint $table) {
//
$table->dropColumn(['is_superadmin', 'permissions']);
});
}
}

0 comments on commit 414adbd

Please sign in to comment.