Skip to content
Snippets Groups Projects
Commit ad66b8c4 authored by Emmanuel Raviart's avatar Emmanuel Raviart
Browse files

Add test page to use budget API

parent 8180b5a1
No related branches found
No related tags found
No related merge requests found
Pipeline #7153 passed
......@@ -14,6 +14,12 @@ API_BASE_URLS="https://simulateur-socio-fiscal.leximpact.dev/api/"
# BASE_URL="https://simulateur-socio-fiscal.leximpact.dev/"
BASE_URL="http://localhost:5173"
# Public HTTP(S) URL of LexImpact Socio-Fiscal Budget server
# BUDGET_API_URL="https://SECRET.DOMAIN.NAME/state_simulation"
# Secret key used to sign JSON web tokens sent to Budget API
# BUDGET_JWT_SECRET="SECRET"
# Key for children in a family
CHILDREN_KEY="enfants"
......
......@@ -65,6 +65,14 @@ export function auditConfig(
auditRequire,
)
}
audit.attribute(
data,
"budgetApiUrl",
true,
errors,
remainingKeys,
auditHttpUrl,
)
for (const key of [
"childrenKey",
"familyEntityKey",
......@@ -84,7 +92,11 @@ export function auditConfig(
auditRequire,
)
}
for (const key of ["githubPersonalAccessToken", "reformName"]) {
for (const key of [
"budgetJwtSecret",
"githubPersonalAccessToken",
"reformName",
]) {
audit.attribute(data, key, true, errors, remainingKeys, auditTrimString)
}
audit.attribute(
......
......@@ -9,6 +9,8 @@ export interface Config {
apiBaseUrls: string[]
apiWebSocketBaseUrls: string[]
baseUrl: string
budgetApiUrl?: string
budgetJwtSecret?: string
childrenKey: string
familyEntityKey: string
githubPersonalAccessToken?: string
......@@ -43,6 +45,8 @@ const [validConfig, error] = validateConfig({
allowRobots: process.env["ALLOW_ROBOTS"],
apiBaseUrls: process.env["API_BASE_URLS"],
baseUrl: process.env["BASE_URL"],
budgetApiUrl: process.env["BUDGET_API_URL"],
budgetJwtSecret: process.env["BUDGET_JWT_SECRET"],
childrenKey: process.env["CHILDREN_KEY"],
familyEntityKey: process.env["FAMILY_KEY"],
githubPersonalAccessToken: process.env["GITHUB_PERSONAL_ACCESS_TOKEN"],
......
import { error } from "@sveltejs/kit"
import type { User } from "$lib/users"
import jwt from "jsonwebtoken"
import config from "$lib/server/config"
import type { PageServerLoad } from "./$types"
export const load: PageServerLoad = async ({ fetch, parent, url }) => {
if (
config.budgetApiUrl === undefined ||
config.budgetJwtSecret === undefined
) {
throw error(
404,
"La configuration de l'application ne permet pas d'accéder aux calculs budgétaires",
)
}
const data = await parent()
const user = data.user as User
if (user === undefined) {
throw error(
401,
"Vous devez être authentifiés pour accéder aux calculs budgétaires",
)
}
const payload = {
exp: user.exp,
iat: user.iat,
user: { ...user },
}
delete payload.user.exp
delete payload.user.iat
const response = await fetch(config.budgetApiUrl, {
body: JSON.stringify(
{
base: 2022,
output_variables: ["rfr", "irpp"],
quantile_nb: 10,
quantile_base_variable: ["rfr"],
quantile_compare_variables: ["irpp"],
plf: 2023,
},
null,
2,
),
headers: {
Accept: "application/json",
"Content-Type": "application/json; charset=utf-8",
"jwt-token": jwt.sign(payload, config.budgetJwtSecret),
},
method: "POST",
})
if (!response.ok) {
console.error(
`${url.pathname}: Error calling budget API:\n${response.status} ${response.statusText}`,
)
console.error(await response.text())
return undefined
}
return { simulation: await response.json() }
}
<script lang="ts">
import type { PageData } from "./$types"
export let data: PageData
</script>
<pre>
{JSON.stringify(data, null, 2)}
</pre>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment