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>; type BodyType = Static<typeof Body>;
export default function register(app: AppInterface, auth: AuthInterface, database: DatabaseInterface) { 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", { app.put<{ Body: BodyType }>("/auth/heirs", {
async handler(request, reply) { async handler(request, reply) {
let jwt = request.headers["authorization"].replace("Bearer", "").trim(); 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> { export async function uploadAsset(session_key: string, file: File): Promise<string> {
let url = new URL('/asset', ASSET_API_ENDPOINT); let url = new URL('/asset', ASSET_API_ENDPOINT);
url.search = `?session_key=${session_key}`; url.search = `?session_key=${session_key}`;

View file

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