From 2e3ac154be0bf0776cd00f7d510f41ec676ae690 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sat, 3 Apr 2021 19:52:41 +0200 Subject: [PATCH] Implemented generation for orgs ref #119 --- .../GenerateRunnerCertificates.svelte | 131 +++++++++--------- 1 file changed, 63 insertions(+), 68 deletions(-) diff --git a/src/components/pdf_generation/GenerateRunnerCertificates.svelte b/src/components/pdf_generation/GenerateRunnerCertificates.svelte index 48e1734b..63134101 100644 --- a/src/components/pdf_generation/GenerateRunnerCertificates.svelte +++ b/src/components/pdf_generation/GenerateRunnerCertificates.svelte @@ -22,7 +22,7 @@ certificates_dropdown_open = false; if (generate_orgs.length > 0) { - generateOrgCards(locale); + generateOrgCertificates(locale); } else if (generate_teams.length > 0) { generateTeamCertificates(locale); } else { @@ -128,7 +128,7 @@ const url = window.URL.createObjectURL(blob); let a = document.createElement("a"); a.href = url; - a.download = "Sponsorings_" + t.name + ".pdf"; + a.download = "Certificates_" + t.name + ".pdf"; document.body.appendChild(a); a.click(); a.remove(); @@ -146,72 +146,67 @@ } } - // async function generateOrgCards(locale) { - // const toast = Toastify({ - // text: $_("generating-pdf"), - // duration: -1, - // }).showToast(); - // let count = 0; - // const current_cards = await RunnerCardService.runnerCardControllerGetAll(); - // for (const o of generate_orgs) { - // const runners = await RunnerOrganizationService.runnerOrganizationControllerGetRunners( - // o.id - // ); - // let cards = []; - // for (let runner of runners) { - // let card = current_cards.find((c) => c.runner?.id == runner.id); - // if (!card) { - // card = await RunnerCardService.runnerCardControllerPost({ - // runner: runner.id, - // }); - // } - // cards.push(card); - // } - // fetch( - // `${config.baseurl}/documents/cards?locale=${locale}&download=true&key=${config.documentserver_key}`, - // { - // method: "POST", - // headers: { - // "Content-Type": "application/json", - // }, - // body: JSON.stringify(cards), - // } - // ) - // .then((response) => { - // if (response.status != "200") { - // toast.hideToast(); - // Toastify({ - // text: $_("pdf-generation-failed"), - // duration: 3500, - // backgroundColor: - // "linear-gradient(90deg, hsla(281, 37%, 45%, 1) 0%, hsla(1, 62%, 48%, 1) 100%)", - // }).showToast(); - // } else { - // return response.blob(); - // } - // }) - // .then((blob) => { - // count++; - // const url = window.URL.createObjectURL(blob); - // let a = document.createElement("a"); - // a.href = url; - // a.download = "Sponsorings_" + o.name + ".pdf"; - // document.body.appendChild(a); - // a.click(); - // a.remove(); - // if (count === generate_orgs.length) { - // toast.hideToast(); - // Toastify({ - // text: $_("pdfs-successfully-generated"), - // duration: 3500, - // backgroundColor: - // "linear-gradient(to right, #00b09b, #96c93d)", - // }).showToast(); - // } - // }) - // .catch((err) => {}); - // } - // } + async function generateOrgCertificates(locale) { + const toast = Toastify({ + text: $_("generating-pdf"), + duration: -1, + }).showToast(); + let count = 0; + const current_donations = await DonationService.donationControllerGetAll(); + for (const o of generate_orgs) { + const runners = await RunnerOrganizationService.runnerOrganizationControllerGetRunners( + o.id + ); + let certificateRunners = []; + for (let runner of generate_runners) { + runner.distanceDonations = current_donations.find((d) => d.runner?.id == runner.id) || []; + certificateRunners.push(runner); + } + fetch( + `${config.baseurl}/documents/cards?locale=${locale}&download=true&key=${config.documentserver_key}`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(certificateRunners), + } + ) + .then((response) => { + if (response.status != "200") { + toast.hideToast(); + Toastify({ + text: $_("pdf-generation-failed"), + duration: 3500, + backgroundColor: + "linear-gradient(90deg, hsla(281, 37%, 45%, 1) 0%, hsla(1, 62%, 48%, 1) 100%)", + }).showToast(); + } else { + return response.blob(); + } + }) + .then((blob) => { + count++; + const url = window.URL.createObjectURL(blob); + let a = document.createElement("a"); + a.href = url; + a.download = "Certificates_" + o.name + ".pdf"; + document.body.appendChild(a); + a.click(); + a.remove(); + if (count === generate_orgs.length) { + toast.hideToast(); + Toastify({ + text: $_("pdfs-successfully-generated"), + duration: 3500, + backgroundColor: + "linear-gradient(to right, #00b09b, #96c93d)", + }).showToast(); + } + }) + .catch((err) => {}); + } + } {#if certificates_show}