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();
|
const blob = await response.blob();
|
||||||
return 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;
|
export default DocumentServer;
|
@ -7,7 +7,10 @@
|
|||||||
} from "@odit/lfk-client-js";
|
} from "@odit/lfk-client-js";
|
||||||
import { init } from "@paralleldrive/cuid2";
|
import { init } from "@paralleldrive/cuid2";
|
||||||
import toast from "svelte-french-toast";
|
import toast from "svelte-french-toast";
|
||||||
|
import DocumentServer from "./DocumentServer";
|
||||||
const createId = init({ length: 10, fingerprint: "lfk-frontend" });
|
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 certificates_show = false;
|
||||||
export let generate_runners = [];
|
export let generate_runners = [];
|
||||||
@ -34,6 +37,17 @@
|
|||||||
generateRunnerCertificates(locale);
|
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) {
|
async function generateRunnerCertificates(locale) {
|
||||||
toast.loading($_("generating-pdf"));
|
toast.loading($_("generating-pdf"));
|
||||||
@ -45,40 +59,15 @@
|
|||||||
current_donations.filter((d) => d.runner?.id == runner.id) || [];
|
current_donations.filter((d) => d.runner?.id == runner.id) || [];
|
||||||
certificateRunners.push(runner);
|
certificateRunners.push(runner);
|
||||||
}
|
}
|
||||||
fetch(
|
documentServer.generateCertificates(certificateRunners, locale)
|
||||||
`${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();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then((blob) => {
|
.then((blob) => {
|
||||||
const url = window.URL.createObjectURL(blob);
|
let fileName = `${$_("certificates")}-${locale}.pdf`
|
||||||
let a = document.createElement("a");
|
|
||||||
a.href = url;
|
|
||||||
if (generate_runners.length == 1) {
|
if (generate_runners.length == 1) {
|
||||||
a.download = `${$_("certificates")}_${
|
fileName = `${$_("certificates")}_${
|
||||||
generate_runners[0].firstname
|
generate_runners[0].firstname
|
||||||
}_${generate_runners[0].lastname}-${locale}-${createId()}.pdf`;
|
}_${generate_runners[0].lastname}-${locale}-${createId()}.pdf`;
|
||||||
} else {
|
|
||||||
a.download = `${$_("certificates")}-${locale}.pdf`;
|
|
||||||
}
|
}
|
||||||
document.body.appendChild(a);
|
download(blob, fileName);
|
||||||
a.click();
|
|
||||||
a.remove();
|
|
||||||
toast.dismiss();
|
|
||||||
toast($_("pdf-successfully-generated"));
|
|
||||||
})
|
})
|
||||||
.catch((err) => {});
|
.catch((err) => {});
|
||||||
}
|
}
|
||||||
@ -98,39 +87,12 @@
|
|||||||
current_donations.filter((d) => d.runner?.id == runner.id) || [];
|
current_donations.filter((d) => d.runner?.id == runner.id) || [];
|
||||||
certificateRunners.push(runner);
|
certificateRunners.push(runner);
|
||||||
}
|
}
|
||||||
fetch(
|
documentServer.generateCertificates(certificateRunners, locale)
|
||||||
`${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();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then((blob) => {
|
.then((blob) => {
|
||||||
count++;
|
count++;
|
||||||
const url = window.URL.createObjectURL(blob);
|
download(blob, `${$_("certificates")}_${
|
||||||
let a = document.createElement("a");
|
|
||||||
a.href = url;
|
|
||||||
a.download = `${$_("certificates")}_${
|
|
||||||
t.name
|
t.name
|
||||||
}-${locale}-${createId()}.pdf`;
|
}-${locale}-${createId()}.pdf`)
|
||||||
document.body.appendChild(a);
|
|
||||||
a.click();
|
|
||||||
a.remove();
|
|
||||||
if (count === generate_teams.length) {
|
|
||||||
toast.dismiss();
|
|
||||||
toast.success($_("pdfs-successfully-generated"));
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.catch((err) => {});
|
.catch((err) => {});
|
||||||
}
|
}
|
||||||
@ -156,38 +118,11 @@
|
|||||||
current_donations.filter((d) => d.runner?.id == runner.id) || [];
|
current_donations.filter((d) => d.runner?.id == runner.id) || [];
|
||||||
certificateRunners.push(runner);
|
certificateRunners.push(runner);
|
||||||
}
|
}
|
||||||
await fetch(
|
await documentServer.generateCertificates(certificateRunners, locale)
|
||||||
`${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();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then((blob) => {
|
.then((blob) => {
|
||||||
const url = window.URL.createObjectURL(blob);
|
download(blob, `${$_("certificates")}_${
|
||||||
let a = document.createElement("a");
|
|
||||||
a.href = url;
|
|
||||||
a.download = `${$_("certificates")}_${
|
|
||||||
o.name
|
o.name
|
||||||
}-${locale}-${createId()}.pdf`;
|
}-${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"));
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.catch((err) => {});
|
.catch((err) => {});
|
||||||
for (const t of o.teams) {
|
for (const t of o.teams) {
|
||||||
@ -201,34 +136,11 @@
|
|||||||
current_donations.filter((d) => d.runner?.id == runner.id) || [];
|
current_donations.filter((d) => d.runner?.id == runner.id) || [];
|
||||||
certificateRunners.push(runner);
|
certificateRunners.push(runner);
|
||||||
}
|
}
|
||||||
await fetch(
|
await documentServer.generateCertificates(certificateRunners, locale)
|
||||||
`${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();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then((blob) => {
|
.then((blob) => {
|
||||||
const url = window.URL.createObjectURL(blob);
|
download(blob, `${$_("certificates")}_${o.name}_${
|
||||||
let a = document.createElement("a");
|
|
||||||
a.href = url;
|
|
||||||
a.download = `${$_("certificates")}_${o.name}_${
|
|
||||||
t.name
|
t.name
|
||||||
}-${locale}-${createId()}.pdf`;
|
}-${locale}-${createId()}.pdf`)
|
||||||
document.body.appendChild(a);
|
|
||||||
a.click();
|
|
||||||
a.remove();
|
|
||||||
if (
|
if (
|
||||||
count === o.teams.length &&
|
count === o.teams.length &&
|
||||||
count_orgs === generate_orgs.length
|
count_orgs === generate_orgs.length
|
||||||
|
Loading…
x
Reference in New Issue
Block a user