Add payload for ContributionController
continuous-integration/drone/push Build is passing Details

This commit is contained in:
philipp lang 2023-03-09 02:14:24 +01:00
parent 7ee1102a5d
commit 8069e2b4dd
6 changed files with 89 additions and 62 deletions

View File

@ -5,6 +5,7 @@ namespace App\Contribution;
use App\Contribution\Documents\SolingenDocument;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Zoomyboy\Tex\BaseCompiler;
use Zoomyboy\Tex\Tex;
@ -12,9 +13,22 @@ class ContributionController extends Controller
{
public function generate(Request $request): BaseCompiler
{
/** @var class-string<SolingenDocument> */
$type = $request->query('type');
$payload = json_decode(base64_decode($request->input('payload', '')), true);
return Tex::compile($type::fromRequest($request));
$validated = Validator::make($payload, [
'dateFrom' => 'required|date|date_format:Y-m-d',
'dateUntil' => 'required|date|date_format:Y-m-d',
'country' => 'required|exists:countries,id',
'eventName' => 'required|max:100',
'members' => 'array',
'members.*' => 'integer|exists:members,id',
'type' => 'required|string',
'zipLocation' => 'required|string',
])->validate();
/** @var class-string<SolingenDocument> */
$type = $validated['type'];
return Tex::compile($type::fromRequest($validated));
}
}

View File

@ -6,7 +6,6 @@ use App\Country;
use App\Member\Member;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Http\Request;
use Zoomyboy\Tex\Engine;
use Zoomyboy\Tex\Template;
@ -33,14 +32,17 @@ class DvDocument extends ContributionDocument
.Carbon::parse($this->dateUntil)->format('d.m.Y');
}
public static function fromRequest(Request $request): self
/**
* @param array<string, string|int> $request
*/
public static function fromRequest(array $request): self
{
return new self(
dateFrom: $request->dateFrom,
dateUntil: $request->dateUntil,
zipLocation: $request->zipLocation,
country: Country::where('id', $request->country)->firstOrFail(),
members: Member::whereIn('id', $request->members)->orderByRaw('lastname, firstname')->get()->toBase()->chunk(17),
dateFrom: $request['dateFrom'],
dateUntil: $request['dateUntil'],
zipLocation: $request['zipLocation'],
country: Country::where('id', $request['country'])->firstOrFail(),
members: Member::whereIn('id', $request['members'])->orderByRaw('lastname, firstname')->get()->toBase()->chunk(17),
);
}

View File

@ -6,7 +6,6 @@ use App\Country;
use App\Member\Member;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Http\Request;
use Zoomyboy\Tex\Engine;
use Zoomyboy\Tex\Template;
@ -38,15 +37,18 @@ class RemscheidDocument extends ContributionDocument
return Carbon::parse($this->dateUntil)->format('d.m.Y');
}
public static function fromRequest(Request $request): self
/**
* @param array<string, string|int> $request
*/
public static function fromRequest(array $request): self
{
[$leaders, $children] = Member::whereIn('id', $request->members)->orderByRaw('lastname, firstname')->get()->partition(fn ($member) => $member->isLeader());
[$leaders, $children] = Member::whereIn('id', $request['members'])->orderByRaw('lastname, firstname')->get()->partition(fn ($member) => $member->isLeader());
return new self(
dateFrom: $request->dateFrom,
dateUntil: $request->dateUntil,
zipLocation: $request->zipLocation,
country: Country::where('id', $request->country)->firstOrFail(),
dateFrom: $request['dateFrom'],
dateUntil: $request['dateUntil'],
zipLocation: $request['zipLocation'],
country: Country::where('id', $request['country'])->firstOrFail(),
leaders: $leaders->values()->toBase()->chunk(6),
children: $children->values()->toBase()->chunk(20),
);

View File

@ -5,7 +5,6 @@ namespace App\Contribution\Documents;
use App\Member\Member;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Zoomyboy\Tex\Engine;
use Zoomyboy\Tex\Template;
@ -25,14 +24,17 @@ class SolingenDocument extends ContributionDocument
) {
}
public static function fromRequest(Request $request): static
/**
* @param array<string, mixed|int> $request
*/
public static function fromRequest(array $request): static
{
return new static(
dateFrom: $request->dateFrom,
dateUntil: $request->dateUntil,
zipLocation: $request->zipLocation,
members: $request->members,
eventName: $request->eventName,
dateFrom: $request['dateFrom'],
dateUntil: $request['dateUntil'],
zipLocation: $request['zipLocation'],
members: $request['members'],
eventName: $request['eventName'],
);
}

