generated from ministryofjustice/hmpps-template-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseClientController.ts
252 lines (208 loc) · 8.3 KB
/
baseClientController.ts
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
import { RequestHandler } from 'express'
import { BaseClientService } from '../services'
import listBaseClientsPresenter from '../views/presenters/listBaseClientsPresenter'
import viewBaseClientPresenter from '../views/presenters/viewBaseClientPresenter'
import nunjucksUtils from '../views/helpers/nunjucksUtils'
import { mapCreateBaseClientForm, mapEditBaseClientDeploymentForm, mapEditBaseClientDetailsForm } from '../mappers'
import { BaseClient } from '../interfaces/baseClientApi/baseClient'
import editBaseClientPresenter from '../views/presenters/editBaseClientPresenter'
import mapFilterForm from '../mappers/forms/mapFilterForm'
export default class BaseClientController {
constructor(private readonly baseClientService: BaseClientService) {}
public displayBaseClients(): RequestHandler {
return async (req, res) => {
const userToken = res.locals.user.token
const baseClients = await this.baseClientService.listBaseClients(userToken)
const presenter = listBaseClientsPresenter(baseClients)
res.render('pages/base-clients.njk', {
presenter,
})
}
}
public filterBaseClients(): RequestHandler {
return async (req, res) => {
const userToken = res.locals.user.token
const filter = mapFilterForm(req)
const baseClients = await this.baseClientService.listBaseClients(userToken)
const presenter = listBaseClientsPresenter(baseClients, filter)
res.render('pages/base-clients.njk', {
presenter,
})
}
}
public displayBaseClient(): RequestHandler {
return async (req, res) => {
const userToken = res.locals.user.token
const { baseClientId } = req.params
const baseClient = await this.baseClientService.getBaseClient(userToken, baseClientId)
const clients = await this.baseClientService.listClientInstances(userToken, baseClient)
const presenter = viewBaseClientPresenter(baseClient, clients)
res.render('pages/base-client.njk', {
baseClient,
presenter,
...nunjucksUtils,
})
}
}
public displayNewBaseClient(): RequestHandler {
return async (req, res) => {
const { grant } = req.query
if (!(grant === 'client-credentials' || grant === 'authorization-code')) {
res.render('pages/new-base-client-grant.njk')
return
}
res.render('pages/new-base-client-details.njk', { grant, ...nunjucksUtils })
}
}
public createBaseClient(): RequestHandler {
return async (req, res, next) => {
const userToken = res.locals.user.token
const baseClient = mapCreateBaseClientForm(req)
// Simple validation
const error = await this.validateCreateBaseClient(userToken, baseClient)
if (error) {
res.render('pages/new-base-client-details.njk', {
errorMessage: { text: error },
grant: baseClient.grantType,
baseClient,
presenter: editBaseClientPresenter(baseClient),
...nunjucksUtils,
})
return
}
// Create base client
const secrets = await this.baseClientService.addBaseClient(userToken, baseClient)
// Display success page
res.render('pages/new-base-client-success.njk', {
title: `Client has been added`,
baseClientId: baseClient.baseClientId,
secrets,
})
}
}
async validateCreateBaseClient(userToken: string, baseClient: BaseClient) {
// if baseClient.baseClientId is null or empty string, throw error
if (!baseClient.baseClientId) {
return 'This field is required'
}
// if baseClient.baseClientId is not unique, throw error
try {
await this.baseClientService.getBaseClient(userToken, baseClient.baseClientId)
return 'A base client with this ID already exists'
} catch (e) {
return ''
}
}
public displayEditBaseClient(): RequestHandler {
return async (req, res) => {
const userToken = res.locals.user.token
const { baseClientId } = req.params
const baseClient = await this.baseClientService.getBaseClient(userToken, baseClientId)
const presenter = editBaseClientPresenter(baseClient)
res.render('pages/edit-base-client-details.njk', {
baseClient,
presenter,
...nunjucksUtils,
})
}
}
public updateBaseClientDetails(): RequestHandler {
return async (req, res, next) => {
const userToken = res.locals.user.token
const { baseClientId } = req.params
// get current values
const baseClient = await this.baseClientService.getBaseClient(userToken, baseClientId)
// map form values to updated base client
const updatedClient = mapEditBaseClientDetailsForm(baseClient, req)
// update base client
await this.baseClientService.updateBaseClient(userToken, updatedClient)
// return to view base client page
res.redirect(`/base-clients/${baseClientId}`)
}
}
public displayEditBaseClientDeployment(): RequestHandler {
return async (req, res) => {
const userToken = res.locals.user.token
const { baseClientId } = req.params
const baseClient = await this.baseClientService.getBaseClient(userToken, baseClientId)
res.render('pages/edit-base-client-deployment.njk', {
baseClient,
})
}
}
public updateBaseClientDeployment(): RequestHandler {
return async (req, res, next) => {
const userToken = res.locals.user.token
const { baseClientId } = req.params
// get current values
const baseClient = await this.baseClientService.getBaseClient(userToken, baseClientId)
// map form values to updated base client
const updatedClient = mapEditBaseClientDeploymentForm(baseClient, req)
// update base client
await this.baseClientService.updateBaseClientDeployment(userToken, updatedClient)
// return to view base client page
res.redirect(`/base-clients/${baseClientId}`)
}
}
public createClientInstance(): RequestHandler {
return async (req, res, next) => {
const userToken = res.locals.user.token
const { baseClientId } = req.params
// get base client
const baseClient = await this.baseClientService.getBaseClient(userToken, baseClientId)
// Create base client
const secrets = await this.baseClientService.addClientInstance(userToken, baseClient)
// Display success page
res.render('pages/new-base-client-success.njk', {
title: `Client has been added`,
baseClientId: baseClient.baseClientId,
secrets,
})
}
}
public displayDeleteClientInstance(): RequestHandler {
return async (req, res, next) => {
const userToken = res.locals.user.token
const { baseClientId, clientId } = req.params
const error = req.query.error === 'clientIdMismatch' ? 'Client ID does not match' : null
// get base client
const baseClient = await this.baseClientService.getBaseClient(userToken, baseClientId)
const clients = await this.baseClientService.listClientInstances(userToken, baseClient)
// Display delete confirmation page
res.render('pages/delete-client-instance.njk', {
baseClient,
clientId,
isLastClient: clients.length === 1,
error,
})
}
}
public deleteClientInstance(): RequestHandler {
return async (req, res, next) => {
const userToken = res.locals.user.token
const { baseClientId, clientId } = req.params
// check client id matches
if (req.body.confirm !== clientId) {
res.redirect(`/base-clients/${baseClientId}/clients/${clientId}/delete?error=clientIdMismatch`)
return
}
// get base client
const baseClient = await this.baseClientService.getBaseClient(userToken, baseClientId)
const clients = await this.baseClientService.listClientInstances(userToken, baseClient)
const client = clients.find(c => c.clientId === clientId)
// check client exists
if (!client) {
res.redirect(`/base-clients/${baseClientId}/clients/${clientId}/delete?error=clientNotFound`)
return
}
// delete client
await this.baseClientService.deleteClientInstance(userToken, client)
// return to view base client screen (or home screen if last client deleted)
if (clients.length === 1) {
res.redirect(`/`)
} else {
res.redirect(`/base-clients/${baseClientId}`)
}
}
}
}