diff options
Diffstat (limited to 'app/src/routes')
| -rw-r--r-- | app/src/routes/+page.svelte | 7 | ||||
| -rw-r--r-- | app/src/routes/methods.remote.ts | 24 | ||||
| -rw-r--r-- | app/src/routes/sb1-authorize/+server.ts | 21 | ||||
| -rw-r--r-- | app/src/routes/status.svelte | 17 |
4 files changed, 50 insertions, 19 deletions
diff --git a/app/src/routes/+page.svelte b/app/src/routes/+page.svelte index ee4148b..b12c471 100644 --- a/app/src/routes/+page.svelte +++ b/app/src/routes/+page.svelte @@ -12,7 +12,8 @@ dryRun: true, }); - async function run() { + async function run(e: SubmitEvent) { + e.preventDefault(); if (!form.mappings.length) { return; } @@ -21,8 +22,7 @@ async function authorize() { navigating = true; - const url = await init_auth_session(); - location.href = url; + location.href = await init_auth_session(); } async function logout() { @@ -74,6 +74,7 @@ </form> <h3>Annet</h3> <Button onclick={logout} loading={navigating}>Logg ut</Button> + <div></div> {:else} <Button onclick={authorize} loading={navigating}>Autentisér hos Sparebanken 1</Button> {/if} diff --git a/app/src/routes/methods.remote.ts b/app/src/routes/methods.remote.ts index 3fca715..d9ba812 100644 --- a/app/src/routes/methods.remote.ts +++ b/app/src/routes/methods.remote.ts @@ -1,8 +1,8 @@ import { db } from "$lib/server/db"; -import { syncSession } from "$lib/server/db/schema"; +import { SyncSessionTable } from "$lib/server/db/schema"; import { command, query } from "$app/server"; import sb1 from "$lib/server/sb1"; -import { import_transactions } from "$lib/server/actual"; +import { import_transactions, init_actual } from "$lib/server/actual"; import { ImportForm } from "$lib/shared"; const init_auth_session = command(async () => { @@ -10,21 +10,31 @@ const init_auth_session = command(async () => { }) const clear_auth_session = query(async () => { - await db.delete(syncSession) + await db.delete(SyncSessionTable) }) const do_import = command(ImportForm, async (form) => { - let x for (const mapping of form.mappings) { const transactions = await sb1.data.get_transactions(mapping.sb1Id) - if (!transactions?.length || x) continue - x = true - console.log(await import_transactions(mapping.actualId, transactions, form.dryRun)) + console.log(transactions) + continue + // if (!transactions?.length) continue + // console.log(await import_transactions(mapping.actualId, transactions, form.dryRun)) } }) +const init_sb1 = command(async () => { + return await sb1.init() +}) + +const _init_actual = command(async () => { + return await init_actual() +}) + export { init_auth_session, do_import, + _init_actual as init_actual, + init_sb1, clear_auth_session } diff --git a/app/src/routes/sb1-authorize/+server.ts b/app/src/routes/sb1-authorize/+server.ts index b3a0cf7..d6b8fbf 100644 --- a/app/src/routes/sb1-authorize/+server.ts +++ b/app/src/routes/sb1-authorize/+server.ts @@ -1,10 +1,11 @@ -import { error, redirect } from '@sveltejs/kit'; +import { error, redirect, json } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; import { db } from '$lib/server/db'; -import { syncSession } from '$lib/server/db/schema'; +import { SyncSessionTable } from '$lib/server/db/schema'; import { eq } from 'drizzle-orm'; import { SB1_ID, SB1_REDIRECT_URI, SB1_SECRET } from '$env/static/private'; import { Temporal } from "temporal-polyfill" +import sb1 from "$lib/server/sb1" export const GET: RequestHandler = async ({ url }) => { const code = url.searchParams.get('code') @@ -13,16 +14,19 @@ export const GET: RequestHandler = async ({ url }) => { if (!code) error(400, "?code is missing") if (!state) error(400, "?state is missing") - const session = await db.select().from(syncSession).where(eq(syncSession.authzState, state)) + const session = await db.select().from(SyncSessionTable).where(eq(SyncSessionTable.authzState, state)) const { id } = session[0] + if (!id) return error(500, "Ingen session") const fd = new URLSearchParams() + fd.set("client_id", SB1_ID) fd.set("client_secret", SB1_SECRET) fd.set("redirect_uri", SB1_REDIRECT_URI) fd.set("code", code) fd.set("state", state) fd.set("grant_type", "authorization_code") + const response = await fetch("https://api.sparebank1.no/oauth/token", { method: "post", headers: { @@ -31,15 +35,14 @@ export const GET: RequestHandler = async ({ url }) => { body: fd }) - const json = await response.json() + const responseJson = await response.json() if (response.ok) { const epoch = Temporal.Now.instant().epochMilliseconds - await db.update(syncSession).set({ tokens: json, accessTokenCreated: epoch.toString(), refreshTokenCreated: epoch.toString() }).where(eq(syncSession.id, id)) + await db.update(SyncSessionTable).set({ tokens: responseJson, accessTokenCreated: epoch.toString(), refreshTokenCreated: epoch.toString() }).where(eq(SyncSessionTable.id, id)) + await sb1.init() redirect(302, "/") } else { - return new Response(json) + return json(responseJson) } - - return new Response() -}
\ No newline at end of file +} diff --git a/app/src/routes/status.svelte b/app/src/routes/status.svelte new file mode 100644 index 0000000..fe09193 --- /dev/null +++ b/app/src/routes/status.svelte @@ -0,0 +1,17 @@ +<script lang="ts"> + type Props = { + type: "sb1" | "actual"; + }; + import { onMount } from "svelte"; + let { type }: Props = $props(); + + onMount(() => {}); +</script> + +<div> + <span>{type}</span> + <div class="status"></div> + <div class="refresh">⟲</div> +</div> + +<style></style> |
