Skip to content

Commit

Permalink
Merge branch 'release/0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
w1lldone committed Jun 7, 2021
2 parents a3c0c84 + cc0b736 commit 8210274
Show file tree
Hide file tree
Showing 27 changed files with 1,311 additions and 54 deletions.
7 changes: 7 additions & 0 deletions app/Http/Controllers/Auth/AuthenticatedSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public function store(LoginRequest $request)

$request->session()->regenerate();

if ($request->wantsJson()) {
return response()->json([
'succeed' => true,
'redirect_url' => redirect()->intended(RouteServiceProvider::HOME)->getTargetUrl()
]);
}

return redirect()->intended(RouteServiceProvider::HOME);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public function callback()

Auth::login($user, true);

return redirect('/');
return redirect()->intended(RouteServiceProvider::HOME);
}
}
28 changes: 25 additions & 3 deletions app/Http/Controllers/OperationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,33 @@ public function index(Request $request)

$station = new Station;

$station = $this->stationQuery($station, $request);
$station = $this->stationQuery($station, $request)->with(['dropboxes.activeLog'])->withCount([
'reports' => function ($report) {
$report->whereNull('resolved_at');
}
]);

$sort = $this->sortIsAllowed($request->sort) ? $request->sort : array_keys(Station::$sorts)[0];
$orderBy = explode(".", $sort);

$stations = $station->orderBy($orderBy[0], $orderBy[1])->paginate(5)->appends($request->all());

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

$stations = $station->with('dropboxes.activeLog')->paginate(5);
/**
* Determine wheater the sort parameter is alllowed
*
* @param string $sort
* @return bool
*/
protected function sortIsAllowed(string $sort = null)
{
if (!$sort) {
return false;
}

return view('operation.index', compact('stations'));
return key_exists($sort, Station::$sorts);
}

public function replace(Station $station, Request $request)
Expand Down
94 changes: 94 additions & 0 deletions app/Http/Controllers/StationReportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Http\Controllers;

use App\Http\Resources\StationMediaResource;
use App\Models\Report;
use App\Models\Station;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class StationReportController extends Controller
{
public function index(Station $station, Request $request)
{
$this->authorize('viewReport', $station);

$report = $station->reports();

if ($request->show_all == false) {
$report = $report->whereNull('resolved_at');
}

$reports = $report->with('reporter')->latest()->get();

return view('reports.index', compact('reports', 'station'));
}

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

$report = $station->reports()->findOrFail($report);

return $report->load('reporter', 'media')->append('photo')->makeHidden('media');
}

public function create(Station $station)
{
// return Report::getConditions();
return view('reports.create', compact('station'));
}

public function store(Station $station, Request $request)
{
$request->validate([
'condition' => [Rule::in(Report::getConditions()), 'required'],
'user_latitude' => 'numeric|between:-90,90|nullable',
'user_longitude' => 'numeric|between:-180,180|nullable',
'photo' => 'file|image|nullable',
]);

/** @var Report */
$report = $station->reports()->create([
'condition' => $request->condition,
'user_latitude' => $request->user_latitude,
'user_longitude' => $request->user_longitude,
'reporter_id' => $request->user()->id,
]);

if ($request->file('photo')) {
$report->addMediaFromRequest('photo')->toMediaCollection();
}

return $report->load('media')->append('photo')->makeHidden('media');
}

public function resolve(Station $station, Request $request)
{
$this->authorize('resolveReport', $station);

$request->validate([
'resolve_all' => 'boolean',
'report_id' => 'array|required_unless:resolve_all,true',
'report_id.*' => 'integer',
]);

$report = $station->reports();

if ($request->resolve_all == true) {
$report = $report->whereNull('resolved_at');
} else {
$report = $report->whereIn('id', $request->report_id);
}

$report->update([
'resolved_at' => now(),
'resolver_id' => $request->user()->id,
]);

return response([
'success' => true
]);
}
}
55 changes: 55 additions & 0 deletions app/Models/Report.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Models;

