-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpromotionsDAO.js
77 lines (70 loc) · 2.2 KB
/
promotionsDAO.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
export default class PromotionsDAO {
constructor() {
this.promotions = null;
}
static async injectDB(conn) {
if (this.promotions) {
return;
}
try {
this.promotions = await conn
.db(process.env.DB_NAME)
.collection("promotions");
} catch (err) {
console.log(`Unable to connect to MongoDB: ${err.message}`);
}
}
static async createPromotion(code, discountRate, expiryDate) {
try {
const promotionDoc = {
code: code,
discountRate: discountRate,
expiryDate: expiryDate,
};
return await this.promotions.insertOne(promotionDoc);
} catch (err) {
console.log(`Unable to create promotion: ${err.message}`);
return { error: err };
}
}
static async getPromotion(code) {
try {
return await this.promotions.findOne({ code: code });
} catch (err) {
console.log(`Unable to get promotion: ${err.message}`);
return { error: err };
}
}
static async getAllPromotions() {
try {
return await this.promotions.find().toArray();
} catch (err) {
console.log(`Unable to get all promotions: ${err.message}`);
return { error: err };
}
}
static async updatePromotion(code, discountRate, expiryDate) {
try {
const filter = { code: code };
let updateDoc = {
$set: {
discountRate: discountRate,
expiryDate: expiryDate,
},
};
return await this.promotions.updateOne(filter, updateDoc);
} catch (err) {
console.log(`Unable to update promotion: ${err.message}`);
return { error: err };
}
}
static async deletePromotion(code) {
try {
const query = { code: code };
return await this.promotions.deleteOne(query);
} catch (err) {
console.log(`Unable to delete promotion: ${err.message}`);
return { error: err };
}
}
}