-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
299 lines (269 loc) · 10.2 KB
/
index.coffee
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
RoleController = require 'members-area/app/controllers/role'
PersonController = require 'members-area/app/controllers/person'
encode = require('members-area/node_modules/entities').encodeXML
module.exports =
customPaymentMethods:
CASH: "Cash"
PAYPAL: "PayPal"
OTHER: "Other"
initialize: (done) ->
@app.addRoute 'all', '/admin/payments', 'members-area-payments#payments#index'
@app.addRoute 'all', '/admin/payments/:id', 'members-area-payments#payments#view'
@app.addRoute 'all', '/subscription', 'members-area-payments#subscription#view'
@hook 'navigation_items', @modifyNavigationItems.bind(this)
@hook 'models:initialize', ({models}) =>
models.Payment.hasOne 'transaction', models.Transaction, reverse: 'payments', autoFetch: false
models.Payment.hasOne 'user', models.User, reverse: 'payments', autoFetch: false
@hook 'render-role-edit', @renderRoleSubscription.bind(this)
@hook 'render-person-view', @renderPersonPayments.bind(this)
@hook 'render-person-index', @renderPersonIndex.bind(this)
RoleController.before @handleRoleSubscription, only: ['edit']
PersonController.before @addPaidUntilClasses, only: ['index']
PersonController.before @addPayment(@customPaymentMethods), only: ['view']
@addCSS "#{__dirname}/css/payments.styl"
done()
modifyNavigationItems: ({addItem}) ->
addItem 'user',
title: 'Subscription'
id: 'members-area-payments-subscription-view'
href: '/subscription'
permissions: []
priority: 42
addItem 'admin',
title: 'Payments'
id: 'members-area-payments'
href: '/admin/payments'
permissions: ['admin']
priority: 20
renderRoleSubscription: (options) ->
{controller, $} = options
$topNode = $('.main form').first().find('.control-group').last()
checked = if !!controller.role.meta.subscriptionRequired then " checked='checked'" else ""
$newNode = $ """
<div class="control-group">
<label for="name" class="control-label">Subscription required</label>
<div class="controls">
<input type="checkbox" name="subscriptionRequired"#{checked}> Check this if a subscription is required from people with this role.
</div>
</div>
"""
$topNode.before $newNode
return
addPayment: (customPaymentMethods) -> (done) ->
# IMPORTANT: this method runs in the context of a PersonController instance
return done() unless @req.method is 'POST' and @req.body?.action is 'add-payment'
return done() unless @loggedInUser.can 'admin'
return done new Error("Invalid YYYY-MM-DD date '#{@req.body.when}'") unless /^201[4-9]-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/.test @req.body.when
return done new Error("Invalid type '#{@req.body.type}'") unless customPaymentMethods[@req.body.type]
periodCount = parseInt(@req.body.period_count, 10)
return done new Error("Invalid period count '#{@req.body.period_count}'") unless 1 <= periodCount <= 12
paidWhen = new Date(Date.parse(@req.body.when))
nextPaymentDate = @user.getPaidUntil paidWhen
payment =
user_id: @user.id
type: @req.body.type
amount: Math.round(parseFloat(@req.body.amount) * 100)
status: 'paid'
include: true
when: paidWhen
period_from: nextPaymentDate
period_count: periodCount
nextPaymentDate = new Date(+nextPaymentDate)
nextPaymentDate.setMonth(nextPaymentDate.getMonth() + periodCount)
@user.paidUntil = nextPaymentDate
@req.models.Payment.create [payment], (err) =>
return done err if err
@user.save done
renderPersonIndex: (options, done) ->
{controller, $} = options
return done() unless controller.loggedInUser.can 'admin'
$main = $(".main").eq(0)
map =
good: "<4 days overdue"
warning: "<30 days overdue"
severeWarning: "<90 days overdue"
error: "Massively overdue"
supporter: "Recent supporter"
none: "No subs required"
rows = []
for k, v of map
rows.push """
<tr>
<td>#{encode v}</td>
<td>#{controller.subscriptionStats[k]}</td>
</tr>
"""
$main.append """
<table style="width: 30%" class="table">
<tr>
<th>Subscription status</th>
<th>Count</th>
</tr>
#{rows.join("")}
</table>
"""
done()
return
renderPersonPayments: (options, done) ->
{controller, $} = options
return done() unless controller.loggedInUser.can 'admin'
controller.req.models.Payment.find()
.order("-when")
.where(user_id: controller.user.id)
.limit(20)
.run (err, payments) =>
$main = $(".main").eq(0)
rows = []
if payments?.length
for payment in payments
rows.push """
<tr class="#{if payment.include then "" else "text-error"}">
<td><a href="/admin/payments/#{payment.id}">#{payment.when.toISOString().substr(0, 10)}</a></td>
<td>#{payment.type}</td>
<td>£#{(payment.amount/100).toFixed(2)}</td>
<td>#{payment.period_from.toISOString().substr(0, 10)}</td>
<td>#{payment.period_count}</td>
<td>#{if payment.include then "✔" else "✘"}</td>
</tr>
"""
else
rows.push """
<tr>
<td colspan="6">
No records to display
</td>
</tr>
"""
paidUntil = controller.user.paidUntil
midnightThisMorning = new Date()
midnightThisMorning.setHours(0)
midnightThisMorning.setMinutes(0)
midnightThisMorning.setSeconds(0)
midnightAMonthAgo = new Date +midnightThisMorning
midnightAMonthAgo.setMonth(midnightAMonthAgo.getMonth()-1)
overdueDays = Math.floor (midnightThisMorning - paidUntil)/(24*60*60*1000)
statusText =
if +paidUntil > +midnightThisMorning
"<p class='text-success'>Payments up to date (next due: #{paidUntil.toISOString().substr(0,10)})</p>"
else if +paidUntil > midnightAMonthAgo
"<p class='text-warning'>Payments #{overdueDays} days overdue</p>"
else
"<p class='text-error'>Payments #{Math.floor overdueDays/7} weeks overdue</p>"
$main.append """
<h3>Add a Payment</h3>
<form method="POST">
<table class="table table-bordered" style="width: auto">
<tr>
<th>Payment type</th>
<td>
<select name='type'>
#{
(for k, v of @customPaymentMethods
"<option value='#{k}'>#{v}</option>"
).join("\n")
}
</select>
</td>
</tr>
<tr>
<th>
Payment date<br>
<small>(YYYY-MM-DD)</small>
</th>
<td>
<input name='when' placeholder='YYYY-MM-DD' value='#{new Date().toISOString().substr(0,10)}'>
</td>
</tr>
<tr>
<th>
Amount<br>
<small>£ (GBP)</small>
</th>
<td>
<input name='amount' value='5.00'>
</td>
</tr>
<tr>
<th>
Duration<br>
<small>(months, the period covered by the payment)
</th>
<td>
<select name='period_count'>
<option value='1' selected='selected'>1 month</option>
<option value='2'>2 months</option>
<option value='3'>3 months</option>
<option value='4'>4 months</option>
<option value='5'>5 months</option>
<option value='6'>6 months</option>
<option value='7'>7 months</option>
<option value='8'>8 months</option>
<option value='9'>9 months</option>
<option value='10'>10 months</option>
<option value='11'>11 months</option>
<option value='12'>12 months</option>
</select>
</td>
</tr>
</table>
<button class='btn btn-warning' type='submit' name='action' value='add-payment'>
Register payment
</button>
</form>
<h3>Payments</h3>
#{statusText}
<table class="table table-striped">
<tr>
<th>Payment Date</th>
<th>Type</th>
<th>Amount</th>
<th>From</th>
<th>Duration (months)</th>
<th>Worked?</th>
</tr>
#{rows.join("\n")}
</table>
"""
done()
return
handleRoleSubscription: ->
# IMPORTANT: this method runs in the context of a RoleController instance
if @req.method is 'POST' and [email protected]
subscriptionRequired = [email protected]
delete @data.subscriptionRequired
@role.setMeta {subscriptionRequired}
addPaidUntilClasses: ->
# IMPORTANT: this method runs in the context of a RoleController instance
return unless @isAdmin
midnightThisMorning = new Date()
midnightThisMorning.setHours(0)
midnightThisMorning.setMinutes(0)
midnightThisMorning.setSeconds(0)
twentyDaysAgo = new Date()
twentyDaysAgo.setDate(twentyDaysAgo.getDate() - 20)
@subscriptionStats =
none: 0
supporter: 0
good: 0
warning: 0
severeWarning: 0
error: 0
for user in @users
overdueDays = Math.floor (midnightThisMorning - user.paidUntil)/(24*60*60*1000)
if overdueDays <= 3
user.classNames += " payments-good"
if user.subscriptionRequired
@subscriptionStats.good++
else if (midnightThisMorning - user.getPaidUntil(twentyDaysAgo)) / (24*60*60*1000) <= 14
@subscriptionStats.supporter++
else
@subscriptionStats.none++
else if overdueDays < 30
user.classNames += " payments-warning"
@subscriptionStats.warning++
else if overdueDays < 90
user.classNames += " payments-severe-warning"
@subscriptionStats.severeWarning++
else
user.classNames += " payments-error"
@subscriptionStats.error++