-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper_endpoint.js
70 lines (63 loc) · 1.9 KB
/
scraper_endpoint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import {getBookByISBNMultiple} from "./database.js";
let reqBody = new Array();
//check for duplicates
async function checkDuplicates(reqBody){
let isbnArray = reqBody.reduce((acc,current) => {
acc.push(current.isbn13);
return acc;
}, []);
let duplicates = await getBookByISBNMultiple(isbnArray);
if (duplicates.length > 0){
console.log('a book with the same isbn already exists');
return false;
}
else {
return true; //no duplicates found
}
}
function checkSixValues(reqBody){
let returnValue = false;
reqBody.forEach(x => {
if ((Object.keys(x).length) != 6){
console.log(x, "has an invalid number of entries");
returnValue = false;
} else {
returnValue = true;
}
});
return returnValue;
}
function areValidStrings(reqBody){
let returnValue = false;
reqBody.forEach((y,objIndex) => {
Object.values(y).forEach((z,valindex) => {
//console.log(z);
if(typeof z !== 'string') {
console.log(`Object ${objIndex}, ${y} is not a string`)
returnValue = false;
}
else {
returnValue = true;
}
});
});
return returnValue; //no duplicates found
}
export async function inputValidation(scraperRequest){
//reqBody request is an array of objects
reqBody = JSON.parse(scraperRequest);
//new architecture, list of flags (true or false)
const sixValues = checkSixValues(reqBody);
const valuesAreStrings = areValidStrings(reqBody);
const noDuplicates = await checkDuplicates(reqBody);
//undefined doesn't show up as 0 :/
const numberOfBooks = reqBody.length;
if (sixValues && valuesAreStrings && noDuplicates){
console.log("input array is valid");
return numberOfBooks; //
}
else {
console.log("invalid input array, check console");
return 0;
}
}