From f9c75433eaeab9604a9d8d521a412f8d3fb76138 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Filip=20Pit=C3=A1k?= <xpitak@fi.muni.cz>
Date: Wed, 24 Apr 2024 04:44:45 +0200
Subject: [PATCH] Add endpoints for processes and manual trigger of scheduled
 payments

---
 m2m-banking-api/transaction-api/openapi.yaml  | 168 +++++++++++++++++-
 .../controller/ProcessController.java         |  41 +++++
 .../controller/TransactionController.java     |  15 +-
 .../service/ScheduledPaymentService.java      |   8 +-
 4 files changed, 228 insertions(+), 4 deletions(-)
 create mode 100644 transaction-processor/src/main/java/cz/muni/pa165/banking/application/controller/ProcessController.java

diff --git a/m2m-banking-api/transaction-api/openapi.yaml b/m2m-banking-api/transaction-api/openapi.yaml
index 1689d72..fff2be5 100644
--- a/m2m-banking-api/transaction-api/openapi.yaml
+++ b/m2m-banking-api/transaction-api/openapi.yaml
@@ -62,6 +62,28 @@ paths:
         '404':
           description: Resource or process by UUID not found
 
+  /transaction/v1/scheduled/trigger:
+    post:
+      tags:
+        - Transaction
+      operationId: executeSchedulePayments
+      summary: Trigger scheduled payments
+      description: |
+        Manual execution of scheduled payments for a given date.
+        The input date is optional, if none is provided then the current day is used.
+      parameters:
+        - in: query
+          name: date
+          schema:
+            type: string
+            format: date
+          required: false
+      responses: 
+        '200':
+          description: Executed
+        '500':
+          description: Failure
+
   /transaction/v1/revert:
     post:
       tags:
@@ -89,6 +111,120 @@ paths:
           description: Process found by UUID but not in expected state (PROCESSED)
         '404':
           description: Resource or process by UUID not found
+  
+  /process/v1/{uuid}/status:
+    get:
+      tags:
+        - Process
+      operationId: getProcessStatus
+      summary: Process Status
+      description: |
+        Method finds an existing process for a transaction request, returning all information about it's state
+        and further information regarding errors.
+      parameters:
+        - in: path
+          name: uuid
+          required: true
+          schema:
+            type: string
+            format: uuid
+      responses:
+        '200':
+          description: OK
+          content:
+            application/json:
+              schema:
+                $ref: '#/components/schemas/ProcessStatusDto'
+        '404':
+          description: Process by UUID not found
+  
+  /processes/v1/{status}:
+    get:
+      tags: 
+        - Process
+      operationId: getProcessesOfState
+      summary: Processes in a state
+      description: Find a collection of Processes in a defined state
+      parameters:
+        - in: path
+          name: status
+          required: true
+          schema:
+            $ref: '#/components/schemas/StatusDto'
+      responses:
+        '200':
+          description: OK
+          content: 
+            application/json:
+              schema:
+                $ref: '#/components/schemas/ProcessStatusListDto'
+  
+  /processes/v1/resolve/threshold:
+    get:
+      tags:
+        - Process
+      operationId: getDefaultThreshold
+      summary: Process resolution date threshold
+      description: Get the default threshold used by the system to mark unresolved processes as failed
+      responses:
+        '200':
+          description: OK
+          content:
+            application/json:
+              schema:
+                type: string
+                format: date
+  
+  /processes/v1/resolve:
+    get:
+      tags:
+        - Process
+      operationId: getUnresolvedProcesses
+      summary: Unresolved processes
+      description: | 
+        Get a collection of unresolved processes to a given date. If no date is provided, the default threshold is used. 
+      parameters:
+        - in: query
+          name: date
+          schema:
+            type: string
+            format: date
+          required: false
+      responses: 
+        '200':
+          description: OK
+          content:
+            application/json:
+              schema:
+                $ref: '#/components/schemas/ProcessStatusListDto'
+    patch:
+      tags:
+        - Process
+      operationId: resolveProcesses
+      summary: Fail unresolved processes
+      description: |
+        Mark unresolved processes as failed. The input date marks as the threshold below which processes
+        in the state CREATED or PENDING shall be marked as resolved by failure. The provided date must be
+        before the current day. If no date is provided, the default threshold is used.
+        Method returns a collection of Process UUID's, which have been resolved.
+      parameters:
+        - in: query
+          name: date
+          schema:
+            type: string
+            format: date
+          required: false
+      responses:
+        '200':
+          description: OK
+          content:
+            application/json:
+              schema:
+                type: array
+                items: 
+                  type: string
+                  format: uuid
+
 
 components: 
   schemas: 
@@ -157,4 +293,34 @@ components:
       allOf:
         - $ref: '#/components/schemas/ProcessDto'
         - $ref: '#/components/schemas/TransactionDto'
