Skip to content

Commit

Permalink
chore: cleaning useles foreignKey fetches and old missing constraints…
Browse files Browse the repository at this point in the history
… logic
  • Loading branch information
Mohamed-Hacene committed Jan 31, 2025
1 parent aaba32c commit 759437e
Show file tree
Hide file tree
Showing 33 changed files with 5 additions and 651 deletions.
15 changes: 1 addition & 14 deletions frontend/src/lib/components/DetailView/DetailView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import List from '$lib/components/List/List.svelte';
import ConfirmModal from '$lib/components/Modals/ConfirmModal.svelte';
import CreateModal from '$lib/components/Modals/CreateModal.svelte';
import MissingConstraintsModal from '$lib/components/Modals/MissingConstraintsModal.svelte';
import ModelTable from '$lib/components/ModelTable/ModelTable.svelte';
import { ISO_8601_REGEX } from '$lib/utils/constants';
import { URL_MODEL_MAP, checkConstraints } from '$lib/utils/crud';
import { URL_MODEL_MAP } from '$lib/utils/crud';
import { getModelInfo } from '$lib/utils/crud.js';
import { formatDateOrDateTime } from '$lib/utils/datetime';
import { isURL } from '$lib/utils/helpers';
Expand Down Expand Up @@ -80,18 +79,6 @@
// Data
title: safeTranslate('add-' + model.info.localName)
};
if (checkConstraints(model.createForm.constraints, model.foreignKeys).length > 0) {
modalComponent = {
ref: MissingConstraintsModal
};
modal = {
type: 'component',
component: modalComponent,
title: m.warning(),
body: safeTranslate('add-' + model.info.localName).toLowerCase(),
value: checkConstraints(model.createForm.constraints, model.foreignKeys)
};
}
modalStore.trigger(modal);
}
Expand Down
37 changes: 0 additions & 37 deletions frontend/src/lib/components/Modals/MissingConstraintsModal.svelte

This file was deleted.

9 changes: 0 additions & 9 deletions frontend/src/lib/utils/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ type GetOptionsParams = {
selfSelect?: boolean;
};

export function checkConstraints(constraints: { [key: string]: any }, foreignKeys: any): string[] {
const emptyConstraintsList = [];
for (const [key, constraint] of Object.entries(constraints)) {
if (constraint.required && foreignKeys[key])
if (foreignKeys[key].length === 0) emptyConstraintsList.push(key);
}
return emptyConstraintsList;
}

