From 4156f4bd27b0c7516173aef1154617523cf41025 Mon Sep 17 00:00:00 2001
From: Toufic Batache <taffou2a@gmail.com>
Date: Tue, 10 Oct 2023 12:13:31 +0200
Subject: [PATCH] Finished unification and fixed clear script
---
.../TestCaseSimulationSharingModal.svelte | 26 +++++++++++++----
src/lib/server/auditors/config.ts | 1 -
src/lib/server/budgets.ts | 12 ++++----
src/lib/server/config.ts | 2 --
src/lib/server/test_cases.ts | 25 +++++++++++++++++
src/routes/budgets/+server.ts | 11 +++++---
src/routes/budgets/simulations/+server.ts | 9 ++++--
.../simulations/[simulation]/+page.server.ts | 4 +--
src/routes/test_cases/+server.ts | 22 ++++++++-------
src/routes/test_cases/simulations/+server.ts | 14 +++++-----
.../simulations/[simulation]/+page.server.ts | 11 ++------
src/scripts/clear_budget_simulations_cache.ts | 28 ++++++++-----------
12 files changed, 99 insertions(+), 66 deletions(-)
create mode 100644 src/lib/server/test_cases.ts
diff --git a/src/lib/components/TestCaseSimulationSharingModal.svelte b/src/lib/components/TestCaseSimulationSharingModal.svelte
index 63e0bfd3e..77713684b 100644
--- a/src/lib/components/TestCaseSimulationSharingModal.svelte
+++ b/src/lib/components/TestCaseSimulationSharingModal.svelte
@@ -107,18 +107,34 @@
<iconify-icon class="block text-2xl" icon="ri-close-line" />
</button>
</div>
- <DialogDescription as="div" class="flex flex-col gap-5 px-44 py-3">
+ <DialogDescription as="div" class="flex flex-col gap-5 px-32 py-3">
<h1 class="w-full text-center text-3xl font-bold">
Partager cette simulation cas type
</h1>
<span class="w-full text-lg">
- Pour partager les impacts cas type de votre réforme, copiez l'URL
- suivante :
+ Pour retrouver ou partager votre écran de travail tel qu'il est
+ visible actuellement, gardez ou transmettez l'URL suivante :
</span>
<div class="w-full flex flex-col gap-2 pb-5">
- <div class="flex justify-center rounded bg-gray-200 p-3">
- <code class="pl-2 text-xs">{url}</code>
+ <div
+ class="group/container flex justify-between items-center rounded bg-gray-200 px-3"
+ >
+ <code
+ class="py-3 text-xs text-ellipsis overflow-hidden whitespace-nowrap"
+ >{url}</code
+ >
+ <button
+ class="opacity-0 group-hover/container:opacity-100 group/button p-1 rounded transition-all duration-100 ease-linear hover:bg-black hover:bg-opacity-5 active:bg-opacity-10"
+ on:click={copyLink}
+ >
+ <iconify-icon
+ class="block text-lg text-[#737278] transition-colors duration-100 ease-linear group-hover/button:text-[#535158]"
+ icon={!hasClickedCopy
+ ? "pajamas:copy-to-clipboard"
+ : "ri-check-line"}
+ />
+ </button>
</div>
<span class="text-xs text-gray-500">
Cette URL sauvegarde la réforme et les cas types. Ce lien est
diff --git a/src/lib/server/auditors/config.ts b/src/lib/server/auditors/config.ts
index 564a457d3..823d02997 100644
--- a/src/lib/server/auditors/config.ts
+++ b/src/lib/server/auditors/config.ts
@@ -74,7 +74,6 @@ export function auditConfig(
"householdEntityKey",
"jwtSecret",
"simulationsBudgetDir",
- "simulationsDir",
"simulationsTestCasesDir",
"taxableHouseholdEntityKey",
"title",
diff --git a/src/lib/server/budgets.ts b/src/lib/server/budgets.ts
index 8be9c86ef..7936c98bf 100644
--- a/src/lib/server/budgets.ts
+++ b/src/lib/server/budgets.ts
@@ -2,24 +2,24 @@ import path from "path"
import config from "$lib/server/config"
-export function getRequestsDirPath(digest: string) {
+export function getBudgetRequestsDirPath(digest: string) {
return path.join(
config.simulationsBudgetDir,
"requests",
digest.substring(0, 2),
)
}
-export function getRequestsFilePath(digest: string) {
- return path.join(getRequestsDirPath(digest), `${digest}.json`)
+export function getBudgetRequestsFilePath(digest: string) {
+ return path.join(getBudgetRequestsDirPath(digest), `${digest}.json`)
}
-export function getCacheDirPath(digest: string) {
+export function getBudgetCacheDirPath(digest: string) {
return path.join(
config.simulationsBudgetDir,
"responses",
digest.substring(0, 2),
)
}
-export function getCacheFilePath(digest: string) {
- return path.join(getCacheDirPath(digest), `${digest}.json`)
+export function getBudgetCacheFilePath(digest: string) {
+ return path.join(getBudgetCacheDirPath(digest), `${digest}.json`)
}
diff --git a/src/lib/server/config.ts b/src/lib/server/config.ts
index e44521115..8a1d114ca 100644
--- a/src/lib/server/config.ts
+++ b/src/lib/server/config.ts
@@ -38,7 +38,6 @@ export interface Config {
showTutorial?: boolean
simulationsBudgetDir: string
simulationsTestCasesDir: string
- simulationsDir: string
taxableHouseholdEntityKey: string
territoiresUrl: string
title: string
@@ -89,7 +88,6 @@ const [validConfig, error] = validateConfig({
revaluationName: process.env["REFORM_REVALUATION"],
showTutorial: process.env["SHOW_TUTORIAL"],
simulationsBudgetDir: process.env["SIMULATIONS_BUDGET_DIR"],
- simulationsDir: process.env["SIMULATIONS_DIR"],
simulationsTestCasesDir: process.env["SIMULATIONS_TEST_CASES_DIR"],
taxableHouseholdEntityKey: process.env["TAXABLE_HOUSEHOLD_KEY"],
territoiresUrl: process.env["TERRITOIRES_URL"],
diff --git a/src/lib/server/test_cases.ts b/src/lib/server/test_cases.ts
new file mode 100644
index 000000000..609f7a766
--- /dev/null
+++ b/src/lib/server/test_cases.ts
@@ -0,0 +1,25 @@
+import path from "path"
+
+import config from "$lib/server/config"
+
+export function getTestCasesRequestsDirPath(digest: string) {
+ return path.join(
+ config.simulationsTestCasesDir,
+ "requests",
+ digest.substring(0, 2),
+ )
+}
+export function getTestCasesRequestsFilePath(digest: string) {
+ return path.join(getTestCasesRequestsDirPath(digest), `${digest}.json`)
+}
+
+export function getTestCasesCacheDirPath(digest: string) {
+ return path.join(
+ config.simulationsTestCasesDir,
+ "responses",
+ digest.substring(0, 2),
+ )
+}
+export function getTestCasesCacheFilePath(digest: string) {
+ return path.join(getTestCasesCacheDirPath(digest), `${digest}.json`)
+}
diff --git a/src/routes/budgets/+server.ts b/src/routes/budgets/+server.ts
index 1689bb191..d3d397945 100644
--- a/src/routes/budgets/+server.ts
+++ b/src/routes/budgets/+server.ts
@@ -6,7 +6,10 @@ import { error, json } from "@sveltejs/kit"
import { hashObject } from "$lib/hash"
import { getParameter, rootParameter } from "$lib/parameters"
import type { ParametricReform } from "$lib/reforms"
-import { getCacheDirPath, getCacheFilePath } from "$lib/server/budgets"
+import {
+ getBudgetCacheDirPath,
+ getBudgetCacheFilePath,
+} from "$lib/server/budgets"
import config from "$lib/server/config"
import type { CachedSimulation } from "$lib/simulations"
import type { User } from "$lib/users"
@@ -82,7 +85,7 @@ export const POST: RequestHandler = async ({ fetch, locals, request }) => {
)
// Path of the no-reform simulation cache
- const baseCacheFilePath = getCacheFilePath(baseCacheDigest)
+ const baseCacheFilePath = getBudgetCacheFilePath(baseCacheDigest)
// Hash digest of the current simulation
const simulationCacheDigest = hashBudgetSimulationCache(
@@ -91,7 +94,7 @@ export const POST: RequestHandler = async ({ fetch, locals, request }) => {
)
// Path of the current simulation's cache
- const simulationCacheFilePath = getCacheFilePath(simulationCacheDigest)
+ const simulationCacheFilePath = getBudgetCacheFilePath(simulationCacheDigest)
// Indicates if the current simulation is calculated and is public
const isSimulationPublic = simulationsIndexContents.find(
@@ -201,7 +204,7 @@ export const POST: RequestHandler = async ({ fetch, locals, request }) => {
)
if (!(await fs.pathExists(simulationCacheFilePath))) {
- await fs.ensureDir(getCacheDirPath(simulationCacheDigest))
+ await fs.ensureDir(getBudgetCacheDirPath(simulationCacheDigest))
await fs.writeFile(
simulationCacheFilePath,
JSON.stringify(responseJson.result, null, 2),
diff --git a/src/routes/budgets/simulations/+server.ts b/src/routes/budgets/simulations/+server.ts
index f5cf772d2..1134215e3 100644
--- a/src/routes/budgets/simulations/+server.ts
+++ b/src/routes/budgets/simulations/+server.ts
@@ -6,7 +6,10 @@ import { error, json } from "@sveltejs/kit"
import type { DisplayMode } from "$lib/displays"
import type { ParametricReform } from "$lib/reforms"
-import { getRequestsDirPath, getRequestsFilePath } from "$lib/server/budgets"
+import {
+ getBudgetRequestsDirPath,
+ getBudgetRequestsFilePath,
+} from "$lib/server/budgets"
import config from "$lib/server/config"
import type { CachedSimulation } from "$lib/simulations"
import type { User } from "$lib/users"
@@ -101,9 +104,9 @@ export const POST: RequestHandler = async ({ locals, request, url }) => {
* Make public the request data (parametric reform and display mode)
*
* */
- const requestParamsFilePath = getRequestsFilePath(hash)
+ const requestParamsFilePath = getBudgetRequestsFilePath(hash)
if (!(await fs.pathExists(requestParamsFilePath))) {
- await fs.ensureDir(getRequestsDirPath(hash))
+ await fs.ensureDir(getBudgetRequestsDirPath(hash))
await fs.writeFile(
requestParamsFilePath,
JSON.stringify({ parametricReform, displayMode }, null, 2),
diff --git a/src/routes/budgets/simulations/[simulation]/+page.server.ts b/src/routes/budgets/simulations/[simulation]/+page.server.ts
index 39e26ecb0..4083e5ae4 100644
--- a/src/routes/budgets/simulations/[simulation]/+page.server.ts
+++ b/src/routes/budgets/simulations/[simulation]/+page.server.ts
@@ -15,7 +15,7 @@ import config from "$lib/server/config"
import type { CachedSimulation } from "$lib/simulations"
import type { PageServerLoad } from "./$types"
-import { getRequestsFilePath } from "$lib/server/budgets"
+import { getBudgetRequestsFilePath } from "$lib/server/budgets"
const { simulationsBudgetDir } = config
@@ -71,7 +71,7 @@ export const load: PageServerLoad = async ({
? await fs.readJson(indexFilePath)
: []
- const simulationFilePath = getRequestsFilePath(digest)
+ const simulationFilePath = getBudgetRequestsFilePath(digest)
if (
!(await fs.pathExists(simulationFilePath)) ||
(locals.user === undefined &&
diff --git a/src/routes/test_cases/+server.ts b/src/routes/test_cases/+server.ts
index 05099c336..3c5ff6a53 100644
--- a/src/routes/test_cases/+server.ts
+++ b/src/routes/test_cases/+server.ts
@@ -1,13 +1,16 @@
import fs from "fs-extra"
-import path from "path"
import { json } from "@sveltejs/kit"
import { hashObject } from "$lib/hash"
import config from "$lib/server/config"
+import {
+ getTestCasesCacheDirPath,
+ getTestCasesCacheFilePath,
+} from "$lib/server/test_cases"
import type { RequestHandler } from "./$types"
-const { apiBaseUrls, simulationsTestCasesDir } = config
+const { apiBaseUrls } = config
export const POST: RequestHandler = async ({ fetch, request }) => {
const input = await request.clone().json()
@@ -24,11 +27,12 @@ export const POST: RequestHandler = async ({ fetch, request }) => {
// Ignore token.
variables: input.variables.sort(),
})
- const cacheDir = path.join(simulationsTestCasesDir, digest.substring(0, 2))
- const cacheFilePath = path.join(cacheDir, `${digest}.json`)
+
+ const cacheDir = getTestCasesCacheDirPath(digest)
+ const cacheFilePath = getTestCasesCacheFilePath(digest)
if (await fs.pathExists(cacheFilePath)) {
- return json((await fs.readJson(cacheFilePath)).output)
+ return json(await fs.readJson(cacheFilePath))
}
const apiBaseUrlIndex = Math.floor(Math.random() * apiBaseUrls.length)
@@ -44,14 +48,12 @@ export const POST: RequestHandler = async ({ fetch, request }) => {
if (!response.ok) {
return response
}
+
const output = await response.json()
+
if (!(await fs.pathExists(cacheFilePath))) {
await fs.ensureDir(cacheDir)
- await fs.writeJson(
- cacheFilePath,
- { digest, input, output },
- { encoding: "utf-8", spaces: 2 },
- )
+ await fs.writeJson(cacheFilePath, output, { encoding: "utf-8", spaces: 2 })
}
return json(output)
}
diff --git a/src/routes/test_cases/simulations/+server.ts b/src/routes/test_cases/simulations/+server.ts
index 058efe33d..981141c0d 100644
--- a/src/routes/test_cases/simulations/+server.ts
+++ b/src/routes/test_cases/simulations/+server.ts
@@ -6,14 +6,14 @@ import {
} from "@auditors/core"
import { error, json } from "@sveltejs/kit"
import fs from "fs-extra"
-import path from "path"
import { hashString } from "$lib/hash"
-import config from "$lib/server/config"
-
-import type { RequestHandler } from "../../../../.svelte-kit/types/src/routes"
+import {
+ getTestCasesRequestsDirPath,
+ getTestCasesRequestsFilePath,
+} from "$lib/server/test_cases"
-const { simulationsDir } = config
+import type { RequestHandler } from "./$types"
function auditBody(audit: Audit, dataUnknown: unknown): [unknown, unknown] {
if (dataUnknown == null) {
@@ -84,8 +84,8 @@ export const POST: RequestHandler = async ({ request, url }) => {
const digest = hashString(bodyJson)
- const simulationDir = path.join(simulationsDir, digest.substring(0, 2))
- const simulationFilePath = path.join(simulationDir, `${digest}.json`)
+ const simulationDir = getTestCasesRequestsDirPath(digest)
+ const simulationFilePath = getTestCasesRequestsFilePath(digest)
if (!(await fs.pathExists(simulationFilePath))) {
await fs.ensureDir(simulationDir)
await fs.writeFile(simulationFilePath, bodyJson)
diff --git a/src/routes/test_cases/simulations/[simulation]/+page.server.ts b/src/routes/test_cases/simulations/[simulation]/+page.server.ts
index 01d4b4ccf..c647ac56e 100644
--- a/src/routes/test_cases/simulations/[simulation]/+page.server.ts
+++ b/src/routes/test_cases/simulations/[simulation]/+page.server.ts
@@ -7,17 +7,14 @@ import {
} from "@auditors/core"
import { error } from "@sveltejs/kit"
import fs from "fs-extra"
-import path from "path"
import type { DisplayMode } from "$lib/displays"
import type { ParametricReform } from "$lib/reforms"
+import { getTestCasesRequestsFilePath } from "$lib/server/test_cases"
import type { Situation } from "$lib/situations"
-import config from "$lib/server/config"
import type { PageServerLoad } from "./$types"
-const { simulationsDir } = config
-
function auditParams(audit: Audit, dataUnknown: unknown): [unknown, unknown] {
if (dataUnknown == null) {
return [dataUnknown, null]
@@ -61,11 +58,7 @@ export const load: PageServerLoad = async ({ params: requestParams, url }) => {
}
const { simulation: digest } = params as { simulation: string }
- const simulationFilePath = path.join(
- simulationsDir,
- digest.substring(0, 2),
- `${digest}.json`,
- )
+ const simulationFilePath = getTestCasesRequestsFilePath(digest)
if (!(await fs.pathExists(simulationFilePath))) {
throw error(404, `Simulation ${digest} not found`)
}
diff --git a/src/scripts/clear_budget_simulations_cache.ts b/src/scripts/clear_budget_simulations_cache.ts
index 857fd3425..cd02c3be1 100644
--- a/src/scripts/clear_budget_simulations_cache.ts
+++ b/src/scripts/clear_budget_simulations_cache.ts
@@ -3,7 +3,7 @@ import path from "path"
import config from "$lib/server/config"
-const { simulationsBudgetDir } = config
+const { simulationsBudgetDir, simulationsTestCasesDir } = config
// Remove private budget simulations from index.
const indexFilePath = path.join(simulationsBudgetDir, "index.json")
@@ -17,20 +17,14 @@ if (fs.pathExistsSync(indexFilePath)) {
})
}
-// Remove every budget simulations from cache.
-for (const dirName of await fs.readdir(simulationsBudgetDir)) {
- if (dirName.startsWith(".")) {
- continue
- }
- if (dirName === "parametric_reforms") {
- const privateDir = path.join(simulationsBudgetDir, dirName, "private")
- if (fs.lstatSync(privateDir).isDirectory()) {
- fs.removeSync(privateDir)
- }
- continue
- }
- const dir = path.join(simulationsBudgetDir, dirName)
- if (fs.lstatSync(dir).isDirectory()) {
- fs.removeSync(dir)
- }
+// Remove every budget simulation from cache.
+const budgetsDir = path.join(simulationsBudgetDir, "responses")
+if (fs.lstatSync(budgetsDir).isDirectory()) {
+ fs.removeSync(budgetsDir)
+}
+
+// Remove every test case simulation from cache.
+const testCasesDir = path.join(simulationsTestCasesDir, "responses")
+if (fs.lstatSync(testCasesDir).isDirectory()) {
+ fs.removeSync(testCasesDir)
}
--
GitLab