Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
12.50% covered (danger)
12.50%
5 / 40
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
AdministratorsController
12.50% covered (danger)
12.50%
5 / 40
20.00% covered (danger)
20.00%
1 / 5
126.22
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 list_administrators
40.00% covered (danger)
40.00%
2 / 5
0.00% covered (danger)
0.00%
0 / 1
2.86
 create_administrator
22.22% covered (danger)
22.22%
2 / 9
0.00% covered (danger)
0.00%
0 / 1
7.23
 update_administrator
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 delete_administrator
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace App\Http\Controllers;
4
5use App\Models\Administrator;
6use App\Models\Client;
7use Illuminate\Support\Facades\App;
8use App\Exceptions\AppException;
9use App\Http\Requests\StoreAdministratorRequest;
10use App\Http\Requests\UpdateAdministratorRequest;
11use App\Http\Resources\AdministratorResource;
12use App\Http\Controllers\Concerns\ChecksRole;
13
14class AdministratorsController extends Controller
15{
16    use ChecksRole;
17
18    public function __construct()
19    {
20        App::setLocale(request()->header('Locale-Id'));
21    }
22
23    /**
24     * GET /administrators
25     * List all administrators with their assigned commercial.
26     */
27    function list_administrators()
28    {
29        try {
30            $data = Administrator::with('assignedCommercial')->orderBy('name')->get();
31
32            return AdministratorResource::collection($data);
33
34        } catch (\Exception $e) {
35            report(AppException::fromException($e, 'LIST_ADMINISTRATORS_EXCEPTION'));
36            return response(['message' => 'KO', 'error' => $e->getMessage()]);
37        }
38    }
39
40    /**
41     * POST /administrators
42     * Create a new administrator.
43     */
44    function create_administrator(StoreAdministratorRequest $request)
45    {
46        if (!$this->canManage()) {
47            return $this->forbidden();
48        }
49
50        try {
51            $data = $request->validated();
52
53            Administrator::create($data);
54
55            $result = Administrator::with('assignedCommercial')->orderBy('name')->get();
56
57            return AdministratorResource::collection($result);
58
59        } catch (\Exception $e) {
60            report(AppException::fromException($e, 'CREATE_ADMINISTRATOR_EXCEPTION'));
61            return response(['message' => 'KO', 'error' => $e->getMessage()]);
62        }
63    }
64
65    /**
66     * PUT /administrators/{id}
67     * Update an administrator.
68     */
69    function update_administrator(UpdateAdministratorRequest $request, $id)
70    {
71        if (!$this->canManage()) {
72            return $this->forbidden();
73        }
74
75        try {
76            $id = (int) $id;
77            $data = $request->validated();
78
79            Administrator::where('id', $id)->update($data);
80
81            $result = Administrator::with('assignedCommercial')->orderBy('name')->get();
82
83            return AdministratorResource::collection($result);
84
85        } catch (\Exception $e) {
86            report(AppException::fromException($e, 'UPDATE_ADMINISTRATOR_EXCEPTION'));
87            return response(['message' => 'KO', 'error' => $e->getMessage()]);
88        }
89    }
90
91    /**
92     * DELETE /administrators/{id}
93     * Delete only if not in use by any client.
94     */
95    function delete_administrator($id)
96    {
97        if (!$this->canDelete()) {
98            return $this->forbidden();
99        }
100
101        try {
102            $id = (int) $id;
103
104            $count = Client::where('administrator_id', $id)->count();
105
106            if ($count > 0) {
107                return response([
108                    'message' => 'KO',
109                    'error' => "Cannot delete because it is in use by {$count} client(s).",
110                ]);
111            }
112
113            Administrator::where('id', $id)->delete();
114
115            $result = Administrator::with('assignedCommercial')->orderBy('name')->get();
116
117            return AdministratorResource::collection($result);
118
119        } catch (\Exception $e) {
120            report(AppException::fromException($e, 'DELETE_ADMINISTRATOR_EXCEPTION'));
121            return response(['message' => 'KO', 'error' => $e->getMessage()]);
122        }
123    }
124}