amount from xlsx

This commit is contained in:
2025-06-09 18:51:15 +02:00
parent e250b6b5d8
commit 9597baf51a
9 changed files with 295 additions and 190 deletions

62
src/api/index.ts Normal file
View File

@@ -0,0 +1,62 @@
import { Auth } from '@auth/core'
import type { AuthAction, Session } from '@auth/core/types'
import authConfig from 'auth:config'
import * as fs from 'fs';
//import { read, readFile } from "xlsx";
import pkg from 'xlsx';
const {readFile, utils} = pkg;
export interface KarKarCarSession extends Session {
userData?: UserAmount | null;
}
/**
* Fetches the current session.
* @param req The request object.
* @returns The current session, or `null` if there is no session.
*/
export async function getUser(req: Request, options = authConfig): Promise<KarKarCarSession | null> {
// @ts-ignore
options.secret ??= import.meta.env.AUTH_SECRET
options.trustHost ??= true
const url = new URL(`${options.prefix}/session`, req.url)
const response = await Auth(new Request(url, { headers: req.headers }), options)
const { status = 200 } = response
const data = await response.json()
console.log("data", data)
if(data != null)
data.userData = await getUserAmount(data.user.email)
if (!data || !Object.keys(data).length) return null
if (status === 200) return data
throw new Error(data.message)
}
export interface UserAmount {
amount?: String | null;
}
async function getUserAmount(email: String | null | undefined): Promise<UserAmount | null> {
const file = readFile("./example-data/saldos.xlsx");
let data = []
const sheets = file.SheetNames
for(let i = 0; i < sheets.length; i++)
{
const temp = utils.sheet_to_json(
file.Sheets[file.SheetNames[i]])
temp.forEach((res) => {
data.push(res)
})
}
let userData = data.filter(data => data.Email == email)
console.log(userData)
return {amount:userData[0]['Saldo Actual']};
}

View File

@@ -0,0 +1,19 @@
---
import type { KarKarCarSession } from "../api/index.ts"
import { getUser } from "../api/index"
import type FullAuthConfig from 'auth-astro'
import config from 'auth:config'
interface Props {
authConfig?: typeof config
}
const { authConfig = config } = Astro.props as Props
let session = await getUser(Astro.request, authConfig)
---
<div>
<Fragment set:html={Astro.slots.render('default', [session])} />
</div>

View File

@@ -4,13 +4,18 @@ import type { Session } from '@auth/core/types';
import { getSession } from 'auth-astro/server';
import { Auth, SignIn, SignOut } from 'auth-astro/components';
import KarKarCarAuth from "../components/KarKarCarAuth.astro"
// 1. Import any dependencies (Full support for JavaScript/TypeScript)
import type { KarKarCarSession } from "../api/index.ts"
const session = await getSession(Astro.request);
console.log(session);
---
<Auth>
{(session: Session) => (
console.log(session),
<KarKarCarAuth>
{(session: KarKarCarSession) => (
<>
{session ?
<SignOut>Logout</SignOut>
@@ -18,8 +23,11 @@ console.log(session);
<SignIn provider="keycloak">Login</SignIn>
}
<p>
{session ? `Logged in as ${session.user?.name}` : 'Not logged in'}
{session ? `Amount ${session.userData.amount}` : 'Not logged in'}
</p>
</>
)}
</Auth>
</KarKarCarAuth>