-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsamplecode.js
More file actions
43 lines (43 loc) · 1.72 KB
/
samplecode.js
File metadata and controls
43 lines (43 loc) · 1.72 KB
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
exports.inviteUser = function (req, res) {
var invitationBody = req.body;
var shopId = req.params.shopId;
var authUrl = "https://url.to.auth.system.com/invitation";
superagent
.post(authUrl)
.send(invitationBody)
.end(function (err, invitationResponse) {
if (invitationResponse.status === 201) {
User.findOneAndUpdate({
authId: invitationResponse.body.authId
}, {
authId: invitationResponse.body.authId,
email: invitationBody.email
}, {
upsert: true,
new: true
}, function (err, createdUser) {
Shop.findById(shopId).exec(function (err, shop) {
if (err || !shop) {
return res.status(500).send(err || {
message: 'No shop found'
});
}
if (shop.invitations.indexOf(invitationResponse.body.invitationId)) {
shop.invitations.push(invitationResponse.body.invitationId);
}
if (shop.users.indexOf(createdUser._id) === -1) {
shop.users.push(createdUser);
}
shop.save();
});
});
} else if (invitationResponse.status === 200) {
res.status(400).json({
error: true,
message: 'User already invited to this shop'
});
return;
}
res.json(invitationResponse);
});
};