ADD EV3-461 Mary-Ker API: rendelésazonosító hosszkorlát-védelem (1.5), kizárási szabályok külön szolgáltatásban
- a 25 karakternél hosszabb vagy üres humanId-jú rendelés kimarad az unprocessed listából, hibaként naplózva - a korlát konfigurálható (maryker.order_id_max_length), a hosszt karakterben számolja - a kizárási szabályok (azonosító, vevőkód, cikkszám) a MaryKerOrderEligibility szolgáltatásba kerültek: egy feldolgozatlan-lekérdezés és PHP-oldali szűrés a rendelésenkénti alkérdések helyett (nincs index, B-03) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5a320837a9
commit
788f5ce2df
@ -4,11 +4,10 @@
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\OrderArchive;
|
||||
use App\Models\OrderArchivesItem;
|
||||
use App\Services\MaryKerLogger;
|
||||
use App\Services\MaryKerOrderEligibility;
|
||||
use App\Services\MaryKerOrderMapper;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
@ -25,17 +24,20 @@
|
||||
*/
|
||||
class MaryKerOrderController extends Controller
|
||||
{
|
||||
public function __construct(private readonly MaryKerOrderMapper $mapper) {}
|
||||
public function __construct(
|
||||
private readonly MaryKerOrderMapper $mapper,
|
||||
private readonly MaryKerOrderEligibility $eligibility,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 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 és a vevőkód meglété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
|
||||
* Idempotens a találati halmaz szempontjából: a lista csak az APIConfirmed-től és az adatok hiánytalanságá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.
|
||||
*
|
||||
* Az a rendelés, amelynek telephelyéhez nincs beszállítói vevőkód, kimarad a listából (Apolló nem tudná
|
||||
* párosítani), és hibaként bekerül a maryker naplóba, amíg a kódot rögzítik.
|
||||
* Ami Apollónak hiányos (azonosító, vevőkód, cikkszám), az a rendelés kimarad a listából, és hibaként
|
||||
* bekerül a maryker naplóba, amíg az adatot rögzítik (ld. MaryKerOrderEligibility).
|
||||
*/
|
||||
public function unProcessed(Request $request): JsonResponse
|
||||
{
|
||||
@ -47,12 +49,8 @@ public function unProcessed(Request $request): JsonResponse
|
||||
return $this->forbidden($header);
|
||||
}
|
||||
|
||||
$this->logOrdersWithoutCustomerCode($supplierId);
|
||||
$withoutProductNumber = $this->ordersWithoutProductNumber($supplierId);
|
||||
|
||||
$page = $this->pending($supplierId)
|
||||
->whereExists($this->customerCode($supplierId))
|
||||
->whereNotIn('id', $withoutProductNumber)
|
||||
$page = OrderArchive::query()
|
||||
->whereIn('id', $this->eligibility->eligibleOrderIds($supplierId))
|
||||
->select([
|
||||
'id', 'humanId', 'confirmed', 'sumPrice', 'deliveryDate', 'note', 'profit_center_id', 'contactName',
|
||||
'profit_center_name', 'deliveryAddressId', 'deliveryAddressName', 'deliveryAddress',
|
||||
@ -131,78 +129,6 @@ public function setProcessed(Request $request): JsonResponse
|
||||
return responder()->success($result)->respond(null, $header);
|
||||
}
|
||||
|
||||
/** A beszállító megerősített, még nem visszaigazolt rendelései. */
|
||||
private function pending(int $supplierId): Builder
|
||||
{
|
||||
return OrderArchive::query()
|
||||
->where('supplier_id', $supplierId)
|
||||
->whereNotNull('confirmed')
|
||||
->whereNull('APIConfirmed');
|
||||
}
|
||||
|
||||
/** Alkérdés: van-e a rendelés telephelyéhez rögzített, nem üres beszállítói vevőkód. */
|
||||
private function customerCode(int $supplierId): \Closure
|
||||
{
|
||||
return fn ($query) => $query->selectRaw('1')
|
||||
->from('profit_center_supplier_codes')
|
||||
->whereColumn('profit_center_supplier_codes.profit_center_id', 'order_archives.profit_center_id')
|
||||
->where('profit_center_supplier_codes.supplier_id', $supplierId)
|
||||
->whereNotNull('profit_center_supplier_codes.customerCode')
|
||||
->where('profit_center_supplier_codes.customerCode', '<>', '')
|
||||
->whereNull('profit_center_supplier_codes.deleted_at');
|
||||
}
|
||||
|
||||
/** A vevőkód nélküli rendelések kimaradnak a listából: hibaként naplózzuk őket (rendelésenként óránként egyszer). */
|
||||
private function logOrdersWithoutCustomerCode(int $supplierId): void
|
||||
{
|
||||
$this->pending($supplierId)
|
||||
->whereNotExists($this->customerCode($supplierId))
|
||||
->orderBy('id')->limit(500)
|
||||
->get(['id', 'humanId', 'profit_center_id'])
|
||||
->each(fn (OrderArchive $order) => MaryKerLogger::once(
|
||||
'error',
|
||||
"customer-code:{$order->id}",
|
||||
'MaryKer: a rendelés kimarad az unprocessed listából, a telephelyhez nincs beszállítói vevőkód',
|
||||
['orderId' => $order->id, 'humanId' => $order->humanId, 'profitCenterId' => $order->profit_center_id],
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Azok a (vevőkóddal rendelkező, feldolgozatlan) rendelések, amelyeknek van üres cikkszámú (supplierProductNumber)
|
||||
* tétele. Az ilyen rendelés teljes egészében kimarad a listából: Apolló a tételt nem tudná párosítani, a tétel
|
||||
* szó nélküli elhagyása pedig a fejléc összegével nem egyező rendelést adna. Hibaként naplózzuk (rendelésenként
|
||||
* óránként egyszer), a hibás tétel azonosítóival.
|
||||
*
|
||||
* Egyetlen csoportos lekérdezés, nem korrelált alkérdés: az order_archives_items táblán nincs index az
|
||||
* order_archive_id-n, ezért a rendelésenkénti keresés minden rendelésnél végigolvasná a táblát (backlog B-03).
|
||||
*
|
||||
* @return array<int, int> a kizárt rendelések azonosítói
|
||||
*/
|
||||
private function ordersWithoutProductNumber(int $supplierId): array
|
||||
{
|
||||
$pendingIds = $this->pending($supplierId)->whereExists($this->customerCode($supplierId))->pluck('id');
|
||||
if ($pendingIds->isEmpty()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$badItems = OrderArchivesItem::whereIn('order_archive_id', $pendingIds)
|
||||
->where(fn ($query) => $query->whereNull('supplierProductNumber')->orWhere('supplierProductNumber', ''))
|
||||
->get(['id', 'order_archive_id'])
|
||||
->groupBy('order_archive_id');
|
||||
|
||||
$humanIds = OrderArchive::whereIn('id', $badItems->keys())->pluck('humanId', 'id');
|
||||
foreach ($badItems as $orderId => $items) {
|
||||
MaryKerLogger::once(
|
||||
'error',
|
||||
"product-number:{$orderId}",
|
||||
'MaryKer: a rendelés kimarad az unprocessed listából, van olyan tétele, aminek nincs beszállítói cikkszáma',
|
||||
['orderId' => $orderId, 'humanId' => $humanIds[$orderId] ?? null, 'itemIds' => $items->pluck('id')->all()],
|
||||
);
|
||||
}
|
||||
|
||||
return $badItems->keys()->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
||||
124
app/Services/MaryKerOrderEligibility.php
Normal file
124
app/Services/MaryKerOrderEligibility.php
Normal file
@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\OrderArchive;
|
||||
use App\Models\OrderArchivesItem;
|
||||
use App\Models\ProfitCenterSupplierCode;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Eldönti, hogy a beszállító feldolgozatlan rendelései közül melyek adhatók ki a Mary-Ker API-n (EV3-461).
|
||||
*
|
||||
* Egy rendelés kimarad, ha Apolló nem tudná helyesen feldolgozni:
|
||||
* 1. a rendelésazonosítója (humanId) üres, vagy hosszabb a megengedettnél (specifikáció 5. pont);
|
||||
* 2. a telephelyéhez nincs beszállítói vevőkód (specifikáció 3.2 pont);
|
||||
* 3. van olyan tétele, aminek nincs beszállítói cikkszáma (specifikáció 3.3 pont): a tétel szó nélküli elhagyása
|
||||
* a fejléc összegével nem egyező rendelést adna.
|
||||
*
|
||||
* A kimaradást hibaként naplózzuk a maryker csatornán (rendelésenként óránként egyszer, ld. MaryKerLogger::once),
|
||||
* és csak az első találó okot: ha a hiba megszűnik, a rendelés a következő lekéréskor megjelenik.
|
||||
*
|
||||
* Szándékosan egyetlen "feldolgozatlan" lekérdezés és PHP-oldali szűrés, nem rendelésenkénti alkérdések: az
|
||||
* order_archives és az order_archives_items táblán nincs index (backlog B-03), így minden korrelált alkérdés
|
||||
* végigolvasná a több százezer / milliós táblát.
|
||||
*/
|
||||
class MaryKerOrderEligibility
|
||||
{
|
||||
/** Egy lekérésnél legfeljebb ennyi kihagyott rendelést naplózunk kategóriánként. */
|
||||
private const MAX_LOGGED = 500;
|
||||
|
||||
/**
|
||||
* @return array<int, int> a kiadható rendelések azonosítói, növekvő sorrendben
|
||||
*/
|
||||
public function eligibleOrderIds(int $supplierId): array
|
||||
{
|
||||
$pending = OrderArchive::query()
|
||||
->where('supplier_id', $supplierId)
|
||||
->whereNotNull('confirmed')
|
||||
->whereNull('APIConfirmed')
|
||||
->orderBy('id')
|
||||
->get(['id', 'humanId', 'profit_center_id']);
|
||||
|
||||
$pending = $this->withValidOrderId($pending);
|
||||
$pending = $this->withCustomerCode($pending, $supplierId);
|
||||
$pending = $this->withProductNumbers($pending);
|
||||
|
||||
return $pending->pluck('id')->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, OrderArchive> $orders
|
||||
* @return Collection<int, OrderArchive>
|
||||
*/
|
||||
private function withValidOrderId(Collection $orders): Collection
|
||||
{
|
||||
$max = (int) config('maryker.order_id_max_length');
|
||||
[$valid, $invalid] = $orders->partition(
|
||||
fn (OrderArchive $order) => $order->humanId !== null && $order->humanId !== '' && mb_strlen($order->humanId) <= $max
|
||||
);
|
||||
|
||||
$invalid->take(self::MAX_LOGGED)->each(fn (OrderArchive $order) => MaryKerLogger::once(
|
||||
'error',
|
||||
"order-id:{$order->id}",
|
||||
'MaryKer: a rendelés kimarad az unprocessed listából, a rendelésazonosító (humanId) üres vagy hosszabb a megengedettnél',
|
||||
['orderId' => $order->id, 'humanId' => $order->humanId, 'length' => mb_strlen((string) $order->humanId), 'max' => $max],
|
||||
));
|
||||
|
||||
return $valid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, OrderArchive> $orders
|
||||
* @return Collection<int, OrderArchive>
|
||||
*/
|
||||
private function withCustomerCode(Collection $orders, int $supplierId): Collection
|
||||
{
|
||||
if ($orders->isEmpty()) {
|
||||
return $orders;
|
||||
}
|
||||
|
||||
$withCode = ProfitCenterSupplierCode::where('supplier_id', $supplierId)
|
||||
->whereIn('profit_center_id', $orders->pluck('profit_center_id')->filter()->unique())
|
||||
->whereNotNull('customerCode')->where('customerCode', '<>', '')
|
||||
->pluck('profit_center_id')->flip();
|
||||
|
||||
[$valid, $missing] = $orders->partition(fn (OrderArchive $order) => $withCode->has($order->profit_center_id));
|
||||
|
||||
$missing->take(self::MAX_LOGGED)->each(fn (OrderArchive $order) => MaryKerLogger::once(
|
||||
'error',
|
||||
"customer-code:{$order->id}",
|
||||
'MaryKer: a rendelés kimarad az unprocessed listából, a telephelyhez nincs beszállítói vevőkód',
|
||||
['orderId' => $order->id, 'humanId' => $order->humanId, 'profitCenterId' => $order->profit_center_id],
|
||||
));
|
||||
|
||||
return $valid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, OrderArchive> $orders
|
||||
* @return Collection<int, OrderArchive>
|
||||
*/
|
||||
private function withProductNumbers(Collection $orders): Collection
|
||||
{
|
||||
if ($orders->isEmpty()) {
|
||||
return $orders;
|
||||
}
|
||||
|
||||
$badItems = OrderArchivesItem::whereIn('order_archive_id', $orders->pluck('id'))
|
||||
->where(fn ($query) => $query->whereNull('supplierProductNumber')->orWhere('supplierProductNumber', ''))
|
||||
->get(['id', 'order_archive_id'])
|
||||
->groupBy('order_archive_id');
|
||||
|
||||
[$valid, $invalid] = $orders->partition(fn (OrderArchive $order) => ! $badItems->has($order->id));
|
||||
|
||||
$invalid->take(self::MAX_LOGGED)->each(fn (OrderArchive $order) => MaryKerLogger::once(
|
||||
'error',
|
||||
"product-number:{$order->id}",
|
||||
'MaryKer: a rendelés kimarad az unprocessed listából, van olyan tétele, aminek nincs beszállítói cikkszáma',
|
||||
['orderId' => $order->id, 'humanId' => $order->humanId, 'itemIds' => $badItems[$order->id]->pluck('id')->all()],
|
||||
));
|
||||
|
||||
return $valid;
|
||||
}
|
||||
}
|
||||
@ -20,6 +20,9 @@
|
||||
// Rendelés / oldal az unprocessed válaszban.
|
||||
'per_page' => (int) env('MARYKER_API_PER_PAGE', 50),
|
||||
|
||||
// A rendelésazonosító (humanId) megengedett legnagyobb hossza: Anita (Multi.hu) szerint legfeljebb 25 karakter.
|
||||
'order_id_max_length' => (int) env('MARYKER_ORDER_ID_MAX_LENGTH', 25),
|
||||
|
||||
// Egy setProcessed kérésben visszaigazolható APICallBackId-k felső határa.
|
||||
'set_processed_max' => (int) env('MARYKER_API_SET_PROCESSED_MAX', 500),
|
||||
|
||||
|
||||
@ -507,6 +507,90 @@ function profitCenterBlock(): array
|
||||
});
|
||||
});
|
||||
|
||||
describe('rendelésazonosító hossza (1.5)', function () {
|
||||
it('a pontosan 25 karakteres azonosító még kiadható, a 26 karakteres kimarad', function () {
|
||||
$spy = maryKerLogSpy();
|
||||
maryKerUser($this->mary);
|
||||
$ok = maryKerOrder($this->mary, ['humanId' => str_repeat('A', 25)]);
|
||||
$tooLong = maryKerOrder($this->mary, ['humanId' => str_repeat('B', 26)]);
|
||||
|
||||
$this->getJson(MARYKER_UNPROCESSED)->assertOk()
|
||||
->assertJsonCount(1, 'data')
|
||||
->assertJsonPath('pagination.total', 1)
|
||||
->assertJsonPath('data.0.orderId', $ok->humanId);
|
||||
|
||||
$spy->shouldHaveReceived('error')->once()->withArgs(
|
||||
fn ($message, $context) => str_contains($message, 'humanId')
|
||||
&& $context['orderId'] === $tooLong->id && $context['humanId'] === $tooLong->humanId
|
||||
&& $context['length'] === 26 && $context['max'] === 25
|
||||
);
|
||||
});
|
||||
|
||||
it('a hosszt karakterben számolja, nem bájtban (ékezetes betűk)', function () {
|
||||
maryKerUser($this->mary);
|
||||
$accented = maryKerOrder($this->mary, ['humanId' => str_repeat('é', 25)]); // 50 bájt, 25 karakter
|
||||
|
||||
$this->getJson(MARYKER_UNPROCESSED)->assertOk()->assertJsonCount(1, 'data')->assertJsonPath('data.0.orderId', $accented->humanId);
|
||||
});
|
||||
|
||||
it('az üres vagy hiányzó azonosítójú rendelés kimarad, és hibaként naplózódik', function (?string $humanId) {
|
||||
$spy = maryKerLogSpy();
|
||||
maryKerUser($this->mary);
|
||||
$order = maryKerOrder($this->mary, ['humanId' => $humanId]);
|
||||
|
||||
$this->getJson(MARYKER_UNPROCESSED)->assertOk()->assertJsonCount(0, 'data')->assertJsonPath('pagination.total', 0);
|
||||
|
||||
$spy->shouldHaveReceived('error')->once()->withArgs(fn ($message, $context) => $context['orderId'] === $order->id && $context['length'] === 0);
|
||||
})->with(['üres szöveg' => [''], 'null' => [null]]);
|
||||
|
||||
it('a korlát konfigurálható', function () {
|
||||
config(['maryker.order_id_max_length' => 10]);
|
||||
maryKerUser($this->mary);
|
||||
maryKerOrder($this->mary, ['humanId' => 'MARY/AKG/26/0001']); // 16 karakter
|
||||
|
||||
$this->getJson(MARYKER_UNPROCESSED)->assertOk()->assertJsonCount(0, 'data');
|
||||
});
|
||||
|
||||
it('a kimaradt rendelést nem érinti az APILastGetting és az APICallBackId, a javítás után megjelenik', function () {
|
||||
maryKerUser($this->mary);
|
||||
$order = maryKerOrder($this->mary, ['humanId' => str_repeat('C', 30)]);
|
||||
|
||||
$this->getJson(MARYKER_UNPROCESSED)->assertOk()->assertJsonCount(0, 'data');
|
||||
expect($order->fresh()->APICallBackId)->toBeNull()->and($order->fresh()->APILastGetting)->toBeNull();
|
||||
|
||||
$order->update(['humanId' => 'MARY/AKG/26/0001']);
|
||||
|
||||
$this->getJson(MARYKER_UNPROCESSED)->assertOk()->assertJsonCount(1, 'data');
|
||||
});
|
||||
|
||||
it('több hiba esetén is csak egy hibát naplóz rendelésenként (az elsőt: azonosító, vevőkód, cikkszám sorrendben)', function () {
|
||||
$spy = maryKerLogSpy();
|
||||
maryKerUser($this->mary);
|
||||
$bare = ProfitCenter::factory()->create(['postCode' => '1111']); // vevőkód sincs
|
||||
$order = maryKerOrder($this->mary, ['humanId' => str_repeat('D', 40), 'profit_center_id' => $bare->id]);
|
||||
maryKerItem($order, ['supplierProductNumber' => null]); // cikkszám sincs
|
||||
|
||||
$this->getJson(MARYKER_UNPROCESSED)->assertOk()->assertJsonCount(0, 'data');
|
||||
|
||||
$spy->shouldHaveReceived('error')->once();
|
||||
});
|
||||
|
||||
it('a másik beszállító túl hosszú azonosítójú rendelése nem zavarja a Mary-Ker listát', function () {
|
||||
$spy = maryKerLogSpy();
|
||||
maryKerUser($this->mary);
|
||||
maryKerOrder($this->mary);
|
||||
maryKerOrder($this->other, ['humanId' => str_repeat('E', 40)]);
|
||||
|
||||
$this->getJson(MARYKER_UNPROCESSED)->assertOk()->assertJsonCount(1, 'data');
|
||||
$spy->shouldNotHaveReceived('error');
|
||||
});
|
||||
|
||||
it('a valós formátum 25 karakter alatt marad (4 betűs beszállító + 6 betűs telephely + év + 4 jegyű sorszám)', function () {
|
||||
// MARY/ABCDEF/26/0001 = 19 karakter: a spec 5. pontja szerinti legrosszabb valós eset + 6 karakter tartalék
|
||||
expect(strlen('MARY/ABCDEF/26/0001'))->toBe(19)->toBeLessThanOrEqual(config('maryker.order_id_max_length'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('vevőkód nélküli rendelések', function () {
|
||||
it('kimaradnak a listából és a lapozás összegéből, a vevőkódosak maradnak', function () {
|
||||
maryKerUser($this->mary);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user