This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathvalidate-session-invitation.js
83 lines (73 loc) · 2.56 KB
/
validate-session-invitation.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
'use strict';
var async = require('async');
var _ = require('lodash');
function validateSessionInvitation (args, callback) {
var seneca = this;
var user = args.user;
var locality = args.locality || 'en_US';
var plugin = args.role;
var invitation = args.invitation;
if (!invitation) return callback(null, {ok: false, why: 'args.invitation is empty'});
var ticketId = invitation.ticketId;
var invitedUserId = invitation.userId;
async.waterfall([
validateRequest,
checkCapacity,
createApplication
], function (err, res) {
if (err) return callback(null, {ok: false, why: err.message});
return callback(null, {ok: true});
});
function validateRequest (done) {
seneca.act({role: plugin, cmd: 'loadTicket', id: ticketId}, function (err, ticket) {
if (err) return done(err);
if (!ticket.invites || _.isEmpty(ticket.invites)) return done(new Error('No invites found'));
var invitedUserFound = _.find(ticket.invites, function (invite) {
return invite.userId === invitedUserId;
});
if (!invitedUserFound) return done(new Error('Invalid session invitation.'));
return done(null, ticket);
});
}
function checkCapacity (ticket, wfCb) {
seneca.act({ role: plugin, cmd: 'searchApplications', query: { ticketId: ticket.id, status: 'approved' } }, (err, applications) => {
if (applications.length < ticket.quantity) {
wfCb(null, ticket);
} else {
callback(null, { http$: { status: 410 } });
}
});
}
function createApplication (ticket, done) {
var application = {
sessionId: ticket.sessionId,
ticketName: ticket.name,
ticketType: ticket.type,
ticketId: ticket.id,
userId: invitedUserId,
status: 'approved',
emailSubject: invitation.emailSubject
};
async.waterfall([
loadSession,
loadEvent,
saveApplication
], done);
function loadSession (done) {
seneca.act({role: plugin, cmd: 'loadSession', id: ticket.sessionId}, done);
}
function loadEvent (session, done) {
seneca.act({role: plugin, cmd: 'getEvent', id: session.eventId}, function (err, event) {
if (err) return done(err);
application.eventId = event.id;
application.dojoId = event.dojoId;
return done(null, event);
});
}
function saveApplication (event, done) {
var applications = [application];
seneca.act({role: plugin, cmd: 'bulkApplyApplications', applications: applications, user: user, locality: locality}, done);
}
}
}
module.exports = validateSessionInvitation;