-
Notifications
You must be signed in to change notification settings - Fork 3
/
moves-by-vehicle.js
97 lines (88 loc) · 2.95 KB
/
moves-by-vehicle.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const { isToday, parseISO } = require('date-fns')
const { isEmpty, get, groupBy, orderBy, sortBy } = require('lodash')
const i18n = require('../../config/i18n').default
const filters = require('../../config/nunjucks/filters')
const componentService = require('../services/component')
const moveToCardComponent = require('./move-to-card-component')
module.exports = function movesByVehicle({
moves,
context = 'outgoing',
showLocations = false,
locationType,
} = {}) {
const groups = []
const grouped = groupBy(moves, '_vehicleRegistration')
const expectedTimeProperty =
context === 'outgoing' ? '_expectedCollectionTime' : '_expectedArrivalTime'
Object.entries(grouped).forEach(([group, groupedItems]) => {
const vehicleRegistration = groupedItems[0]._vehicleRegistration
const sortedByTime = sortBy(groupedItems, expectedTimeProperty)
const expectedTime = get(sortedByTime[0], expectedTimeProperty)
const isComplete = groupedItems.every(
move => move[context === 'outgoing' ? '_hasLeftCustody' : '_hasArrived']
)
const showRelativeTime = !isComplete && isToday(parseISO(expectedTime))
const items = sortBy(groupedItems, 'profile.person._fullname').map(
moveToCardComponent({
showToLocation: showLocations || context === 'outgoing',
showFromLocation: showLocations || context === 'incoming',
locationType,
})
)
const timeData = {
datetime: expectedTime,
text: filters.formatTime(expectedTime),
}
const timeComponent = componentService.getComponent('appTime', timeData)
let timeHTML = expectedTime
? timeComponent
: i18n.t('collections::labels.no_expected_time')
if (showRelativeTime) {
timeHTML += componentService.getComponent('appTime', {
...timeData,
relative: true,
displayAsTag: true,
imminentOffset: 60,
})
}
if (isComplete) {
timeHTML += componentService.getComponent('govukTag', {
html: i18n.t('collections::labels.complete', {
context,
}),
classes: 'govuk-tag--green',
})
}
groups.push({
expectedTime,
items,
isComplete,
hasVehicle: !isEmpty(vehicleRegistration),
vehicleRegistration,
header: [
{
label: i18n.t('collections::labels.vehicle_registration'),
value:
vehicleRegistration ||
i18n.t('collections::labels.awaiting_vehicle'),
classes: 'govuk-grid-column-one-quarter',
},
{
label: i18n.t('collections::labels.expected_time', { context }),
value: timeHTML,
classes: 'govuk-grid-column-one-half',
},
{
label: i18n.t('people'),
value: items.length,
classes: 'govuk-grid-column-one-quarter',
},
],
})
})
return orderBy(
groups,
['isComplete', 'hasVehicle', 'expectedTime', 'vehicleRegistration'],
['asc', 'desc', 'asc', 'asc']
)
}