-
Notifications
You must be signed in to change notification settings - Fork 2
/
fn.go
533 lines (447 loc) · 13.9 KB
/
fn.go
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
package templatefn
import (
"fmt"
"html/template"
"reflect"
"slices"
"strings"
"time"
humanize "github.com/dustin/go-humanize"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/date"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lpastore"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/place"
)
// Globals contains values that are used in templates and do not change as the
// app runs.
type Globals struct {
DevMode bool
Tag string
Region string
OneloginURL string
StaticHash string
RumConfig RumConfig
ActorTypes actor.Types
Paths page.AppPaths
}
type RumConfig struct {
GuestRoleArn string
Endpoint string
ApplicationRegion string
IdentityPoolID string
ApplicationID string
}
func All(globals *Globals) map[string]any {
return map[string]any{
"global": func() *Globals { return globals },
"isEnglish": isEnglish,
"isWelsh": isWelsh,
"input": input,
"button": button,
"items": items,
"item": item,
"fieldID": fieldID,
"errorMessage": errorMessage,
"details": details,
"inc": inc,
"link": link,
"fromLink": fromLink,
"stringContains": strings.Contains,
"tr": tr,
"trFormat": trFormat,
"trFormatHtml": trFormatHtml,
"trHtml": trHtml,
"trCount": trCount,
"trFormatCount": trFormatCount,
"now": now,
"addDays": addDays,
"formatDate": formatDate,
"formatTime": formatTime,
"formatDateTime": formatDateTime,
"formatPhone": formatPhone,
"lowerFirst": localize.LowerFirst,
"listAttorneys": listAttorneys,
"listPeopleToNotify": listPeopleToNotify,
"possessive": possessive,
"card": card,
"printStruct": printStruct,
"concatAnd": concatAnd,
"concatOr": concatOr,
"concatComma": concatComma,
"penceToPounds": penceToPounds,
"donorCanGoTo": page.DonorCanGoTo,
"content": content,
"notificationBanner": notificationBanner,
"checkboxEq": checkboxEq,
"lpaDecisions": lpaDecisions,
"summaryRow": summaryRow,
"addressSummaryRow": addressSummaryRow,
"optionalSummaryRow": optionalSummaryRow,
}
}
func isEnglish(lang localize.Lang) bool {
return lang == localize.En
}
func isWelsh(lang localize.Lang) bool {
return lang == localize.Cy
}
func input(top interface{}, name, label string, value interface{}, attrs ...interface{}) map[string]interface{} {
field := map[string]interface{}{
"top": top,
"name": name,
"label": label,
"value": value,
}
if len(attrs)%2 != 0 {
panic("must have even number of attrs")
}
for i := 0; i < len(attrs); i += 2 {
field[attrs[i].(string)] = attrs[i+1]
}
return field
}
func button(app page.AppData, label string, attrs ...any) map[string]any {
field := map[string]any{
"app": app,
"label": label,
}
if len(attrs)%2 != 0 {
panic("must have even number of attrs")
}
for i := 0; i < len(attrs); i += 2 {
field[attrs[i].(string)] = attrs[i+1]
}
return field
}
func items(top interface{}, name string, value interface{}, items ...interface{}) map[string]interface{} {
return map[string]interface{}{
"top": top,
"name": name,
"value": value,
"items": items,
}
}
func item(value, label string, attrs ...interface{}) map[string]interface{} {
item := map[string]interface{}{
"value": value,
"label": label,
}
if len(attrs)%2 != 0 {
panic("must have even number of attrs")
}
for i := 0; i < len(attrs); i += 2 {
item[attrs[i].(string)] = attrs[i+1]
}
return item
}
func fieldID(name string, i int) string {
if i == 0 {
return name
}
return fmt.Sprintf("%s-%d", name, i+1)
}
func errorMessage(top interface{}, name string) map[string]interface{} {
return map[string]interface{}{
"top": top,
"name": name,
}
}
func details(top interface{}, name, detail string, open bool) map[string]interface{} {
return map[string]interface{}{
"top": top,
"name": name,
"detail": detail,
"open": open,
}
}
func inc(i int) int {
return i + 1
}
func link(app page.AppData, path string) string {
return app.Lang.URL(path)
}
type lpaIDPath interface{ Format(string) string }
func fromLink(app page.AppData, path lpaIDPath, field string) string {
return app.Lang.URL(path.Format(app.LpaID)) + "?from=" + app.Page + field
}
// checkboxEq allows matching in the checkboxes.gohtml template for a value that
// is a list of strings, or a single string (where we are emulating a switch)
func checkboxEq(needle string, in any) bool {
if in == nil {
return false
}
if str, ok := in.(string); ok {
return needle == str
}
if slist, ok := in.([]string); ok {
return slices.Contains(slist, needle)
}
return false
}
func tr(app page.AppData, messageID string) string {
if messageID == "" {
return ""
}
return app.Localizer.T(messageID)
}
func trFormat(app page.AppData, messageID string, args ...interface{}) string {
if messageID == "" {
return ""
}
if len(args)%2 != 0 {
panic("must have even number of args")
}
data := map[string]interface{}{}
for i := 0; i < len(args); i += 2 {
data[args[i].(string)] = args[i+1]
}
return app.Localizer.Format(messageID, data)
}
func trFormatHtml(app page.AppData, messageID string, args ...interface{}) template.HTML {
if messageID == "" {
return ""
}
if len(args)%2 != 0 {
panic("must have even number of args")
}
data := map[string]interface{}{}
for i := 0; i < len(args); i += 2 {
data[args[i].(string)] = args[i+1]
}
return template.HTML(app.Localizer.Format(messageID, data))
}
func trHtml(app page.AppData, messageID string) template.HTML {
if messageID == "" {
return ""
}
return template.HTML(app.Localizer.T(messageID))
}
func trCount(app page.AppData, messageID string, count int) string {
if messageID == "" {
return ""
}
return app.Localizer.Count(messageID, count)
}
func trFormatCount(app page.AppData, messageID string, count int, args ...interface{}) string {
if messageID == "" {
return ""
}
if len(args)%2 != 0 {
panic("must have even number of args")
}
data := map[string]interface{}{}
for i := 0; i < len(args); i += 2 {
data[args[i].(string)] = args[i+1]
}
return app.Localizer.FormatCount(messageID, count, data)
}
func now() time.Time {
return time.Now()
}
func addDays(days int, t time.Time) time.Time {
return t.AddDate(0, 0, days)
}
func formatDate(app page.AppData, t date.TimeOrDate) string {
return app.Localizer.FormatDate(t)
}
func formatTime(app page.AppData, t time.Time) string {
return app.Localizer.FormatTime(t)
}
func formatDateTime(app page.AppData, t time.Time) string {
return app.Localizer.FormatDateTime(t)
}
func formatPhone(s string) string {
stripped := strings.ReplaceAll(s, " ", "")
// 07005 060 702
if len(stripped) == 11 && stripped[0] == '0' {
return stripped[:5] + " " + stripped[5:8] + " " + stripped[8:]
}
// +44 7005 060 702
if len(stripped) == 13 && stripped[:3] == "+44" {
return stripped[:3] + " " + stripped[3:7] + " " + stripped[7:10] + " " + stripped[10:]
}
return s
}
type attorneySummaryData struct {
App page.AppData
CanChange bool
TrustCorporation lpastore.TrustCorporation
Attorneys []lpastore.Attorney
Link attorneySummaryDataLinks
HeadingLevel int
}
type attorneySummaryDataLinks struct {
TrustCorporation, TrustCorporationAddress, RemoveTrustCorporation string
Attorney, AttorneyAddress, RemoveAttorney string
}
func listAttorneys(app page.AppData, attorneys any, attorneyType string, headingLevel int, canChange bool) attorneySummaryData {
data := attorneySummaryData{
App: app,
CanChange: canChange,
HeadingLevel: headingLevel,
}
switch v := attorneys.(type) {
case lpastore.Attorneys:
data.Attorneys = v.Attorneys
data.TrustCorporation = v.TrustCorporation
case actor.Attorneys:
for _, a := range v.Attorneys {
data.Attorneys = append(data.Attorneys, lpastore.Attorney{
UID: a.UID,
FirstNames: a.FirstNames,
LastName: a.LastName,
DateOfBirth: a.DateOfBirth,
Email: a.Email,
Address: a.Address,
})
}
if t := v.TrustCorporation; t.Name != "" {
data.TrustCorporation = lpastore.TrustCorporation{
UID: t.UID,
Name: t.Name,
CompanyNumber: t.CompanyNumber,
Email: t.Email,
Address: t.Address,
}
}
default:
panic("unsupported type of attorneys for listAttorneys")
}
if attorneyType == "replacement" {
data.Link.Attorney = fmt.Sprintf("%s?from=%s", page.Paths.ChooseReplacementAttorneys.Format(app.LpaID), app.Page)
data.Link.AttorneyAddress = fmt.Sprintf("%s?from=%s", page.Paths.ChooseReplacementAttorneysAddress.Format(app.LpaID), app.Page)
data.Link.RemoveAttorney = fmt.Sprintf("%s?from=%s", page.Paths.RemoveReplacementAttorney.Format(app.LpaID), app.Page)
data.Link.TrustCorporation = fmt.Sprintf("%s?from=%s", page.Paths.EnterReplacementTrustCorporation.Format(app.LpaID), app.Page)
data.Link.TrustCorporationAddress = fmt.Sprintf("%s?from=%s", page.Paths.EnterReplacementTrustCorporationAddress.Format(app.LpaID), app.Page)
data.Link.RemoveTrustCorporation = fmt.Sprintf("%s?from=%s", page.Paths.RemoveReplacementTrustCorporation.Format(app.LpaID), app.Page)
} else {
data.Link.Attorney = fmt.Sprintf("%s?from=%s", page.Paths.ChooseAttorneys.Format(app.LpaID), app.Page)
data.Link.AttorneyAddress = fmt.Sprintf("%s?from=%s", page.Paths.ChooseAttorneysAddress.Format(app.LpaID), app.Page)
data.Link.RemoveAttorney = fmt.Sprintf("%s?from=%s", page.Paths.RemoveAttorney.Format(app.LpaID), app.Page)
data.Link.TrustCorporation = fmt.Sprintf("%s?from=%s", page.Paths.EnterTrustCorporation.Format(app.LpaID), app.Page)
data.Link.TrustCorporationAddress = fmt.Sprintf("%s?from=%s", page.Paths.EnterTrustCorporationAddress.Format(app.LpaID), app.Page)
data.Link.RemoveTrustCorporation = fmt.Sprintf("%s?from=%s", page.Paths.RemoveTrustCorporation.Format(app.LpaID), app.Page)
}
return data
}
func listPeopleToNotify(app page.AppData, peopleToNotify actor.PeopleToNotify, headingLevel int, canChange bool) map[string]interface{} {
return map[string]interface{}{
"App": app,
"HeadingLevel": headingLevel,
"PeopleToNotify": peopleToNotify,
"CanChange": canChange,
}
}
func card(app page.AppData, item any) map[string]any {
return map[string]interface{}{
"App": app,
"Item": item,
}
}
// printStruct is a quick way to print out an easy-to-read text representation of a struct in a template:
// {{ trHtml .App (printStruct .Lpa) }}
func printStruct(s interface{}) string {
v := reflect.ValueOf(s)
typeOfS := v.Type()
var output string
if typeOfS.Kind() == reflect.Ptr {
for i := 0; i < v.Elem().NumField(); i++ {
output = output + fmt.Sprintf("<p>%s: %v</p>", typeOfS.Elem().Field(i).Name, v.Elem().Field(i).Interface())
}
} else {
for i := 0; i < v.NumField(); i++ {
output = output + fmt.Sprintf("<p>%s: %v</p>", typeOfS.Field(i).Name, v.Field(i).Interface())
}
}
return output
}
func possessive(app page.AppData, s string) string {
return app.Localizer.Possessive(s)
}
func concatAnd(app page.AppData, list []string) string {
return app.Localizer.Concat(list, "and")
}
func concatOr(app page.AppData, list []string) string {
return app.Localizer.Concat(list, "or")
}
func concatComma(list []string) string {
return strings.Join(list, ", ")
}
func penceToPounds(pence int) string {
return humanize.CommafWithDigits(float64(pence)/100, 2)
}
func content(app page.AppData, content string) map[string]interface{} {
return map[string]interface{}{
"App": app,
"Content": content,
}
}
type notificationBannerData struct {
App page.AppData
Title string
Content template.HTML
Heading bool
Success bool
}
func notificationBanner(app page.AppData, title string, content template.HTML, options ...string) notificationBannerData {
return notificationBannerData{
App: app,
Title: title,
Content: content,
Heading: slices.Contains(options, "heading"),
Success: slices.Contains(options, "success"),
}
}
type lpaDecisionsData struct {
App page.AppData
Lpa *lpastore.Lpa
CanChange bool
}
func lpaDecisions(app page.AppData, lpa any, canChange bool) lpaDecisionsData {
data := lpaDecisionsData{
App: app,
CanChange: canChange,
}
switch v := lpa.(type) {
case *lpastore.Lpa:
data.Lpa = v
case *actor.DonorProvidedDetails:
data.Lpa = lpastore.FromDonorProvidedDetails(v)
}
return data
}
func summaryRow(app page.AppData, label, value, changeLink, fullName string, canChange, summarisingSelf bool) map[string]any {
return map[string]any{
"App": app,
"Label": label,
"Value": value,
"ChangeLink": changeLink,
"FullName": fullName,
"CanChange": canChange,
"SummarisingSelf": summarisingSelf,
}
}
func addressSummaryRow(app page.AppData, label string, address place.Address, changeLink, fullName string, canChange, summarisingSelf bool) map[string]any {
return map[string]any{
"App": app,
"Label": label,
"Address": address,
"ChangeLink": changeLink,
"FullName": fullName,
"CanChange": canChange,
"SummarisingSelf": summarisingSelf,
}
}
func optionalSummaryRow(app page.AppData, label, value, changeLink, fullName string, canChange, summarisingSelf bool) map[string]any {
return map[string]any{
"App": app,
"Label": label,
"Value": value,
"ChangeLink": changeLink,
"FullName": fullName,
"CanChange": canChange,
"SummarisingSelf": summarisingSelf,
}
}