|
| 1 | +package org.apache.fineract.portfolio.overdraftproduct.api; |
| 2 | + |
| 3 | +import jakarta.validation.Valid; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Optional; |
| 6 | +import lombok.RequiredArgsConstructor; |
| 7 | +import org.apache.fineract.portfolio.overdraftproduct.domain.OverdraftProductStatus; |
| 8 | +import org.apache.fineract.portfolio.overdraftproduct.serialization.OverdraftFeeCommand; |
| 9 | +import org.apache.fineract.portfolio.overdraftproduct.serialization.OverdraftProductAccountingCommand; |
| 10 | +import org.apache.fineract.portfolio.overdraftproduct.serialization.OverdraftProductCommand; |
| 11 | +import org.apache.fineract.portfolio.overdraftproduct.serialization.OverdraftProductData; |
| 12 | +import org.apache.fineract.portfolio.overdraftproduct.serialization.OverdraftProductLifecycleCommand; |
| 13 | +import org.apache.fineract.portfolio.overdraftproduct.serialization.OverdraftProductValidationResult; |
| 14 | +import org.apache.fineract.portfolio.overdraftproduct.serialization.OverdraftUsageRuleCommand; |
| 15 | +import org.apache.fineract.portfolio.overdraftproduct.service.OverdraftProductReadPlatformService; |
| 16 | +import org.apache.fineract.portfolio.overdraftproduct.service.OverdraftProductWritePlatformService; |
| 17 | +import org.springframework.http.HttpStatus; |
| 18 | +import org.springframework.http.ResponseEntity; |
| 19 | +import org.springframework.security.access.prepost.PreAuthorize; |
| 20 | +import org.springframework.validation.annotation.Validated; |
| 21 | +import org.springframework.web.bind.annotation.GetMapping; |
| 22 | +import org.springframework.web.bind.annotation.PathVariable; |
| 23 | +import org.springframework.web.bind.annotation.PostMapping; |
| 24 | +import org.springframework.web.bind.annotation.PutMapping; |
| 25 | +import org.springframework.web.bind.annotation.RequestBody; |
| 26 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 27 | +import org.springframework.web.bind.annotation.RequestParam; |
| 28 | +import org.springframework.web.bind.annotation.RestController; |
| 29 | + |
| 30 | +@RestController |
| 31 | +@RequiredArgsConstructor |
| 32 | +@RequestMapping("/overdraft-products") |
| 33 | +@Validated |
| 34 | +public class OverdraftProductApiResource { |
| 35 | + |
| 36 | + private final OverdraftProductWritePlatformService writeService; |
| 37 | + private final OverdraftProductReadPlatformService readService; |
| 38 | + |
| 39 | + @PostMapping |
| 40 | + @PreAuthorize("hasAuthority('OVERDRAFT_PRODUCT_ADMIN')") |
| 41 | + public ResponseEntity<OverdraftProductData> create(@Valid @RequestBody OverdraftProductCommand command) { |
| 42 | + OverdraftProductData data = writeService.createProduct(command); |
| 43 | + return new ResponseEntity<>(data, HttpStatus.CREATED); |
| 44 | + } |
| 45 | + |
| 46 | + @PutMapping("/{id}") |
| 47 | + @PreAuthorize("hasAuthority('OVERDRAFT_PRODUCT_ADMIN')") |
| 48 | + public ResponseEntity<OverdraftProductData> update(@PathVariable Long id, |
| 49 | + @Valid @RequestBody OverdraftProductCommand command) { |
| 50 | + return ResponseEntity.ok(writeService.updateProduct(id, command)); |
| 51 | + } |
| 52 | + |
| 53 | + @PostMapping("/{id}:activate") |
| 54 | + @PreAuthorize("hasAuthority('OVERDRAFT_PRODUCT_ADMIN')") |
| 55 | + public ResponseEntity<OverdraftProductData> activate(@PathVariable Long id, |
| 56 | + @RequestBody(required = false) OverdraftProductLifecycleCommand command) { |
| 57 | + return ResponseEntity.ok(writeService.activateProduct(id, command)); |
| 58 | + } |
| 59 | + |
| 60 | + @PostMapping("/{id}:retire") |
| 61 | + @PreAuthorize("hasAuthority('OVERDRAFT_PRODUCT_ADMIN')") |
| 62 | + public ResponseEntity<OverdraftProductData> retire(@PathVariable Long id, |
| 63 | + @RequestBody(required = false) OverdraftProductLifecycleCommand command) { |
| 64 | + return ResponseEntity.ok(writeService.retireProduct(id, command)); |
| 65 | + } |
| 66 | + |
| 67 | + @GetMapping |
| 68 | + @PreAuthorize("hasAnyAuthority('OVERDRAFT_PRODUCT_ADMIN','OVERDRAFT_PRODUCT_VIEW')") |
| 69 | + public ResponseEntity<List<OverdraftProductData>> retrieveAll(@RequestParam(value = "status", required = false) String status, |
| 70 | + @RequestParam(value = "currency", required = false) String currency) { |
| 71 | + Optional<OverdraftProductStatus> statusFilter = Optional.ofNullable(status).map(OverdraftProductStatus::fromValue); |
| 72 | + Optional<String> currencyFilter = Optional.ofNullable(currency); |
| 73 | + return ResponseEntity.ok(readService.retrieveAll(statusFilter, currencyFilter)); |
| 74 | + } |
| 75 | + |
| 76 | + @GetMapping("/{id}") |
| 77 | + @PreAuthorize("hasAnyAuthority('OVERDRAFT_PRODUCT_ADMIN','OVERDRAFT_PRODUCT_VIEW')") |
| 78 | + public ResponseEntity<OverdraftProductData> retrieveOne(@PathVariable Long id) { |
| 79 | + return ResponseEntity.ok(readService.retrieveOne(id)); |
| 80 | + } |
| 81 | + |
| 82 | + @GetMapping("/{id}/validate") |
| 83 | + @PreAuthorize("hasAnyAuthority('OVERDRAFT_PRODUCT_ADMIN','OVERDRAFT_PRODUCT_VIEW')") |
| 84 | + public ResponseEntity<OverdraftProductValidationResult> validate(@PathVariable Long id) { |
| 85 | + return ResponseEntity.ok(readService.validate(id)); |
| 86 | + } |
| 87 | + |
| 88 | + @PostMapping("/{id}/fees") |
| 89 | + @PreAuthorize("hasAuthority('OVERDRAFT_PRODUCT_ADMIN')") |
| 90 | + public ResponseEntity<OverdraftProductData> updateFees(@PathVariable Long id, |
| 91 | + @Valid @RequestBody List<OverdraftFeeCommand> fees) { |
| 92 | + return ResponseEntity.ok(writeService.updateFees(id, fees)); |
| 93 | + } |
| 94 | + |
| 95 | + @PostMapping("/{id}/usage-rules") |
| 96 | + @PreAuthorize("hasAuthority('OVERDRAFT_PRODUCT_ADMIN')") |
| 97 | + public ResponseEntity<OverdraftProductData> updateUsageRules(@PathVariable Long id, |
| 98 | + @Valid @RequestBody List<OverdraftUsageRuleCommand> rules) { |
| 99 | + return ResponseEntity.ok(writeService.updateUsageRules(id, rules)); |
| 100 | + } |
| 101 | + |
| 102 | + @PostMapping("/{id}/accounting") |
| 103 | + @PreAuthorize("hasAnyAuthority('OVERDRAFT_PRODUCT_ADMIN','FINANCE_ADMIN')") |
| 104 | + public ResponseEntity<OverdraftProductData> updateAccounting(@PathVariable Long id, |
| 105 | + @Valid @RequestBody OverdraftProductAccountingCommand command) { |
| 106 | + return ResponseEntity.ok(writeService.updateAccounting(id, command)); |
| 107 | + } |
| 108 | +} |
0 commit comments