62 lines
2.7 KiB
PHP
62 lines
2.7 KiB
PHP
<?php
|
|
|
|
use App\Models\Requisition;
|
|
use App\Models\Supplier;
|
|
use App\Models\User;
|
|
use App\Services\QuotationExchange;
|
|
use App\Services\SupplierBankWorkflow;
|
|
use Illuminate\Contracts\Console\Kernel;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
|
|
|
require __DIR__.'/../../vendor/autoload.php';
|
|
$database = $argv[1] ?? '';
|
|
if (! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', $database)) {
|
|
throw new LogicException('Base de teste inválida.');
|
|
}
|
|
$app = require __DIR__.'/../../bootstrap/app.php';
|
|
$app->make(Kernel::class)->bootstrap();
|
|
if (! $app->environment('testing') || DB::getDriverName() !== 'pgsql' || DB::connection()->getDatabaseName() !== $database) {
|
|
throw new LogicException('Worker não isolado.');
|
|
}
|
|
if (($argv[2] ?? '') === 'migrate') {
|
|
if (Schema::getConnection()->getDatabaseName() !== $database || DB::connection('pgsql')->getDatabaseName() !== $database) {
|
|
throw new LogicException('Esquema fora da base isolada.');
|
|
}
|
|
Artisan::call('migrate', ['--database' => 'pgsql', '--force' => true]);
|
|
echo json_encode(['isolated_schema_prepared' => true], JSON_THROW_ON_ERROR).PHP_EOL;
|
|
exit(0);
|
|
}
|
|
if (($argv[2] ?? '') === 'bank_prepare') {
|
|
require __DIR__.'/../Support/PaymentFixture.php';
|
|
$f = paymentFixture();
|
|
echo json_encode(['tenant' => $f['tenant']->id, 'actor' => $f['issuer']->id, 'supplier' => $f['supplier']->id, 'bank_data' => $f['bank_data']], JSON_THROW_ON_ERROR);
|
|
exit(0);
|
|
}
|
|
if (($argv[2] ?? '') === 'prepare') {
|
|
require __DIR__.'/../Support/QualificationFixture.php';
|
|
$f = qualificationFixture();
|
|
echo json_encode(['tenant' => $f['tenant']->id, 'actor' => $f['analyst']->id, 'request' => $f['r']->id], JSON_THROW_ON_ERROR);
|
|
exit(0);
|
|
}
|
|
$input = json_decode($argv[3] ?? '', true, 16, JSON_THROW_ON_ERROR);
|
|
DB::select('select set_config(?, ?, false)', ['application_name', (($argv[2] ?? '') === 'bank_write' ? 'sgc_bank_write_' : 'sgc_fx_write_').getmypid()]);
|
|
$actor = User::query()->findOrFail($input['actor']);
|
|
auth()->guard()->setUser($actor);
|
|
try {
|
|
if (($argv[2] ?? '') === 'bank_write') {
|
|
$detail = app(SupplierBankWorkflow::class)->propose($actor, Supplier::query()->findOrFail($input['supplier']), $input['rate']);
|
|
} else {
|
|
app(QuotationExchange::class)->record($actor, Requisition::query()->findOrFail($input['request']), $input['rate']);
|
|
}
|
|
$status = 200;
|
|
} catch (ValidationException) {
|
|
$status = 422;
|
|
} catch (HttpExceptionInterface $error) {
|
|
$status = $error->getStatusCode();
|
|
}
|
|
echo json_encode(['status' => $status, 'id' => isset($detail) ? $detail->id : null], JSON_THROW_ON_ERROR);
|