fix heirs

This commit is contained in:
Sofía Aritz 2024-06-30 18:26:40 +02:00
parent b9f36b5c2c
commit daa487fe26
Signed by: sofia
GPG key ID: 90B5116E3542B28F
3 changed files with 162 additions and 126 deletions

View file

@ -29,6 +29,35 @@ const Body = Type.Object({
type BodyType = Static<typeof Body>;
export default function register(app: AppInterface, auth: AuthInterface, database: DatabaseInterface) {
app.get("/auth/heirs", {
async handler(request, reply) {
let jwt = request.headers["authorization"].replace("Bearer", "").trim();
let { payload } = await auth.verifyJwt(jwt);
if (payload.uid == null) {
reply.status(401);
return;
}
return (await database.listHeirs(payload.uid))
.map((v) => {
v["contactMethod"] = "email";
return v;
})
.map((v) => {
v["value"] = v["email"];
return v;
})
.map((v) => {
v["email"] = undefined;
return v;
});
},
schema: {
headers: { $ref: "schema://identity/authorization" },
},
});
app.put<{ Body: BodyType }>("/auth/heirs", {
async handler(request, reply) {
let jwt = request.headers["authorization"].replace("Bearer", "").trim();

View file

@ -143,6 +143,10 @@ export async function removeHeir(credentials: Credentials, heirID: string): Prom
}));
}
export async function listHeirs(credentials: Credentials): Promise<AccountHeir[]> {
return await asJson(sendRequest('/auth/heirs', credentials));
}
export async function uploadAsset(session_key: string, file: File): Promise<string> {
let url = new URL('/asset', ASSET_API_ENDPOINT);
url.search = `?session_key=${session_key}`;

View file

@ -5,27 +5,29 @@
<script lang="ts">
import { createForm } from 'felte';
import { account, credentials, refreshAccount } from '$lib/stores';
import { type AccountHeir, insertHeir, removeHeir as removeDBHeir } from '$lib/api';
import { type AccountHeir, type InsertHeir, insertHeir, listHeirs, removeHeir as removeDBHeir } from '$lib/api';
credentials.subscribe(
(v) => v == null && setTimeout(() => (window.location.pathname = '/auth/login'), 200)
);
let heirWizard = false;
let heirs = listHeirs($credentials!);
function resfreshHeirs() {
heirs = listHeirs($credentials!);
}
const { form, errors } = createForm({
/*onSubmit: async (values) => {
let heir: AccountHeir = {
onSubmit: async (values) => {
let heir: InsertHeir = {
contactMethod: values.contactMethod,
name: values.name,
value: values.contactDetails
};
let currentHeirs = structuredClone($account!.heirs);
let updatedHeirs = [heir, ...currentHeirs];
//await updateHeirs($credentials!, updatedHeirs);
await refreshAccount();
await insertHeir($credentials!, heir);
await resfreshHeirs();
heirWizard = false;
},
@ -45,15 +47,12 @@
}
return errors;
}*/
}
});
async function removeHeir(heir: AccountHeir) {
let currentHeirs = structuredClone($account!.heirs);
let updatedHeirs = currentHeirs.filter((v) => v.value !== heir.value);
await updateHeirs($credentials!, updatedHeirs);
await refreshAccount();
await removeDBHeir($credentials!, heir.id);
await resfreshHeirs();
}
</script>
@ -63,10 +62,13 @@
Welcome back, <span class="font-bold">{$account?.name}</span>
.
</h1>
{#await heirs}
<span>Loading heirs...</span>
{:then heirs}
<div>
<div class="mb-2 flex justify-between">
<h2 class="pb-2.5 text-xl">Heirs</h2>
{#if $account?.heirs.length > 0}
{#if heirs.length > 0}
<button
on:click={() => (heirWizard = !heirWizard)}
class="rounded-lg bg-violet-700 px-2.5 py-1 text-center text-white hover:bg-violet-800 focus:ring-4 focus:ring-violet-300"
@ -75,7 +77,7 @@
</button>
{/if}
</div>
{#if !heirWizard && $account?.heirs.length === 0}
{#if !heirWizard && heirs.length === 0}
<div class="flex flex-col">
<button
on:click={() => (heirWizard = true)}
@ -156,7 +158,7 @@
</form>
</div>
{/if}
{#each $account?.heirs || [] as heir (heir.value)}
{#each heirs || [] as heir (heir.id)}
<div class="mb-2.5 flex w-full flex-col rounded-lg border border-gray-200 p-3.5 shadow">
<div class="flex justify-between">
<span class="block text-sm font-medium text-gray-900">
@ -177,5 +179,6 @@
</div>
{/each}
</div>
{/await}
</div>
</div>