139 lines
3.4 KiB
TypeScript
139 lines
3.4 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, BedarfsausweisWohnenClient, BildClient, ObjektClient, UnterlageClient, UploadedGebaeudeBild, VerbrauchsausweisGewerbeClient, VerbrauchsausweisWohnenClient, } from "#components/Ausweis/types.js";
|
|
import { Enums } from "@ibcornelsen/database/client";
|
|
|
|
export async function nachweisSpeichern(
|
|
nachweis: VerbrauchsausweisWohnenClient | VerbrauchsausweisGewerbeClient | BedarfsausweisWohnenClient,
|
|
objekt: ObjektClient,
|
|
aufnahme: AufnahmeClient,
|
|
bilder: BildClient[],
|
|
unterlagen: UnterlageClient[],
|
|
ausweisart: Enums.Ausweisart
|
|
) {
|
|
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"]),
|
|
baujahr_gebaeude: aufnahme.baujahr_gebaeude || [],
|
|
baujahr_klima: aufnahme.baujahr_klima || [],
|
|
baujahr_heizung: aufnahme.baujahr_heizung || [],
|
|
}, {
|
|
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
|
|
}
|
|
|
|
let patchRoute: any;
|
|
let putRoute: any;
|
|
if (ausweisart == Enums.Ausweisart.GEGNachweisWohnen) {
|
|
patchRoute = api["geg-nachweis-wohnen"]._uid.PATCH
|
|
putRoute = api["geg-nachweis-wohnen"].PUT
|
|
} else if (ausweisart === Enums.Ausweisart.GEGNachweisGewerbe) {
|
|
patchRoute = api["geg-nachweis-gewerbe"]._uid.PATCH
|
|
putRoute = api["geg-nachweis-gewerbe"].PUT
|
|
}
|
|
|
|
if (nachweis.uid) {
|
|
await patchRoute.fetch({
|
|
...exclude(nachweis, ["uid"])
|
|
}, {
|
|
params: {
|
|
uid: nachweis.uid
|
|
},
|
|
headers: {
|
|
"Authorization": `Bearer ${Cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)}`
|
|
}
|
|
})
|
|
} else {
|
|
const { uid } = await putRoute.fetch({
|
|
nachweis,
|
|
uid_aufnahme: aufnahme.uid
|
|
}, {
|
|
headers: {
|
|
"Authorization": `Bearer ${Cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)}`
|
|
}
|
|
})
|
|
|
|
nachweis.uid = uid;
|
|
}
|
|
|
|
await api.aufnahme._uid.bilder.PUT.fetch(bilder.map(bild => bild.uid), {
|
|
params: {
|
|
uid: aufnahme.uid
|
|
},
|
|
headers: {
|
|
"Authorization": `Bearer ${Cookies.get(API_ACCESS_TOKEN_COOKIE_NAME)}`
|
|
}
|
|
})
|
|
|
|
return {
|
|
uid_nachweis: nachweis.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;
|
|
}
|