Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 53 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| RegionEmailFlagsController | |
0.00% |
0 / 53 |
|
0.00% |
0 / 2 |
210 | |
0.00% |
0 / 1 |
| getFlags | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
42 | |||
| setFlags | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
72 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers; |
| 4 | |
| 5 | use App\Exceptions\AppException; |
| 6 | use App\Models\TblCompanies; |
| 7 | use App\Services\SendgridDispatcher; |
| 8 | use Illuminate\Http\Request; |
| 9 | use Illuminate\Support\Facades\Log; |
| 10 | |
| 11 | /** |
| 12 | * FIRE-1930 — per-region kill switch for the four email buckets in Berta's |
| 13 | * checklist (follow-up reminders, approval requests + reminders, commercial |
| 14 | * catch-all notifications, client-facing emails). |
| 15 | * |
| 16 | * Mirrors the invoice_reminder_active precedent in FacturasController: flags |
| 17 | * stored per-company on tbl_companies, toggled per-region via the Region |
| 18 | * header. A region is "off" for a bucket when every company in it has 0; |
| 19 | * partial states display as off so an admin never sees a green switch that |
| 20 | * silently masks a still-sending company. |
| 21 | */ |
| 22 | class RegionEmailFlagsController extends Controller |
| 23 | { |
| 24 | private const BUCKET_COLUMNS = SendgridDispatcher::ALL_BUCKETS; |
| 25 | |
| 26 | public function getFlags() |
| 27 | { |
| 28 | try { |
| 29 | $region = urldecode((string) request()->header('Region')); |
| 30 | |
| 31 | if (! $region) { |
| 32 | throw new \Exception('No region provided'); |
| 33 | } |
| 34 | |
| 35 | $scope = fn () => $region === 'All' |
| 36 | ? TblCompanies::query() |
| 37 | : TblCompanies::where('region', $region); |
| 38 | |
| 39 | $total = $scope()->count(); |
| 40 | |
| 41 | // Empty scope (region with no companies, or 'All' before any |
| 42 | // company exists) reports every bucket as off. Same defensive |
| 43 | // stance as FacturasController::getInvoiceReminderActive — the |
| 44 | // switch answers "off" whenever the state can't be conclusively |
| 45 | // "on for every company". |
| 46 | $response = ['success' => true]; |
| 47 | |
| 48 | foreach (self::BUCKET_COLUMNS as $column) { |
| 49 | $active = $scope()->where($column, 1)->count(); |
| 50 | $response[$column] = $total > 0 && $active === $total; |
| 51 | } |
| 52 | |
| 53 | return response()->json($response); |
| 54 | } catch (\Exception $e) { |
| 55 | report(AppException::fromException($e, 'GET_REGION_EMAIL_FLAGS_EXCEPTION')); |
| 56 | Log::channel('email_log')->error('Failed to read region email flags: '.$e->getMessage()); |
| 57 | |
| 58 | return response()->json([ |
| 59 | 'success' => false, |
| 60 | 'message' => $e->getMessage(), |
| 61 | ], 500); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public function setFlags(Request $request) |
| 66 | { |
| 67 | try { |
| 68 | $region = urldecode((string) request()->header('Region')); |
| 69 | |
| 70 | if (! $region) { |
| 71 | throw new \Exception('No region provided'); |
| 72 | } |
| 73 | |
| 74 | // Only accept known bucket columns from the payload — a typo or |
| 75 | // an attempt to update an unrelated column on tbl_companies is |
| 76 | // dropped silently. Keeps the endpoint from being repurposed as |
| 77 | // a generic writer to the companies table. |
| 78 | $updates = []; |
| 79 | foreach (self::BUCKET_COLUMNS as $column) { |
| 80 | if ($request->has($column)) { |
| 81 | $updates[$column] = $request->boolean($column) ? 1 : 0; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (empty($updates)) { |
| 86 | return response()->json([ |
| 87 | 'success' => false, |
| 88 | 'message' => 'No known bucket flags in request body', |
| 89 | ], 400); |
| 90 | } |
| 91 | |
| 92 | $scope = $region === 'All' |
| 93 | ? TblCompanies::query() |
| 94 | : TblCompanies::where('region', $region); |
| 95 | |
| 96 | $affected = $scope->update($updates); |
| 97 | |
| 98 | Log::channel('email_log')->warning('Region email flags updated', [ |
| 99 | 'region' => $region, |
| 100 | 'updates' => $updates, |
| 101 | 'affected_companies' => $affected, |
| 102 | 'actor' => request()->user()?->name ?? 'unknown', |
| 103 | ]); |
| 104 | |
| 105 | return response()->json([ |
| 106 | 'success' => true, |
| 107 | 'affected_companies' => $affected, |
| 108 | 'updated' => $updates, |
| 109 | ]); |
| 110 | } catch (\Exception $e) { |
| 111 | report(AppException::fromException($e, 'SET_REGION_EMAIL_FLAGS_EXCEPTION')); |
| 112 | Log::channel('email_log')->error('Failed to set region email flags: '.$e->getMessage()); |
| 113 | |
| 114 | return response()->json([ |
| 115 | 'success' => false, |
| 116 | 'message' => $e->getMessage(), |
| 117 | ], 500); |
| 118 | } |
| 119 | } |
| 120 | } |