diff --git a/app/Contribution/ContributionFactory.php b/app/Contribution/ContributionFactory.php index 5a0f4d89..c5bf5a09 100644 --- a/app/Contribution/ContributionFactory.php +++ b/app/Contribution/ContributionFactory.php @@ -63,10 +63,12 @@ class ContributionFactory } public function validateType(HasContributionData $request) { + // @todo remove payload from hasContributionData and move the entire validation logic to the action Validator::make(['type' => $request->type()], $this->typeRule())->validate(); } public function validatePayload(HasContributionData $request) { + // @todo remove payload from hasContributionData and move the entire validation logic to the action Validator::make($request->payload(), $this->rules($request->type()))->validate(); } } diff --git a/app/Form/Actions/GenerateContributionAction.php b/app/Form/Actions/GenerateContributionAction.php new file mode 100644 index 00000000..a885b44c --- /dev/null +++ b/app/Form/Actions/GenerateContributionAction.php @@ -0,0 +1,43 @@ +type()::fromPayload($request)); + } + + public function asController(Form $form): BaseCompiler|JsonResponse + { + app(ContributionFactory::class)->validateType($form); + app(ContributionFactory::class)->validatePayload($form); + dd('I'); + + return $request->input('validate') + ? response()->json([]) + : $this->handle($request); + } + + /** + * @return array + */ + public function rules(): array + { + return [ + 'payload' => [new JsonBase64Rule()], + ]; + } +} diff --git a/app/Form/Models/Form.php b/app/Form/Models/Form.php index ca2681b5..c0cac643 100644 --- a/app/Form/Models/Form.php +++ b/app/Form/Models/Form.php @@ -2,6 +2,8 @@ namespace App\Form\Models; +use App\Contribution\Contracts\HasContributionData; +use App\Country; use App\Form\Actions\UpdateParticipantSearchIndexAction; use App\Form\Data\ExportData; use App\Form\Data\FieldCollection; @@ -9,11 +11,14 @@ use App\Form\Data\FormConfigData; use App\Lib\Editor\Condition; use App\Lib\Editor\EditorData; use App\Lib\Sorting; +use App\Member\Data\MemberData; +use Carbon\Carbon; use Cviebrock\EloquentSluggable\Sluggable; use Database\Factories\Form\Models\FormFactory; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Support\Collection; use Laravel\Scout\Searchable; use Spatie\Image\Enums\Fit; use Spatie\MediaLibrary\HasMedia; @@ -22,7 +27,7 @@ use Spatie\MediaLibrary\MediaCollections\Models\Media; use Zoomyboy\MedialibraryHelper\DefersUploads; /** @todo replace editor content with EditorData cast */ -class Form extends Model implements HasMedia +class Form extends Model implements HasMedia, HasContributionData { /** @use HasFactory */ use HasFactory; @@ -201,4 +206,45 @@ class Form extends Model implements HasMedia return true; } + + public function payload(): array + { + return [ + 'dateFrom' => $this->dateFrom()->format('Y-m-d'), + 'eventName' => $this->name, + 'members' => [], + ]; + } + + /** + * @return class-string + */ + public function type(): string + { + return request()->input('type'); + } + + public function dateFrom(): Carbon { + return now(); + } + + public function dateUntil(): Carbon { + return now(); + } + + public function zipLocation(): string { + return ''; + } + + public function eventName(): string { + return $this->name; + } + + public function members(): Collection { + return MemberData::fromApi([]); + } + + public function country(): ?Country { + return Country::first(); + } } diff --git a/docker-compose.yml b/docker-compose.yml index aa27c717..48522bec 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -92,6 +92,8 @@ services: socketi: image: quay.io/soketi/soketi:89604f268623cf799573178a7ba56b7491416bde-16-debian + ports: + - "6001:6001" environment: SOKETI_DEFAULT_APP_ID: adremaid SOKETI_DEFAULT_APP_KEY: adremakey @@ -104,6 +106,8 @@ services: meilisearch: image: getmeili/meilisearch:v1.6 + ports: + - "7700:7700" volumes: - ./data/meilisearch:/meili_data env_file: diff --git a/routes/web.php b/routes/web.php index 0c8c488a..65846888 100644 --- a/routes/web.php +++ b/routes/web.php @@ -34,6 +34,7 @@ use App\Form\Actions\FormtemplateStoreAction; use App\Form\Actions\FormtemplateUpdateAction; use App\Form\Actions\FormUpdateAction; use App\Form\Actions\FormUpdateMetaAction; +use App\Form\Actions\GenerateContributionAction; use App\Form\Actions\IsDirtyAction; use App\Form\Actions\ParticipantAssignAction; use App\Form\Actions\ParticipantDestroyAction; @@ -178,6 +179,7 @@ Route::group(['middleware' => 'auth:web'], function (): void { Route::patch('/participant/{participant}', ParticipantUpdateAction::class)->name('participant.update'); Route::post('/form/{form}/participant', ParticipantStoreAction::class)->name('form.participant.store'); Route::post('/form/{form}/copy', FormCopyAction::class)->name('form.copy'); + Route::get('/form/{form}/contribution', GenerateContributionAction::class)->name('form.contribution'); // ------------------------------------ fileshare ----------------------------------- Route::post('/fileshare', FileshareStoreAction::class)->name('fileshare.store'); diff --git a/tests/Feature/Form/GenerateContributionTest.php b/tests/Feature/Form/GenerateContributionTest.php new file mode 100644 index 00000000..3abbf7b8 --- /dev/null +++ b/tests/Feature/Form/GenerateContributionTest.php @@ -0,0 +1,116 @@ +login()->loginNami(); + + $form = Form::factory() + ->fields([]) + ->has(Participant::factory()) + ->create(); + + $this->json('GET', route('form.contribution', [ + 'type' => CitySolingenDocument::class, + 'form' => $form, + 'validate' => '1', + ]))->assertJsonValidationErrors(['firstname' => 'Kein Feld für Vorname vorhanden']); +}); + +// it('compiles documents via base64 param', function (string $type, array $bodyChecks) { +// $this->withoutExceptionHandling(); +// Tex::spy(); +// $this->login()->loginNami(); +// $member1 = Member::factory()->defaults()->male()->create(['address' => 'Maxstr 44', 'zip' => '42719', 'firstname' => 'Max', 'lastname' => 'Muster']); +// $member2 = Member::factory()->defaults()->female()->create(['address' => 'Maxstr 44', 'zip' => '42719', 'firstname' => 'Jane', 'lastname' => 'Muster']); +// +// $response = $this->call('GET', '/contribution-generate', [ +// 'payload' => ContributionRequestFactory::new()->type($type)->state([ +// 'dateFrom' => '1991-06-15', +// 'dateUntil' => '1991-06-16', +// 'eventName' => 'Super tolles Lager', +// 'members' => [$member1->id, $member2->id], +// 'type' => $type, +// 'zipLocation' => '42777 SG', +// ])->toBase64(), +// ]); +// +// $response->assertSessionDoesntHaveErrors(); +// $response->assertOk(); +// Tex::assertCompiled($type, fn ($document) => $document->hasAllContent($bodyChecks)); +// })->with('contribution-assertions'); +// +// it('testItCompilesGroupNameInSolingenDocument', function () { +// $this->withoutExceptionHandling()->login()->loginNami(); +// Tex::spy(); +// InvoiceSettings::fake(['from_long' => 'Stamm BiPi']); +// +// $this->call('GET', '/contribution-generate', [ +// 'payload' => ContributionRequestFactory::new()->type(CitySolingenDocument::class)->toBase64(), +// ]); +// +// Tex::assertCompiled(CitySolingenDocument::class, fn ($document) => $document->hasAllContent(['Stamm BiPi'])); +// }); +// +// it('testItCompilesContributionDocumentsViaApi', function (string $type, array $bodyChecks) { +// $this->withoutExceptionHandling(); +// Tex::spy(); +// Gender::factory()->female()->create(); +// Gender::factory()->male()->create(); +// Passport::actingAsClient(Client::factory()->create(), ['contribution-generate']); +// +// $response = $this->postJson('/api/contribution-generate', [ +// 'country' => Country::factory()->create()->id, +// 'dateFrom' => '1991-06-15', +// 'dateUntil' => '1991-06-16', +// 'eventName' => 'Super tolles Lager', +// 'type' => $type, +// 'zipLocation' => '42777 SG', +// 'member_data' => [ +// ContributionMemberApiRequestFactory::new()->create(['address' => 'Maxstr 44', 'zip' => '42719', 'firstname' => 'Max', 'lastname' => 'Muster']), +// ContributionMemberApiRequestFactory::new()->create(['address' => 'Maxstr 44', 'zip' => '42719', 'firstname' => 'Jane', 'lastname' => 'Muster']), +// ], +// ]); +// +// $response->assertSessionDoesntHaveErrors(); +// $response->assertOk(); +// Tex::assertCompiled($type, fn ($document) => $document->hasAllContent($bodyChecks)); +// })->with('contribution-assertions'); +// +// it('testInputShouldBeBase64EncodedJson', function (string $payload) { +// $this->login()->loginNami(); +// +// $this->call('GET', '/contribution-generate', ['payload' => $payload])->assertSessionHasErrors('payload'); +// })->with([ +// [""], +// ["aaaa"], +// ["YWFhCg=="], +// ]); +// +// it('testItValidatesInput', function (array $input, string $documentClass, string $errorField) { +// $this->login()->loginNami(); +// Country::factory()->create(); +// Member::factory()->defaults()->create(); +// +// $this->postJson('/contribution-validate', ContributionRequestFactory::new()->type($documentClass)->state($input)->create()) +// ->assertJsonValidationErrors($errorField); +// })->with('contribution-validation'); +// +// it('testItValidatesInputBeforeGeneration', function (array $input, string $documentClass, string $errorField) { +// $this->login()->loginNami(); +// Country::factory()->create(); +// Member::factory()->defaults()->create(); +// +// $this->call('GET', '/contribution-generate', [ +// 'payload' => ContributionRequestFactory::new()->type($documentClass)->state($input)->toBase64(), +// ])->assertSessionHasErrors($errorField); +// })->with('contribution-validation');