This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.ts
81 lines (64 loc) · 2.13 KB
/
mongo.ts
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
import { Collection, MongoClient } from 'mongodb';
export class MongoManager {
client: MongoClient;
constructor(url: string) {
this.client = new MongoClient(url);
}
connect() {
return this.client.connect();
}
collection() {
type Code = {
survey_code: string,
coupon_code: string | null,
inserted: Date,
solved: Date | null,
used: Date | null
}
const codes: Collection<Code> = this.client.db("McD").collection('codes');
return codes;
}
templates() {
type Template = {
code: string,
}
const templates: Collection<Template> = this.client.db("McD").collection('templates');
return templates;
}
async markUsed(code: string) {
return await this.collection().updateOne({ coupon_code: code }, { $set: { used: new Date() } })
}
async getUnusedCodes() {
return await this.collection().find({
used: null,
coupon_code: {$ne:null}
}).toArray()
}
async insertSurveyCode(code: string) {
return await this.collection().insertOne({
survey_code: code,
coupon_code: null,
inserted: new Date(),
solved: null,
used: null
});
}
async checkSurveyCode(code: string) {
const query = { survey_code: code }
const result = await this.collection().findOne(query)
if (result == null) return true; else return false
}
async insertCouponCode(survey_code: string, coupon_code: string) {
return await this.collection().updateOne({ survey_code: survey_code }, { $set: { coupon_code: coupon_code, solved: new Date() } })
}
async getTemplates() {
return await this.templates().find({}).toArray()
}
async updateTemplate(code: string, template: string) {
return await this.templates().updateOne({ code: template }, { $set: { code: code } })
}
async deleteTemplate(code: string) {
return await this.templates().deleteOne({ code: code })
}
}
export default MongoManager;