Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Trim unwanted questionnaire fields on export #1584

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions client/src/app/api/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
identity?: Ref;
createTime?: string;
createUser?: string;
id: any;

Check warning on line 215 in client/src/app/api/models.ts

View workflow job for this annotation

GitHub Actions / unit-test (18.x)

Unexpected any. Specify a different type
enabled: boolean;
}

Expand Down Expand Up @@ -369,7 +369,7 @@

export interface TaskgroupTask {
name: string;
data: any;

Check warning on line 372 in client/src/app/api/models.ts

View workflow job for this annotation

GitHub Actions / unit-test (18.x)

Unexpected any. Specify a different type
application: Ref;
}

Expand Down Expand Up @@ -646,19 +646,26 @@
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 @@
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 @@

try {
const response = await axios.get(url, {
responseType: "blob",
responseType: "text",
headers: {
Accept: "application/x-yaml",
},
Expand All @@ -131,13 +132,24 @@
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;

Check warning on line 135 in client/src/app/queries/questionnaires.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/questionnaires.ts#L135

Added line #L135 was not covered by tests

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

Check warning on line 140 in client/src/app/queries/questionnaires.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/questionnaires.ts#L137-L140

Added lines #L137 - L140 were not covered by tests

const newYamlData = yaml.dump(yamlData);

Check warning on line 142 in client/src/app/queries/questionnaires.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/questionnaires.ts#L142

Added line #L142 was not covered by tests

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

Check warning on line 144 in client/src/app/queries/questionnaires.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/questionnaires.ts#L144

Added line #L144 was not covered by tests

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 });
};
Loading