feat(form-builder): per-schema default_crowd_type_id replaces silent oldest() heuristic (WS-6)

Session 2's PersonProvisioner picked CrowdType::oldest() for the org —
silently wrong for multi-crowd_type orgs (Volunteer + Crew + Press are
three distinct crowd_types in one org). Schemas now declare their
target crowd_type explicitly via form_schemas.default_crowd_type_id.
RequiresDefaultCrowdType publish guard prevents misconfigured
event_registration schemas from publishing.

PersonProvisioner: oldest() fallback removed entirely. Misconfiguration
throws no_default_crowd_type at runtime; publish guard prevents it at
config time.

Migration uses a plain ulid() column without DB-level FK because
SQLite's table-rebuild on ALTER ADD FOREIGN KEY cascade-deletes
form_fields rows (form_fields.form_schema_id has cascadeOnDelete on
form_schemas). Application-level integrity via FormSchema::defaultCrowdType()
belongsTo + the publish guard + the runtime failsafe — three load-bearing
checks, none of which require the DB-level constraint.

Three pre-existing migration backfill tests bumped step counts +1 to
account for the new migration sitting between WS-5c and WS-5d:
FormFieldBindingMigrationTest (16→17, 14→15), FormFieldConfigBackfillAndDropTest
(11→12), FormFieldValidationRuleBackfillTest (14→15),
ConditionalLogicBackfillTest (5→6).

Six event_registration test fixtures updated to set default_crowd_type_id
to satisfy the new publish guard.

FormBuilderDevSeeder.resolveDefaultCrowdTypeId() — VOLUNTEER → first-active
→ create-as-needed fallback chain; documented contract for future seeders.

SCHEMA.md updated to v2.7.
Refs: RFC-WS-6.md v1.1 §3 Q8 addendum (Task 4 of this session)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-27 23:47:32 +02:00
parent 1fdd254a8a
commit d2059e3cff
20 changed files with 311 additions and 36 deletions

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* RFC-WS-6 v1.1 §3 Q8 addendum per-schema default CrowdType for
* Person provisioning. Replaces the silent oldest() heuristic from
* WS-6 session 2 with explicit configuration; RequiresDefaultCrowdType
* publish guard ensures event_registration schemas declare it.
*
* No backfill: pre-launch the table is empty. Dev seeders populate
* the column when reseeding (FormBuilderDevSeeder).
*/
return new class extends Migration
{
public function up(): void
{
// Plain nullable ULID column. NO database-level foreign key —
// SQLite's rebuild-on-FK-add cascade-deletes form_fields rows
// (form_fields.form_schema_id has cascadeOnDelete on
// form_schemas), which corrupts running migration tests.
// Application-level integrity: FormSchema::defaultCrowdType()
// belongsTo loads from CrowdType; CrowdTypeObserver could add
// a soft-handle on delete if needed in production. The new
// RequiresDefaultCrowdType publish guard plus the runtime
// failsafe in PersonProvisioner are the load-bearing checks.
Schema::table('form_schemas', function (Blueprint $table): void {
$table->ulid('default_crowd_type_id')
->nullable()
->after('purpose');
$table->index(['organisation_id', 'default_crowd_type_id'], 'fs_org_default_crowd_type_idx');
});
}
public function down(): void
{
Schema::table('form_schemas', function (Blueprint $table): void {
$table->dropIndex('fs_org_default_crowd_type_idx');
$table->dropColumn('default_crowd_type_id');
});
}
};

View File

@@ -10,6 +10,7 @@ use App\Enums\FormBuilder\FormSchemaSnapshotMode;
use App\Enums\FormBuilder\FormSubmissionMode;
use App\Enums\FormBuilder\FormSubmissionStatus;
use App\Enums\FormBuilder\FormValueStorageHint;
use App\Models\CrowdType;
use App\Models\Event;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormSchema;
@@ -132,6 +133,7 @@ final class FormBuilderDevSeeder
'name' => $event->name.' — registratie',
'slug' => Str::slug($event->slug.'-registratie'),
'purpose' => FormPurpose::EVENT_REGISTRATION,
'default_crowd_type_id' => self::resolveDefaultCrowdTypeId((string) $event->organisation_id),
'description' => "Registratieformulier voor {$event->name}.",
'is_published' => in_array(
$event->status,
@@ -391,6 +393,7 @@ final class FormBuilderDevSeeder
'name' => $name,
'slug' => $slug,
'purpose' => FormPurpose::EVENT_REGISTRATION,
'default_crowd_type_id' => self::resolveDefaultCrowdTypeId((string) $org->id),
'description' => "Demo-formulier voor het end-to-end doorlopen van de vrijwilligersregistratie voor {$event->name}.",
'is_published' => true,
'submission_mode' => FormSubmissionMode::DRAFT_SINGLE,
@@ -615,6 +618,46 @@ final class FormBuilderDevSeeder
->count();
}
/**
* RFC-WS-6 v1.1 §3 Q8 addendum event_registration schemas declare
* a target CrowdType. DevSeeder picks the VOLUNTEER system_type
* (DevSeeder::run() always seeds it for every dev org); falls back
* to the first active CrowdType if VOLUNTEER is somehow missing,
* and creates a minimal CrowdType row as last-resort to satisfy the
* NOT NULL FK on Person.crowd_type_id.
*
* Future seeders MUST keep this lookup contract: schemas need a
* resolvable CrowdType per org or the RequiresDefaultCrowdType
* publish guard will block publish.
*/
private static function resolveDefaultCrowdTypeId(string $organisationId): string
{
$crowdType = CrowdType::query()
->withoutGlobalScopes()
->where('organisation_id', $organisationId)
->where('is_active', true)
->where('system_type', 'VOLUNTEER')
->first()
?? CrowdType::query()
->withoutGlobalScopes()
->where('organisation_id', $organisationId)
->where('is_active', true)
->oldest()
->first();
if ($crowdType === null) {
$crowdType = CrowdType::create([
'organisation_id' => $organisationId,
'name' => 'Vrijwilliger',
'system_type' => 'VOLUNTEER',
'color' => '#10b981',
'is_active' => true,
]);
}
return (string) $crowdType->id;
}
private static function uniqueSchemaSlug(Organisation $org, string $base): string
{
$candidate = $base;