Skip to content

Commit

Permalink
Merge pull request #766 from intuitem/CA-420-cannot-edit-evidence-wit…
Browse files Browse the repository at this point in the history
…h-attachment

Ca 420 cannot edit evidence with attachment
  • Loading branch information
monsieurswag authored Aug 28, 2024
2 parents 59d7e94 + 7176600 commit 8b3ee4c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 31 deletions.
4 changes: 3 additions & 1 deletion frontend/src/lib/utils/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export interface ModelMapEntry {
foreignKeyFields?: ForeignKeyField[];
reverseForeignKeyFields?: ForeignKeyField[];
selectFields?: SelectField[];
fileFields?: string[];
filters?: SelectField[];
path?: string;
}
Expand Down Expand Up @@ -367,6 +368,7 @@ export const URL_MODEL_MAP: ModelMap = {
localNamePlural: 'evidences',
verboseName: 'Evidence',
verboseNamePlural: 'Evidences',
fileFields: ['attachment'],
foreignKeyFields: [
{ field: 'folder', urlModel: 'folders' },
{ field: 'applied_controls', urlModel: 'applied-controls' },
Expand Down Expand Up @@ -596,7 +598,7 @@ export const urlParamModelSelectFields = (model: string): SelectField[] => {
return URL_MODEL_MAP[model]?.selectFields || [];
};

export const getModelInfo = (model: urlModel): ModelMapEntry => {
export const getModelInfo = (model: urlModel | string): ModelMapEntry => {
const map = URL_MODEL_MAP[model] || {};
map['urlModel'] = model;
return map;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/utils/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export const ComplianceAssessmentSchema = baseNamedObject({
});

export const EvidenceSchema = baseNamedObject({
attachment: z.instanceof(File).optional().nullable(),
attachment: z.any().optional().nullable(),
folder: z.string(),
applied_controls: z.preprocess(toArrayPreprocessor, z.array(z.string().optional())).optional(),
requirement_assessments: z.string().optional().array().optional(),
Expand Down
12 changes: 6 additions & 6 deletions frontend/src/routes/(app)/[model=urlmodel]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ export const actions: Actions = {
return fail(400, { form: form });
}

const model: ModelInfo = getModelInfo(event.params.model!);
const endpoint = `${BASE_API_URL}/${event.params.model}/`;

const fileFields = Object.fromEntries(
Object.entries(form.data).filter(([, value]) => value instanceof File)
const model = getModelInfo(event.params.model!);

const fileFields: Record<string, File> = Object.fromEntries(
Object.entries(form.data).filter(([key]) => model.fileFields?.includes(key) ?? false)
);

Object.keys(fileFields).forEach((key) => {
Expand Down Expand Up @@ -121,9 +122,8 @@ export const actions: Actions = {

if (fileFields) {
for (const [, file] of Object.entries(fileFields)) {
if (file.size <= 0) {
continue;
}
if (!file) continue;
if (file.size <= 0) continue;
const fileUploadEndpoint = `${BASE_API_URL}/${event.params.model}/${createdObject.id}/upload/`;
const fileUploadRequestInitOptions: RequestInit = {
headers: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BASE_API_URL } from '$lib/utils/constants';
import { urlParamModelVerboseName } from '$lib/utils/crud';
import { getModelInfo, urlParamModelVerboseName } from '$lib/utils/crud';

import { localItems, toCamelCase } from '$lib/utils/locales';
import * as m from '$paraglide/messages';
Expand Down Expand Up @@ -32,8 +32,10 @@ export const actions: Actions = {

const endpoint = `${BASE_API_URL}/${urlModel}/`;

const fileFields = Object.fromEntries(
Object.entries(form.data).filter(([, value]) => value instanceof File)
const model = getModelInfo(event.params.model!);

const fileFields: Record<string, File> = Object.fromEntries(
Object.entries(form.data).filter(([key]) => model.fileFields?.includes(key) ?? false)
);

Object.keys(fileFields).forEach((key) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { safeTranslate } from '$lib/utils/i18n';
import { BASE_API_URL } from '$lib/utils/constants';
import { getModelInfo, urlParamModelVerboseName } from '$lib/utils/crud';
import { getSecureRedirect } from '$lib/utils/helpers';
import { safeTranslate } from '$lib/utils/i18n';
import { modelSchema } from '$lib/utils/schemas';
import { fail, type Actions } from '@sveltejs/kit';
import { setError, superValidate } from 'sveltekit-superforms';
import { fail, redirect, type Actions } from '@sveltejs/kit';
import { setFlash } from 'sveltekit-flash-message/server';
import { urlParamModelVerboseName } from '$lib/utils/crud';
import { getSecureRedirect } from '$lib/utils/helpers';
import { redirect } from '@sveltejs/kit';
import { setError, superValidate } from 'sveltekit-superforms';

import { localItems, toCamelCase } from '$lib/utils/locales';
import { toCamelCase } from '$lib/utils/locales';
import * as m from '$paraglide/messages';
import { languageTag } from '$paraglide/runtime';
import { zod } from 'sveltekit-superforms/adapters';

export const actions: Actions = {
Expand All @@ -31,12 +29,14 @@ export const actions: Actions = {

const endpoint = `${BASE_API_URL}/${event.params.model}/${event.params.id}/`;

const fileFields = Object.fromEntries(
Object.entries(form.data).filter(([, value]) => value instanceof File)
const model = getModelInfo(event.params.model!);

const fileFields: Record<string, File> = Object.fromEntries(
Object.entries(form.data).filter(([key]) => model.fileFields?.includes(key) ?? false)
);

Object.keys(fileFields).forEach((key) => {
form.data[key] = undefined;
delete form.data[key];
});

const requestInitOptions: RequestInit = {
Expand Down Expand Up @@ -67,9 +67,8 @@ export const actions: Actions = {

if (fileFields) {
for (const [, file] of Object.entries(fileFields)) {
if (file.size <= 0) {
continue;
}
if (!file) continue;
if (file.size <= 0) continue;
const fileUploadEndpoint = `${BASE_API_URL}/${event.params.model}/${createdObject.id}/upload/`;
const fileUploadRequestInitOptions: RequestInit = {
headers: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,10 @@ export const actions: Actions = {

const endpoint = `${BASE_API_URL}/evidences/`;

const fileFields = Object.fromEntries(
Object.entries(form.data).filter(([, value]) => value instanceof File)
const model = getModelInfo(event.params.model!);

const fileFields: Record<string, File> = Object.fromEntries(
Object.entries(form.data).filter(([key]) => model.fileFields?.includes(key) ?? false)
);

Object.keys(fileFields).forEach((key) => {
Expand Down Expand Up @@ -377,9 +379,8 @@ export const actions: Actions = {

if (fileFields) {
for (const [, file] of Object.entries(fileFields)) {
if (file.size <= 0) {
continue;
}
if (!file) continue;
if (file.size <= 0) continue;
const fileUploadEndpoint = `${BASE_API_URL}/${'evidences'}/${createdObject.id}/upload/`;
const fileUploadRequestInitOptions: RequestInit = {
headers: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { redirect, fail, type Actions } from '@sveltejs/kit';
import { getModelInfo } from '$lib/utils/crud';
import { setFlash } from 'sveltekit-flash-message/server';
import * as m from '$paraglide/messages';
import { languageTag } from '$paraglide/runtime';
import { localItems } from '$lib/utils/locales';
import { zod } from 'sveltekit-superforms/adapters';

Expand Down

0 comments on commit 8b3ee4c

Please sign in to comment.