feat: phase 2 — master data (products, customers, drivers) via InsForge-backed Filament pages

This commit is contained in:
sean
2026-08-25 18:33:56 +07:00
parent aad2b5e256
commit 4298544381
12 changed files with 870 additions and 38 deletions
+67 -29
View File
@@ -7,13 +7,19 @@ use Illuminate\Support\Facades\Http;
class InsForgeFake
{
public static array $profiles = [];
/** @var array<string, array<string, array<string, mixed>>> table => id => row */
public static array $store = [];
public static string $accessToken = 'fake.header.';
public static string $accessToken = '';
public static function reset(): void
{
self::$profiles = [];
self::$store = [];
}
public static function seed(string $table, array $row): void
{
self::$store[$table][$row['id']] = $row;
}
public static function fakeLoginSuccess(array $profile): void
@@ -25,7 +31,7 @@ class InsForgeFake
'exp' => now()->addHour()->getTimestamp(),
])).'.sig';
self::$profiles[$profile['email']] = $profile;
self::seed('users', $profile);
Http::fake([
'*/api/auth/sessions' => Http::response([
@@ -39,12 +45,8 @@ class InsForgeFake
], 200, [
'Set-Cookie' => 'insforge_refresh_token=fake-refresh-cookie; Path=/; HttpOnly',
]),
'*/api/auth/refresh' => Http::response([
'accessToken' => self::$accessToken,
], 200),
'*/api/database/records/users*' => function (Request $request) {
return self::handleUsersRequest($request);
},
'*/api/auth/refresh' => Http::response(['accessToken' => self::$accessToken], 200),
'*/api/database/records/*' => fn (Request $request) => self::handleRecordsRequest($request),
]);
}
@@ -61,25 +63,54 @@ class InsForgeFake
]);
}
public static function fakeProfile(array $profile): void
public static function fakeRest(): void
{
self::$profiles[$profile['email']] = $profile;
Http::fake([
'*/api/auth/refresh' => Http::response(['accessToken' => self::$accessToken], 200),
'*/api/database/records/*' => fn (Request $request) => self::handleRecordsRequest($request),
]);
}
private static function handleUsersRequest(Request $request)
public static function handleRecordsRequest(Request $request)
{
$path = parse_url($request->url(), PHP_URL_PATH) ?? '';
$table = str_replace('/api/database/records/', '', $path);
parse_str((string) parse_url($request->url(), PHP_URL_QUERY) ?? '', $query);
$method = strtoupper($request->method());
$rows = array_values(self::$store[$table] ?? []);
if ($method === 'GET') {
foreach (self::$profiles as $profile) {
if (($query['email'] ?? null) === 'eq.'.$profile['email']
|| ($query['id'] ?? null) === 'eq.'.$profile['id']) {
return Http::response([$profile], 200);
foreach ($query as $key => $value) {
if (in_array($key, ['select', 'limit', 'offset', 'order', 'or'], true)) {
continue;
}
if (str_starts_with((string) $value, 'eq.')) {
$column = $key;
$expected = substr((string) $value, 3);
$rows = array_values(array_filter($rows, fn (array $row): bool => (string) ($row[$column] ?? '') === $expected));
}
}
return Http::response([], 200);
$total = count($rows);
$rows = array_slice(
$rows,
(int) ($query['offset'] ?? 0),
(int) ($query['limit'] ?? 25),
);
return Http::response(
array_map(
fn (array $row): array => isset($query['select']) && $query['select'] !== '*'
? array_intersect_key($row, array_flip(explode(',', (string) $query['select']))) ?: $row
: $row,
$rows,
),
200,
['Content-Range' => sprintf('0-%d/%d', max(count($rows) - 1, 0), $total)],
);
}
if ($method === 'POST') {
@@ -89,25 +120,32 @@ class InsForgeFake
return Http::response([], 201);
}
self::$profiles[$payload['email']] = array_merge([
'role' => 'operator',
'is_active' => true,
], $payload);
$payload = array_merge($payload, ['id' => $payload['id'] ?? fake()->uuid()]);
self::$store[$table][$payload['id']] = $payload;
return Http::response([], 201);
}
if ($method === 'PATCH') {
$targetId = str_replace('eq.', '', $query['id'] ?? '');
$targetId = str_replace('eq.', '', $query['id'] ?? '');
foreach (self::$profiles as &$profile) {
if ($profile['id'] === $targetId) {
$profile = array_merge($profile, $request->data());
if ($method === 'PATCH') {
$payload = $request->data();
foreach (self::$store[$table] as &$row) {
if (($row['id'] ?? null) === $targetId) {
$row = array_merge($row, $payload);
}
}
unset($profile);
unset($row);
return Http::response([], 200);
return Http::response([], 204);
}
if ($method === 'DELETE') {
unset(self::$store[$table][$targetId]);
return Http::response([], 204);
}
return Http::response([], 200);