-
-
Notifications
You must be signed in to change notification settings - Fork 337
/
AttendeeApiController.java
164 lines (141 loc) · 6.87 KB
/
AttendeeApiController.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
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.controller.api.v1;
import alfio.manager.AttendeeManager;
import alfio.manager.support.SponsorAttendeeData;
import alfio.manager.support.TicketAndCheckInResult;
import alfio.model.SponsorScan;
import alfio.model.result.Result;
import alfio.model.support.TicketWithAdditionalFields;
import alfio.repository.SponsorScanRepository;
import alfio.util.EventUtil;
import alfio.util.Wrappers;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.security.Principal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import static alfio.util.Wrappers.optionally;
@RestController
@RequestMapping("/api/attendees")
public class AttendeeApiController {
public static final String ALFIO_OPERATOR_HEADER = "Alfio-Operator";
private static final Logger log = LoggerFactory.getLogger(AttendeeApiController.class);
private final AttendeeManager attendeeManager;
@Autowired
public AttendeeApiController(AttendeeManager attendeeManager) {
this.attendeeManager = attendeeManager;
}
@ExceptionHandler({DataIntegrityViolationException.class, IllegalArgumentException.class})
public ResponseEntity<String> handleDataIntegrityException(Exception e) {
log.warn("bad input detected", e);
return new ResponseEntity<>("bad input parameters", HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<String> handleGenericException(RuntimeException e) {
log.error("unexpected exception", e);
return new ResponseEntity<>("unexpected error", HttpStatus.INTERNAL_SERVER_ERROR);
}
@PostMapping("/sponsor-scan")
public ResponseEntity<TicketAndCheckInResult> scanBadge(@RequestBody SponsorScanRequest request,
Principal principal,
@RequestHeader(name = ALFIO_OPERATOR_HEADER, required = false) String operator) {
return ResponseEntity.ok(attendeeManager.registerSponsorScan(request.eventName, request.ticketIdentifier, request.notes, request.leadStatus, principal.getName(), operator));
}
@PostMapping("/sponsor-scan/bulk")
public ResponseEntity<List<TicketAndCheckInResult>> scanBadges(@RequestBody List<SponsorScanRequest> requests,
Principal principal,
@RequestHeader(name = ALFIO_OPERATOR_HEADER, required = false) String operator) {
String username = principal.getName();
return ResponseEntity.ok(requests.stream()
.map(request -> attendeeManager.registerSponsorScan(request.eventName, request.ticketIdentifier, request.notes, request.leadStatus, username, operator))
.collect(Collectors.toList()));
}
@GetMapping("/{eventKey}/sponsor-scan/mine")
public ResponseEntity<List<SponsorAttendeeData>> getScannedBadges(@PathVariable("eventKey") String eventShortName, @RequestParam(value = "from", required = false) String from, Principal principal) {
ZonedDateTime start = Optional.ofNullable(StringUtils.trimToNull(from))
.map(EventUtil.JSON_DATETIME_FORMATTER::parse)
.flatMap(d -> Wrappers.safeSupplier(() -> ZonedDateTime.of(LocalDateTime.from(d), ZoneOffset.UTC)))
.orElse(SponsorScanRepository.DEFAULT_TIMESTAMP);
return attendeeManager.retrieveScannedAttendees(eventShortName, principal.getName(), start).map(ResponseEntity::ok).orElse(notFound());
}
/**
* API for external apps that load the ticket using its UUID. It is possible to retrieve a ticket only if <b>all</b> the following conditions are met:
*
* <ul>
* <li>An event with key {@code eventKey} exists</li>
* <li>The user and the event belong to the same organization</li>
* <li>A ticket with UUID {@code UUID} exists</li>
* </ul>
*
* otherwise, an error is returned.
*
* @param eventShortName
* @param uuid
* @param principal
* @return
*/
@GetMapping("/{eventKey}/ticket/{UUID}")
public Result<TicketWithAdditionalFields> getTicketDetails(@PathVariable("eventKey") String eventShortName, @PathVariable("UUID") String uuid, Principal principal) {
return attendeeManager.retrieveTicket(eventShortName, uuid, principal.getName());
}
private static <T> ResponseEntity<T> notFound() {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
public static class SponsorScanRequest {
private final String eventName;
private final String ticketIdentifier;
private final String notes;
private final SponsorScan.LeadStatus leadStatus;
public String getEventName() {
return eventName;
}
public String getTicketIdentifier() {
return ticketIdentifier;
}
public String getNotes() {
return notes;
}
public SponsorScan.LeadStatus getLeadStatus() {
return leadStatus;
}
@JsonCreator
public SponsorScanRequest(@JsonProperty("eventName") String eventName,
@JsonProperty("ticketIdentifier") String ticketIdentifier,
@JsonProperty("notes") String notes,
@JsonProperty("leadStatus") String leadStatus) {
this.eventName = eventName;
this.ticketIdentifier = ticketIdentifier;
this.notes = notes;
this.leadStatus = Optional.ofNullable(leadStatus)
.flatMap(l -> optionally(() -> SponsorScan.LeadStatus.valueOf(l)))
.orElse(SponsorScan.LeadStatus.WARM);
}
}
}