Skip to content
Snippets Groups Projects
Commit a84ac6b0 authored by Emmanuel Raviart's avatar Emmanuel Raviart Committed by Toufic Batache
Browse files

WIP d'envoi d'email de demande

parent e1219404
No related branches found
No related tags found
1 merge request!174Ajout des demandes du calcul budget
...@@ -10,14 +10,20 @@ ...@@ -10,14 +10,20 @@
import { browser } from "$app/environment" import { browser } from "$app/environment"
import { goto } from "$app/navigation" import { goto } from "$app/navigation"
import { page } from "$app/stores" import { page } from "$app/stores"
import type { DisplayMode } from "$lib/displays"
import { import {
trackBudgetPublicSimulation, trackBudgetPublicSimulation,
trackBudgetSignInButton, trackBudgetSignInButton,
} from "$lib/matomo" } from "$lib/matomo"
import type { ParametricReform } from "$lib/reforms"
import type { CachedSimulation } from "$lib/simulations" import type { CachedSimulation } from "$lib/simulations"
export let displayMode: DisplayMode
export let isOpen = false export let isOpen = false
const budgetSimulation = getContext("budgetSimulation") as Writable<
BudgetSimulation | undefined
>
let cachedSimulations: CachedSimulation[] = [] let cachedSimulations: CachedSimulation[] = []
$: if (browser && isOpen) { $: if (browser && isOpen) {
...@@ -39,6 +45,36 @@ ...@@ -39,6 +45,36 @@
function onClose() { function onClose() {
isOpen = false isOpen = false
} }
async function sendSimulationRequest() {
const urlString = "/budget/demande"
const res = await fetch(urlString, {
body: JSON.stringify({
displayMode,
parametricReform: Object.entries($parametricReform)
.sort(([keyA], [keyB]) => keyA.localeCompare(keyB))
.reduce((filtered: ParametricReform, [parameterName, value]) => {
if (budgetEditableParametersName.has(parameterName)) {
filtered[parameterName] = value
}
return filtered
}, {}),
}),
headers: {
Accept: "application/json",
"Content-Type": "application/json; charset=utf-8",
},
method: "POST",
})
if (!res.ok) {
console.error(
`Error ${
res.status
} while sending a simulation request at ${urlString}\n\n${await res.text()}`,
)
return
}
}
</script> </script>
<Transition appear show={isOpen}> <Transition appear show={isOpen}>
...@@ -154,8 +190,34 @@ ...@@ -154,8 +190,34 @@
<div <div
class="flex md:flex-row flex-col w-full px-0 md:px-10 items-center gap-5" class="flex md:flex-row flex-col w-full px-0 md:px-10 items-center gap-5"
> >
==== BASE ====
Voir la simulation<iconify-icon
class="align-[-0.25rem] text-xl"
icon="ri-arrow-right-line"
/>
</a>
</div>
{/if}
<div class="flex flex-col gap-10 bg-gray-100 p-5 border-t mt-10">
{#if currentSimulationCache === undefined}
<div>
<h2 class="w-full text-left text-2xl font-bold">
Demandez le calcul de votre réforme au service
LexImpact&nbsp;:
</h2>
<p class="w-full text-base font-normal leading-6 mt-1.5 mb-5">
Après vérification par nos services, si elle est calculable
avec les données dont nous disposons et répond au secret
statistique, la simulation sera rendue publique. Vous serez
alors informé par e-mail :
</p>
<span class="font-bold text-sm py-2 pl-10"
>Votre adresse e-mail :</span
>
<div <div
class="flex rounded-t-md border-b-2 border-b-black bg-white px-2 grow max-w-lg" class="flex md:flex-row flex-col w-full px-0 md:px-10 items-center gap-5"
==== BASE ====
> >
<input <input
autocomplete="off" autocomplete="off"
...@@ -178,9 +240,26 @@ ...@@ -178,9 +240,26 @@
class="ml-2 align-[-0.25rem] text-xl" class="ml-2 align-[-0.25rem] text-xl"
icon="ri-send-plane-2-line" icon="ri-send-plane-2-line"
/> />
==== BASE ====
</div>
<div>
<a
class="flex items-center gap-2 py-2 px-5 shadow-lg bg-white hover:bg-gray-100 active:bg-gray-200 rounded-md border-2 border-le-bleu text-le-bleu text-sm font-bold tracking-[0.085em] uppercase"
data-sveltekit-reload
href={`/auth/login?redirect=${encodeURIComponent(
$page.url.toString(),
)}`}
title="Envoyer votre réforme budgétaire avec cet e-mail"
>
Demander&nbsp;le&nbsp;calcul <iconify-icon
class="ml-2 align-[-0.25rem] text-xl"
icon="ri-send-plane-2-line"
/>
</a> </a>
</div> </div>
</div> </div>
==== BASE ====
</div>
</div> </div>
{#if cachedSimulations !== undefined && cachedSimulations.length > 0} {#if cachedSimulations !== undefined && cachedSimulations.length > 0}
<div class="flex flex-col items-center"> <div class="flex flex-col items-center">
......
...@@ -1762,7 +1762,10 @@ ...@@ -1762,7 +1762,10 @@
</div> </div>
</div> </div>
<BudgetConnexionModal bind:isOpen={isBudgetConnexionModalOpen} /> <BudgetConnexionModal
bind:isOpen={isBudgetConnexionModalOpen}
{displayMode}
/>
{/if} {/if}
<!--Bouton flottant "simulation publique" --> <!--Bouton flottant "simulation publique" -->
......
import { auditRequire, cleanAudit, type Audit } from "@auditors/core"
import { error, json } from "@sveltejs/kit"
import fs from "fs-extra"
import path from "path"
import { getParameter, rootParameter } from "$lib/parameters"
import type { ParametricReform } from "$lib/reforms"
import config from "$lib/server/config"
import type { CachedSimulation } from "$lib/simulations"
import type { RequestHandler } from "./$types"
import { hashObject } from "$lib/hash"
const { simulationsBudgetDir } = config
function auditBody(audit: Audit, dataUnknown: unknown): [unknown, unknown] {
if (dataUnknown == null) {
return [dataUnknown, null]
}
if (typeof dataUnknown !== "object") {
return audit.unexpectedType(dataUnknown, "object")
}
const data = { ...dataUnknown }
const errors: { [key: string]: unknown } = {}
const remainingKeys = new Set(Object.keys(data))
audit.attribute(
data,
"displayMode",
true,
errors,
remainingKeys,
// TODO
auditRequire,
)
audit.attribute(
data,
"parametricReform",
true,
errors,
remainingKeys,
// TODO
auditRequire,
)
return audit.reduceRemaining(data, errors, remainingKeys)
}
export const POST: RequestHandler = async ({ request, url }) => {
const [body, bodyError] = auditBody(cleanAudit, await request.json())
if (bodyError !== null) {
console.error(
`Error in ${url.pathname} body:\n${JSON.stringify(
body,
null,
2,
)}\n\nError:\n${JSON.stringify(bodyError, null, 2)}`,
)
throw error(400, `Invalid body: ${JSON.stringify(bodyError, null, 2)}`)
}
const modifiedParametersTitles = Object.keys(
(body as { parametricReform: ParametricReform }).parametricReform,
).map((parameterName) => getParameter(rootParameter, parameterName)?.title)
const modifiedParametersNames = Object.keys(
(body as { parametricReform: ParametricReform }).parametricReform,
).map((parameterName) => getParameter(rootParameter, parameterName)?.name)
const bodyJson = JSON.stringify(body, null, 2)
const digest = hashObject(
(body as { parametricReform: ParametricReform }).parametricReform,
)
const simulationDir = path.join(simulationsBudgetDir, digest.substring(0, 2))
const simulationFilePath = path.join(simulationDir, `${digest}.json`)
if (!(await fs.pathExists(simulationFilePath))) {
await fs.ensureDir(simulationDir)
await fs.writeFile(simulationFilePath, bodyJson)
}
const indexDir = path.join(simulationsBudgetDir, "index.json")
const indexContents: CachedSimulation[] = (await fs.pathExists(indexDir))
? await fs.readJson(indexDir)
: []
const contents: CachedSimulation[] = [
...indexContents,
{
date: new Intl.DateTimeFormat("fr-FR").format(new Date()),
hash: digest,
parameters: modifiedParametersNames,
title: modifiedParametersTitles.join(" | "),
} as CachedSimulation,
].filter(
(value, index, self) =>
index === self.findLastIndex((el) => el.hash === value.hash),
)
await fs.writeFile(indexDir, JSON.stringify(contents))
return json({ token: digest })
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment