Skip to content

Commit 1d9f27f

Browse files
Strehkclaude
andauthored
fix (management / userCard): Refactor PDF generation to use service functions (#405)
Co-authored-by: Claude <noreply@anthropic.com>
1 parent cbde180 commit 1d9f27f

File tree

4 files changed

+121
-18
lines changed

4 files changed

+121
-18
lines changed

messages/de.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@
742742
"http404": "Die angeforderte Seite wurde nicht gefunden.",
743743
"http500": "Es ist ein interner Serverfehler aufgetreten.",
744744
"httpGenericError": "Es ist ein unbekannter Fehler aufgetreten.",
745+
"httpMissingRequiredData": "Erforderliche Daten fehlen. Bitte stellen Sie sicher, dass alle Teilnehmendendaten (Adresse, Geburtstag) vollständig sind.",
745746
"iban": "IBAN",
746747
"ibanMustBe22Characters": "IBAN muss 22 Zeichen ohne Leerzeichen haben",
747748
"identityCheck": "Identitätsprüfung",

messages/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@
742742
"http404": "The requested page was not found.",
743743
"http500": "An internal server error occurred.",
744744
"httpGenericError": "An unknown error has occurred.",
745+
"httpMissingRequiredData": "Required data is missing. Please ensure all participant details (address, birthday) are complete.",
745746
"iban": "IBAN",
746747
"ibanMustBe22Characters": "IBAN must be 22 characters without spaces",
747748
"identityCheck": "Identity Check",

src/lib/components/UserCard/UserCardContent.svelte

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@
180180
startConference
181181
endConference
182182
title
183+
postalName
184+
postalStreet
185+
postalApartment
186+
postalZip
187+
postalCity
188+
postalCountry
183189
}
184190
}
185191
`);
@@ -279,6 +285,7 @@
279285
status={participantStatus}
280286
{userId}
281287
{conferenceId}
288+
{user}
282289
{conference}
283290
birthday={user?.birthday}
284291
{isConferenceSupervisor}

src/lib/components/UserCard/tabs/ParticipantStatusTab.svelte

Lines changed: 112 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
import { getBaseDocumentsForPostal } from '$lib/queries/getBaseDocuments';
1919
import { ofAgeAtConference } from '$lib/services/ageChecker';
2020
import GuardianConsentNotNeeded from '$lib/components/GuardianConsentNotNeeded.svelte';
21+
import {
22+
downloadCompletePostalRegistrationPDF,
23+
downloadCompleteCertificate,
24+
type ParticipantData,
25+
type RecipientData
26+
} from '$lib/services/pdfGenerator';
27+
import formatNames from '$lib/services/formatNames';
2128
2229
type AdministrativeStatus = 'DONE' | 'PENDING' | 'PROBLEM';
2330
@@ -44,12 +51,32 @@
4451
| undefined;
4552
userId: string;
4653
conferenceId: string;
54+
user:
55+
| {
56+
id: string;
57+
given_name?: string | null;
58+
family_name?: string | null;
59+
street?: string | null;
60+
apartment?: string | null;
61+
zip?: string | null;
62+
city?: string | null;
63+
country?: string | null;
64+
birthday?: Date | null;
65+
}
66+
| null
67+
| undefined;
4768
conference:
4869
| {
4970
id: string;
5071
startConference?: string | null;
5172
endConference?: string | null;
5273
title?: string | null;
74+
postalName?: string | null;
75+
postalStreet?: string | null;
76+
postalApartment?: string | null;
77+
postalZip?: number | null;
78+
postalCity?: string | null;
79+
postalCountry?: string | null;
5380
}
5481
| null
5582
| undefined;
@@ -62,6 +89,7 @@
6289
status,
6390
userId,
6491
conferenceId,
92+
user,
6593
conference,
6694
birthday,
6795
isConferenceSupervisor,
@@ -88,36 +116,102 @@
88116
89117
const downloadPostalDocs = async () => {
90118
try {
91-
const result = await getBaseDocumentsForPostal.fetch({
92-
variables: { visitorId: userId, conferenceId }
119+
const baseContent = await getBaseDocumentsForPostal.fetch({
120+
variables: { conferenceId }
93121
});
94-
if (result.data?.postalRegistrationPDF) {
95-
const a = document.createElement('a');
96-
a.href = `data:application/pdf;base64,${result.data.postalRegistrationPDF}`;
97-
a.download = `postal-registration-${userId}.pdf`;
98-
a.click();
99-
} else {
122+
123+
if (baseContent.errors) {
124+
toast.error(m.httpGenericError());
125+
return;
126+
}
127+
128+
if (
129+
!conference?.postalName ||
130+
!conference?.postalStreet ||
131+
!conference?.postalZip ||
132+
!conference?.postalCity ||
133+
!conference?.postalCountry
134+
) {
100135
toast.error(m.httpGenericError());
136+
return;
101137
}
102-
} catch {
138+
139+
if (user) {
140+
if (!user.birthday) {
141+
toast.error(m.httpMissingRequiredData());
142+
return;
143+
}
144+
145+
const recipientData: RecipientData = {
146+
name: `${conference.postalName}`,
147+
address: `${conference.postalStreet} ${conference.postalApartment ?? ''}`,
148+
zip: conference.postalZip?.toString() ?? '',
149+
city: conference.postalCity ?? '',
150+
country: conference.postalCountry ?? ''
151+
};
152+
153+
const participantData: ParticipantData = {
154+
id: user.id,
155+
name: formatNames(user.given_name ?? undefined, user.family_name ?? undefined, {
156+
givenNameFirst: true,
157+
familyNameUppercase: true,
158+
givenNameUppercase: true
159+
}),
160+
address: [
161+
[user.street, user.apartment].filter(Boolean).join(' '),
162+
[user.zip, user.city].filter(Boolean).join(' '),
163+
user.country
164+
]
165+
.filter(Boolean)
166+
.join(', '),
167+
birthday: user.birthday.toLocaleDateString()
168+
};
169+
170+
await downloadCompletePostalRegistrationPDF(
171+
ofAgeAtConference(conference.startConference, user.birthday),
172+
participantData,
173+
recipientData,
174+
baseContent.data?.findUniqueConference?.contractContent ?? undefined,
175+
baseContent.data?.findUniqueConference?.guardianConsentContent ?? undefined,
176+
baseContent.data?.findUniqueConference?.mediaConsentContent ?? undefined,
177+
baseContent.data?.findUniqueConference?.termsAndConditionsContent ?? undefined,
178+
`${formatNames(user.given_name ?? undefined, user.family_name ?? undefined, {
179+
givenNameFirst: false,
180+
delimiter: '_'
181+
})}_postal_registration.pdf`
182+
);
183+
}
184+
} catch (error) {
185+
console.error('Error generating PDF:', error);
103186
toast.error(m.httpGenericError());
104187
}
105188
};
106189
107190
const downloadCertificate = async () => {
108191
try {
109-
const result = await certificateQuery.fetch({
110-
variables: { visitorId: userId, conferenceId }
192+
const certificateData = await certificateQuery.fetch({
193+
variables: { conferenceId, userId }
111194
});
112-
if (result.data?.certificate) {
113-
const a = document.createElement('a');
114-
a.href = `data:application/pdf;base64,${result.data.certificate}`;
115-
a.download = `certificate-${userId}.pdf`;
116-
a.click();
117-
} else {
195+
196+
const jwtData = certificateData.data?.getCertificateJWT;
197+
198+
if (!jwtData?.fullName || !jwtData?.jwt) {
118199
toast.error(m.certificateDownloadError());
200+
return;
201+
}
202+
203+
if (user) {
204+
await downloadCompleteCertificate(
205+
jwtData,
206+
certificateData.data?.findUniqueConference?.certificateContent ?? undefined,
207+
`${formatNames(user.given_name ?? undefined, user.family_name ?? undefined, {
208+
givenNameFirst: false,
209+
delimiter: '_'
210+
})}_certificate.pdf`
211+
);
119212
}
120-
} catch {
213+
} catch (error) {
214+
console.error('Error generating PDF:', error);
121215
toast.error(m.certificateDownloadError());
122216
}
123217
};

0 commit comments

Comments
 (0)