diff --git a/app/Controllers/CustomerApiController.php b/app/Controllers/CustomerApiController.php new file mode 100644 index 0000000..bedfe4d --- /dev/null +++ b/app/Controllers/CustomerApiController.php @@ -0,0 +1,76 @@ +input('customer_type_id') ?? 0); + + $repo = new CustomerRepository(database()); + $rows = $customerTypeId > 0 + ? $repo->allByTypeWithType($customerTypeId) + : $repo->allWithType(); + + return $this->json(array_map([$this, 'formatCustomer'], $rows)); + } + + public function customer(string $id): Response + { + $repo = new CustomerRepository(database()); + $row = $repo->findWithType((int) $id); + + if ($row === null) { + return Response::json(['error' => 'Not found'], 404); + } + + return $this->json($this->formatCustomer($row)); + } + + public function customerTypes(): Response + { + $repo = new CustomerTypeRepository(database()); + $rows = $repo->allOrderedByName(); + + $data = array_map(static function (array $row): array { + $attributes = []; + if (!empty($row['attributes'])) { + $attributes = json_decode((string) $row['attributes'], true) ?? []; + } + + return [ + 'id' => (int) $row['id'], + 'name' => (string) $row['name'], + 'api_match_field' => (string) ($row['api_match_field'] ?? ''), + 'attributes' => $attributes, + ]; + }, $rows); + + return $this->json($data); + } + + private function formatCustomer(array $row): array + { + $attributeValues = []; + if (!empty($row['attribute_values'])) { + $attributeValues = json_decode((string) $row['attribute_values'], true) ?? []; + } + + return [ + 'id' => (int) $row['id'], + 'customer_type_id' => (int) $row['customer_type_id'], + 'customer_type_name' => (string) ($row['customer_type_name'] ?? ''), + 'attribute_values' => $attributeValues, + ]; + } +} 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/Repositories/CustomerRepository.php b/app/Repositories/CustomerRepository.php index a2d2309..bf263c4 100644 --- a/app/Repositories/CustomerRepository.php +++ b/app/Repositories/CustomerRepository.php @@ -48,20 +48,39 @@ class CustomerRepository extends Repository 'SELECT c.id, c.customer_type_id, c.attribute_values, c.created_at, c.updated_at, ct.name AS customer_type_name, - ct.attributes AS customer_type_attributes + ct.attributes AS customer_type_attributes, + ct.api_match_field FROM customer c INNER JOIN customer_type ct ON c.customer_type_id = ct.id ORDER BY c.id DESC' ); } + /** @return list> */ + public function allByTypeWithType(int $typeId): array + { + return $this->database->query( + 'SELECT c.id, c.customer_type_id, c.attribute_values, + c.created_at, c.updated_at, + ct.name AS customer_type_name, + ct.attributes AS customer_type_attributes, + ct.api_match_field + FROM customer c + INNER JOIN customer_type ct ON c.customer_type_id = ct.id + WHERE c.customer_type_id = :type_id + ORDER BY c.id DESC', + ['type_id' => $typeId] + ); + } + public function findWithType(int $id): ?array { return $this->database->first( 'SELECT c.id, c.customer_type_id, c.attribute_values, c.created_at, c.updated_at, ct.name AS customer_type_name, - ct.attributes AS customer_type_attributes + ct.attributes AS customer_type_attributes, + ct.api_match_field FROM customer c INNER JOIN customer_type ct ON c.customer_type_id = ct.id WHERE c.id = :id', 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
-
+
+ +