Add Contribution Generation for Forms

This commit is contained in:
philipp lang 2025-07-08 22:57:39 +02:00
parent 144af1bf71
commit 10b71f7a36
13 changed files with 403 additions and 15 deletions

View File

@ -34,8 +34,8 @@ class ContributionFactory
public function compilerSelect(): Collection
{
return collect($this->documents)->map(fn ($document) => [
'title' => $document::buttonName(),
'class' => $document,
'name' => $document::getName(),
'id' => $document,
]);
}

View File

@ -37,11 +37,6 @@ abstract class ContributionDocument extends Document
];
}
public static function buttonName(): string
{
return 'Für ' . static::getName() . ' erstellen';;
}
public function setEventName(string $eventName): void
{
$this->eventName = $eventName;

View File

@ -16,7 +16,7 @@ class GenerateRequest extends ActionRequest implements HasContributionData {
/**
* @return array<string, string>
*/
protected function payloada(): array
protected function payload(): array
{
return json_decode(rawurldecode(base64_decode($this->input('payload', ''))), true);
}

View File

@ -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()],
];
}
}

View File

@ -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);
}

View File

@ -0,0 +1,103 @@
<?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
{
$payload = json_decode(rawurldecode(base64_decode(request()->input('payload', ''))), true);
return $payload['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'],
[SpecialType::GENDER, 'gender']
];
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();
}
}

View File

