-
Notifications
You must be signed in to change notification settings - Fork 39
/
parsing.js
126 lines (107 loc) · 3.84 KB
/
parsing.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
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
const { default: mongoose } = require("mongoose");
function parseBeaconObject(beaconObject) {
if (typeof beaconObject === "string") {
return convertToObjectId(beaconObject);
}
var model = {
_id: convertToObjectId(beaconObject._id),
title: beaconObject.title,
shortcode: beaconObject.shortcode,
startsAt: convertToDate(beaconObject.startsAt),
expiresAt: convertToDate(beaconObject.expiresAt),
group: parseGroupObject(beaconObject.group),
leader: parseUserObject(beaconObject.leader),
location: beaconObject.location,
followers: Array.isArray(beaconObject.followers)
? beaconObject.followers.map(follower => parseUserObject(follower))
: [],
landmarks: Array.isArray(beaconObject.landmarks)
? beaconObject.landmarks.map(landmark => parseLandmarkObject(landmark))
: [],
route: beaconObject.route.map(single => parseLocationObject(single)),
geofence: beaconObject.geofence,
updatedAt: convertToDate(beaconObject.updatedAt),
__v: beaconObject.__v,
};
return model;
}
function parseUserObject(userObject) {
if (typeof userObject === "string") {
return convertToObjectId(userObject);
}
try {
var model = {
_id: convertToObjectId(userObject._id),
name: userObject.name,
email: userObject.email,
password: userObject.password,
groups: Array.isArray(userObject.groups) ? userObject.groups.map(group => parseGroupObject(group)) : [],
beacons: Array.isArray(userObject.beacons)
? userObject.beacons.map(beacon => parseBeaconObject(beacon))
: [],
location: userObject.location,
createdAt: convertToDate(userObject.createdAt),
updatedAt: convertToDate(userObject.updatedAt),
__v: userObject.__v,
};
} catch (error) {
console.log(error);
}
return model;
}
function parseGroupObject(groupObject) {
if (typeof groupObject === "string") {
return convertToObjectId(groupObject);
}
var model = {
_id: convertToObjectId(groupObject._id),
title: groupObject.title,
shortcode: groupObject.shortcode,
createdAt: new Date(groupObject.createdAt),
updatedAt: new Date(groupObject.updatedAtt),
leader: parseUserObject(groupObject.leader),
members: Array.isArray(groupObject.members) ? groupObject.members.map(member => parseUserObject(member)) : [],
beacons: Array.isArray(groupObject.beacons) ? groupObject.beacons.map(beacon => parseBeaconObject(beacon)) : [],
__v: groupObject.__v,
};
return model;
}
function parseLandmarkObject(landmarkObject) {
if (typeof landmarkObject === "string") {
return convertToObjectId(landmarkObject);
}
var model = {
_id: convertToObjectId(landmarkObject._id),
title: landmarkObject.title,
location: parseLocationObject(landmarkObject.location),
createdBy: parseUserObject(landmarkObject.createdBy),
createdAt: convertToDate(landmarkObject.createdAt),
updatedAt: convertToDate(landmarkObject.updatedAt),
__v: landmarkObject.__v,
};
return model;
}
function parseLocationObject(locationObject) {
console.log(JSON.stringify(locationObject));
if (locationObject == null) return undefined;
var model = {
lat: locationObject.lat,
lon: locationObject.lon,
};
return model;
}
function convertToObjectId(id) {
return new mongoose.Types.ObjectId(id);
}
function convertToDate(date) {
return new Date(date);
}
module.exports = {
parseUserObject,
parseBeaconObject,
parseLandmarkObject,
parseLocationObject,
parseGroupObject,
convertToObjectId,
convertToDate,
};