Skip to content

Commit

Permalink
🐛 Trim unwanted questionnaire fields on export (#1584)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibolton336 authored Dec 4, 2023
1 parent 293a1d0 commit 4a90493
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
11 changes: 9 additions & 2 deletions client/src/app/api/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,19 +645,26 @@ export type HubFile = {
path: string;
};

export interface LooseQuestionnaire {
[key: string]: any;
}

export interface Questionnaire {
id: number;
name: string;
description: string;
revision: number;
questions: number;
rating: string;
createTime: string;
required: boolean;
builtin?: boolean;
sections: Section[];
thresholds: Thresholds;
riskMessages: RiskMessages;
builtin?: boolean;
createTime?: string;
createUser?: string;
updateTime?: string;
updateUser?: string;
}

export interface RiskMessages {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ export const ExportQuestionnaireDropdownItem: React.FC<
IExportQuestionnaireDropdownItemProps
> = ({ id }) => {
const { t } = useTranslation();
const {
mutate: downloadFile,
isLoading,
isError,
} = useDownloadQuestionnaire();
const { mutate: downloadFile } = useDownloadQuestionnaire();

const handleDownload = () => {
downloadFile(id);
Expand Down
18 changes: 15 additions & 3 deletions client/src/app/queries/questionnaires.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import axios, { AxiosError } from "axios";
import yaml from "js-yaml";

import {
QUESTIONNAIRES,
Expand All @@ -9,7 +10,7 @@ import {
getQuestionnaires,
updateQuestionnaire,
} from "@app/api/rest";
import { Questionnaire } from "@app/api/models";
import { LooseQuestionnaire, Questionnaire } from "@app/api/models";
import saveAs from "file-saver";

export const QuestionnairesQueryKey = "questionnaires";
Expand Down Expand Up @@ -121,7 +122,7 @@ export const downloadQuestionnaire = async (

try {
const response = await axios.get(url, {
responseType: "blob",
responseType: "text",
headers: {
Accept: "application/x-yaml",
},
Expand All @@ -131,13 +132,24 @@ export const downloadQuestionnaire = async (
throw new Error("Network response was not ok when downloading file.");
}

const blob = new Blob([response.data]);
const yamlData = yaml.load(response.data) as LooseQuestionnaire;

delete yamlData.createUser;
delete yamlData.updateUser;
delete yamlData.createTime;
delete yamlData.id;

const newYamlData = yaml.dump(yamlData);

const blob = new Blob([newYamlData], { type: "application/x-yaml" });

saveAs(blob, `questionnaire-${id}.yaml`);
} catch (error) {
console.error("There was an error downloading the file:", error);
throw error;
}
};

export const useDownloadQuestionnaire = () => {
return useMutation({ mutationFn: downloadQuestionnaire });
};

0 comments on commit 4a90493

Please sign in to comment.