Repository navigation
Expand file tree
/
Copy pathdatabase.js
More file actions
170 lines (140 loc) · 4.05 KB
/
Copy pathdatabase.js
File metadata and controls
170 lines (140 loc) · 4.05 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const uuidv4 = require('uuid/v4');
const timestamp = require('unix-timestamp');
let asks = [];
let gives = [];
function copyJsonObject(object) {
return JSON.parse(JSON.stringify(object));
}
function isEmpty(object) {
for (let key in object) {
if (object.hasOwnProperty(key)) {
return false;
}
}
return true;
}
const requestState = [
'unmatched',
'matched',
'fulfilled'
];
function findAsk(id) {
for (let i = 0; i < asks.length; i++) {
if (id === asks[i]['id']) {
return asks[i];
}
}
return {};
}
function findGive(id) {
for (let i = 0; i < gives.length; i++) {
if (id === gives[i]['id']) {
return gives[i];
}
}
return {};
}
function convertAskGiveBody(requestBody) {
let obj = { // Cloned fields from requestBody
'user_name': requestBody['user_name'],
'user_email': requestBody['user_email'],
'user_location': requestBody['user_location'],
'item': requestBody['item'],
'quantity': requestBody['quantity']
}
// Additional fields
let id = uuidv4();
obj['id'] = id;
obj['posted'] = timestamp.now();
obj['status_value'] = 0;
return obj;
};
function getStatus(obj) {
return requestState[obj['status_value']];
}
function convertStatusValueToStatus(obj) {
obj['status'] = getStatus(obj);
delete obj['status_value'];
}
function connectAskGive(req, ask, give) {
give['status_value']++;
give['match_id'] = ask['id'];
give['match_name'] = ask['user_name']
give['match_email'] = ask['user_email']
give['match_location'] = ask['user_location']
ask['status_value']++;
ask['match_id'] = give['id']
ask['match_name'] = give['user_name']
ask['match_email'] = give['user_email']
ask['match_location'] = give['user_location']
console.log(
`[DEBUG][REQ ${req.num}] Connected corresponding unmatched Give with ` +
`id {${give['id']}} to this Ask`);
}
module.exports = {
addAsk: function addAsk(req, requestBody) {
let obj = convertAskGiveBody(requestBody);
// Optional matching
for (let i = 0; i < gives.length; i++) {
// First unmatched Give which has the same item
if (gives[i]['status_value'] == 0 && gives[i]['item'] === obj['item']) {
connectAskGive(req, obj, gives[i]);
break;
}
}
console.log(
`[DEBUG][REQ ${req.num}] Creating Ask ID {${obj['id']} ` +
`} containing ${JSON.stringify(obj)}`);
asks.push(obj);
return obj['id'];
},
// This function also fulfills the Give linked to this Ask, if any exists.
// If the status is already fulfilled, it will not be modified, but
// the linked Give (if any) will still be fulfilled.
//
// Returns:
// On success: New status (String)
// If the ID was not found: Empty string
fulfillAskStatus: function fulfillAskStatus(req, requestBody) {
let id = requestBody['id'];
let ask = findAsk(id);
if (isEmpty(ask)) {
return "";
}
if ('match_id' in ask) {
console.log(`[DEBUG][REQ ${req.num}] Fulfilling linked Give ` +
`with id {${ask['match_id']}}`)
let give = findGive(ask['match_id']);
give['status_value'] = requestState.length - 1; // Last value
} else {
console.log(`[DEBUG][REQ ${req.num}] No linked Give`)
}
ask['status_value'] = requestState.length - 1; // Last value
return getStatus(ask);
},
addGive: function addGive(req, requestBody) {
let obj = convertAskGiveBody(requestBody);
console.log(`[DEBUG][REQ ${req.num}] Creating Give ID {${obj['id']}} ` +
`containing ${JSON.stringify(obj)}`);
gives.push(obj);
return obj['id'];
},
getAllAsks: function getAllAsks(req) {
let asksReturn = copyJsonObject(asks);
for (let i = 0; i < asksReturn.length; i++) {
convertStatusValueToStatus(asksReturn[i])
}
return asksReturn;
},
getAllGives: function getAllGives(req) {
let givesReturn = copyJsonObject(gives);
for (let i = 0; i < givesReturn.length; i++) {
convertStatusValueToStatus(givesReturn[i])
}
return givesReturn;
},
clearAll: function clearAll(req) {
asks = [];
gives = [];
}
};