Deploy Ladill Merchant / deploy (push) Successful in 27s
AfiaController::context() called QrTypeCatalog::eventTypes(), which does not exist on this app's catalog (leftover from the Events-app template) — every Afia chat 500'd with 'Afia could not respond right now' before the LLM was ever reached. Use storefrontTypes() (shop/menu/booking), the QR types this merchant app actually owns. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
88 lines
2.8 KiB
PHP
88 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Qr;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\QrCode;
|
|
use App\Services\Afia\AfiaService;
|
|
use App\Services\Billing\BillingClient;
|
|
use App\Support\Qr\QrTypeCatalog;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class AfiaController extends Controller
|
|
{
|
|
public function chat(Request $request, AfiaService $afia): JsonResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'message' => ['required', 'string', 'max:2000'],
|
|
'history' => ['nullable', 'array', 'max:20'],
|
|
'history.*.role' => ['nullable', 'string'],
|
|
'history.*.text' => ['nullable', 'string'],
|
|
]);
|
|
|
|
if (! $afia->enabled()) {
|
|
return response()->json(['message' => 'Afia is not available right now.'], 503);
|
|
}
|
|
|
|
try {
|
|
$reply = $afia->chat(trim($validated['message']), $validated['history'] ?? [], $this->context());
|
|
} catch (\Throwable $e) {
|
|
report($e);
|
|
|
|
return response()->json(['message' => 'Afia could not respond right now. Please try again.'], 502);
|
|
}
|
|
|
|
return response()->json(['reply' => $reply]);
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private function context(): array
|
|
{
|
|
$account = ladill_account();
|
|
|
|
if (! $account) {
|
|
return ['signed_in' => 'no'];
|
|
}
|
|
|
|
$types = QrTypeCatalog::storefrontTypes();
|
|
$codes = $account->qrCodes()->whereIn('type', $types);
|
|
|
|
$ctx = [
|
|
'signed_in' => 'yes',
|
|
'qr_codes_total' => (clone $codes)->count(),
|
|
'qr_codes_active' => (clone $codes)->where('is_active', true)->count(),
|
|
'scans_total' => (int) (clone $codes)->sum('scans_total'),
|
|
'top_code_type' => (clone $codes)
|
|
->selectRaw('type, count(*) as total')
|
|
->groupBy('type')
|
|
->orderByDesc('total')
|
|
->value('type') ?: 'none yet',
|
|
];
|
|
|
|
if ($account->public_id) {
|
|
try {
|
|
$ctx['wallet_balance_ghs'] = number_format(app(BillingClient::class)->balanceMinor((string) $account->public_id) / 100, 2);
|
|
} catch (\Throwable) {
|
|
}
|
|
}
|
|
|
|
$recent = $account->qrCodes()
|
|
->whereIn('type', $types)
|
|
->latest('updated_at')
|
|
->limit(3)
|
|
->get(['type', 'label', 'short_code', 'scans_total']);
|
|
|
|
if ($recent->isNotEmpty()) {
|
|
$ctx['recent_codes'] = $recent->map(fn (QrCode $code): string => sprintf(
|
|
'%s (%s, %d scans)',
|
|
$code->label ?: $code->short_code,
|
|
QrTypeCatalog::label($code->type),
|
|
$code->scans_total,
|
|
))->implode('; ');
|
|
}
|
|
|
|
return $ctx;
|
|
}
|
|
}
|