|
18 | 18 | import { getBaseDocumentsForPostal } from '$lib/queries/getBaseDocuments'; |
19 | 19 | import { ofAgeAtConference } from '$lib/services/ageChecker'; |
20 | 20 | 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'; |
21 | 28 |
|
22 | 29 | type AdministrativeStatus = 'DONE' | 'PENDING' | 'PROBLEM'; |
23 | 30 |
|
|
44 | 51 | | undefined; |
45 | 52 | userId: string; |
46 | 53 | 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; |
47 | 68 | conference: |
48 | 69 | | { |
49 | 70 | id: string; |
50 | 71 | startConference?: string | null; |
51 | 72 | endConference?: string | null; |
52 | 73 | 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; |
53 | 80 | } |
54 | 81 | | null |
55 | 82 | | undefined; |
|
62 | 89 | status, |
63 | 90 | userId, |
64 | 91 | conferenceId, |
| 92 | + user, |
65 | 93 | conference, |
66 | 94 | birthday, |
67 | 95 | isConferenceSupervisor, |
|
88 | 116 |
|
89 | 117 | const downloadPostalDocs = async () => { |
90 | 118 | try { |
91 | | - const result = await getBaseDocumentsForPostal.fetch({ |
92 | | - variables: { visitorId: userId, conferenceId } |
| 119 | + const baseContent = await getBaseDocumentsForPostal.fetch({ |
| 120 | + variables: { conferenceId } |
93 | 121 | }); |
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 | + ) { |
100 | 135 | toast.error(m.httpGenericError()); |
| 136 | + return; |
101 | 137 | } |
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); |
103 | 186 | toast.error(m.httpGenericError()); |
104 | 187 | } |
105 | 188 | }; |
106 | 189 |
|
107 | 190 | const downloadCertificate = async () => { |
108 | 191 | try { |
109 | | - const result = await certificateQuery.fetch({ |
110 | | - variables: { visitorId: userId, conferenceId } |
| 192 | + const certificateData = await certificateQuery.fetch({ |
| 193 | + variables: { conferenceId, userId } |
111 | 194 | }); |
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) { |
118 | 199 | 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 | + ); |
119 | 212 | } |
120 | | - } catch { |
| 213 | + } catch (error) { |
| 214 | + console.error('Error generating PDF:', error); |
121 | 215 | toast.error(m.certificateDownloadError()); |
122 | 216 | } |
123 | 217 | }; |
|
0 commit comments