refactor(pdfgeneration): Switch to new document-server api
Some checks failed
ci/woodpecker/push/build Pipeline failed
Some checks failed
ci/woodpecker/push/build Pipeline failed
This commit is contained in:
parent
f99b7f4bb8
commit
878d9714cb
@ -78,6 +78,53 @@ class DocumentServer {
|
||||
const blob = await response.blob();
|
||||
return blob;
|
||||
}
|
||||
|
||||
async generateCertificates(runners: any[], locale: string) {
|
||||
const generateRunners = new Array<any>();
|
||||
|
||||
for (let i = 0; i < runners.length; i++) {
|
||||
const certificate = {
|
||||
id: runners[i].id,
|
||||
first_name: runners[i].firstname,
|
||||
middle_name: runners[i].middlename,
|
||||
last_name: runners[i].lastname,
|
||||
group: {
|
||||
id: runners[i].group.id,
|
||||
name: runners[i].group.name,
|
||||
},
|
||||
distance: runners[i].distance,
|
||||
distance_donations: runners[i].distanceDonations.map((distanceDonation: any) =>{
|
||||
return {
|
||||
id: distanceDonation.id,
|
||||
amount: distanceDonation.amount,
|
||||
amount_per_distance: distanceDonation.amountPerDistance,
|
||||
donor: {
|
||||
id: distanceDonation.donor.id,
|
||||
first_name: distanceDonation.donor.firstname,
|
||||
middle_name: distanceDonation.donor.middlename,
|
||||
last_name: distanceDonation.donor.lastname,
|
||||
}
|
||||
}
|
||||
}),
|
||||
}
|
||||
generateRunners.push(certificate)
|
||||
|
||||
}
|
||||
|
||||
const response = await fetch(`${this.baseUrl}/v1/pdfs/certificates?key=${this.apiKey}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
locale,
|
||||
runners: generateRunners,
|
||||
}),
|
||||
});
|
||||
|
||||
const blob = await response.blob();
|
||||
return blob;
|
||||
}
|
||||
}
|
||||
|
||||
export default DocumentServer;
|
@ -7,7 +7,10 @@
|
||||
} from "@odit/lfk-client-js";
|
||||
import { init } from "@paralleldrive/cuid2";
|
||||
import toast from "svelte-french-toast";
|
||||
import DocumentServer from "./DocumentServer";
|
||||
const createId = init({ length: 10, fingerprint: "lfk-frontend" });
|
||||
const documentServer = new DocumentServer(config.baseurl_documentserver,config.documentserver_key);
|
||||
|
||||
|
||||
export let certificates_show = false;
|
||||
export let generate_runners = [];
|
||||
@ -34,6 +37,17 @@
|
||||
generateRunnerCertificates(locale);
|
||||
}
|
||||
}
|
||||
function download (blob, fileName){
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
let a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = fileName;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
toast.dismiss();
|
||||
toast($_("pdf-successfully-generated"));
|
||||
}
|
||||
|
||||
async function generateRunnerCertificates(locale) {
|
||||
toast.loading($_("generating-pdf"));
|
||||
@ -45,40 +59,15 @@
|
||||
current_donations.filter((d) => d.runner?.id == runner.id) || [];
|
||||
certificateRunners.push(runner);
|
||||
}
|
||||
fetch(
|
||||
`${config.baseurl_documentserver}/certificates?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.dismiss();
|
||||
toast.error($_("pdf-generation-failed"));
|
||||
} else {
|
||||
return response.blob();
|
||||
}
|
||||
})
|
||||
documentServer.generateCertificates(certificateRunners, locale)
|
||||
.then((blob) => {
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
let a = document.createElement("a");
|
||||
a.href = url;
|
||||
let fileName = `${$_("certificates")}-${locale}.pdf`
|
||||
if (generate_runners.length == 1) {
|
||||
a.download = `${$_("certificates")}_${
|
||||
fileName = `${$_("certificates")}_${
|
||||
generate_runners[0].firstname
|
||||
}_${generate_runners[0].lastname}-${locale}-${createId()}.pdf`;
|
||||
} else {
|
||||
a.download = `${$_("certificates")}-${locale}.pdf`;
|
||||
}
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
toast.dismiss();
|
||||
toast($_("pdf-successfully-generated"));
|
||||
download(blob, fileName);
|
||||
})
|
||||
.catch((err) => {});
|
||||
}
|
||||
@ -98,39 +87,12 @@
|
||||
current_donations.filter((d) => d.runner?.id == runner.id) || [];
|
||||
certificateRunners.push(runner);
|
||||
}
|
||||
fetch(
|
||||
`${config.baseurl_documentserver}/certificates?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.dismiss();
|
||||
toast.error($_("pdf-generation-failed"));
|
||||
} else {
|
||||
return response.blob();
|
||||
}
|
||||
})
|
||||
documentServer.generateCertificates(certificateRunners, locale)
|
||||
.then((blob) => {
|
||||
count++;
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
let a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = `${$_("certificates")}_${
|
||||
download(blob, `${$_("certificates")}_${
|
||||
t.name
|
||||
}-${locale}-${createId()}.pdf`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
if (count === generate_teams.length) {
|
||||
toast.dismiss();
|
||||
toast.success($_("pdfs-successfully-generated"));
|
||||
}
|
||||
}-${locale}-${createId()}.pdf`)
|
||||
})
|
||||
.catch((err) => {});
|
||||
}
|
||||
@ -156,38 +118,11 @@
|
||||
current_donations.filter((d) => d.runner?.id == runner.id) || [];
|
||||
certificateRunners.push(runner);
|
||||
}
|
||||
await fetch(
|
||||
`${config.baseurl_documentserver}/certificates?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.dismiss();
|
||||
toast.error($_("pdf-generation-failed"));
|
||||
} else {
|
||||
return response.blob();
|
||||
}
|
||||
})
|
||||
await documentServer.generateCertificates(certificateRunners, locale)
|
||||
.then((blob) => {
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
let a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = `${$_("certificates")}_${
|
||||
download(blob, `${$_("certificates")}_${
|
||||
o.name
|
||||
}-${locale}-${createId()}.pdf`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
if (count === o.teams.length && count_orgs === generate_orgs.length) {
|
||||
toast.dismiss();
|
||||
toast.success($_("pdfs-successfully-generated"));
|
||||
}
|
||||
}-${locale}-${createId()}.pdf`)
|
||||
})
|
||||
.catch((err) => {});
|
||||
for (const t of o.teams) {
|
||||
@ -201,34 +136,11 @@
|
||||
current_donations.filter((d) => d.runner?.id == runner.id) || [];
|
||||
certificateRunners.push(runner);
|
||||
}
|
||||
await fetch(
|
||||
`${config.baseurl_documentserver}/certificates?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.dismiss();
|
||||
toast.error($_("pdf-generation-failed"));
|
||||
} else {
|
||||
return response.blob();
|
||||
}
|
||||
})
|
||||
await documentServer.generateCertificates(certificateRunners, locale)
|
||||
.then((blob) => {
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
let a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = `${$_("certificates")}_${o.name}_${
|
||||
download(blob, `${$_("certificates")}_${o.name}_${
|
||||
t.name
|
||||
}-${locale}-${createId()}.pdf`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
}-${locale}-${createId()}.pdf`)
|
||||
if (
|
||||
count === o.teams.length &&
|
||||
count_orgs === generate_orgs.length
|
||||
|
Loading…
x
Reference in New Issue
Block a user