refactor(pdfgeneration): Switched contract generation over to new document-server
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
2024-12-17 17:19:20 +01:00
parent e23098410c
commit f99b7f4bb8
2 changed files with 62 additions and 123 deletions

View File

@@ -4,10 +4,12 @@
RunnerOrganizationService,
RunnerTeamService,
} from "@odit/lfk-client-js";
import DocumentServer from "./DocumentServer";
import { init } from "@paralleldrive/cuid2";
import toast from "svelte-french-toast";
const createId = init({ length: 10, fingerprint: "lfk-frontend" });
const documentServer = new DocumentServer(config.baseurl_documentserver,config.documentserver_key);
export let sponsoring_contracts_show = false;
export let generate_runners = [];
@@ -34,6 +36,17 @@
generateRunnerContracts(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 generateTeamContracts(locale) {
toast.loading($_("generating-pdfs"));
@@ -43,38 +56,11 @@
const runners = await RunnerTeamService.runnerTeamControllerGetRunners(
t.id
);
fetch(
`${config.baseurl_documentserver}/contracts?locale=${locale}&download=true&key=${config.documentserver_key}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(runners),
}
)
.then((response) => {
if (response.status != "200") {
toast.dismiss();
toast.error($_("pdf-generation-failed"));
} else {
return response.blob();
}
})
documentServer.generateContracts(runners, locale)
.then((blob) => {
const url = window.URL.createObjectURL(blob);
let a = document.createElement("a");
a.href = url;
a.download = `${$_("sponsorings")}_${
download(blob, `${$_("sponsorings")}_${
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) => {});
}
@@ -91,38 +77,11 @@
o.id,
true
);
await fetch(
`${config.baseurl_documentserver}/contracts?locale=${locale}&download=true&key=${config.documentserver_key}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(runners),
}
)
.then((response) => {
if (response.status != "200") {
toast.dismiss();
toast.error($_("pdf-generation-failed"));
} else {
return response.blob();
}
})
await documentServer.generateContracts(runners, locale)
.then((blob) => {
const url = window.URL.createObjectURL(blob);
let a = document.createElement("a");
a.href = url;
a.download = `${$_("sponsorings")}_${
download(blob, `${$_("sponsorings")}_${
o.name
}_direct-${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"));
}
}_direct-${locale}-${createId()}.pdf`)
})
.catch((err) => {});
for (const t of o.teams) {
@@ -130,41 +89,11 @@
let runners = await RunnerTeamService.runnerTeamControllerGetRunners(
t.id
);
await fetch(
`${config.baseurl_documentserver}/contracts?locale=${locale}&download=true&key=${config.documentserver_key}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(runners),
}
)
.then((response) => {
if (response.status != "200") {
toast.dismiss();
toast.error($_("pdf-generation-failed"));
} else {
return response.blob();
}
})
await documentServer.generateContracts(runners, locale)
.then((blob) => {
const url = window.URL.createObjectURL(blob);
let a = document.createElement("a");
a.href = url;
a.download = `${$_("sponsorings")}_${o.name}_${
download(blob, `${$_("sponsorings")}_${o.name}_${
t.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($_("pdfs-successfully-generated"));
}
}-${locale}-${createId()}.pdf`)
})
.catch((err) => {});
}
@@ -173,39 +102,15 @@
function generateRunnerContracts(locale) {
toast.loading($_("generating-pdf"));
fetch(
`${config.baseurl_documentserver}/contracts?locale=${locale}&download=true&key=${config.documentserver_key}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(generate_runners),
}
)
.then((response) => {
if (response.status != "200") {
toast.dismiss();
toast.error($_("pdf-generation-failed"));
} else {
return response.blob();
}
})
documentServer.generateContracts(generate_runners, locale)
.then((blob) => {
const url = window.URL.createObjectURL(blob);
let a = document.createElement("a");
a.href = url;
let fileName = `${$_("sponsorings")}-${locale}-${createId()}.pdf`
if (generate_runners.length == 1) {
a.download = `${$_("sponsorings")}_${generate_runners[0].firstname}_${
fileName= `${$_("sponsorings")}_${generate_runners[0].firstname}_${
generate_runners[0].lastname
}-${locale}-${createId()}.pdf`;
}
a.download = `${$_("sponsorings")}-${locale}-${createId()}.pdf`;
document.body.appendChild(a);
a.click();
a.remove();
toast.dismiss();
toast($_("pdf-successfully-generated"));
download(blob, fileName);
})
.catch((err) => {
console.error(err);