-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClientController.java
443 lines (420 loc) · 16.5 KB
/
ClientController.java
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
package ca.bc.gov.api.oracle.legacy.controller;
import ca.bc.gov.api.oracle.legacy.ApplicationConstants;
import ca.bc.gov.api.oracle.legacy.dto.ClientLocationDto;
import ca.bc.gov.api.oracle.legacy.dto.ClientPublicViewDto;
import ca.bc.gov.api.oracle.legacy.dto.ClientViewDto;
import ca.bc.gov.api.oracle.legacy.service.ClientLocationService;
import ca.bc.gov.api.oracle.legacy.service.ClientService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.headers.Header;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@RestController
@Slf4j
@Tag(
name = "Client API",
description = "Deals with client data checks and validation"
)
@RequestMapping(value = "/api/clients", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class ClientController {
private final ClientService clientService;
private final ClientLocationService locationService;
@GetMapping("/findByClientNumber/{clientNumber}")
@Operation(
summary = "Search clients by client number. It will return active and inactive",
responses = {
@ApiResponse(
responseCode = "200",
description = "Returns a client based on it's number",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(
name = "ClientView",
implementation = ClientPublicViewDto.class
)
)
),
@ApiResponse(
responseCode = "400",
description = "Client number is invalid, usually not a numeric value",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = String.class),
examples = {@ExampleObject(value = "Invalid Client Number")}
)
),
@ApiResponse(
responseCode = "404",
description = "Unable find a client based on the provided id",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = String.class),
examples = {@ExampleObject(value = "Client not found")}
)
)
}
)
public Mono<ClientPublicViewDto> findByClientNumber(
@Parameter(
description = "The client number to look for",
example = "00000002"
)
@PathVariable String clientNumber
) {
return clientService.findByClientNumber(clientNumber);
}
@GetMapping("/findByClientNumberOrName/{clientNumberOrName}")
@Operation(
summary = "Search clients by client number or client name. It will return active and inactive",
responses = {
@ApiResponse(
responseCode = "200",
description = "Returns a client based on it's number or name",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(
name = "ClientView",
implementation = ClientPublicViewDto.class
)
),
headers = {
@Header(
name = ApplicationConstants.X_TOTAL_COUNT,
description = "Total number of records found"
)
}
),
@ApiResponse(
responseCode = "404",
description = "Unable find a client based on the provided parameter",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = String.class),
examples = {@ExampleObject(value = "Client not found")}
)
)
}
)
public Flux<ClientViewDto> findByClientNumberOrName(
@Parameter(description = "The one index page number, defaults to 0", example = "0")
@RequestParam(value = "page", required = false, defaultValue = "0")
Integer page,
@Parameter(description = "The amount of data to be returned per page, defaults to 10",
example = "10")
@RequestParam(value = "size", required = false, defaultValue = "10")
Integer size,
@Parameter(
description = "The client number to look for",
example = "00000002"
)
@PathVariable String clientNumberOrName,
ServerHttpResponse serverResponse
) {
return clientService
.findByClientNumberOrName(page, size, clientNumberOrName)
.doOnNext(dto -> serverResponse.getHeaders()
.putIfAbsent(ApplicationConstants.X_TOTAL_COUNT, List.of(dto.getCount().toString())));
}
@GetMapping("/findAllNonIndividuals")
@Operation(
summary = "Search all non-individual client. It will return active and inactive",
responses = {
@ApiResponse(
responseCode = "200",
description = "Returns a list of client",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
array = @ArraySchema(
schema = @Schema(
name = "ClientView",
implementation = ClientPublicViewDto.class
)
)
),
headers = {
@Header(
name = ApplicationConstants.X_TOTAL_COUNT,
description = "Total number of records found"
)
}
)
}
)
public Flux<ClientPublicViewDto> findAllNonIndividuals(
@Parameter(description = "The one index page number, defaults to 0", example = "0")
@RequestParam(value = "page", required = false, defaultValue = "0")
Integer page,
@Parameter(description = "The amount of data to be returned per page, defaults to 10",
example = "10")
@RequestParam(value = "size", required = false, defaultValue = "10")
Integer size,
@Parameter(
description = "Column name to sort by, defaults to clientName",
example = "clientName")
@RequestParam(value = "sortedColumnName", required = false, defaultValue = "clientName")
String sortedColumn,
ServerHttpResponse serverResponse
) {
return clientService
.findAllNonIndividualClients(page, size, sortedColumn)
.doOnNext(dto -> serverResponse.getHeaders()
.putIfAbsent(ApplicationConstants.X_TOTAL_COUNT, List.of(dto.getCount().toString())));
}
@GetMapping("/findByNames")
@Operation(
summary = """
Search a client by it's name (including first, middle and last) and client type.
It will return active and inactive""",
responses = {
@ApiResponse(
responseCode = "200",
description = "Returns a list of client based on provided parameters",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
array = @ArraySchema(
schema = @Schema(
name = "ClientView",
implementation = ClientPublicViewDto.class
)
)
),
headers = {
@Header(
name = ApplicationConstants.X_TOTAL_COUNT,
description = "Total number of records found"
)
}
),
@ApiResponse(
responseCode = "400",
description = "If no parameter is provided",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = String.class),
examples = {@ExampleObject(value = "No search parameter found")}
)
)
}
)
public Flux<ClientPublicViewDto> findByNames(
@Parameter(description = "The one index page number, defaults to 0", example = "0")
@RequestParam(value = "page", required = false, defaultValue = "0")
Integer page,
@Parameter(description = "The amount of data to be returned per page, defaults to 10",
example = "10")
@RequestParam(value = "size", required = false, defaultValue = "10")
Integer size,
@Parameter(
description = "The name of the entity or individual's last name",
example = "Baxter")
@RequestParam(value = "clientName", required = false)
String clientName,
@Parameter(
description = "The client's first name",
example = "James")
@RequestParam(value = "clientFirstName", required = false)
String clientFirstName,
@Parameter(
description = "The client's middle name",
example = "Canter")
@RequestParam(value = "clientMiddleName", required = false)
String clientMiddleName,
@Parameter(description = """
The type of client, can be any of the following:<br>
A (Association),<br>
B (First Nation Band),<br>
C (Corporation),<br>
F (Ministry of Forests and Range),<br>
G (Government),<br>
I (Individual),<br>
L (Limited Partnership),<br>
P (General Partnership),<br>
R (First Nation Group),<br>
S (Society),<br>
T (First Nation Tribal Council),<br>
U (Unregistered Company),<br>
Z (Sole Proprietorship)""",
example = "I")
@RequestParam(value = "clientTypeCodes", required = false)
List<String> clientTypeCodes,
ServerHttpResponse serverResponse
) {
return clientService
.searchByNames(
clientName,
clientFirstName,
clientMiddleName,
clientTypeCodes,
page,
size
)
.doOnNext(dto -> serverResponse.getHeaders()
.putIfAbsent(ApplicationConstants.X_TOTAL_COUNT, List.of(dto.getCount().toString())));
}
@GetMapping("/findByAcronym")
@Operation(
summary = "Search a client by it's acronym. It will return active and inactive",
responses = {
@ApiResponse(
responseCode = "200",
description = "Returns a client based on it's acronym",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
array = @ArraySchema(
schema = @Schema(
name = "ClientView",
implementation = ClientPublicViewDto.class
)
)
),
headers = {
@Header(
name = ApplicationConstants.X_TOTAL_COUNT,
description = "Total number of records found"
)
}
),
@ApiResponse(
responseCode = "400",
description = "If no parameter is provided",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = String.class),
examples = {@ExampleObject(value = "No acronym parameter found")}
)
),
@ApiResponse(
responseCode = "404",
description = "If no client found for that acronym",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = String.class),
examples = {@ExampleObject(value = "No client found with the acronym Baxter")}
)
)
}
)
public Flux<ClientPublicViewDto> findByAcronym(
@Parameter(
description = "The acronym to look for",
example = "Baxter")
@RequestParam(value = "acronym")
@NotNull
String acronym,
ServerHttpResponse serverResponse
) {
return clientService
.searchByAcronym(acronym)
.doOnNext(dto -> serverResponse.getHeaders()
.put(ApplicationConstants.X_TOTAL_COUNT, List.of(dto.getCount().toString())));
}
@GetMapping("/{clientNumber}/locations")
@Operation(
summary = "Search for client location based on client number",
responses = {
@ApiResponse(
responseCode = "200",
description = "Returns a list of client locations",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
array = @ArraySchema(
schema = @Schema(
name = "ClientLocation",
implementation = ClientLocationDto.class
)
)
),
headers = {
@Header(
name = ApplicationConstants.X_TOTAL_COUNT,
description = "Total number of records found"
)
}
)
}
)
public Flux<ClientLocationDto> listClientLocations(
@Parameter(description = "The one index page number, defaults to 0", example = "0")
@RequestParam(value = "page", required = false, defaultValue = "0")
Integer page,
@Parameter(description = "The amount of data to be returned per page, defaults to 10",
example = "10")
@RequestParam(value = "size", required = false, defaultValue = "10")
Integer size,
@Parameter(
description = "ID of the client to filter by",
example = "00000001")
@PathVariable(value = "clientNumber")
String clientNumber,
ServerHttpResponse serverResponse
) {
return
locationService
.countClientLocations(clientNumber)
.doOnNext(dto -> serverResponse.getHeaders().putIfAbsent(
ApplicationConstants.X_TOTAL_COUNT, List.of(dto.toString())))
.flatMapMany(count ->
locationService
.listClientLocations(clientNumber, page, size)
);
}
@GetMapping("/{clientNumber}/locations/{locationNumber}")
@Operation(
summary = "Get the client location based on client number and location id",
responses = {
@ApiResponse(
responseCode = "200",
description = "Returns a client location",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(
name = "ClientLocation",
implementation = ClientLocationDto.class
)
)
),
@ApiResponse(
responseCode = "404",
description = "If no client location found for that client number and location id",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = String.class),
examples = {@ExampleObject(value = "No client location found")}
)
)
}
)
public Mono<ClientLocationDto> getClientLocationDetails(
@Parameter(
description = "ID of the client to filter by",
example = "00000001")
@PathVariable(value = "clientNumber")
String clientNumber,
@Parameter(
description = "ID of the client location to filter by",
example = "00000001")
@PathVariable(value = "locationNumber")
String locationNumber
) {
return locationService.getClientLocationDetails(clientNumber, locationNumber);
}
}