-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathregistration.js
More file actions
115 lines (100 loc) · 3.65 KB
/
Copy pathregistration.js
File metadata and controls
115 lines (100 loc) · 3.65 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
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
/* jshint node: true */
/* jshint esnext: true */
'use strict';
const Q = require('q');
const _ = require('lodash');
const pug = require('pug');
const moment = require('moment');
const config = require('config');
const calculator = require('../domain/costCalculator');
const db = require('../service/util/dbHelper');
const mails = require('../service/util/mails');
const participants = require('../service/participants');
const couponcodes = require('../service/couponcodes');
const startNumbers = require('../service/startNumbers');
const tokens = require('../service/tokens');
const tshirts = require('../service/tshirts');
const editUrlHelper = require('../domain/editUrlHelper');
const registration = {};
registration.isClosed = () => {
return db.select("SELECT data->>'is_closed' as is_closed FROM race;")
.then(result => {
return _.isEmpty(result) || result[0].is_closed === 'true';
});
};
registration.close = () => {
return db.update("UPDATE race SET data = jsonb_set(data, '{is_closed}', 'true');");
};
registration.reopen = () => {
return db.update("UPDATE race SET data = jsonb_set(data, '{is_closed}', 'false');");
};
registration.confirm = (participantId) => {
const deferred = Q.defer();
participants.markPayed(participantId)
.then(() => {
participants.get.byId(participantId)
.then(result => {
pug.renderFile('views/admin/paymentValidation/text.pug',
{name: result.firstname, editUrl: editUrlHelper.generateUrl(result.secureid)},
(error, html) =>
mails.sendEmail(result.email, 'Lauf gegen Rechts: Zahlung erhalten', html, error)
);
deferred.resolve();
});
})
.fail(err =>
deferred.reject(err)
);
return deferred.promise;
};
registration.sendConfirmationMail = (participant, paymentToken) => {
pug.renderFile('views/registration/confirmationText.pug',
{
name: participant.firstname,
token: paymentToken,
bank: config.get('contact.bank'),
amount: calculator.priceFor(participant),
free: calculator.priceFor(participant) < 1,
editUrl: editUrlHelper.generateUrl(participant.secureID),
startnr: participant.start_number
},
(error, html) => {
return mails.sendEmail(participant.email, 'Lauf Gegen Rechts: Registrierung erfolgreich', html, error);
}
);
};
registration.start = (participant) => {
const deferred = Q.defer();
var resultPromise = couponcodes.validateCode(participant.couponcode, participant.discount);
resultPromise.then(isValidCode => {
if (isValidCode) {
tokens.createUnique().then((paymentToken) => {
startNumbers.next().then((nr) => {
let p = participant
.withToken(paymentToken)
.withSecureId(editUrlHelper.generateSecureID())
.withStartNr(nr)
.withStartBlock(participants.choseStartBlock(nr))
.withRegistrationTime(moment().format());
participants.save(p)
.then(id => {
if (!_.isEmpty(p.tshirt)) {
tshirts.addFor(p.tshirt, id);
}
if (calculator.priceFor(p) === 0) {
participants.markPayed(id);
}
registration.sendConfirmationMail(p, paymentToken);
deferred.resolve({'id': id, 'token': paymentToken, secureid: p.secureID, startnr: p.start_number});
couponcodes.markAsUsed(participant.couponcode);
})
.fail(deferred.reject);
});
}).fail(deferred.reject);
} else {
deferred.reject(new TypeError('Ungültiger Couponcode'));
}
});
return deferred.promise;
};
module.exports = registration;