- /api/v1/maryker/order/unprocessed és setProcessed, auth:sanctum + maryker:orders ability mögött - a szűrés a token tulajdonosának supplier_id-jából jön, konfigurálható oldalméret - APICallBackId az első lekéréskor generálódik és stabil; írás toBase()-en (nincs updated_by szivárgás) - setProcessed: kötelező, validált tömb, 422 hibás bemenetre - maryker:api-token parancs szerepkör nélküli API-felhasználóval - 401 JSON a Mary-Ker útvonalakon (a Hooreyca-útvonalak viselkedése változatlan) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
151 lines
6.2 KiB
PHP
151 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\api\v1;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\OrderArchive;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Mary-Ker (Apolló) rendeléslekérő és visszaigazoló API (EV3-461).
|
|
*
|
|
* A Hooreyca-mintától (OrderController) szándékosan elkülönített: saját útvonal, saját token. A szűrés
|
|
* a token tulajdonosának supplier_id-jából jön, így a token más beszállító rendeléséhez nem fér hozzá.
|
|
*
|
|
* Az order_archives írásai szándékosan toBase()-en mennek: nincs modell-esemény, így nem íródik át
|
|
* az updated_by / updated_at az API-felhasználóra, és nincs mit kiszivárogtatni a válaszba.
|
|
*/
|
|
class MaryKerOrderController extends Controller
|
|
{
|
|
/**
|
|
* Feldolgozatlan rendelések (confirmed nem null, APIConfirmed null), régi elöl, lapozva.
|
|
*
|
|
* Idempotens a találati halmaz szempontjából: a lista csak az APIConfirmed-től függ, ezért ugyanazt
|
|
* adja, amíg a setProcessed meg nem történik. Az APICallBackId-t csak az első lekérésnél generálja,
|
|
* utána stabil. Az egyetlen mellékhatás az APILastGetting bélyeg.
|
|
*/
|
|
public function unProcessed(Request $request): JsonResponse
|
|
{
|
|
$updateDate = Carbon::now();
|
|
$header = $this->header($updateDate);
|
|
|
|
$supplierId = $request->user()?->supplier_id;
|
|
if (! $supplierId) {
|
|
return $this->forbidden($header);
|
|
}
|
|
|
|
$page = OrderArchive::query()
|
|
->select(['id', 'humanId', 'confirmed', 'sumPrice', 'deliveryDate', 'note'])
|
|
->where('supplier_id', $supplierId)
|
|
->whereNotNull('confirmed')
|
|
->whereNull('APIConfirmed')
|
|
->orderBy('id')
|
|
->paginate(max(1, (int) config('maryker.per_page')));
|
|
|
|
$ids = $page->getCollection()->pluck('id');
|
|
$callbackIds = $this->ensureCallbackIds($ids->all());
|
|
|
|
OrderArchive::query()->toBase()->whereIn('id', $ids)->update(['APILastGetting' => $updateDate]);
|
|
|
|
// TODO 1.2: profitCenter (sellerProfitCenterId, szállítási alegység, cím) és items (sellerItemNumber, sumPrice ...)
|
|
$page->setCollection($page->getCollection()->map(fn (OrderArchive $order) => [
|
|
'orderId' => $order->humanId,
|
|
'APICallBackId' => $callbackIds[$order->id],
|
|
'confirmedAt' => $order->confirmed,
|
|
'orderSum' => $order->sumPrice,
|
|
'orderDeliveryDate' => $order->deliveryDate,
|
|
'orderDescription' => (string) $order->note,
|
|
]));
|
|
|
|
Log::channel('api')->info('MaryKer unprocessed', [
|
|
'userId' => $request->user()->id, 'page' => $page->currentPage(), 'orderIds' => $ids->all(),
|
|
]);
|
|
|
|
return responder()->success($page)->respond(null, $header);
|
|
}
|
|
|
|
/**
|
|
* Visszaigazolás: az APICallBackId-k alapján beállítja az APIConfirmed-et.
|
|
* Kötelezően nem üres tömb; egy elem csak akkor "updated", ha a token beszállítójáé, lekért és még
|
|
* nem visszaigazolt rendeléshez tartozik.
|
|
*/
|
|
public function setProcessed(Request $request): JsonResponse
|
|
{
|
|
$updateDate = Carbon::now();
|
|
$header = $this->header($updateDate);
|
|
|
|
$supplierId = $request->user()?->supplier_id;
|
|
if (! $supplierId) {
|
|
return $this->forbidden($header);
|
|
}
|
|
|
|
// A kliens nem biztos, hogy JSON Content-Type-ot küld (a Hooreyca sem teszi), ezért a nyers törzset olvassuk.
|
|
$body = json_decode($request->getContent(), true);
|
|
$validator = Validator::make(is_array($body) ? $body : [], [
|
|
'APICallBackId' => ['required', 'array', 'min:1', 'max:' . (int) config('maryker.set_processed_max')],
|
|
'APICallBackId.*' => ['required', 'string', 'max:36', 'distinct'],
|
|
]);
|
|
if (! is_array($body) || $validator->fails()) {
|
|
$message = is_array($body) ? $validator->errors()->first() : 'A kérés törzse nem érvényes JSON objektum.';
|
|
Log::channel('api')->warning('MaryKer setProcessed invalid data', ['userId' => $request->user()->id, 'message' => $message]);
|
|
|
|
return responder()->error('invalid data', $message)->respond(422, $header);
|
|
}
|
|
|
|
$result = [];
|
|
foreach ($body['APICallBackId'] as $callbackId) {
|
|
$updated = OrderArchive::query()->toBase()
|
|
->where('supplier_id', $supplierId)
|
|
->where('APICallBackId', $callbackId)
|
|
->whereNotNull('confirmed')
|
|
->whereNotNull('APILastGetting')
|
|
->whereNull('APIConfirmed')
|
|
->update(['APIConfirmed' => $updateDate]);
|
|
|
|
$result[$callbackId] = $updated ? 'updated' : 'invalid Id';
|
|
}
|
|
|
|
Log::channel('api')->info('MaryKer setProcessed', ['userId' => $request->user()->id, 'result' => $result]);
|
|
|
|
return responder()->success($result)->respond(null, $header);
|
|
}
|
|
|
|
/**
|
|
* Az üres APICallBackId-kat feltölti (versenyhelyzet-biztosan: csak null-t ír felül), majd visszaadja
|
|
* az összes megadott rendelés azonosítóját id => uuid alakban.
|
|
*
|
|
* @param array<int, int> $orderIds
|
|
* @return array<int, string>
|
|
*/
|
|
private function ensureCallbackIds(array $orderIds): array
|
|
{
|
|
$missing = OrderArchive::query()->toBase()->whereIn('id', $orderIds)->whereNull('APICallBackId')->pluck('id');
|
|
|
|
foreach ($missing as $id) {
|
|
OrderArchive::query()->toBase()->where('id', $id)->whereNull('APICallBackId')
|
|
->update(['APICallBackId' => (string) Str::orderedUuid()]);
|
|
}
|
|
|
|
return OrderArchive::query()->toBase()->whereIn('id', $orderIds)->pluck('APICallBackId', 'id')->all();
|
|
}
|
|
|
|
/** @return array{apiVersion: string, processedTime: string} */
|
|
private function header(Carbon $time): array
|
|
{
|
|
$header = parseAPIVersionFromNamespace(__NAMESPACE__);
|
|
$header['processedTime'] = $time->format('Y.m.d. H:i:s.u');
|
|
|
|
return $header;
|
|
}
|
|
|
|
private function forbidden(array $header): JsonResponse
|
|
{
|
|
return responder()->error('forbidden', 'A tokenhez nem tartozik beszállító.')->respond(403, $header);
|
|
}
|
|
}
|