Skip to content

Commit b2a2e73

Browse files
committed
feat(http): support auto publish on form create and patch
1 parent 4c971da commit b2a2e73

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

src/Http/Controllers/FormManagementController.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ public function create(Request $request, FormMutationService $mutations, Idempot
476476
}
477477

478478
$definition = $mutations->create($payload, $request->user(), $owner);
479+
if ($this->shouldAutoPublish($payload)) {
480+
$definition = $mutations->publish((string) $definition->key, $request->user(), $owner);
481+
}
479482
$data = $this->serializeFormDefinition($request, $mutations, $definition);
480483
$body = [
481484
'data' => $data,
@@ -522,6 +525,9 @@ public function patch(
522525
}
523526

524527
$definition = $mutations->patch($key, $payload, $request->user(), $owner);
528+
if ($this->shouldAutoPublish($payload)) {
529+
$definition = $mutations->publish((string) $definition->key, $request->user(), $owner);
530+
}
525531
$data = $this->serializeFormDefinition($request, $mutations, $definition);
526532
$body = [
527533
'data' => $data,
@@ -857,6 +863,19 @@ private function formDefinitionResourceClass(): ?string
857863
return $configured;
858864
}
859865

866+
private function shouldAutoPublish(array $payload): bool
867+
{
868+
if (array_key_exists('auto_publish', $payload)) {
869+
return $this->toBool($payload['auto_publish']);
870+
}
871+
872+
if (array_key_exists('autoPublish', $payload)) {
873+
return $this->toBool($payload['autoPublish']);
874+
}
875+
876+
return false;
877+
}
878+
860879
private function resolvedOwner(Request $request, string $action): ?OwnershipReference
861880
{
862881
$owner = $this->resolveOwner($request, $action);

tests/Feature/HttpApiTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,47 @@
11551155
->assertJsonPath('data.title', 'Survey V2');
11561156
});
11571157

1158+
it('creates and auto publishes a form when requested', function (): void {
1159+
$response = $this->postJson('/api/formforge/v1/forms', [
1160+
'title' => 'Auto Published Create',
1161+
'fields' => [
1162+
['type' => 'text', 'name' => 'name', 'required' => true],
1163+
],
1164+
'auto_publish' => true,
1165+
]);
1166+
1167+
$response
1168+
->assertCreated()
1169+
->assertJsonPath('data.is_published', true)
1170+
->assertJsonPath('data.version_number', 2);
1171+
});
1172+
1173+
it('patches and auto publishes a form when requested', function (): void {
1174+
$create = $this->postJson('/api/formforge/v1/forms', [
1175+
'title' => 'Auto Published Patch V1',
1176+
'fields' => [
1177+
['type' => 'text', 'name' => 'name', 'required' => true],
1178+
],
1179+
])->assertCreated();
1180+
1181+
$key = (string) $create->json('data.key');
1182+
1183+
$patch = $this->patchJson("/api/formforge/v1/forms/{$key}", [
1184+
'title' => 'Auto Published Patch V2',
1185+
'fields' => [
1186+
['type' => 'text', 'name' => 'name', 'required' => true],
1187+
['type' => 'email', 'name' => 'email', 'required' => true],
1188+
],
1189+
'auto_publish' => true,
1190+
]);
1191+
1192+
$patch
1193+
->assertOk()
1194+
->assertJsonPath('data.version_number', 3)
1195+
->assertJsonPath('data.title', 'Auto Published Patch V2')
1196+
->assertJsonPath('data.is_published', true);
1197+
});
1198+
11581199
it('clears form category when patched with explicit null', function (): void {
11591200
$category = $this->postJson('/api/formforge/v1/categories', [
11601201
'name' => 'Survey',

0 commit comments

Comments
 (0)