function getValue(object: { [key: string]: any }, keys: string | string[]) {
if (typeof keys === 'string') {
return object[keys];
Expand Down
14 changes: 0 additions & 14 deletions frontend/src/routes/(app)/(internal)/[model=urlmodel]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<script lang="ts">
import { safeTranslate } from '$lib/utils/i18n';
import CreateModal from '$lib/components/Modals/CreateModal.svelte';
import MissingConstraintsModal from '$lib/components/Modals/MissingConstraintsModal.svelte';
import ModelTable from '$lib/components/ModelTable/ModelTable.svelte';
import type { ModalComponent, ModalSettings, ModalStore } from '@skeletonlabs/skeleton';
import { getModalStore } from '@skeletonlabs/skeleton';
import type { ActionData, PageData } from './$types';
import * as m from '$paraglide/messages';
import { checkConstraints } from '$lib/utils/crud';
import { getSecureRedirect } from '$lib/utils/helpers';
import { goto } from '$app/navigation';
import { driverInstance } from '$lib/utils/stores';
Expand Down Expand Up @@ -55,18 +53,6 @@
// Data
title: safeTranslate('importFolder')
};
if (checkConstraints(data.createForm.constraints, data.model.foreignKeys).length > 0) {
modalComponent = {
ref: MissingConstraintsModal
};
modal = {
type: 'component',
component: modalComponent,
title: m.warning(),
body: safeTranslate('add-' + data.model.localName).toLowerCase(),
value: checkConstraints(data.createForm.constraints, data.model.foreignKeys)
};
}
modalStore.trigger(modal);
}
Expand Down
16 changes: 0 additions & 16 deletions frontend/src/routes/(app)/(internal)/ebios-rm/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,8 @@ export const load: PageServerLoad = async ({ params, fetch }) => {
const createSchema = modelSchema(URLModel);
const createForm = await superValidate(zod(createSchema));
const model: ModelInfo = getModelInfo(URLModel);
const foreignKeyFields = urlParamModelForeignKeyFields(URLModel);
const selectFields = urlParamModelSelectFields(URLModel);

const foreignKeys: Record<string, any> = {};

for (const keyField of 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}`);
}
}

model['foreignKeys'] = foreignKeys;

const selectOptions: Record<string, any> = {};

for (const selectField of selectFields) {
Expand Down
22 changes: 0 additions & 22 deletions frontend/src/routes/(app)/(internal)/ebios-rm/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import type { ModalComponent, ModalSettings, ModalStore } from '@skeletonlabs/skeleton';
import { getModalStore } from '@skeletonlabs/skeleton';
import CreateModal from '$lib/components/Modals/CreateModal.svelte';
import MissingConstraintsModal from '$lib/components/Modals/MissingConstraintsModal.svelte';
import { checkConstraints } from '$lib/utils/crud';
import * as m from '$paraglide/messages.js';
const modalStore: ModalStore = getModalStore();
Expand All @@ -29,25 +26,6 @@
// Data
title: safeTranslate('add-' + data.model.localName)
};
if (
checkConstraints(
data.createForm.constraints,
Object.fromEntries(
Object.entries(data.model.foreignKeys).filter(([key]) => key !== 'risk_matrix')
)
).length > 0
) {
modalComponent = {
ref: MissingConstraintsModal
};
modal = {
type: 'component',
component: modalComponent,
title: m.warning(),
body: safeTranslate('add-' + data.model.localName).toLowerCase(),
value: checkConstraints(data.createForm.constraints, data.model.foreignKeys)
};
}
modalStore.trigger(modal);
}
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,8 @@ export const load: PageServerLoad = async ({ params, fetch }) => {
errors: false
});
const riskModel = getModelInfo('risk-assessments');
const foreignKeyFields = urlParamModelForeignKeyFields(riskModel.urlModel);
const selectFields = urlParamModelSelectFields(riskModel.urlModel);

const foreignKeys: Record<string, any> = {};

for (const keyField of foreignKeyFields) {
const queryParams = keyField.urlParams ? `?${keyField.urlParams}` : '';
const keyModel = getModelInfo(keyField.urlModel);
const url = keyModel.endpointUrl
? `${BASE_API_URL}/${keyModel.endpointUrl}/${queryParams}`
: `${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}`);
}
}

riskModel['foreignKeys'] = foreignKeys;

const selectOptions: Record<string, any> = {};

for (const selectField of selectFields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import CreateModal from '$lib/components/Modals/CreateModal.svelte';
import MissingConstraintsModal from '$lib/components/Modals/MissingConstraintsModal.svelte';
import { checkConstraints } from '$lib/utils/crud';
import { getSecureRedirect } from '$lib/utils/helpers';
import { safeTranslate } from '$lib/utils/i18n';
import * as m from '$paraglide/messages';
Expand Down Expand Up @@ -148,21 +146,6 @@
// Data
title: safeTranslate('add-' + data.riskModel.localName)
};
if (
checkConstraints(data.createRiskAnalysisForm.constraints, data.riskModel.foreignKeys).length >
0
) {
modalComponent = {
ref: MissingConstraintsModal
};
modal = {
type: 'component',
component: modalComponent,
title: m.warning(),
body: safeTranslate('add-' + data.riskModel.localName).toLowerCase(),
value: checkConstraints(data.createRiskAnalysisForm.constraints, data.riskModel.foreignKeys)
};
}
modalStore.trigger(modal);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { defaultDeleteFormAction, defaultWriteFormAction } from '$lib/utils/actions';
import { BASE_API_URL } from '$lib/utils/constants';
import {
getModelInfo,
urlParamModelForeignKeyFields,
urlParamModelSelectFields
} from '$lib/utils/crud';
import { getModelInfo } from '$lib/utils/crud';
import { modelSchema } from '$lib/utils/schemas';
import type { ModelInfo, urlModel } from '$lib/utils/types';
import { type Actions } from '@sveltejs/kit';
Expand All @@ -31,46 +27,8 @@ export const load: PageServerLoad = async ({ params, fetch }) => {
const object = await objectResponse.json();
const updateForm = await superValidate(object, zod(updateSchema), { errors: false });
const model: ModelInfo = getModelInfo(URLModel);
const foreignKeyFields = urlParamModelForeignKeyFields(URLModel);
const updateForeignKeyFields = urlParamModelForeignKeyFields('ebios-rm');
const selectFields = model.selectFields;

const foreignKeys: Record<string, any> = {};

for (const keyField of foreignKeyFields) {
const model = getModelInfo(keyField.urlModel);
const queryParams = keyField.urlParams ? `?${keyField.urlParams}` : '';
const url = model.endpointUrl
? `${BASE_API_URL}/${model.endpointUrl}/${queryParams}`
: `${BASE_API_URL}/${model.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}`);
}
}

model['foreignKeys'] = foreignKeys;

const updateForeignKeys: Record<string, any> = {};

for (const keyField of updateForeignKeyFields) {
const model = getModelInfo(keyField.urlModel);
const queryParams = keyField.urlParams ? `?${keyField.urlParams}` : '';
const url = model.endpointUrl
? `${BASE_API_URL}/${model.endpointUrl}/${queryParams}`
: `${BASE_API_URL}/${model.urlModel}/${queryParams}`;
const response = await fetch(url);
if (response.ok) {
updateForeignKeys[keyField.field] = await response.json().then((data) => data.results);
} else {
console.error(`Failed to fetch data for ${keyField.field}: ${response.statusText}`);
}
}

updatedModel['foreignKeys'] = updateForeignKeys;

const selectOptions: Record<string, any> = {};

if (selectFields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import type { ModalComponent, ModalSettings, ModalStore } from '@skeletonlabs/skeleton';
import { getModalStore } from '@skeletonlabs/skeleton';
import CreateModal from '$lib/components/Modals/CreateModal.svelte';
import MissingConstraintsModal from '$lib/components/Modals/MissingConstraintsModal.svelte';
import { checkConstraints } from '$lib/utils/crud';
import * as m from '$paraglide/messages.js';
import UpdateModal from '$lib/components/Modals/UpdateModal.svelte';
Expand All @@ -30,25 +28,6 @@
// Data
title: safeTranslate('add-' + data.model.localName)
};
if (
checkConstraints(
data.createForm.constraints,
Object.fromEntries(
Object.entries(data.model.foreignKeys).filter(([key]) => key !== 'risk_matrix')
)
).length > 0
) {
modalComponent = {
ref: MissingConstraintsModal
};
modal = {
type: 'component',
component: modalComponent,
title: m.warning(),
body: safeTranslate('add-' + data.model.localName).toLowerCase(),
value: checkConstraints(data.createForm.constraints, data.model.foreignKeys)
};
}
modalStore.trigger(modal);
}
Expand All @@ -59,7 +38,6 @@
form: data.updateForm,
model: data.updatedModel,
object: data.object,
foreignKeys: data.updatedModel.foreignKeys,
context: 'selectAudit'
}
};
Expand All @@ -69,25 +47,6 @@
// Data
title: m.selectAudit()
};
if (
checkConstraints(
data.updateForm.constraints,
Object.fromEntries(
Object.entries(data.updatedModel.foreignKeys).filter(([key]) => key !== 'risk_matrix')
)
).length > 0
) {
modalComponent = {
ref: MissingConstraintsModal
};
modal = {
type: 'component',
component: modalComponent,
title: m.warning(),
body: safeTranslate('add-' + data.updatedModel.localName).toLowerCase(),
value: checkConstraints(data.updateForm.constraints, data.updatedModel.foreignKeys)
};
}
modalStore.trigger(modal);
}
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import { safeTranslate } from '$lib/utils/i18n';
import { page } from '$app/stores';
import CreateModal from '$lib/components/Modals/CreateModal.svelte';
import MissingConstraintsModal from '$lib/components/Modals/MissingConstraintsModal.svelte';
import ModelTable from '$lib/components/ModelTable/ModelTable.svelte';
import type { ModalComponent, ModalSettings, ModalStore } from '@skeletonlabs/skeleton';
import { TabGroup, Tab, getModalStore } from '@skeletonlabs/skeleton';
import { checkConstraints } from '$lib/utils/crud';
import Anchor from '$lib/components/Anchor/Anchor.svelte';
const modalStore: ModalStore = getModalStore();
Expand Down Expand Up @@ -40,18 +38,6 @@
// Data
title: safeTranslate('add-' + model.info.localName)
};
if (checkConstraints(model.createForm.constraints, model.foreignKeys).length > 0) {
modalComponent = {
ref: MissingConstraintsModal
};
modal = {
type: 'component',
component: modalComponent,
title: m.warning(),
body: safeTranslate('add-' + model.info.localName).toLowerCase(),
value: checkConstraints(model.createForm.constraints, model.foreignKeys)
};
}
modalStore.trigger(modal);
}
Expand Down
Loading

0 comments on commit 759437e

Please sign in to comment.