View File

@ -1,32 +1,11 @@
<template>
<form action="/contribution/generate" target="_BLANK" class="max-w-4xl w-full mx-auto gap-6 grid-cols-2 grid p-6">
<f-text
id="eventName"
name="eventName"
class="col-span-2"
v-model="values.eventName"
label="Veranstaltungs-Name"
required
></f-text>
<form target="_BLANK" class="max-w-4xl w-full mx-auto gap-6 grid-cols-2 grid p-6">
<f-text id="eventName" name="eventName" class="col-span-2" v-model="values.eventName" label="Veranstaltungs-Name" required></f-text>
<f-text id="dateFrom" name="dateFrom" type="date" v-model="values.dateFrom" label="Datum von" required></f-text>
<f-text
id="dateUntil"
name="dateUntil"
type="date"
v-model="values.dateUntil"
label="Datum bis"
required
></f-text>
<f-text id="dateUntil" name="dateUntil" type="date" v-model="values.dateUntil" label="Datum bis" required></f-text>
<f-text id="zipLocation" name="zipLocation" v-model="values.zipLocation" label="PLZ / Ort" required></f-text>
<f-select
id="country"
:options="countries"
name="country"
v-model="values.country"
label="Land"
required
></f-select>
<f-select id="country" :options="countries" name="country" v-model="values.country" label="Land" required></f-select>
<div class="border-gray-200 shadow shadow-primary-700 p-3 shadow-[0_0_4px_gray] col-span-2">
<f-text
@ -57,10 +36,10 @@
<button
v-for="(compiler, index) in compilers"
target="_BLANK"
type="submit"
name="type"
:value="compiler.class"
@click.prevent="
values.type = compiler.class;
submit();
"
class="btn btn-primary mt-3 inline-block"
v-text="compiler.title"
></button>
@ -78,6 +57,7 @@ export default {
results: [],
},
values: {
type: null,
members: [],
event_name: '',
dateFrom: '',
@ -98,7 +78,7 @@ export default {
get() {
return this.search.s;
},
set: debounce(async function(event) {
set: debounce(async function (event) {
this.search.s = event;
var response = await this.axios.post('/api/member/search', {search: event, minLength: 3});
@ -108,6 +88,10 @@ export default {
},
},
methods: {
submit() {
var payload = btoa(JSON.stringify(this.values));
window.open(`/contribution/generate?payload=${payload}`);
},
onSubmitMemberResult(selected) {
if (this.values.members.find((m) => m === selected.id) !== undefined) {
this.values.members = this.values.members.filter((m) => m === selected.id);
@ -127,6 +111,5 @@ export default {
this.onSubmitMemberResult(this.search.results[0]);
},
},
};
</script>

View File

@ -2,6 +2,7 @@
namespace Tests\Feature\Contribution;
use App\Contribution\Documents\SolingenDocument;
use App\Country;
use App\Member\Member;
use Illuminate\Foundation\Testing\DatabaseTransactions;
@ -29,17 +30,40 @@ class StoreTest extends TestCase
$member2 = Member::factory()->defaults()->create(['address' => 'Maxstr 44', 'zip' => '42719', 'firstname' => 'Jane', 'lastname' => 'Muster']);
$response = $this->call('GET', '/contribution/generate', [
'country' => $country->id,
'dateFrom' => '1991-06-15',
'dateUntil' => '1991-06-16',
'eventName' => 'Super tolles Lager',
'members' => [$member1->id, $member2->id],
'type' => $type,
'zipLocation' => '42777 SG',
'payload' => base64_encode(json_encode([
'country' => $country->id,
'dateFrom' => '1991-06-15',
'dateUntil' => '1991-06-16',
'eventName' => 'Super tolles Lager',
'members' => [$member1->id, $member2->id],
'type' => $type,
'zipLocation' => '42777 SG',
])),
]);
$response->assertSessionDoesntHaveErrors();
$response->assertOk();
Tex::assertCompiled($type, fn ($document) => $document->hasAllContent($bodyChecks));
}
public function testItValidatesInput(): void
{
$this->login()->loginNami();
$country = Country::factory()->create();
$member1 = Member::factory()->defaults()->create();
$response = $this->call('GET', '/contribution/generate', [
'payload' => base64_encode(json_encode([
'country' => $country->id,
'dateFrom' => '',
'dateUntil' => '1991-06-16',
'eventName' => 'Super tolles Lager',
'members' => [$member1->id],
'type' => SolingenDocument::class,
'zipLocation' => '42777 SG',
])),
]);
$response->assertSessionHasErrors('dateFrom');
}
}