-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.js
89 lines (75 loc) · 2.22 KB
/
models.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
var FieldSet = require('./db').FieldSet,
makeNiceId = require('./db').makeNiceId,
db = require('./db'),
settings = require('./settings'),
crypto = require('crypto');
var make = function(collection, options, c) {
// Magic args
if (typeof options == 'function') {
c = options;
options = {};
}
options = options || {};
// Generate the object
var F = c || function() {};
F.prototype = new FieldSet(collection);
F.prototype.constructor = F;
// Ensure geoindex
if (options.geo)
db.ensureIndex(collection, options.geo, '2d');
return F;
};
var Auth = make('authentication');
var Listing = make('item', {geo: 'location'});
Listing.prototype.genId = function(callback) {
var self = this;
makeNiceId(this.getCollection(), function(id) {
self._id = id;
if (callback) callback();
});
return this;
};
var User = make('user');
User.prototype.genId = function(callback) {
var self = this;
makeNiceId(this.getCollection(), function(id) {
self._id = id;
if (callback) callback();
});
};
var Offer = make('offer');
var Message = make('message');
var Inquiry = make('inquiry');
var Convo = make('convo');
var ClientError = make('clienterror');
var OutgoingEmail = make('outgoingemail');
var IncomingEmail = make('incomingemail');
var File = make('staticfile');
File.prototype.generateHash = function() {
this.hash = crypto.createHash('md5').update(this.data).digest('hex');
};
File.prototype.getUrl = function() {
return settings.serverUri + '/' + this._id;
};
var AwaitedSMS = make('awaitedsms');
exports.Auth = Auth;
exports.Listing = Listing;
exports.User = User;
exports.Offer = Offer;
exports.Message = Message;
exports.Inquiry = Inquiry;
exports.File = File;
exports.Convo = Convo;
exports.ClientError = ClientError;
exports.IncomingEmail = IncomingEmail;
exports.OutgoingEmail = OutgoingEmail;
exports.AwaitedSMS = AwaitedSMS;
// Expose the lowercase versions too
var list = [];
for (var i in exports) if (exports.hasOwnProperty(i))
if (exports[i].prototype && exports[i].prototype instanceof FieldSet)
list.push(i);
for (var i=0; i<list.length; i++)
exports[list[i][0].toLowerCase() + list[i].substr(1)] = exports[list[i]];
// Manually map item -> Listing
exports.item = Listing;