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

View File

@@ -1,5 +1,14 @@
# How to:
## Start
- ```
docker run --name keycloak \
-e KEYCLOAK_ADMIN=admin \
-e KEYCLOAK_ADMIN_PASSWORD=admin \
--network=host \
quay.io/keycloak/keycloak:24.0.0 \
start-dev \
--http-port=8180
```
- `docker-compose up`
- `docker-compose up -d` | Modo demonio
- Keycloak: http://localhost:8180/ user/pass admin:admin
@@ -28,4 +37,9 @@ KEYCLOAK_ISSUER=http://localhost:8180/realms/master
### OpenId info
- http://localhost:8180/realms/master/.well-known/openid-configuration
- http://localhost:8180/realms/master/.well-known/openid-configuration
# ToDo:
- Service: saldo from mail | read csv,xlsx
- I+D: host astro server

View File

@@ -12,6 +12,8 @@ export default defineConfig({
clientId: import.meta.env.KEYCLOAK_CLIENT_ID,
clientSecret: import.meta.env.KEYCLOAK_CLIENT_SECRET,
issuer: import.meta.env.KEYCLOAK_ISSUER,
}),
],

View File

@@ -0,0 +1 @@
,oier,XPS24,09.06.2025 18:50,file:///home/oier/.config/libreoffice/4;

BIN
example-data/saldos.xlsx Normal file

Binary file not shown.

356
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -9,9 +9,10 @@
"astro": "astro"
},
"dependencies": {
"@astrojs/node": "^9.2.1",
"@auth/core": "^0.39.0",
"astro": "^5.7.12",
"auth-astro": "^4.2.0"
"@astrojs/node": "^9.2.2",
"@auth/core": "^0.37.3",
"astro": "^5.9.2",
"auth-astro": "^4.2.0",
"xlsx": "^0.18.5"
}
}

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>