This repository has been archived by the owner on Sep 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction_controll.js
95 lines (87 loc) · 2.84 KB
/
transaction_controll.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
var Mongo = require('./db');
getTransaction = function (id, col) {
Mongo.execute(function (err, db) {
if (!err) {
var collection = db.collection('transaction');
collection.findOne({'id': id}, function (err, doc) {
if (!err) {
if (doc) {
doc.transaction = doc._id;
delete doc._id;
}
col(doc, null);
} else {
col(null, err);
}
});
} else {
//Ошибка чтения из бд
col(null, err);
}
});
};
getTransactions = function (from, to, col) {
Mongo.execute(function (err, db) {
if (!err) {
var collection = db.collection('transaction');
collection.find({$and: [{time: {$lte: to}}, {time: {$gte: from}}]}, function (err, docs) {
if (!err) {
docs.toArray(function (err, docs, ) {
col(docs, null);
});
} else {
col(null, err);
}
});
} else {
//Ошибка чтения из бд
col(null, err);
}
});
};
saveNewTransaction = function (trans, col) {
Mongo.execute(function (err, db) {
if (!err) {
var collection = db.collection('transaction');
collection.insertOne(trans, function (err, res) {
if (!err) {
//Успешная запись
trans.transaction = res.insertedId;
col(trans, null);
} else {
//Ошибка записи
col(null, err);
}
});
} else {
//Ошибка чтения из бд
col(null, err);
}
});
};
saveTransaction = function (trans, col) {
Mongo.execute(function (err, db) {
if (!err) {
var collection = db.collection('transaction');
trans_no = trans.transaction;
delete trans.transaction;
collection.update({id: trans.id}, trans, function (err, res) {
if (!err) {
//Успешная запись
trans.transaction = trans_no;
col(res.result.nModified===1?trans:null, null);
} else {
//Ошибка записи
col(null, err);
}
});
} else {
//Ошибка чтения из бд
col(null, err);
}
});
};
exports.getTransaction = getTransaction;
exports.saveTransaction = saveTransaction;
exports.saveNewTransaction = saveNewTransaction;
exports.getTransactions = getTransactions;