Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ShiftsController
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 history
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 deleteTransaction
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Controllers\Admin;
6
7use Engelsystem\Controllers\BaseController;
8use Engelsystem\Controllers\HasUserNotifications;
9use Engelsystem\Http\Redirector;
10use Engelsystem\Http\Request;
11use Engelsystem\Http\Response;
12use Engelsystem\Models\Shifts\Shift;
13use Illuminate\Database\Eloquent\Collection;
14use Psr\Log\LoggerInterface;
15
16class ShiftsController extends BaseController
17{
18    use HasUserNotifications;
19
20    /** @var array<string> */
21    protected array $permissions = [
22        'admin_shifts',
23    ];
24
25    public function __construct(
26        protected LoggerInterface $log,
27        protected Shift $shift,
28        protected Redirector $redirect,
29        protected Response $response
30    ) {
31    }
32
33    public function history(): Response
34    {
35        $shifts = $this->shift
36            ->select()
37            ->selectRaw('MIN(start) AS start')
38            ->selectRaw('MAX(end) AS end')
39            ->selectRaw('COUNT(*) AS count')
40            ->selectRaw('MIN(created_at) AS created_at')
41            ->with(['schedule', 'createdBy'])
42            ->whereNotNull('transaction_id')
43            ->groupBy('transaction_id')
44            ->orderByDesc('created_at')
45            ->get();
46        return $this->response->withView('admin/shifts/history', ['shifts' => $shifts]);
47    }
48
49    public function deleteTransaction(Request $request): Response
50    {
51        $transactionId = $request->postData('transaction_id');
52
53        /** @var Shift[]|Collection $shifts */
54        $shifts = $this->shift->with([
55            'location',
56            'shiftEntries',
57            'shiftEntries.angelType',
58            'shiftEntries.user',
59            'shiftType',
60        ])->where('transaction_id', $transactionId)->get();
61
62        $this->log->info(
63            'Deleting {count} shifts with transaction ID: {id}',
64            ['count' => $shifts->count(), 'id' => $transactionId]
65        );
66
67        foreach ($shifts as $shift) {
68            event('shift.deleting', ['shift' => $shift]);
69            $shift->delete();
70
71            $this->log->info(
72                'Deleted shift ' . $shift->title . ' / ' . $shift->shiftType->name
73                . ' from ' . $shift->start->format('Y-m-d H:i')
74                . ' to ' . $shift->end->format('Y-m-d H:i')
75            );
76        }
77
78        $this->addNotification('shifts.history.delete.success');
79
80        return $this->redirect->back();
81    }
82}