-        
\ No newline at end of file
+    
+    StatusDetailDto:
+      type: object
+      properties: 
+        created:
+          type: string
+          format: date-time
+        status:
+          $ref: '#/components/schemas/StatusDto'
+        information:
+          type: string
+      
+    ProcessStatusDto:
+      type: object
+      properties:
+        identifier:
+          type: string
+          format: uuid
+        status:
+          $ref: '#/components/schemas/StatusDto'
+
+    ProcessStatusListDto:
+      type: object
+      properties: 
+        when:
+          type: string
+          format: date-time
+        processes:
+          type: array
+          items: 
+            $ref: '#/components/schemas/ProcessStatusDto'
\ No newline at end of file
diff --git a/transaction-processor/src/main/java/cz/muni/pa165/banking/application/controller/ProcessController.java b/transaction-processor/src/main/java/cz/muni/pa165/banking/application/controller/ProcessController.java
new file mode 100644
index 0000000..3cbe527
--- /dev/null
+++ b/transaction-processor/src/main/java/cz/muni/pa165/banking/application/controller/ProcessController.java
@@ -0,0 +1,41 @@
+package cz.muni.pa165.banking.application.controller;
+
+import cz.muni.pa165.banking.transaction.processor.ProcessApi;
+import cz.muni.pa165.banking.transaction.processor.dto.ProcessStatusDto;
+import cz.muni.pa165.banking.transaction.processor.dto.ProcessStatusListDto;
+import cz.muni.pa165.banking.transaction.processor.dto.StatusDto;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.time.LocalDate;
+import java.util.List;
+import java.util.UUID;
+
+@RestController
+public class ProcessController implements ProcessApi {
+    
+    @Override
+    public ResponseEntity<LocalDate> getDefaultThreshold() {
+        return null;
+    }
+
+    @Override
+    public ResponseEntity<ProcessStatusDto> getProcessStatus(UUID uuid) {
+        return null;
+    }
+
+    @Override
+    public ResponseEntity<ProcessStatusListDto> getProcessesOfState(StatusDto status) {
+        return null;
+    }
+
+    @Override
+    public ResponseEntity<ProcessStatusListDto> getUnresolvedProcesses(LocalDate date) {
+        return null;
+    }
+
+    @Override
+    public ResponseEntity<List<UUID>> resolveProcesses(LocalDate date) {
+        return null;
+    }
+}
diff --git a/transaction-processor/src/main/java/cz/muni/pa165/banking/application/controller/TransactionController.java b/transaction-processor/src/main/java/cz/muni/pa165/banking/application/controller/TransactionController.java
index af908fa..7cf6096 100644
--- a/transaction-processor/src/main/java/cz/muni/pa165/banking/application/controller/TransactionController.java
+++ b/transaction-processor/src/main/java/cz/muni/pa165/banking/application/controller/TransactionController.java
@@ -1,6 +1,7 @@
 package cz.muni.pa165.banking.application.controller;
 
 import cz.muni.pa165.banking.application.facade.TransactionFacade;
+import cz.muni.pa165.banking.application.service.ScheduledPaymentService;
 import cz.muni.pa165.banking.transaction.processor.TransactionApi;
 import cz.muni.pa165.banking.transaction.processor.dto.ProcessDetailDto;
 import cz.muni.pa165.banking.transaction.processor.dto.ProcessDto;
@@ -8,15 +9,19 @@ import cz.muni.pa165.banking.transaction.processor.dto.TransactionDto;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
+import java.time.LocalDate;
 import java.util.UUID;
 
 @RestController
 public class TransactionController implements TransactionApi {
     
     private final TransactionFacade facade;
+    
+    private final ScheduledPaymentService scheduledPaymentService;
 
-    public TransactionController(TransactionFacade facade) {
+    public TransactionController(TransactionFacade facade, ScheduledPaymentService scheduledPaymentService) {
         this.facade = facade;
+        this.scheduledPaymentService = scheduledPaymentService;
     }
 
 
@@ -38,4 +43,12 @@ public class TransactionController implements TransactionApi {
         return ResponseEntity.ok(revertingProcess);
     }
 
+    @Override
+    public ResponseEntity<Void> executeSchedulePayments(LocalDate date) {
+        if (date == null) {
+            date = LocalDate.now();
+        }
+        scheduledPaymentService.executeScheduledPayments(date);
+        return ResponseEntity.ok(null);
+    }
 }
diff --git a/transaction-processor/src/main/java/cz/muni/pa165/banking/application/service/ScheduledPaymentService.java b/transaction-processor/src/main/java/cz/muni/pa165/banking/application/service/ScheduledPaymentService.java
index eee4667..958ae04 100644
--- a/transaction-processor/src/main/java/cz/muni/pa165/banking/application/service/ScheduledPaymentService.java
+++ b/transaction-processor/src/main/java/cz/muni/pa165/banking/application/service/ScheduledPaymentService.java
@@ -47,9 +47,13 @@ public class ScheduledPaymentService {
     }
 
     @Scheduled(cron = "${scheduled-payments.cron.expression}")
-    public void executeScheduledPayments() {
+    public void autoExecute() {
         LocalDate now = LocalDate.now();
-        ResponseEntity<ScheduledPaymentsDto> response = accountApi.getScheduledPaymentsOf(now);
+        executeScheduledPayments(now);
+    }
+    
+    public void executeScheduledPayments(LocalDate date) {
+        ResponseEntity<ScheduledPaymentsDto> response = accountApi.getScheduledPaymentsOf(date);
         if (!response.getStatusCode().is2xxSuccessful()) {
             throw new ServerError("Call to Account Management service unsuccessful.");
         }
-- 
GitLab