From 3ded5fec71d6d90e725cd0022ca39b5f606f09d4 Mon Sep 17 00:00:00 2001 From: Daniel Covington Date: Mon, 18 May 2026 15:14:45 -0400 Subject: [PATCH] add customer API endpoints and customer_lookup job attribute - Add GET /api/customers, /api/customers/{id}, /api/customer-types endpoints - Add allByTypeWithType() and include api_match_field in customer queries - Add customer_lookup attribute type to job types: stores customer_type_id and imports the customer type's attributes as real fields at design time - Job form: customer_lookup renders a searchable dropdown that fetches /api/customers?customer_type_id=N and auto-fills all matching attribute values when a customer is selected --- app/Controllers/JobTypeController.php | 32 ++++--- app/Views/job-types/create.php | 6 +- app/Views/job-types/edit.php | 6 +- app/Views/jobs/create.php | 42 ++++++++- app/Views/jobs/edit.php | 42 ++++++++- public/js/app.js | 128 ++++++++++++++++++++++---- 6 files changed, 215 insertions(+), 41 deletions(-) diff --git a/app/Controllers/JobTypeController.php b/app/Controllers/JobTypeController.php index 9d27ce0..5c62f14 100644 --- a/app/Controllers/JobTypeController.php +++ b/app/Controllers/JobTypeController.php @@ -223,12 +223,13 @@ class JobTypeController extends Controller ->maxLength('name', $name, 255, 'Name must be 255 characters or fewer.') ->errors()); - $attributeAliases = (array) ($request->input('attribute_alias') ?? []); - $attributeApiUrls = (array) ($request->input('attribute_api_url') ?? []); - $attributeApiFormats = (array) ($request->input('attribute_api_format') ?? []); - $attributeApiReturnTypes = (array) ($request->input('attribute_api_return_type') ?? []); - $attributeApiMatchFields = (array) ($request->input('attribute_api_match_field') ?? []); - $attributeApiAutoFills = (array) ($request->input('attribute_api_auto_fill') ?? []); + $attributeAliases = (array) ($request->input('attribute_alias') ?? []); + $attributeApiUrls = (array) ($request->input('attribute_api_url') ?? []); + $attributeApiFormats = (array) ($request->input('attribute_api_format') ?? []); + $attributeApiReturnTypes = (array) ($request->input('attribute_api_return_type') ?? []); + $attributeApiMatchFields = (array) ($request->input('attribute_api_match_field') ?? []); + $attributeApiAutoFills = (array) ($request->input('attribute_api_auto_fill') ?? []); + $attributeCustomerTypeIds = (array) ($request->input('attribute_customer_type_id') ?? []); $attributes = []; @@ -237,11 +238,10 @@ class JobTypeController extends Controller $attrType = trim((string) ($attributeTypes[$i] ?? 'text')); if ($attrName === '') continue; - // 'customer' is a design-time placeholder that is expanded into real attribute rows - // by the Job Type editor JS before submit. If one slips through, drop it. + // 'customer' is a legacy design-time placeholder (now replaced by customer_lookup). Drop if it slips through. if ($attrType === 'customer') continue; - $validatedType = in_array($attrType, ['text', 'number', 'date', 'boolean', 'api_lookup'], true) ? $attrType : 'text'; + $validatedType = in_array($attrType, ['text', 'number', 'date', 'boolean', 'api_lookup', 'customer_lookup'], true) ? $attrType : 'text'; $attr = [ 'name' => $attrName, @@ -262,6 +262,11 @@ class JobTypeController extends Controller $attr['api_auto_fill'] = trim((string) ($attributeApiAutoFills[$i] ?? '')); } + if ($validatedType === 'customer_lookup') { + $attr['customer_type_id'] = (int) ($attributeCustomerTypeIds[$i] ?? 0); + $attr['api_match_field'] = trim((string) ($attributeApiMatchFields[$i] ?? '')); + } + $attributes[] = $attr; } @@ -289,14 +294,15 @@ class JobTypeController extends Controller return new CustomerTypeRepository(database()); } - /** @return list */ + /** @return list */ private function loadCustomerTypes(): array { return array_map(static function (array $t): array { return [ - 'id' => (int) $t['id'], - 'name' => (string) $t['name'], - 'attributes' => json_decode((string) ($t['attributes'] ?? '[]'), true) ?? [], + 'id' => (int) $t['id'], + 'name' => (string) $t['name'], + 'api_match_field' => (string) ($t['api_match_field'] ?? ''), + 'attributes' => json_decode((string) ($t['attributes'] ?? '[]'), true) ?? [], ]; }, $this->customerTypeRepo()->allOrderedByName()); } diff --git a/app/Views/job-types/create.php b/app/Views/job-types/create.php index 844331e..6dcb6a9 100644 --- a/app/Views/job-types/create.php +++ b/app/Views/job-types/create.php @@ -74,7 +74,7 @@ window.__customerTypes = customerTypes, JSON_HEX_T - +
@@ -121,7 +121,9 @@ window.__customerTypes = customerTypes, JSON_HEX_T
-
+
+ +
@@ -128,7 +128,9 @@ window.__customerTypes = customerTypes, JSON_HEX_T
-
+
+ +