/
AdminReservationApiController.java
379 lines (335 loc) · 23.1 KB
/
AdminReservationApiController.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
/**
* 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.admin;
import alfio.controller.api.support.BookingInfoTicket;
import alfio.controller.api.support.BookingInfoTicketLoader;
import alfio.controller.api.support.PageAndContent;
import alfio.manager.*;
import alfio.model.*;
import alfio.model.PurchaseContext.PurchaseContextType;
import alfio.model.modification.AdminReservationModification;
import alfio.model.result.ErrorCode;
import alfio.model.result.Result;
import alfio.model.subscription.SubscriptionWithUsageDetails;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.security.Principal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static alfio.util.FileUtil.sendPdf;
@RequestMapping("/admin/api/reservation")
@RestController
@AllArgsConstructor
public class AdminReservationApiController {
private final AdminReservationManager adminReservationManager;
private final EventManager eventManager;
private final PurchaseContextManager purchaseContextManager;
private final PurchaseContextSearchManager purchaseContextSearchManager;
private final TicketReservationManager ticketReservationManager;
private final BookingInfoTicketLoader bookingInfoTicketLoader;
private final AccessService accessService;
@PostMapping("/{purchaseContextType}/{publicIdentifier}/new")
public Result<String> createNew(@PathVariable PurchaseContextType purchaseContextType,
@PathVariable String publicIdentifier,
@RequestBody AdminReservationModification reservation,
Principal principal) {
if(purchaseContextType != PurchaseContextType.event) {
return Result.error(ErrorCode.EventError.NOT_FOUND);
}
accessService.checkEventMembership(principal, publicIdentifier, AccessService.MEMBERSHIP_ROLES); // ADMIN, OWNER _and_ SUPERVISOR can create a new reservation
return adminReservationManager.createReservation(reservation, publicIdentifier, principal.getName()).map(r -> r.getLeft().getId());
}
@GetMapping("/{purchaseContextType}/{publicIdentifier}/reservations/all-status")
public TicketReservation.TicketReservationStatus[] getAllStatus(@PathVariable PurchaseContextType purchaseContextType, @PathVariable String publicIdentifier) {
return TicketReservation.TicketReservationStatus.values();
}
@GetMapping("/{purchaseContextType}/{publicIdentifier}/reservations/list")
public PageAndContent<List<TicketReservation>> findAll(@PathVariable PurchaseContextType purchaseContextType,
@PathVariable String publicIdentifier,
@RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "search", required = false) String search,
@RequestParam(value = "status", required = false) List<TicketReservation.TicketReservationStatus> status,
Principal principal) {
return purchaseContextManager.findBy(purchaseContextType, publicIdentifier)
.map(purchaseContext -> {
accessService.checkOrganizationOwnership(principal, purchaseContext.getOrganizationId());
Pair<List<TicketReservation>, Integer> res = purchaseContextSearchManager.findAllReservationsFor(purchaseContext, page, search, status);
return new PageAndContent<>(res.getLeft(), res.getRight());
}).orElseGet(() -> new PageAndContent<>(Collections.emptyList(), 0));
}
@PutMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/confirm")
public Result<TicketReservationDescriptor> confirmReservation(@PathVariable PurchaseContextType purchaseContextType,
@PathVariable String publicIdentifier,
@PathVariable String reservationId,
Principal principal) {
accessService.checkReservationMembership(principal, purchaseContextType, publicIdentifier, reservationId);
return adminReservationManager.confirmReservation(purchaseContextType, publicIdentifier, reservationId, principal.getName(), AdminReservationModification.Notification.EMPTY)
.map(triple -> toReservationDescriptor(reservationId, triple));
}
@PostMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}")
public Result<Boolean> updateReservation(@PathVariable PurchaseContextType purchaseContextType,
@PathVariable String publicIdentifier,
@PathVariable String reservationId,
@RequestBody AdminReservationModification arm,
Principal principal) {
accessService.checkReservationMembership(principal, purchaseContextType, publicIdentifier, reservationId);
return adminReservationManager.updateReservation(purchaseContextType, publicIdentifier, reservationId, arm, principal.getName());
}
@PutMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/notify")
public Result<Boolean> notifyReservation(@PathVariable PurchaseContextType purchaseContextType,
@PathVariable String publicIdentifier,
@PathVariable String reservationId,
@RequestBody AdminReservationModification arm,
Principal principal) {
accessService.checkReservationMembership(principal, purchaseContextType, publicIdentifier, reservationId);
return adminReservationManager.notify(purchaseContextType, publicIdentifier, reservationId, arm, principal.getName());
}
@PutMapping("/event/{publicIdentifier}/{reservationId}/notify-attendees")
public Result<Boolean> notifyAttendees(@PathVariable String publicIdentifier,
@PathVariable String reservationId,
@RequestBody List<Integer> ids,
Principal principal) {
accessService.checkReservationMembership(principal, PurchaseContextType.event, publicIdentifier, reservationId);
return adminReservationManager.notifyAttendees(publicIdentifier, reservationId, ids, principal.getName());
}
@GetMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/audit")
public Result<List<Audit>> getAudit(@PathVariable PurchaseContextType purchaseContextType,
@PathVariable String publicIdentifier,
@PathVariable String reservationId, Principal principal) {
accessService.checkReservationMembership(principal, purchaseContextType, publicIdentifier, reservationId);
return adminReservationManager.getAudit(purchaseContextType, publicIdentifier, reservationId, principal.getName());
}
@GetMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/billing-documents")
public Result<List<BillingDocument>> getBillingDocuments(@PathVariable PurchaseContextType purchaseContextType, @PathVariable String publicIdentifier, @PathVariable String reservationId, Principal principal) {
accessService.checkReservationOwnership(principal, purchaseContextType, publicIdentifier, reservationId);
return adminReservationManager.getBillingDocuments(publicIdentifier, reservationId, principal.getName());
}
@DeleteMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/billing-document/{documentId}")
public ResponseEntity<Boolean> invalidateBillingDocument(@PathVariable PurchaseContextType purchaseContextType,
@PathVariable String publicIdentifier,
@PathVariable String reservationId,
@PathVariable long documentId,
Principal principal) {
accessService.checkBillingDocumentOwnership(principal, purchaseContextType, publicIdentifier, reservationId, documentId);
Result<Boolean> invalidateResult = adminReservationManager.invalidateBillingDocument(reservationId, documentId, principal.getName());
if(invalidateResult.isSuccess()) {
return ResponseEntity.ok(invalidateResult.getData());
} else {
return ResponseEntity.badRequest().build();
}
}
@PutMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/billing-document/{documentId}/restore")
public ResponseEntity<Boolean> restoreBillingDocument(@PathVariable PurchaseContextType purchaseContextType,
@PathVariable String publicIdentifier,
@PathVariable String reservationId,
@PathVariable long documentId,
Principal principal) {
accessService.checkReservationOwnership(principal, purchaseContextType, publicIdentifier, reservationId);
Result<Boolean> restoreResult = adminReservationManager.restoreBillingDocument(reservationId, documentId, principal.getName());
if(restoreResult.isSuccess()) {
return ResponseEntity.ok(restoreResult.getData());
} else {
return ResponseEntity.badRequest().build();
}
}
@GetMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/billing-document/{documentId}")
public ResponseEntity<Void> getBillingDocument(@PathVariable PurchaseContextType purchaseContextType,
@PathVariable String publicIdentifier,
@PathVariable String reservationId,
@PathVariable long documentId,
Principal principal,
HttpServletResponse response) {
accessService.checkReservationOwnership(principal, purchaseContextType, publicIdentifier, reservationId);
Result<Boolean> result = adminReservationManager.getSingleBillingDocumentAsPdf(purchaseContextType, publicIdentifier, reservationId, documentId, principal.getName())
.map(res -> sendPdf(res.getRight(), response, publicIdentifier, reservationId, res.getLeft()));
if(result.isSuccess()) {
return ResponseEntity.ok(null);
} else {
return ResponseEntity.notFound().build();
}
}
@GetMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}")
public Result<TicketReservationDescriptor> loadReservation(@PathVariable PurchaseContextType purchaseContextType, @PathVariable String publicIdentifier, @PathVariable String reservationId, Principal principal) {
accessService.checkReservationMembership(principal, purchaseContextType, publicIdentifier, reservationId);
return adminReservationManager.loadReservation(purchaseContextType, publicIdentifier, reservationId, principal.getName())
.map(triple -> toReservationDescriptor(reservationId, triple));
}
@GetMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/ticket/{ticketId}")
public Result<Ticket> loadTicket(@PathVariable PurchaseContextType purchaseContextType, @PathVariable String publicIdentifier, @PathVariable String reservationId, @PathVariable int ticketId, Principal principal) {
accessService.checkTicketMembership(principal, publicIdentifier, reservationId, ticketId);
return adminReservationManager.loadReservation(purchaseContextType, publicIdentifier, reservationId, principal.getName()).flatMap(triple ->
//not optimal
triple.getMiddle().stream()
.filter(t -> t.getId() == ticketId)
.findFirst()
.map(Result::success)
.orElse(Result.error(ErrorCode.custom("not_found", "not found")))
);
}
@GetMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/tickets-with-additional-data")
public List<Integer> ticketsWithAdditionalData(@PathVariable PurchaseContextType purchaseContextType,
@PathVariable String publicIdentifier,
@PathVariable String reservationId,
Principal principal) {
if(purchaseContextType != PurchaseContextType.event) {
return List.of();
}
accessService.checkReservationMembership(principal, purchaseContextType, publicIdentifier, reservationId);
return adminReservationManager.getTicketIdsWithAdditionalData(purchaseContextType, publicIdentifier, reservationId);
}
@PostMapping("/event/{publicIdentifier}/{reservationId}/remove-tickets")
public Result<RemoveResult> removeTickets(@PathVariable String publicIdentifier,
@PathVariable String reservationId,
@RequestBody RemoveTicketsModification toRemove,
Principal principal) {
accessService.checkReservationMembership(principal, PurchaseContextType.event, publicIdentifier, reservationId);
List<Integer> toRefund = toRemove.getRefundTo().entrySet().stream()
.filter(Map.Entry::getValue)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
boolean issueCreditNote = adminReservationManager.removeTickets(publicIdentifier,
reservationId,
toRemove.getTicketIds(),
toRefund,
toRemove.getNotify(),
toRemove.issueCreditNote,
principal.getName()).getData();
return Result.success(new RemoveResult(true, issueCreditNote));
}
@GetMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/payment-info")
public Result<TransactionAndPaymentInfo> getPaymentInfo(@PathVariable PurchaseContextType purchaseContextType, @PathVariable String publicIdentifier, @PathVariable String reservationId, Principal principal) {
accessService.checkReservationMembership(principal, purchaseContextType, publicIdentifier, reservationId);
return adminReservationManager.getPaymentInfo(reservationId);
}
@PostMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/cancel")
public Result<Boolean> removeReservation(@PathVariable PurchaseContextType purchaseContextType, @PathVariable String publicIdentifier,
@PathVariable String reservationId,
@RequestParam("refund") boolean refund,
@RequestParam(value = "notify", defaultValue = "false") boolean notify,
@RequestParam(value = "issueCreditNote", defaultValue = "false") boolean issueCreditNote,
Principal principal) {
accessService.checkReservationMembership(principal, purchaseContextType, publicIdentifier, reservationId);
return adminReservationManager.removeReservation(purchaseContextType, publicIdentifier, reservationId, refund, notify, issueCreditNote, principal.getName());
}
@PostMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/credit")
public Result<Boolean> creditReservation(@PathVariable PurchaseContextType purchaseContextType, @PathVariable String publicIdentifier, @PathVariable String reservationId, @RequestParam("refund") boolean refund,
@RequestParam(value = "notify", defaultValue = "false") boolean notify,
Principal principal) {
accessService.checkReservationOwnership(principal, purchaseContextType, publicIdentifier, reservationId);
adminReservationManager.creditReservation(purchaseContextType, publicIdentifier, reservationId, refund, notify, principal.getName());
return Result.success(true);
}
@PutMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/regenerate-billing-document")
public Result<Boolean> regenerateBillingDocument(@PathVariable PurchaseContextType purchaseContextType, @PathVariable String publicIdentifier, @PathVariable String reservationId, Principal principal) {
accessService.checkReservationOwnership(principal, purchaseContextType, publicIdentifier, reservationId);
return adminReservationManager.regenerateBillingDocument(purchaseContextType, publicIdentifier, reservationId, principal.getName());
}
@PostMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/refund")
public Result<Boolean> refund(@PathVariable PurchaseContextType purchaseContextType, @PathVariable String publicIdentifier, @PathVariable String reservationId, @RequestBody RefundAmount amount, Principal principal) {
accessService.checkReservationOwnership(principal, purchaseContextType, publicIdentifier, reservationId);
return adminReservationManager.refund(purchaseContextType, publicIdentifier, reservationId, new BigDecimal(amount.amount), principal.getName());
}
@GetMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/email-list")
public Result<List<LightweightMailMessage>> getEmailList(@PathVariable PurchaseContextType purchaseContextType, @PathVariable String publicIdentifier, @PathVariable String reservationId, Principal principal) {
accessService.checkReservationOwnership(principal, purchaseContextType, publicIdentifier, reservationId);
return adminReservationManager.getEmailsForReservation(purchaseContextType, publicIdentifier, reservationId, principal.getName());
}
@GetMapping("/{purchaseContextType}/{publicIdentifier}/{reservationId}/ticket/{ticketId}/full-data")
public ResponseEntity<BookingInfoTicket> loadFullTicketData(@PathVariable PurchaseContextType purchaseContextType,
@PathVariable String publicIdentifier,
@PathVariable String reservationId,
@PathVariable("ticketId") String ticketUUID,
Principal principal) {
if(purchaseContextType != PurchaseContextType.event) {
return ResponseEntity.notFound().build();
}
accessService.checkReservationOwnership(principal, purchaseContextType, publicIdentifier, reservationId);
return ResponseEntity.of(
adminReservationManager.loadFullTicketInfo(reservationId, publicIdentifier, ticketUUID)
.map(eventAndTicket -> bookingInfoTicketLoader.toBookingInfoTicket(eventAndTicket.getRight(), eventAndTicket.getLeft(), PurchaseContextFieldConfiguration.EVENT_RELATED_CONTEXTS))
);
}
private TicketReservationDescriptor toReservationDescriptor(String reservationId, Triple<TicketReservation, List<Ticket>, PurchaseContext> triple) {
List<SerializablePair<TicketCategory, List<Ticket>>> tickets = triple.getMiddle().stream().collect(Collectors.groupingBy(Ticket::getCategoryId)).entrySet().stream()
.map(entry -> SerializablePair.of(eventManager.getTicketCategoryById(entry.getKey(), triple.getRight().event().orElseThrow().getId()), entry.getValue()))
.collect(Collectors.toList());
TicketReservation reservation = triple.getLeft();
return new TicketReservationDescriptor(reservation,
ticketReservationManager.loadAdditionalInfo(reservationId),
ticketReservationManager.orderSummaryForReservationId(reservationId, triple.getRight()),
tickets,
buildSubscriptionDetails(triple.getRight(), reservation));
}
private SubscriptionWithUsageDetails buildSubscriptionDetails(PurchaseContext purchaseContext, TicketReservation reservation) {
if(purchaseContext.ofType(PurchaseContextType.subscription)) {
// load statistics
return ticketReservationManager.findSubscriptionDetails(reservation.getId()).orElse(null);
}
return null;
}
@RequiredArgsConstructor
@Getter
public static class TicketReservationDescriptor {
private final TicketReservation reservation;
private final TicketReservationAdditionalInfo additionalInfo;
private final OrderSummary orderSummary;
private final List<SerializablePair<TicketCategory, List<Ticket>>> ticketsByCategory;
private final SubscriptionWithUsageDetails subscriptionDetails;
}
@RequiredArgsConstructor
@Getter
public static class RemoveTicketsModification {
private final List<Integer> ticketIds;
private final Map<Integer, Boolean> refundTo;
private final Boolean notify;
private final Boolean issueCreditNote;
public Boolean getNotify() {
return Boolean.TRUE.equals(notify);
}
public Boolean getIssueCreditNote() {
return Boolean.TRUE.equals(issueCreditNote);
}
}
@RequiredArgsConstructor
@Getter
public static class RefundAmount {
private final String amount;
}
public static class RemoveResult {
private final boolean success;
private final boolean creditNoteGenerated;
public RemoveResult(boolean success, boolean creditNoteGenerated) {
this.success = success;
this.creditNoteGenerated = creditNoteGenerated;
}
public boolean isSuccess() {
return success;
}
public boolean isCreditNoteGenerated() {
return creditNoteGenerated;
}
}
}