feat(helpdesk): pass field values directly during handover creation
Avoids the separate updateFields call after insert by accepting fieldValues in HandoverService::create() and validating/encoding them before the insert. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -53,12 +53,16 @@ class HandoverService
|
||||
*
|
||||
* @return array{ok: bool, id: int|null, errors: array<string, string>}
|
||||
*/
|
||||
/**
|
||||
* @param array<string, mixed> $fieldValues
|
||||
*/
|
||||
public function create(
|
||||
int $tenantId,
|
||||
string $debitorNo,
|
||||
string $debitorName,
|
||||
string $productCode,
|
||||
int $userId,
|
||||
array $fieldValues = [],
|
||||
): array {
|
||||
$errors = [];
|
||||
|
||||
@@ -91,6 +95,18 @@ class HandoverService
|
||||
return ['ok' => false, 'id' => null, 'errors' => ['product_code' => t('Software product has no handover protocol schema')]];
|
||||
}
|
||||
|
||||
// Validate field values against schema if provided
|
||||
if ($fieldValues !== []) {
|
||||
$validationErrors = $this->validateFieldValues($fieldValues, $schema['fields']);
|
||||
if ($validationErrors !== []) {
|
||||
return ['ok' => false, 'id' => null, 'errors' => $validationErrors];
|
||||
}
|
||||
}
|
||||
|
||||
$fieldValuesJson = $fieldValues !== []
|
||||
? json_encode($fieldValues, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
|
||||
: json_encode(new \stdClass(), JSON_FORCE_OBJECT);
|
||||
|
||||
$id = $this->repository->insert([
|
||||
'tenant_id' => $tenantId,
|
||||
'debitor_no' => $debitorNo,
|
||||
@@ -98,7 +114,7 @@ class HandoverService
|
||||
'product_code' => $productCode,
|
||||
'status' => self::STATUS_DRAFT,
|
||||
'schema_snapshot' => $schemaRaw,
|
||||
'field_values' => json_encode(new \stdClass(), JSON_FORCE_OBJECT),
|
||||
'field_values' => $fieldValuesJson,
|
||||
'created_by' => $userId,
|
||||
]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user