forked from mrvautin/expressCart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesthelper.js
97 lines (85 loc) · 2.61 KB
/
testhelper.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const moment = require('moment');
const fixProductDates = (products) => {
let index = 0;
products.forEach(() => {
products[index].productAddedDate = new Date();
index++;
});
return products;
};
const fixReveiewDates = (reviews) => {
let index = 0;
reviews.forEach(() => {
reviews[index].date = new Date();
index++;
});
return reviews;
};
const fixDiscountDates = (discounts) => {
let index = 0;
discounts.forEach(() => {
let startDate = moment().subtract(1, 'days').toDate();
let endDate = moment().add(7, 'days').toDate();
const expiredStart = moment().subtract(14, 'days').toDate();
const expiredEnd = moment().subtract(7, 'days').toDate();
const futureStart = moment().add(7, 'days').toDate();
const futureEnd = moment().add(14, 'days').toDate();
// If code is expired, make sure the dates are correct
if(discounts[index].code.substring(0, 7) === 'expired'){
startDate = expiredStart;
endDate = expiredEnd;
}
// If code is future, make sure the dates are correct
if(discounts[index].code.substring(0, 6) === 'future'){
startDate = futureStart;
endDate = futureEnd;
}
// Set the expiry dates
discounts[index].start = startDate;
discounts[index].end = endDate;
index++;
});
return discounts;
};
const fixProductIds = async (jsonVariants, products) => {
const variants = [];
let productIndex = 0;
// Loop products
products.forEach((product) => {
// Only add variants to certain products
if(productIndex % 2 === 0){
// Add all available variants
jsonVariants.forEach((jsonVariant) => {
const variant = {};
Object.assign(variant, jsonVariant);
// Add the product to the variant
variant.product = product._id;
variants.push(variant);
});
}
productIndex++;
});
return variants;
};
const fixReviews = async (reviews, products, customers) => {
const fixedReviews = [];
// Loop products
reviews.forEach((review) => {
review.product = products[0]._id;
review.customer = customers[0]._id;
review.date = new Date();
fixedReviews.push(review);
});
return fixedReviews;
};
const getRandom = (max) => {
return Math.floor(Math.random() * Math.floor(max));
};
module.exports = {
fixProductDates,
fixReveiewDates,
fixDiscountDates,
fixProductIds,
fixReviews,
getRandom
};