--wip-- [skip ci]
This commit is contained in:
parent
c6fcd56623
commit
6202ede59c
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace App\Form\Actions;
|
||||
|
||||
use App\Contribution\Contracts\HasContributionData;
|
||||
use App\Contribution\ContributionFactory;
|
||||
use App\Form\Models\Form;
|
||||
use App\Form\Requests\FormCompileRequest;
|
||||
use App\Rules\JsonBase64Rule;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Lorisleiva\Actions\ActionRequest;
|
||||
use Lorisleiva\Actions\Concerns\AsAction;
|
||||
use Zoomyboy\Tex\BaseCompiler;
|
||||
use Zoomyboy\Tex\Tex;
|
||||
|
||||
class GenerateContributionAction
|
||||
{
|
||||
use AsAction;
|
||||
|
||||
public function handle(HasContributionData $request): BaseCompiler
|
||||
{
|
||||
return Tex::compile($request->type()::fromPayload($request));
|
||||
}
|
||||
|
||||
public function asController(ActionRequest $request, Form $form): BaseCompiler|JsonResponse
|
||||
{
|
||||
$r = FormCompileRequest::from(['form' => $form]);
|
||||
app(ContributionFactory::class)->validateType($r);
|
||||
$r->validateContribution();
|
||||
|
||||
return $request->input('validate')
|
||||
? response()->json([])
|
||||
: $this->handle($r);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'payload' => [new JsonBase64Rule()],
|
||||
];
|
||||
}
|
||||
}
|
|
@ -114,7 +114,12 @@ class FieldCollection extends Collection
|
|||
return $this->map(fn ($field) => $field->presentRaw())->toArray();
|
||||
}
|
||||
|
||||
private function findBySpecialType(SpecialType $specialType): ?Field
|
||||
public function hasSpecialType(SpecialType $specialType): bool
|
||||
{
|
||||
return $this->findBySpecialType($specialType) !== null;
|
||||
}
|
||||
|
||||
public function findBySpecialType(SpecialType $specialType): ?Field
|
||||
{
|
||||
return $this->first(fn ($field) => $field->specialType === $specialType);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
|
||||
namespace App\Form\Requests;
|
||||
|
||||
use App\Contribution\Contracts\HasContributionData;
|
||||
use App\Contribution\Data\MemberData;
|
||||
use App\Contribution\Documents\ContributionDocument;
|
||||
use App\Country;
|
||||
use App\Form\Enums\SpecialType;
|
||||
use App\Form\Models\Form;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Spatie\LaravelData\Data;
|
||||
|
||||
class FormCompileRequest extends Data implements HasContributionData {
|
||||
|
||||
public function __construct(public Form $form) {}
|
||||
|
||||
/**
|
||||
* @return class-string<ContributionDocument>
|
||||
*/
|
||||
public function type(): string
|
||||
{
|
||||
return request()->input('type');
|
||||
}
|
||||
|
||||
public function dateFrom(): Carbon
|
||||
{
|
||||
return $this->form->from;
|
||||
}
|
||||
|
||||
public function dateUntil(): Carbon
|
||||
{
|
||||
return $this->form->to;
|
||||
}
|
||||
|
||||
public function zipLocation(): string
|
||||
{
|
||||
return $this->form->zip.' '.$this->form->location;
|
||||
}
|
||||
|
||||
public function eventName(): string
|
||||
{
|
||||
return $this->form->name;
|
||||
}
|
||||
|
||||
public function members(): Collection
|
||||
{
|
||||
$members = [];
|
||||
$fields = [
|
||||
[SpecialType::FIRSTNAME, 'firstname'],
|
||||
[SpecialType::LASTNAME, 'lastname'],
|
||||
[SpecialType::BIRTHDAY, 'birthday'],
|
||||
[SpecialType::ADDRESS, 'address'],
|
||||
[SpecialType::ZIP, 'zip'],
|
||||
[SpecialType::LOCATION, 'location']
|
||||
];
|
||||
|
||||
foreach ($this->form->participants as $participant) {
|
||||
$member = [];
|
||||
foreach ($fields as [$type, $name]) {
|
||||
$f = $this->form->getFields()->findBySpecialType($type);
|
||||
$member[$name] = $participant->getFields()->find($f)->value;
|
||||
}
|
||||
|
||||
$members[] = [
|
||||
'is_leader' => false,
|
||||
'gender' => 'weiblich',
|
||||
...$member,
|
||||
];
|
||||
}
|
||||
|
||||
return MemberData::fromApi($members);
|
||||
}
|
||||
|
||||
public function country(): ?Country
|
||||
{
|
||||
return Country::first();
|
||||
}
|
||||
|
||||
public function validateContribution(): void
|
||||
{
|
||||
Validator::make($this->form->toArray(), [
|
||||
'zip' => 'required',
|
||||
'location' => 'required'
|
||||
])
|
||||
->after(function($validator) {
|
||||
foreach ($this->type()::requiredFormSpecialTypes() as $type) {
|
||||
if (!$this->form->getFields()->hasSpecialType($type)) {
|
||||
$validator->errors()->add($type->name, 'Kein Feld für ' . $type->value . ' vorhanden.');
|
||||
}
|
||||
}
|
||||
if ($this->form->participants->count() === 0) {
|
||||
$validator->errors()->add('participants', 'Veranstaltung besitzt noch keine Teilnehmer*innen.');
|
||||
}
|
||||
})
|
||||
->validate();
|
||||
}
|
||||
}
|
|
@ -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:
|
||||
|
|
|
@ -0,0 +1,164 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\Form;
|
||||
|
||||
use App\Contribution\Documents\CitySolingenDocument;
|
||||
use App\Contribution\Documents\RdpNrwDocument;
|
||||
use App\Country;
|
||||
use App\Form\Enums\SpecialType;
|
||||
use App\Form\Models\Form;
|
||||
use App\Form\Models\Participant;
|
||||
use App\Gender;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Tests\Lib\CreatesFormFields;
|
||||
use Zoomyboy\Tex\Tex;
|
||||
|
||||
uses(DatabaseTransactions::class);
|
||||
uses(CreatesFormFields::class);
|
||||
|
||||
beforeEach(function() {
|
||||
Country::factory()->create();
|
||||
Gender::factory()->male()->create();
|
||||
Gender::factory()->female()->create();
|
||||
});
|
||||
|
||||
it('doesnt create document when no special fields given', function (array $fields, string $field, string $message, string $type) {
|
||||
$this->login()->loginNami();
|
||||
|
||||
$form = Form::factory()
|
||||
->fields($fields)
|
||||
->has(Participant::factory())
|
||||
->create();
|
||||
|
||||
$this->json('GET', route('form.contribution', [
|
||||
'type' => $type,
|
||||
'form' => $form,
|
||||
'validate' => '1',
|
||||
]))->assertJsonValidationErrors([$field => $message]);
|
||||
})
|
||||
->with([
|
||||
[fn() => [], 'FIRSTNAME', 'Kein Feld für Vorname vorhanden.'],
|
||||
[fn() => [test()->textField('f')->specialType(SpecialType::FIRSTNAME)], 'LASTNAME', 'Kein Feld für Nachname vorhanden.'],
|
||||
[fn() => [test()->textField('f')->specialType(SpecialType::FIRSTNAME), test()->textField('l')->specialType(SpecialType::LASTNAME)], 'BIRTHDAY', 'Kein Feld für Geburtsdatum vorhanden.'],
|
||||
[fn() => [test()->textField('f')->specialType(SpecialType::FIRSTNAME), test()->textField('l')->specialType(SpecialType::LASTNAME), test()->dateField('b')->specialType(SpecialType::BIRTHDAY)], 'ZIP', 'Kein Feld für PLZ vorhanden.'],
|
||||
[fn() => [test()->textField('f')->specialType(SpecialType::FIRSTNAME), test()->textField('l')->specialType(SpecialType::LASTNAME), test()->dateField('b')->specialType(SpecialType::BIRTHDAY), test()->dateField('p')->specialType(SpecialType::ZIP)], 'LOCATION', 'Kein Feld für Ort vorhanden.'],
|
||||
])->with('contribution-documents');
|
||||
|
||||
it('validates special types of each document', function (string $type, array $fields, string $field, string $message) {
|
||||
$this->login()->loginNami();
|
||||
|
||||
$form = Form::factory()->fields([
|
||||
test()->textField('f')->specialType(SpecialType::FIRSTNAME),
|
||||
test()->textField('l')->specialType(SpecialType::LASTNAME),
|
||||
test()->dateField('b')->specialType(SpecialType::BIRTHDAY),
|
||||
test()->dateField('p')->specialType(SpecialType::ZIP),
|
||||
test()->dateField('l')->specialType(SpecialType::LOCATION),
|
||||
...$fields,
|
||||
])
|
||||
->has(Participant::factory())
|
||||
->create();
|
||||
|
||||
$this->json('GET', route('form.contribution', [
|
||||
'type' => $type,
|
||||
'form' => $form,
|
||||
'validate' => '1',
|
||||
]))->assertJsonValidationErrors([$field => $message]);
|
||||
})
|
||||
->with([
|
||||
[CitySolingenDocument::class, [], 'ADDRESS', 'Kein Feld für Adresse vorhanden.'],
|
||||
[RdpNrwDocument::class, [], 'GENDER', 'Kein Feld für Geschlecht vorhanden.'],
|
||||
]);
|
||||
|
||||
it('throws error when not validating but fields are not present', function () {
|
||||
$this->login()->loginNami();
|
||||
|
||||
$form = Form::factory()->fields([])
|
||||
->has(Participant::factory())
|
||||
->create();
|
||||
|
||||
$this->json('GET', route('form.contribution', [
|
||||
'type' => CitySolingenDocument::class,
|
||||
'form' => $form,
|
||||
]))->assertStatus(422);
|
||||
});
|
||||
|
||||
it('throws error when form doesnt have meta', function () {
|
||||
$this->login()->loginNami();
|
||||
|
||||
$form = Form::factory()->fields([])
|
||||
->has(Participant::factory())
|
||||
->zip('')
|
||||
->location('')
|
||||
->create();
|
||||
|
||||
$this->json('GET', route('form.contribution', [
|
||||
'type' => CitySolingenDocument::class,
|
||||
'form' => $form,
|
||||
]))->assertStatus(422)->assertJsonValidationErrors([
|
||||
'zip' => 'PLZ ist erforderlich.',
|
||||
'location' => 'Ort ist erforderlich.'
|
||||
]);
|
||||
});
|
||||
|
||||
it('throws error when form doesnt have participants', function () {
|
||||
$this->login()->loginNami();
|
||||
|
||||
$form = Form::factory()->fields([])->create();
|
||||
|
||||
$this->json('GET', route('form.contribution', [
|
||||
'type' => CitySolingenDocument::class,
|
||||
'form' => $form,
|
||||
'validate' => '1',
|
||||
]))->assertJsonValidationErrors(['participants' => 'Veranstaltung besitzt noch keine Teilnehmer*innen.']);
|
||||
});
|
||||
|
||||
it('creates document when fields are present', function () {
|
||||
Tex::spy();
|
||||
$this->login()->loginNami();
|
||||
|
||||
$form = Form::factory()->fields([
|
||||
test()->textField('fn')->specialType(SpecialType::FIRSTNAME),
|
||||
test()->textField('ln')->specialType(SpecialType::LASTNAME),
|
||||
test()->dateField('bd')->specialType(SpecialType::BIRTHDAY),
|
||||
test()->dateField('zip')->specialType(SpecialType::ZIP),
|
||||
test()->dateField('loc')->specialType(SpecialType::LOCATION),
|
||||
test()->dateField('add')->specialType(SpecialType::ADDRESS),
|
||||
])
|
||||
->has(Participant::factory()->data(['fn' => 'Baum', 'ln' => 'Muster', 'bd' => '1991-05-06', 'zip' => '33333', 'loc' => 'Musterstadt', 'add' => 'Laastr 4']))
|
||||
->create();
|
||||
|
||||
$this->json('GET', route('form.contribution', [
|
||||
'type' => CitySolingenDocument::class,
|
||||
'form' => $form,
|
||||
]))->assertOk();
|
||||
Tex::assertCompiled(CitySolingenDocument::class, fn($document) => $document->hasAllContent(['Baum', 'Muster', '1991', 'Musterstadt', 'Laastr 4', '33333']));
|
||||
});
|
||||
|
||||
|
||||
it('creates document with form meta', function () {
|
||||
Tex::spy();
|
||||
$this->login()->loginNami();
|
||||
|
||||
$form = Form::factory()->fields([
|
||||
test()->textField('fn')->specialType(SpecialType::FIRSTNAME),
|
||||
test()->textField('ln')->specialType(SpecialType::LASTNAME),
|
||||
test()->dateField('bd')->specialType(SpecialType::BIRTHDAY),
|
||||
test()->dateField('zip')->specialType(SpecialType::ZIP),
|
||||
test()->dateField('loc')->specialType(SpecialType::LOCATION),
|
||||
test()->dateField('add')->specialType(SpecialType::ADDRESS),
|
||||
test()->dateField('gen')->specialType(SpecialType::GENDER),
|
||||
])
|
||||
->has(Participant::factory()->data(['fn' => 'Baum', 'ln' => 'Muster', 'bd' => '1991-05-06', 'zip' => '33333', 'loc' => 'Musterstadt', 'add' => 'Laastr 4', 'gen' => 'weiblich']))
|
||||
->name('Sommerlager')
|
||||
->from('2008-06-20')
|
||||
->to('2008-06-22')
|
||||
->zip('12345')
|
||||
->location('Frankfurt')
|
||||
->create();
|
||||
|
||||
$this->json('GET', route('form.contribution', [
|
||||
'type' => RdpNrwDocument::class,
|
||||
'form' => $form,
|
||||
]))->assertOk();
|
||||
Tex::assertCompiled(RdpNrwDocument::class, fn($document) => $document->hasAllContent(['20.06.2008', '22.06.2008', '12345 Frankfurt']));
|
||||
});
|
Loading…
Reference in New Issue