Skip to content

Commit 3e1f574

Browse files
author
dushimsam
committed
fix(MULTIPLE-API-CALLS) merge multiple upload-api calls into one
1 parent 133c1af commit 3e1f574

File tree

4 files changed

+112
-41
lines changed

4 files changed

+112
-41
lines changed

src/api/upload.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,57 @@ export const createUploadApi = (
5555
};
5656

5757
// Create Uploads from Version Control System
58-
export const createUploadVcsApi = (header, body) => {
58+
export const createUploadVcsApi = (header, vcsData, scanData) => {
5959
const url = endpoints.upload.uploadCreate();
60+
const { bucket, copyrightEmailAuthor, ecc, keyword, mime, monk, nomos, ojo } =
61+
scanData?.analysis;
62+
const { nomosMonk, bulkReused, newScanner, ojoDecider } = scanData?.decider;
63+
const {
64+
reuseUpload,
65+
reuseGroup,
66+
reuseMain,
67+
reuseEnhanced,
68+
reuseReport,
69+
reuseCopyright,
70+
} = scanData?.reuse;
71+
6072
return sendRequest({
6173
url,
6274
method: "POST",
6375
headers: {
6476
...header,
6577
Authorization: getToken(),
6678
},
67-
body,
79+
body: {
80+
vcsData,
81+
scanOptions: {
82+
analysis: {
83+
bucket,
84+
copyright_email_author: copyrightEmailAuthor,
85+
ecc,
86+
keyword,
87+
mime,
88+
monk,
89+
nomos,
90+
ojo,
91+
package: scanData.analysis.package,
92+
},
93+
decider: {
94+
nomos_monk: nomosMonk,
95+
bulk_reused: bulkReused,
96+
new_scanner: newScanner,
97+
ojo_decider: ojoDecider,
98+
},
99+
reuse: {
100+
reuse_upload: reuseUpload,
101+
reuse_group: reuseGroup,
102+
reuse_main: reuseMain,
103+
reuse_enhanced: reuseEnhanced,
104+
reuse_report: reuseReport,
105+
reuse_copyright: reuseCopyright,
106+
},
107+
},
108+
},
68109
});
69110
};
70111

src/api/upload.test.js

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,38 @@ describe("upload", () => {
104104

105105
test("createUploadVcsApi", () => {
106106
const header = "header";
107-
const body = "body";
107+
const vcsData = "vcsData";
108+
const scanData = {
109+
analysis: {
110+
bucket: "bucket",
111+
copyrightEmailAuthor: "copyrightEmailAuthor",
112+
ecc: "ecc",
113+
keyword: "keyword",
114+
mime: "mime",
115+
monk: "monk",
116+
nomos: "nomos",
117+
ojo: "ojo",
118+
package: "package",
119+
},
120+
decider: {
121+
nomosMonk: "nomosMonk",
122+
bulkReused: "bulkReused",
123+
newScanner: "newScanner",
124+
ojoDecider: "ojoDecider",
125+
},
126+
reuse: {
127+
reuseUpload: "reuseUpload",
128+
reuseGroup: "reuseGroup",
129+
reuseMain: "reuseMain",
130+
reuseEnhanced: "reuseEnhanced",
131+
reuseReport: "reuseReport",
132+
reuseCopyright: "reuseCopyright",
133+
},
134+
};
108135
const url = endpoints.upload.uploadCreate();
109136
sendRequest.mockImplementation(() => true);
110137

111-
expect(createUploadVcsApi(header, body)).toBe(sendRequest({}));
138+
expect(createUploadVcsApi(header, vcsData, scanData)).toBe(sendRequest({}));
112139
expect(sendRequest).toHaveBeenCalledWith(
113140
expect.objectContaining({
114141
url,
@@ -117,7 +144,36 @@ describe("upload", () => {
117144
...header,
118145
Authorization: getToken(),
119146
},
120-
body,
147+
body: {
148+
vcsData,
149+
scanOptions: {
150+
analysis: {
151+
bucket: scanData.analysis.bucket,
152+
copyright_email_author: scanData.analysis.copyrightEmailAuthor,
153+
ecc: scanData.analysis.ecc,
154+
keyword: scanData.analysis.keyword,
155+
mime: scanData.analysis.mime,
156+
monk: scanData.analysis.monk,
157+
nomos: scanData.analysis.nomos,
158+
ojo: scanData.analysis.ojo,
159+
package: scanData.analysis.package,
160+
},
161+
decider: {
162+
nomos_monk: scanData.decider.nomosMonk,
163+
bulk_reused: scanData.decider.bulkReused,
164+
new_scanner: scanData.decider.newScanner,
165+
ojo_decider: scanData.decider.ojoDecider,
166+
},
167+
reuse: {
168+
reuse_upload: scanData.reuse.reuseUpload,
169+
reuse_group: scanData.reuse.reuseGroup,
170+
reuse_main: scanData.reuse.reuseMain,
171+
reuse_enhanced: scanData.reuse.reuseEnhanced,
172+
reuse_report: scanData.reuse.reuseReport,
173+
reuse_copyright: scanData.reuse.reuseCopyright,
174+
},
175+
},
176+
},
121177
})
122178
);
123179
});

src/pages/Upload/Vcs/index.jsx

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818

1919
import React, { useState, useEffect } from "react";
20-
import messages from "constants/messages";
2120

2221
// Title
2322
import Title from "components/Title";
@@ -30,8 +29,7 @@ import CommonFields from "components/Upload/CommonFields";
3029

3130
// Required functions for calling APIs
3231
import { getAllFolders } from "services/folders";
33-
import { createUploadVcs, getUploadById } from "services/upload";
34-
import { scheduleAnalysis } from "services/jobs";
32+
import { createUploadVcs } from "services/upload";
3533

3634
// Default Agents list
3735
import {
@@ -44,11 +42,9 @@ import {
4442

4543
// Helper function for error handling
4644
import { handleError } from "shared/helper";
45+
import messages from "../../../constants/messages";
4746

4847
const UploadFromVcs = () => {
49-
// Upload Id required for scheduling Analysis
50-
let uploadId;
51-
5248
// Data required for creating the upload
5349
const [uploadVcsData, setUploadVcsData] = useState(initialStateVcs);
5450

@@ -69,36 +65,14 @@ const UploadFromVcs = () => {
6965
const handleSubmit = (e) => {
7066
e.preventDefault();
7167
setLoading(true);
72-
createUploadVcs(uploadVcsData, vcsData)
73-
.then((res) => {
68+
createUploadVcs(uploadVcsData, vcsData, scanFileData)
69+
.then(() => {
7470
window.scrollTo({ top: 0 });
7571
setMessage({
7672
type: "success",
77-
text: `${messages.queuedUpload} #${res.message}`,
73+
text: `${messages.uploadSuccess}`,
7874
});
79-
uploadId = res.message;
8075
})
81-
// Calling the api for maximum 10 times to check whether the upload is unpacked by the agent
82-
.then(() => getUploadById(uploadId, 10))
83-
.then(() =>
84-
setTimeout(
85-
() =>
86-
scheduleAnalysis(uploadVcsData.folderId, uploadId, scanFileData)
87-
.then(() => {
88-
window.scrollTo({ top: 0 });
89-
setMessage({
90-
type: "success",
91-
text: messages.scheduledAnalysis,
92-
});
93-
setUploadVcsData(initialStateVcs);
94-
setScanFileData(initialScanFileData);
95-
})
96-
.catch((error) => {
97-
handleError(error, setMessage);
98-
}),
99-
200000
100-
)
101-
)
10276
.catch((error) => {
10377
handleError(error, setMessage);
10478
})

src/services/upload.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export const createUploadFile = ({
4242
};
4343

4444
// Create Uploads from Version Control System
45-
export const createUploadVcs = (header, body) => {
46-
return createUploadVcsApi(header, body).then((res) => {
45+
export const createUploadVcs = (header, vcsData, scanData) => {
46+
return createUploadVcsApi(header, vcsData, scanData).then((res) => {
4747
return res;
4848
});
4949
};
@@ -55,21 +55,21 @@ export const createUploadUrl = (header, body) => {
5555
});
5656
};
5757

58-
// Getting a Upload by id
58+
// Getting an Upload by id
5959
export const getUploadById = (uploadId, retries) => {
6060
return getUploadByIdApi(uploadId, retries).then((res) => {
6161
return res;
6262
});
6363
};
6464

65-
// Getting a Upload Summary
65+
// Getting an Upload Summary
6666
export const getUploadSummary = (uploadId) => {
6767
return getUploadSummaryApi(uploadId).then((res) => {
6868
return res;
6969
});
7070
};
7171

72-
// Getting a Upload License
72+
// Getting an Upload License
7373
export const getUploadLicense = (uploadId, agent) => {
7474
return getUploadLicenseApi(uploadId, agent).then((res) => {
7575
return res;

0 commit comments

Comments
 (0)