use App\Http\Resources\StationMediaResource;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;

class Report extends Model implements HasMedia
{
use HasFactory, HasMediaTrait;

public static $conditions = [
'full' => 'Dropbox Penuh',
'missing' => 'Dropbox Hilang/Tidak Pada Tempatnya',
'damaged' => 'Dropbox Rusak'
];

protected $guarded = ['id'];
protected $dates = ['resolved_at'];
protected $casts = [
'resolved_at' => 'datetime:Y-m-d\TH:i:sP',
];

public static function getConditions()
{
return array_keys(self::$conditions);
}

public function station()
{
return $this->belongsTo('App\Models\Station');
}

public function reporter()
{
return $this->belongsTo('App\Models\User', 'reporter_id');
}

public function resolver()
{
return $this->belongsTo('App\Models\User', 'resolver_id');
}

public function getPhotoAttribute()
{
if ($media = $this->getFirstMedia()) {
return new StationMediaResource($media);
}

return null;
}
}
11 changes: 11 additions & 0 deletions app/Models/Station.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class Station extends Model implements HasMedia
protected $casts = [
'last_operation_at' => 'datetime:Y-m-d\TH:i:sP',
];
public static $sorts = [
'id.asc' => 'Dibuat (Lama - Baru)',
'id.desc' => 'Dibuat (Baru - Lama)',
'reports_count.desc' => 'Laporan Terbanyak',
'last_operation_at.asc' => 'Pengukuran Terakhir'
];

public function registerMediaCollections(): void
{
Expand All @@ -35,6 +41,11 @@ public function schedules()
return $this->hasMany('App\Models\Schedule');
}

public function reports()
{
return $this->hasMany('App\Models\Report');
}

/**
* Scope a query to include distance of the given coordincates
*
Expand Down
24 changes: 23 additions & 1 deletion app/Policies/StationPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function before(User $user, $ability)
*/
public function viewAny(User $user)
{

}

/**
Expand Down Expand Up @@ -85,4 +85,26 @@ public function delete(User $user, Station $station)
{
//
}

public function viewReport(User $user, Station $station)
{
if ($user->hasPermission('manage stations')) {
return true;
}

if ($user->hasPermission('operation stations')) {
return true;
}
}

public function resolveReport(User $user, Station $station)
{
if ($user->hasPermission('manage stations')) {
return true;
}

if ($user->hasPermission('operation stations')) {
return true;
}
}
}
27 changes: 24 additions & 3 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -60,5 +61,25 @@ protected function configureRateLimiting()
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
});

RateLimiter::for('report.store', function (Request $request)
{
if ($request->user()->is_superadmin) {
return Limit::none();
}

if ($request->user()->hasPermission('manage stations')) {
return Limit::none();
}

if ($request->user()->hasPermission('operate stations')) {
return Limit::none();
}

return Limit::perMinutes(3, 5)->by($request->user()->id)->response(function () use ($request)
{
return response("Maximum attempts reached. Please try again in few minutes", 429);
});
});
}
}
36 changes: 36 additions & 0 deletions database/factories/ReportFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Database\Factories;

use App\Models\Report;
use App\Models\Station;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

class ReportFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Report::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'station_id' => Station::factory(),
'reporter_id' => User::factory(),
'user_latitude' => $this->faker->latitude,
'user_longitude' => $this->faker->longitude,
'condition' => $this->faker->randomElement(Report::getConditions()),
'resolved_at' => null,
'resolver_id' => null,
];
}
}
38 changes: 38 additions & 0 deletions database/migrations/2021_05_18_073320_create_reports_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateReportsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('reports', function (Blueprint $table) {
$table->id();
$table->foreignId('station_id')->index();
$table->foreignId('reporter_id')->index();
$table->string('user_latitude')->nullable();
$table->string('user_longitude')->nullable();
$table->string('condition')->nullable();
$table->timestamp('resolved_at')->nullable();
$table->foreignId('resolver_id')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('reports');
}
}
Loading

0 comments on commit 8210274

Please sign in to comment.