Files
crewli/api/app/Http/Resources/FormBuilder/FormSchemaResource.php
bert.hausmans b9343f6eec refactor(form-builder): drop custom purpose escape from schemas
Reduces the FormPurpose vocabulary from 22 variants + a `custom` escape
to the seven v1.0 purposes registered in the new PurposeRegistry.

- Purge migration deletes any form_schemas row whose `purpose` is not
  in the v1.0 set (cascades through form_fields, form_submissions,
  form_values, form_value_options, form_schema_sections,
  form_submission_section_statuses, form_submission_delegations,
  form_schema_webhooks, form_webhook_deliveries via existing FK).
- Drop migration removes the `custom_purpose_slug` column + its index.
- Both migrations declare their `down()` as a hard failure — we do not
  support reversing a purge (pre-launch, no production data).
- `FormPurpose` enum slims to the seven cases; the legacy helpers
  (defaultSubmissionMode / defaultSubjectType / allowsPublicAccess)
  now delegate to PurposeRegistry so callers keep working.
- FormSchema fillable / FormSchemaResource / StoreFormSchemaRequest /
  UpdateFormSchemaRequest / FormSchemaFactory drop every reference to
  `custom_purpose_slug` and the `custom` purpose.
- VerifyFormsDataIntegrity drops the custom-slug mismatch check and
  sources the subject-type allow-list from PurposeRegistry.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 14:35:37 +02:00

98 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Resources\FormBuilder;
use App\Models\FormBuilder\FormSchema;
use App\Models\FormBuilder\FormSubmission;
use App\Services\FormBuilder\FieldAccessService;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* @mixin FormSchema
*/
final class FormSchemaResource extends JsonResource
{
/**
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
$fieldsCollection = $this->relationLoaded('fields')
? $this->fields
: $this->fields()->get();
$visible = app(FieldAccessService::class)
->filterVisibleFields($request->user(), $fieldsCollection);
$submissionsCount = $this->whenCounted(
'submissions',
default: FormSubmission::query()->where('form_schema_id', $this->id)->count(),
);
return [
'id' => $this->id,
'organisation_id' => $this->organisation_id,
'owner_type' => $this->owner_type,
'owner_id' => $this->owner_id,
'name' => $this->name,
'slug' => $this->slug,
'purpose' => $this->purpose instanceof \BackedEnum ? $this->purpose->value : $this->purpose,
'description' => $this->description,
'is_published' => (bool) $this->is_published,
'submission_mode' => $this->submission_mode instanceof \BackedEnum ? $this->submission_mode->value : $this->submission_mode,
'locale' => $this->locale,
'settings' => $this->settings,
'snapshot_mode' => $this->snapshot_mode instanceof \BackedEnum ? $this->snapshot_mode->value : $this->snapshot_mode,
'freeze_on_submit' => (bool) $this->freeze_on_submit,
'retention_days' => $this->retention_days,
'consent_version' => $this->consent_version,
'section_level_submit' => (bool) $this->section_level_submit,
'auto_save_enabled' => (bool) $this->auto_save_enabled,
'max_submissions' => $this->max_submissions,
'version' => (int) $this->version,
'public_token' => $this->public_token,
'public_token_previous' => $this->public_token_previous,
'public_token_rotated_at' => optional($this->public_token_rotated_at)->toIso8601String(),
'submission_deadline' => optional($this->submission_deadline)->toIso8601String(),
'created_by_user_id' => $this->created_by_user_id,
'last_updated_by_user_id' => $this->last_updated_by_user_id,
'edit_lock_user_id' => $this->edit_lock_user_id,
'edit_lock_expires_at' => optional($this->edit_lock_expires_at)->toIso8601String(),
'is_locked' => $this->isLocked(),
'public_form_url' => $this->publicFormUrl(),
'fields_count' => $fieldsCollection->count(),
'submissions_count' => $submissionsCount,
'has_submissions' => is_int($submissionsCount) ? $submissionsCount > 0 : null,
'fields' => FormFieldResource::collection($visible),
'sections' => FormSchemaSectionResource::collection(
$this->relationLoaded('sections') ? $this->sections : $this->sections()->get(),
),
'created_at' => optional($this->created_at)->toIso8601String(),
'updated_at' => optional($this->updated_at)->toIso8601String(),
];
}
private function isLocked(): bool
{
if ($this->edit_lock_user_id === null || $this->edit_lock_expires_at === null) {
return false;
}
return $this->edit_lock_expires_at->isFuture();
}
private function publicFormUrl(): ?string
{
if (empty($this->public_token)) {
return null;
}
$base = rtrim((string) config('crewli.portal_url', config('app.url')), '/');
return $base.'/f/'.$this->public_token;
}
}