diff --git a/frontend/src/routes/(app)/risk-assessments/[id=uuid]/+layout.server.ts b/frontend/src/routes/(app)/risk-assessments/[id=uuid]/+layout.server.ts index 5d03bbcc8..e70ce5904 100644 --- a/frontend/src/routes/(app)/risk-assessments/[id=uuid]/+layout.server.ts +++ b/frontend/src/routes/(app)/risk-assessments/[id=uuid]/+layout.server.ts @@ -1,15 +1,11 @@ import { BASE_API_URL } from '$lib/utils/constants'; -import { getModelInfo, type ModelMapEntry } from '$lib/utils/crud'; +import { getModelInfo } from '$lib/utils/crud'; import { modelSchema } from '$lib/utils/schemas'; -import { listViewFields } from '$lib/utils/table'; +import { tableSourceMapper, type TableSource } from '@skeletonlabs/skeleton'; import { superValidate } from 'sveltekit-superforms/server'; -import { z, type AnyZodObject } from 'zod'; +import { z } from 'zod'; import type { LayoutServerLoad } from './$types'; -import type { UUID } from 'crypto'; -import { tableSourceMapper, type TableSource } from '@skeletonlabs/skeleton'; -import type { SuperValidated } from 'sveltekit-superforms'; -import type { urlModel } from '$lib/utils/types'; export const load: LayoutServerLoad = async ({ fetch, params }) => { const endpoint = `${BASE_API_URL}/risk-assessments/${params.id}/`; @@ -18,139 +14,43 @@ export const load: LayoutServerLoad = async ({ fetch, params }) => { const scenarios = await fetch(`${BASE_API_URL}/risk-scenarios/?risk_assessment=${params.id}`) .then((res) => res.json()) .then((res) => res.results); - const scenariosFilter: string = - '?' + - scenarios.map((scenario: Record) => `risk_scenarios=${scenario.id}`).join('&'); - const measures = await fetch(`${BASE_API_URL}/security-measures/${scenariosFilter}`).then((res) => - res.json().then((res) => { - const sorted = res.results.sort((a: Record, b: Record) => { - const dateA = new Date(a.created_at); - const dateB = new Date(b.created_at); - return dateA.getTime() - dateB.getTime(); - }); - return sorted; - }) - ); const risk_matrix = await fetch( `${BASE_API_URL}/risk-matrices/${risk_assessment.risk_matrix.id}/` ).then((res) => res.json()); - // Create a lookup for measures based on their id - const measureLookup: { [id: string]: Record } = measures.reduce( - (acc: Record, measure: Record) => { - acc[measure.id] = measure; - return acc; - }, - {} - ); - - // Replace the measures' UUIDs in each scenario with the corresponding measure instances - const transformedScenarios = scenarios.map((scenario: Record) => ({ - ...scenario, - security_measures: scenario.security_measures.map((childId: UUID) => measureLookup[childId]) - })); - - risk_assessment.risk_scenarios = transformedScenarios; - risk_assessment.risk_matrix = risk_matrix; - - type RelatedModel = { - urlModel: urlModel; - info: ModelMapEntry; - table: TableSource; - deleteForm: SuperValidated; - createForm: SuperValidated; - foreignKeys: Record; - selectOptions: Record; - }; - - type RelatedModels = { - [K in urlModel]: RelatedModel; + const scenariosTable: TableSource = { + head: [ + 'rid', + 'name', + 'threats', + 'existingMeasures', + 'currentLevel', + 'securityMeasures', + 'residualLevel' + ], + body: tableSourceMapper(scenarios, [ + 'rid', + 'name', + 'threats', + 'existing_measures', + 'current_level', + 'security_measures', + 'residual_level' + ]), + meta: scenarios }; - const model = getModelInfo('risk-assessments'); - const relatedModels = {} as RelatedModels; - - if (model.reverseForeignKeyFields) { - await Promise.all( - model.reverseForeignKeyFields.map(async (e) => { - const relEndpoint = `${BASE_API_URL}/${e.urlModel}/?${e.field}=${params.id}`; - const res = await fetch(relEndpoint); - const data = await res.json().then((res) => res.results); - - const metaData = tableSourceMapper(data, ['id']); - - const bodyData = tableSourceMapper( - data, - listViewFields[e.urlModel].body.filter((field) => field !== 'risk_assessment') - ); - - const table: TableSource = { - head: listViewFields[e.urlModel].head.filter((field) => field !== 'riskAssessment'), - body: bodyData, - meta: metaData - }; - - const info = getModelInfo(e.urlModel); - const urlModel = e.urlModel; - - const deleteForm = await superValidate(z.object({ id: z.string().uuid() })); - const createSchema = modelSchema(e.urlModel); - const createForm = await superValidate( - { risk_assessment: risk_assessment.id }, - createSchema, - { - errors: false - } - ); - - const foreignKeys: Record = {}; + risk_assessment.risk_scenarios = scenarios; + risk_assessment.risk_matrix = risk_matrix; - if (info.foreignKeyFields) { - for (const keyField of info.foreignKeyFields) { - const queryParams = keyField.urlParams ? `?${keyField.urlParams}` : ''; - const url = `${BASE_API_URL}/${keyField.urlModel}/${queryParams}`; - const response = await fetch(url); - if (response.ok) { - foreignKeys[keyField.field] = await response.json().then((data) => data.results); - } else { - console.error(`Failed to fetch data for ${keyField.field}: ${response.statusText}`); - } - } - } + const deleteSchema = z.object({ id: z.string() }); + const scenarioDeleteForm = await superValidate(deleteSchema); - const selectOptions: Record = {}; + const scenarioSchema = modelSchema('risk-scenarios'); + const scenarioCreateForm = await superValidate(scenarioSchema); - if (info.selectFields) { - for (const selectField of info.selectFields) { - const url = `${BASE_API_URL}/${urlModel}/${selectField.field}/`; - const response = await fetch(url); - if (response.ok) { - selectOptions[selectField.field] = await response.json().then((data) => - Object.entries(data).map(([key, value]) => ({ - label: value, - value: key - })) - ); - } else { - console.error( - `Failed to fetch data for ${selectField.field}: ${response.statusText}` - ); - } - } - } - relatedModels[e.urlModel] = { - urlModel, - info, - table, - deleteForm, - createForm, - foreignKeys, - selectOptions - }; - }) - ); - } + const scenarioModel = getModelInfo('risk-scenarios'); - return { risk_assessment, relatedModels }; + return { risk_assessment, scenarioModel, scenariosTable, scenarioDeleteForm, scenarioCreateForm }; }; diff --git a/frontend/src/routes/(app)/risk-assessments/[id=uuid]/+page.svelte b/frontend/src/routes/(app)/risk-assessments/[id=uuid]/+page.svelte index b9d267d5b..948844bc5 100644 --- a/frontend/src/routes/(app)/risk-assessments/[id=uuid]/+page.svelte +++ b/frontend/src/routes/(app)/risk-assessments/[id=uuid]/+page.svelte @@ -1,26 +1,23 @@