forked from mudphone/LiveBus
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmodel.js
74 lines (63 loc) · 2.04 KB
/
model.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
Vehicles = new Meteor.Collection("vehicles");
Vehicles.allow({
insert: function (userId, vehicle) {
return false; // no cowboy inserts -- use createVehicle method
},
update: function (userId, vehicle, fields, modifier) {
var allowed = ["vehicleId", "latitude", "longitude", "lastUpdate"];
if (_.difference(fields, allowed).length)
return false; // tried to write to forbidden field
// A good improvement would be to validate the type of the new
// value of the field (and if a string, the length.) In the
// future Meteor will have a schema system to makes that easier.
return true;
},
remove: function (userId, vehicle) {
return true;
}
});
var NonEmptyString = Match.Where(function (x) {
check(x, String);
return x.length !== 0;
});
var Coordinate = Match.Where(function (x) {
check(x, Number);
return true;
});
var UnixTimestamp = Match.Where(function (x) {
check(x, Number);
return x > 1302223652000;
});
updateVehicle = function (options) {
check(options, {
vehicleId: NonEmptyString,
lastUpdate: UnixTimestamp,
latitude: Coordinate,
longitude: Coordinate
});
if (options.vehicleId.length > 10)
throw new Meteor.Error(413, "Vehicle ID too long");
var matchingVehicle = Vehicles.findOne({vehicleId:options.vehicleId});
if (_U.existy(matchingVehicle)) {
if (parseFloat(matchingVehicle.latitude) === parseFloat(options.latitude) &&
parseFloat(matchingVehicle.longitude) === parseFloat(options.longitude)) return;
return Vehicles.update(
{vehicleId: options.vehicleId},
{$set: {lastUpdate: options.lastUpdate,
latitude: options.latitude,
longitude: options.longitude}});
} else {
return Vehicles.insert({
vehicleId: options.vehicleId,
lastUpdate: options.lastUpdate,
latitude: options.latitude,
longitude: options.longitude
});
}
};
Meteor.methods({
// options should include: title, description, x, y, public
createUpdateVehicle: function (options) {
updateVehicle(options);
},
});