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,
|
||||
]);
|
||||
|
||||
|
||||
@@ -139,7 +139,8 @@ if ($step === 2) {
|
||||
$wizardData['debitor_no'],
|
||||
$wizardData['debitor_name'],
|
||||
$wizardData['product_code'],
|
||||
$userId
|
||||
$userId,
|
||||
$fieldValues
|
||||
);
|
||||
|
||||
if (!($result['ok'] ?? false)) {
|
||||
@@ -148,14 +149,6 @@ if ($step === 2) {
|
||||
} else {
|
||||
$handoverId = $result['id'];
|
||||
|
||||
// Save field values
|
||||
$authService = app(\MintyPHP\Service\Access\AuthorizationService::class);
|
||||
$canManage = $authService->authorize(HelpdeskAuthorizationPolicy::ABILITY_HANDOVERS_MANAGE, ['actor_user_id' => $userId])->isAllowed();
|
||||
$permLevel = $canManage
|
||||
? HandoverService::PERMISSION_MANAGE
|
||||
: HandoverService::PERMISSION_CREATE;
|
||||
$handoverService->updateFields($tenantId, $handoverId, $fieldValues, $userId, $permLevel);
|
||||
|
||||
// Clear wizard session
|
||||
$sessionStore->remove($sessionKey);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user