Files
online-energieausweis/src/client/lib/verbrauchsausweisWohnenSpeichern.ts
2025-02-05 10:55:12 +07:00

135 lines
3.0 KiB
TypeScript

import { api } from "astro-typesafe-api/client"
import { exclude } from "#lib/exclude.js";
import Cookies from "js-cookie";
import { API_ACCESS_TOKEN_COOKIE_NAME } from "#lib/constants.js";
import { AufnahmeClient, ObjektClient, UploadedGebaeudeBild, VerbrauchsausweisWohnenClient, } from "#components/Ausweis/types.js";
export async function verbrauchsausweisWohnenSpeichern(
ausweis: VerbrauchsausweisWohnenClient,
objekt: ObjektClient,
aufnahme: AufnahmeClient,
bilder: (UploadedGebaeudeBild & { base64?: string })[]
) {
if (objekt.uid) {
await api.objekt._uid.PATCH.fetch({
...exclude(objekt, ["uid"])
}, {
params: {
uid: objekt.uid
},
headers: {
"Authorization": `Bearer ${Cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)}`
}
})
} else {
const { uid } = await api.objekt.PUT.fetch({
...exclude(objekt, ["uid"])
}, {
headers: {
"Authorization": `Bearer ${Cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)}`
}
})
objekt.uid = uid;
}
if (aufnahme.uid) {
await api.aufnahme._uid.PATCH.fetch({
...exclude(aufnahme, ["uid"])
}, {
params: {
uid: aufnahme.uid
},
headers: {
"Authorization": `Bearer ${Cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)}`
}
})
} else {
const { uid } = await api.aufnahme.PUT.fetch({
aufnahme,
uid_objekt: objekt.uid
}, {
headers: {
"Authorization": `Bearer ${Cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)}`
}
})
aufnahme.uid = uid
}
if (ausweis.uid) {
await api["verbrauchsausweis-wohnen"]._uid.PATCH.fetch({
...exclude(ausweis, ["uid"])
}, {
params: {
uid: ausweis.uid
},
headers: {
"Authorization": `Bearer ${Cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)}`
}
})
} else {
const { uid } = await api["verbrauchsausweis-wohnen"].PUT.fetch({
ausweis,
uid_aufnahme: aufnahme.uid
}, {
headers: {
"Authorization": `Bearer ${Cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)}`
}
})
ausweis.uid = uid;
}
for (const bild of bilder) {
if (bild.uid) {
continue;
}
const response = await api.objekt._uid.bilder.PUT.fetch({
base64: bild.base64,
kategorie: bild.kategorie
}, {
params: {
uid: objekt.uid
},
headers: {
"Authorization": `Bearer ${Cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)}`
}
})
bild.uid = response.uid
}
return {
uid_ausweis: ausweis.uid,
uid_aufnahme: aufnahme.uid,
uid_objekt: objekt.uid
}
// await client.v1.tickets.erstellen.mutate({
// titel: "Ausweis konnte nicht gespeichert werden",
// beschreibung: e.stack,
// email: user.email ?? "",
// metadata: JSON.stringify({
// ausweis,
// }),
// });
// addNotification({
// dismissable: false,
// message:
// "Ausweis konnte nicht gespeichert werden, bitte versuchen sie es erneut.",
// subtext:
// "Sollte das Problem weiterhin bestehen, kontaktieren sie bitte den Support.",
// timeout: 6000,
// type: "error",
// });
return null;
}