Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 106 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ZeusSyncController | |
0.00% |
0 / 106 |
|
0.00% |
0 / 8 |
812 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| upsertQuotation | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 | |||
| uploadQuotationFile | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
20 | |||
| deleteQuotation | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
20 | |||
| mapResult | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| mapUploadResult | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
| resultToAction | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
90 | |||
| authUserId | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers; |
| 4 | |
| 5 | use App\Models\TblAuthUsers; |
| 6 | use App\Models\TblZeusInboundLog; |
| 7 | use App\Services\ZeusSyncService; |
| 8 | use Illuminate\Contracts\Routing\ResponseFactory; |
| 9 | use Illuminate\Http\Request; |
| 10 | use Illuminate\Http\Response as HttpResponse; |
| 11 | |
| 12 | /** |
| 13 | * FIRE-1733 — Zeus → Titan sync endpoints. |
| 14 | * |
| 15 | * Two thin adapters: they parse the request, hand the payload to |
| 16 | * ZeusSyncService, log the outcome, and map the service result to an HTTP |
| 17 | * response. All business logic lives in the service; the controller does not |
| 18 | * touch tbl_quotations directly. |
| 19 | * |
| 20 | * Middleware chain (see routes/api.php): |
| 21 | * auth.token — validates the bearer token, populates $request->user() |
| 22 | * zeus.inbound — 503 if disabled, 403 if the caller is not the configured |
| 23 | * Zeus consumer, flags $request->attributes['is_from_zeus'] |
| 24 | * so ZeusBridge never re-forwards this write. |
| 25 | */ |
| 26 | class ZeusSyncController extends Controller |
| 27 | { |
| 28 | public function __construct(private ZeusSyncService $sync) {} |
| 29 | |
| 30 | /** |
| 31 | * `POST /api/zeus/upsertQuotation` — idempotent create-or-update on |
| 32 | * `zeus_id`. See docs/zeus-inbound-contract.md for the full contract. |
| 33 | */ |
| 34 | public function upsertQuotation(Request $request): ResponseFactory|HttpResponse |
| 35 | { |
| 36 | $startedAt = microtime(true); |
| 37 | $payload = $request->all(); |
| 38 | $rawBody = $request->getContent(); |
| 39 | |
| 40 | $result = $this->sync->upsert($payload); |
| 41 | |
| 42 | [$httpStatus, $body] = $this->mapResult($result); |
| 43 | |
| 44 | $this->sync->log( |
| 45 | authUserId: $this->authUserId($request), |
| 46 | endpoint: TblZeusInboundLog::ENDPOINT_UPSERT, |
| 47 | zeusId: $result['zeus_id'], |
| 48 | quotationId: $result['titan_id'], |
| 49 | action: $this->resultToAction($result['result']), |
| 50 | httpStatus: $httpStatus, |
| 51 | rawPayload: $rawBody, |
| 52 | zeusUpdatedAt: is_string($payload['zeus_updated_at'] ?? null) ? $payload['zeus_updated_at'] : null, |
| 53 | error: $result['errors'] === [] ? null : json_encode($result['errors']), |
| 54 | durationMs: (int) round((microtime(true) - $startedAt) * 1000), |
| 55 | ); |
| 56 | |
| 57 | return response($body, $httpStatus); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * `POST /api/zeus/uploadQuotationFile` — attach one or more files to an |
| 62 | * existing quote. multipart/form-data, files under `files[]`, identity via |
| 63 | * `zeus_id` or `titan_id`, optional `uploaded_by` string. |
| 64 | * |
| 65 | * FIRE-1733 comment 27842: Zeus needs a way to sync attachments alongside |
| 66 | * the JSON payload. Kept as a separate endpoint (not folded into upsert) |
| 67 | * because Titan already uses multipart for its own uploads — reusing that |
| 68 | * pattern is simpler than shoehorning base64 into the JSON contract. |
| 69 | */ |
| 70 | public function uploadQuotationFile(Request $request): ResponseFactory|HttpResponse |
| 71 | { |
| 72 | $startedAt = microtime(true); |
| 73 | $payload = $request->all(); |
| 74 | |
| 75 | // Accept both `files[]` (Titan's convention) and single-file `file`. |
| 76 | $files = $request->file('files') ?? []; |
| 77 | if (! is_array($files)) { |
| 78 | $files = [$files]; |
| 79 | } |
| 80 | if ($request->hasFile('file')) { |
| 81 | $files[] = $request->file('file'); |
| 82 | } |
| 83 | |
| 84 | $result = $this->sync->uploadFiles($payload, $files); |
| 85 | |
| 86 | [$httpStatus, $body] = $this->mapUploadResult($result); |
| 87 | |
| 88 | $this->sync->log( |
| 89 | authUserId: $this->authUserId($request), |
| 90 | endpoint: TblZeusInboundLog::ENDPOINT_UPLOAD_FILE, |
| 91 | zeusId: $result['zeus_id'], |
| 92 | quotationId: $result['titan_id'], |
| 93 | action: $this->resultToAction($result['result']), |
| 94 | httpStatus: $httpStatus, |
| 95 | // Multipart body itself is large binary; log a placeholder pointer |
| 96 | // instead of the file bytes so tbl_zeus_inbound_log stays sane. |
| 97 | rawPayload: json_encode(array_merge( |
| 98 | array_diff_key($payload, array_flip(['file', 'files'])), |
| 99 | ['_files_count' => $result['uploaded'] ?? 0], |
| 100 | )), |
| 101 | zeusUpdatedAt: null, |
| 102 | error: $result['errors'] === [] ? null : json_encode($result['errors']), |
| 103 | durationMs: (int) round((microtime(true) - $startedAt) * 1000), |
| 104 | ); |
| 105 | |
| 106 | return response($body, $httpStatus); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * `POST /api/zeus/deleteQuotation` — soft delete resolved by `titan_id`. |
| 111 | */ |
| 112 | public function deleteQuotation(Request $request): ResponseFactory|HttpResponse |
| 113 | { |
| 114 | $startedAt = microtime(true); |
| 115 | $payload = $request->all(); |
| 116 | $rawBody = $request->getContent(); |
| 117 | |
| 118 | $result = $this->sync->delete($payload); |
| 119 | |
| 120 | $httpStatus = 200; |
| 121 | $body = [ |
| 122 | 'status' => $result['result'], |
| 123 | 'titan_id' => $result['titan_id'], |
| 124 | ]; |
| 125 | |
| 126 | $this->sync->log( |
| 127 | authUserId: $this->authUserId($request), |
| 128 | endpoint: TblZeusInboundLog::ENDPOINT_DELETE, |
| 129 | // Logged for correlation only — the row is resolved by titan_id. |
| 130 | zeusId: is_numeric($payload['zeus_id'] ?? null) ? (int) $payload['zeus_id'] : null, |
| 131 | quotationId: $result['titan_id'], |
| 132 | action: $this->resultToAction($result['result']), |
| 133 | httpStatus: $httpStatus, |
| 134 | rawPayload: $rawBody, |
| 135 | zeusUpdatedAt: is_string($payload['zeus_deleted_at'] ?? null) ? $payload['zeus_deleted_at'] : null, |
| 136 | error: $result['errors'] === [] ? null : json_encode($result['errors']), |
| 137 | durationMs: (int) round((microtime(true) - $startedAt) * 1000), |
| 138 | ); |
| 139 | |
| 140 | return response($body, $httpStatus); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Map the service result to an HTTP status + response body. |
| 145 | * |
| 146 | * @param array{result: string, titan_id: ?int, zeus_id: ?int, errors: array<string, array<int, string>>} $result |
| 147 | * @return array{0: int, 1: array<string, mixed>} |
| 148 | */ |
| 149 | private function mapResult(array $result): array |
| 150 | { |
| 151 | if ($result['result'] === ZeusSyncService::RESULT_REJECTED) { |
| 152 | return [422, [ |
| 153 | 'status' => 'rejected', |
| 154 | 'errors' => $result['errors'], |
| 155 | ]]; |
| 156 | } |
| 157 | |
| 158 | $body = [ |
| 159 | 'status' => $result['result'], |
| 160 | 'titan_id' => $result['titan_id'], |
| 161 | 'zeus_id' => $result['zeus_id'], |
| 162 | ]; |
| 163 | |
| 164 | return [200, $body]; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @param array{result: string, titan_id: ?int, zeus_id: ?int, uploaded: int, errors: array<string, array<int, string>>} $result |
| 169 | * @return array{0: int, 1: array<string, mixed>} |
| 170 | */ |
| 171 | private function mapUploadResult(array $result): array |
| 172 | { |
| 173 | if ($result['result'] === ZeusSyncService::RESULT_REJECTED) { |
| 174 | return [422, [ |
| 175 | 'status' => 'rejected', |
| 176 | 'errors' => $result['errors'], |
| 177 | ]]; |
| 178 | } |
| 179 | if ($result['result'] === ZeusSyncService::RESULT_SKIPPED_NOT_FOUND) { |
| 180 | return [404, [ |
| 181 | 'status' => 'skipped_not_found', |
| 182 | 'titan_id' => $result['titan_id'], |
| 183 | 'zeus_id' => $result['zeus_id'], |
| 184 | ]]; |
| 185 | } |
| 186 | |
| 187 | return [200, [ |
| 188 | 'status' => $result['result'], |
| 189 | 'titan_id' => $result['titan_id'], |
| 190 | 'zeus_id' => $result['zeus_id'], |
| 191 | 'uploaded' => $result['uploaded'], |
| 192 | ]]; |
| 193 | } |
| 194 | |
| 195 | private function resultToAction(string $serviceResult): string |
| 196 | { |
| 197 | return match ($serviceResult) { |
| 198 | ZeusSyncService::RESULT_CREATED => TblZeusInboundLog::ACTION_CREATED, |
| 199 | ZeusSyncService::RESULT_UPDATED => TblZeusInboundLog::ACTION_UPDATED, |
| 200 | ZeusSyncService::RESULT_DELETED => TblZeusInboundLog::ACTION_DELETED, |
| 201 | ZeusSyncService::RESULT_FILES_UPLOADED => TblZeusInboundLog::ACTION_FILES_UPLOADED, |
| 202 | ZeusSyncService::RESULT_SKIPPED_STALE => TblZeusInboundLog::ACTION_SKIPPED_STALE, |
| 203 | ZeusSyncService::RESULT_SKIPPED_NOT_FOUND => TblZeusInboundLog::ACTION_SKIPPED_NOT_FOUND, |
| 204 | ZeusSyncService::RESULT_REJECTED => TblZeusInboundLog::ACTION_REJECTED, |
| 205 | default => TblZeusInboundLog::ACTION_REJECTED, |
| 206 | }; |
| 207 | } |
| 208 | |
| 209 | private function authUserId(Request $request): ?int |
| 210 | { |
| 211 | $user = $request->user(); |
| 212 | |
| 213 | return $user instanceof TblAuthUsers ? (int) $user->id : null; |
| 214 | } |
| 215 | } |