-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusers.js
186 lines (165 loc) · 5.18 KB
/
users.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'use strict';
const path = require('path'),
_ = require('lodash');
const Serializer = require('jsonapi-serializer').Serializer;
const config = require(path.resolve('./config/config'));
const newUserSerializer = new Serializer('users', {
attributes: ['username', 'password', 'email']
});
exports.newUser = function (data) {
const output = newUserSerializer.serialize(data);
delete output.data.id;
return output;
};
const userSerializer = new Serializer('users', {
attributes: ['givenName', 'familyName', 'username', 'description', 'email', 'location', 'preciseLocation', 'locationUpdated'],
keyForAttribute: 'camelCase',
topLevelLinks: {
self: ({ id }) => `${config.url.all}/users/${id}`
},
id: 'username'
});
exports.user = function (data) {
return userSerializer.serialize(data);
};
// serialize new userTag
const newUserTagSerializer = new Serializer('user-tags', {
attributes: ['username', 'tagname', 'story', 'relevance']
});
exports.newUserTag = function (data) {
return newUserTagSerializer.serialize(data);
};
// serialize userTags
const userTagsSerializer = new Serializer('user-tags', {
attributes: ['username', 'tagname', 'story', 'relevance', 'user', 'tag'],
keyForAttribute: 'camelCase',
topLevelLinks: {
self(data) {
if(!Array.isArray(data)) {
const { user: { username }, tag: { tagname } } = data;
return `${config.url.all}/users/${username}/tags/${tagname}`;
}
}
},
tag: {
ref: 'tagname',
attributes: ['tagname'],
includedLinks: {
self: ({ tagname }) => `${config.url.all}/tags/${tagname}`
},
relationshipLinks: {}
},
user: {
ref: 'username',
attributes: ['givenName', 'familyName', 'username', 'description', 'location'],
includedLinks: {
self: ({ username }) => `${config.url.all}/users/${username}`
},
relationshipLinks: {}
}
});
exports.userTag = function (data) {
data.id = `${data.user.username}--${data.tag.tagname}`;
data.username = data.user.username;
data.tagname = data.tag.tagname;
return userTagsSerializer.serialize(data);
};
exports.userTags = function ({ username, userTags }) {
for (const userTag of userTags) {
const username = userTag.user.username;
const tagname = userTag.tag.tagname;
userTag.id = `${username}--${tagname}`;
userTag.username = username;
userTag.tagname = tagname;
}
const serialized = userTagsSerializer.serialize(userTags);
serialized.links = {
self: `${config.url.all}/users/${username}/tags`
};
return serialized;
};
const usersByTagsSerializer = new Serializer('users', {
attributes: ['givenName', 'familyName', 'username', 'description', 'tags'],
keyForAttribute: 'camelCase',
typeForAttribute(attribute) {
if (attribute === 'tags') {
return 'user-tags';
}
},
id: 'username',
// include user-tags which were found
tags: {
ref: 'id',
attributes: ['username', 'tagname', 'story', 'relevance', 'tag'],
includedLinks: {
self: (data, { username, tagname }) => `${config.url.all}/users/${username}/tags/${tagname}`
},
relationshipLinks: {
self: ({ username }) => `${config.url.all}/users/${username}/relationships/tags`,
related: ({ username }) => `${config.url.all}/users/${username}/tags`
},
tag: {
ref: 'tagname',
attributes: ['tagname'],
includedLinks: {
self: (data, { tagname }) => `${config.url.all}/tags/${tagname}`
}
}
}
});
exports.usersByTags = function (users) {
_.each(users, user => {
user.tags = _.map(user.userTags, userTag => {
userTag.id = `${user.username}--${userTag.tag.tagname}`;
userTag.username = user.username;
userTag.tagname = userTag.tag.tagname;
return userTag;
});
delete user.userTags;
});
const serialized = usersByTagsSerializer.serialize(users);
return serialized;
};
const usersByMyTagsSerializer = new Serializer('users', {
attributes: ['givenName', 'familyName', 'username', 'description', 'tags'],
keyForAttribute: 'camelCase',
typeForAttribute(attribute) {
if (attribute === 'tags') {
return 'user-tags';
}
},
id: 'username',
// include user-tags which were found
tags: {
ref: 'id',
attributes: ['username', 'tagname', 'story', 'relevance', 'tag'],
includedLinks: {
self: (data, { username, tagname }) => `${config.url.all}/users/${username}/tags/${tagname}`
},
relationshipLinks: {
self: ({ username }) => `${config.url.all}/users/${username}/relationships/tags`,
related: ({ username }) => `${config.url.all}/users/${username}/tags`
},
tag: {
ref: 'tagname',
attributes: ['tagname'],
includedLinks: {
self: (data, { tagname }) => `${config.url.all}/tags/${tagname}`
}
}
}
});
exports.usersByMyTags = function (users) {
_.each(users, user => {
user.tags = _.map(user.userTags, userTag => {
userTag.id = `${user.username}--${userTag.tag.tagname}`;
userTag.username = user.username;
userTag.tagname = userTag.tag.tagname;
return userTag;
});
delete user.userTags;
});
const serialized = usersByMyTagsSerializer.serialize(users);
return serialized;
};
exports.usersWithTags = exports.usersByMyTags;