Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| GamificationRuleController | |
0.00% |
0 / 20 |
|
0.00% |
0 / 4 |
20 | |
0.00% |
0 / 1 |
| index | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| store | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| update | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| validated | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\Admin; |
| 4 | |
| 5 | use App\Enums\GamificationMetric; |
| 6 | use App\Enums\Status; |
| 7 | use App\Http\Controllers\Concerns\ResolvesContext; |
| 8 | use App\Http\Controllers\Controller; |
| 9 | use App\Models\Badge; |
| 10 | use App\Models\GamificationRule; |
| 11 | use Illuminate\Http\RedirectResponse; |
| 12 | use Illuminate\Http\Request; |
| 13 | use Illuminate\View\View; |
| 14 | |
| 15 | class GamificationRuleController extends Controller |
| 16 | { |
| 17 | use ResolvesContext; |
| 18 | |
| 19 | public function index(): View |
| 20 | { |
| 21 | return view('admin.gamification.index', [ |
| 22 | 'rules' => GamificationRule::with('badge')->where('store_id', $this->currentStore()->id)->orderBy('threshold_value')->get(), |
| 23 | 'badges' => Badge::orderBy('sort_order')->get(), |
| 24 | 'metrics' => GamificationMetric::cases(), |
| 25 | ]); |
| 26 | } |
| 27 | |
| 28 | public function store(Request $request): RedirectResponse |
| 29 | { |
| 30 | $data = $this->validated($request); |
| 31 | $store = $this->currentStore(); |
| 32 | GamificationRule::create($data + ['tenant_id' => $store->tenant_id, 'store_id' => $store->id, 'status' => Status::Active]); |
| 33 | return back()->with('success', 'Regra criada.'); |
| 34 | } |
| 35 | |
| 36 | public function update(Request $request, GamificationRule $gamification): RedirectResponse |
| 37 | { |
| 38 | $gamification->update($this->validated($request)); |
| 39 | return back()->with('success', 'Regra atualizada.'); |
| 40 | } |
| 41 | |
| 42 | private function validated(Request $request): array |
| 43 | { |
| 44 | return $request->validate([ |
| 45 | 'badge_id' => ['nullable', 'exists:badges,id'], |
| 46 | 'name' => ['required', 'string', 'max:255'], |
| 47 | 'metric' => ['required', 'string'], |
| 48 | 'threshold_value' => ['required', 'numeric', 'min:0'], |
| 49 | 'bonus_percentage' => ['required', 'numeric', 'min:0', 'max:100'], |
| 50 | 'starts_at' => ['nullable', 'date'], |
| 51 | 'ends_at' => ['nullable', 'date', 'after_or_equal:starts_at'], |
| 52 | ]); |
| 53 | } |
| 54 | } |