@ -15,6 +15,7 @@ use App\Group;
use App\Lib\Editor\EditorData;
use App\Lib\HasMeta;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Contribution\ContributionFactory;
/**
* @mixin Form
@ -66,6 +67,7 @@ class FormResource extends JsonResource
'frontend' => str(app(FormSettings::class)->registerUrl)->replace('{slug}', $this->slug),
'export' => route('form.export', $this->getModel()),
'copy' => route('form.copy', $this->getModel()),
'contribution' => route('form.contribution', $this->getModel()),
]
];
}
@ -88,6 +90,7 @@ class FormResource extends JsonResource
'namiTypes' => NamiType::forSelect(),
'specialTypes' => SpecialType::forSelect(),
'countries' => Country::forSelect(),
'contribution_types' => app(ContributionFactory::class)->compilerSelect(),
'default' => [
'description' => [],
'is_active' => true,

View File

@ -1,11 +1,15 @@
<template>
<ui-popup v-for="(popup, index) in swal.popups" :key="index" :icon="popup.icon" :heading="popup.title" @close="popup.reject(popup.id)">
<div class="text-center mt-4" v-text="popup.body" />
<div class="mt-4">
<div class="text-center" v-text="popup.body" />
<div class="flex justify-center space-x-4 mt-8">
<ui-button type="button" class="btn-primary" @click.prevent="popup.resolve(popup.id)">{{ popup.confirmButton }}</ui-button>
<ui-button type="button" class="btn-default" @click.prevent="popup.reject(popup.id)">{{ popup.cancelButton }}</ui-button>
</div>
<template v-for="field in popup.fields">
<f-text v-if="field.type === 'text'" :id="field.name" :key="field.name" v-model="popup.payload[field.name]" :name="field.name" :label="field.label" />
<f-select v-if="field.type === 'select'" :id="field.name" :key="field.name" v-model="popup.payload[field.name]" :name="field.name" :label="field.label" :options="field.options" />
</template>
</div>
<div class="flex justify-center space-x-4 mt-6">
<ui-button type="button" class="btn-primary" @click.prevent="popup.resolve(popup.id)">{{ popup.confirmButton }}</ui-button>
<ui-button type="button" class="btn-default" @click.prevent="popup.reject(popup.id)">{{ popup.cancelButton }}</ui-button>
</div>
</ui-popup>
</template>

View File

@ -1,6 +1,7 @@
import { defineStore } from 'pinia';
import { v4 as uuidv4 } from 'uuid';
type Payload = Record<string, string|null>;
interface Popup {
id: string;
@ -11,6 +12,16 @@ interface Popup {
cancelButton: string;
resolve: (id: string) => void;
reject: (id: string) => void;
fields: SwalField[];
payload: Payload;
}
interface SwalField {
name: string;
label: string;
required: boolean;
type: 'select' | 'text';
options: [],
}
export default defineStore('swal', {
@ -30,6 +41,8 @@ export default defineStore('swal', {
reject,
id: uuidv4(),
icon: 'warning-triangle-light',
fields: [],
payload: {},
});
}).then((id) => {
this.remove(id);
@ -41,8 +54,40 @@ export default defineStore('swal', {
});
},
ask(title: string, body: string, fields: SwalField[] = []): Promise<Payload> {
return new Promise<Payload>((resolve, reject) => {
new Promise<string>((resolve, reject) => {
const payload: Payload = {};
fields.forEach(f => payload[f.name] = null);
this.popups.push({
title,
body,
confirmButton: 'Okay',
cancelButton: 'Abbrechen',
resolve,
reject,
id: uuidv4(),
icon: 'warning-triangle-light',
fields: fields,
payload: payload,
});
}).then((id) => {
const p = this.find(id)?.payload;
this.remove(id);
resolve(p || {});
}).catch((id) => {
this.remove(id);
reject();
});
});
},
remove(id: string) {
this.popups = this.popups.filter(p => p.id !== id);
},
find(id: string): Popup|undefined {
return this.popups.find(p => p.id === id);
}
},
});

View File

@ -25,7 +25,7 @@
</div>
</div>
<button v-for="(compiler, index) in compilers" :key="index" class="btn btn-primary mt-3 inline-block" @click.prevent="download('/contribution-generate', {...values, type: compiler.class})" v-text="compiler.title" />
<button v-for="(compiler, index) in compilers" :key="index" class="btn btn-primary mt-3 inline-block" @click.prevent="download('/contribution-generate', {...values, type: compiler.id})" v-text="`Für ${compiler.name} erstellen`" />
</form>
</page-layout>
</template>

View File

@ -166,6 +166,7 @@
<ui-action-button tooltip="Teilnehmende anzeigen" class="btn-info" icon="user" @click.prevent="showParticipants(form)" />
<ui-action-button :href="form.links.frontend" target="_BLANK" tooltip="zur Anmeldeseite" class="btn-info" icon="eye" />
<ui-action-button tooltip="Kopieren" class="btn-info" icon="copy" @click="onCopy(form)" />
<ui-action-button tooltip="Zuschuss-Liste erstellen" class="btn-info" icon="contribution" @click="onGenerateContribution(form)" />
<ui-action-button :href="form.links.export" target="_BLANK" tooltip="als Tabellendokument exportieren" class="btn-info" icon="document" />
<ui-action-button tooltip="Löschen" class="btn-danger" icon="trash" @click.prevent="onDelete(form)" />
</div>
@ -188,11 +189,13 @@ import Conditions from './Conditions.vue';
import ConditionsForm from './ConditionsForm.vue';
import { useToast } from 'vue-toastification';
import useSwal from '@/stores/swalStore.ts';
import useDownloads from '@/composables/useDownloads.ts';
const props = defineProps(indexProps);
const { meta, data, reloadPage, reload, create, single, edit, cancel, submit, remove, getFilter, setFilter } = useIndex(props.data, 'form');
const axios = inject('axios');
const toast = useToast();
const {download} = useDownloads();
const showing = ref(null);
const fileSettingPopup = ref(null);
@ -224,6 +227,20 @@ async function onCopy(form) {
reload(false);
}
async function onGenerateContribution(form) {
const response = await swal.ask('Zuschussliste erstellen', 'Hiermit erstellst du eine Zuschussliste mit allen angemeldeten Mitgliedern. Bite wähle aus, für welche Organisation du eine Liste erstellen willst.', [
{
name: 'type',
label: 'Organisation',
required: true,
type: 'select',
options: meta.value.contribution_types,
}
]);
await download(form.links.contribution, {type: response.type, validate: '1'});
await download(form.links.contribution, {type: response.type});
}
async function onDelete(form) {
await swal.confirm('Diese Veranstaltung löschen?', `Die Veranstaltung ${form.name} wird gelöscht werden.`);
await remove(form);

View File

@ -2,6 +2,7 @@
namespace Tests\EndToEnd\Form;
use App\Contribution\Documents\RdpNrwDocument;
use App\Contribution\Enums\Country;
use App\Fileshare\Data\FileshareResourceData;
use App\Form\Data\ExportData;
@ -65,6 +66,7 @@ it('testItDisplaysForms', function () {
->assertInertiaPath('data.data.0.registration_until', '2023-04-01 05:00:00')
->assertInertiaPath('data.data.0.links.participant_index', route('form.participant.index', ['form' => $form]))
->assertInertiaPath('data.data.0.links.export', route('form.export', ['form' => $form]))
->assertInertiaPath('data.data.0.links.contribution', route('form.contribution', ['form' => $form]))
->assertInertiaPath('data.meta.links.store', route('form.store'))
->assertInertiaPath('data.meta.links.formtemplate_index', route('formtemplate.index'))
->assertInertiaPath('data.meta.default.name', '')
@ -86,6 +88,8 @@ it('testItDisplaysForms', function () {
->assertInertiaPath('data.meta.namiTypes.0', ['id' => 'Vorname', 'name' => 'Vorname'])
->assertInertiaPath('data.meta.specialTypes.0', ['id' => 'Vorname', 'name' => 'Vorname'])
->assertInertiaPath('data.meta.section_default.name', '')
->assertInertiaPath('data.meta.contribution_types.0.id', RdpNrwDocument::class)
->assertInertiaPath('data.meta.contribution_types.0.name', 'RdP NRW')
->assertInertiaPath('data.meta.default.zip', '')
->assertInertiaPath('data.meta.default.location', '');
});

View File

@ -0,0 +1,167 @@
<?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\Form\Requests\FormCompileRequest;
use App\Gender;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\Lib\CreatesFormFields;
use Zoomyboy\Tex\Tex;
uses(DatabaseTransactions::class);
uses(CreatesFormFields::class);
mutates(FormCompileRequest::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']));
});