1
0

add SEPA-mandate API+Controller

This commit is contained in:
Michael Hoennig
2022-10-15 15:32:32 +02:00
parent 67e850f9b2
commit e305c3c935
11 changed files with 913 additions and 47 deletions

View File

@@ -15,9 +15,12 @@ public interface HsOfficeBankAccountRepository extends Repository<HsOfficeBankAc
SELECT c FROM HsOfficeBankAccountEntity c
WHERE :holder is null
OR lower(c.holder) like lower(concat(:holder, '%'))
ORDER BY c.holder
""")
List<HsOfficeBankAccountEntity> findByOptionalHolderLike(String holder);
List<HsOfficeBankAccountEntity> findByIbanOrderByIban(String iban);
HsOfficeBankAccountEntity save(final HsOfficeBankAccountEntity entity);
int deleteByUuid(final UUID uuid);

View File

@@ -0,0 +1,151 @@
package net.hostsharing.hsadminng.hs.office.sepamandate;
import com.vladmihalcea.hibernate.type.range.Range;
import net.hostsharing.hsadminng.Mapper;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.hs.office.bankaccount.HsOfficeBankAccountEntity;
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeSepaMandatesApi;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import javax.persistence.EntityManager;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;
import java.util.function.BiConsumer;
import static net.hostsharing.hsadminng.Mapper.map;
@RestController
public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi {
@Autowired
private Context context;
@Autowired
private HsOfficeSepaMandateRepository SepaMandateRepo;
@Autowired
private EntityManager em;
@Override
@Transactional(readOnly = true)
public ResponseEntity<List<HsOfficeSepaMandateResource>> listSepaMandatesByIban(
final String currentUser,
final String assumedRoles,
final String iban) {
context.define(currentUser, assumedRoles);
final var entities = SepaMandateRepo.findSepaMandateByOptionalIban(iban);
final var resources = Mapper.mapList(entities, HsOfficeSepaMandateResource.class,
SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.ok(resources);
}
@Override
@Transactional
public ResponseEntity<HsOfficeSepaMandateResource> addSepaMandate(
final String currentUser,
final String assumedRoles,
@Valid final HsOfficeSepaMandateInsertResource body) {
context.define(currentUser, assumedRoles);
final var entityToSave = map(body, HsOfficeSepaMandateEntity.class, SEPA_MANDATE_RESOURCE_TO_ENTITY_POSTMAPPER);
entityToSave.setUuid(UUID.randomUUID());
final var saved = SepaMandateRepo.save(entityToSave);
final var uri =
MvcUriComponentsBuilder.fromController(getClass())
.path("/api/hs/office/SepaMandates/{id}")
.buildAndExpand(entityToSave.getUuid())
.toUri();
final var mapped = map(saved, HsOfficeSepaMandateResource.class,
SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.created(uri).body(mapped);
}
@Override
@Transactional(readOnly = true)
public ResponseEntity<HsOfficeSepaMandateResource> getSepaMandateByUuid(
final String currentUser,
final String assumedRoles,
final UUID sepaMandateUuid) {
context.define(currentUser, assumedRoles);
final var result = SepaMandateRepo.findByUuid(sepaMandateUuid);
if (result.isEmpty()) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(map(result.get(), HsOfficeSepaMandateResource.class,
SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER));
}
@Override
@Transactional
public ResponseEntity<Void> deleteSepaMandateByUuid(
final String currentUser,
final String assumedRoles,
final UUID sepaMandateUuid) {
context.define(currentUser, assumedRoles);
final var result = SepaMandateRepo.deleteByUuid(sepaMandateUuid);
if (result == 0) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.noContent().build();
}
@Override
@Transactional
public ResponseEntity<HsOfficeSepaMandateResource> patchSepaMandate(
final String currentUser,
final String assumedRoles,
final UUID sepaMandateUuid,
final HsOfficeSepaMandatePatchResource body) {
context.define(currentUser, assumedRoles);
final var current = SepaMandateRepo.findByUuid(sepaMandateUuid).orElseThrow();
current.setValidity(toPostgresDateRange(current.getValidity().lower(), body.getValidTo()));
final var saved = SepaMandateRepo.save(current);
final var mapped = map(saved, HsOfficeSepaMandateResource.class, SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.ok(mapped);
}
private static Range<LocalDate> toPostgresDateRange(
final LocalDate validFrom,
final LocalDate validTo) {
return validTo != null
? Range.closedOpen(validFrom, validTo.plusDays(1))
: Range.closedInfinite(validFrom);
}
final BiConsumer<HsOfficeSepaMandateEntity, HsOfficeSepaMandateResource> SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> {
resource.setDebitor(map(entity.getDebitor(), HsOfficeDebitorResource.class));
resource.setBankAccount(map(entity.getBankAccount(), HsOfficeBankAccountResource.class));
resource.setValidFrom(entity.getValidity().lower());
if (entity.getValidity().hasUpperBound()) {
resource.setValidTo(entity.getValidity().upper().minusDays(1));
}
};
final BiConsumer<HsOfficeSepaMandateInsertResource, HsOfficeSepaMandateEntity> SEPA_MANDATE_RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> {
entity.setDebitor(em.getReference(HsOfficeDebitorEntity.class, resource.getDebitorUuid()));
entity.setBankAccount(em.getReference(HsOfficeBankAccountEntity.class, resource.getBankAccountUuid()));
entity.setValidity(toPostgresDateRange(resource.getValidFrom(), resource.getValidTo()));
};
}

View File

@@ -15,8 +15,9 @@ public interface HsOfficeSepaMandateRepository extends Repository<HsOfficeSepaMa
SELECT mandate FROM HsOfficeSepaMandateEntity mandate
WHERE :iban is null
OR mandate.bankAccount.iban like concat(:iban, '%')
ORDER BY mandate.bankAccount.iban
""")
List<HsOfficeSepaMandateEntity> findSepaMandateByOptionalIBAN(String iban);
List<HsOfficeSepaMandateEntity> findSepaMandateByOptionalIban(String iban);
HsOfficeSepaMandateEntity save(final HsOfficeSepaMandateEntity entity);

View File

@@ -3,6 +3,7 @@ openapi-processor-mapping: v2
options:
package-name: net.hostsharing.hsadminng.hs.office.generated.api.v1
model-name-suffix: Resource
bean-validation: true
map:
result: org.springframework.http.ResponseEntity
@@ -10,6 +11,7 @@ map:
types:
- type: array => java.util.List
- type: string:uuid => java.util.UUID
- type: string:format => java.lang.String
paths:
/api/hs/office/partners/{partnerUUID}:
@@ -24,3 +26,5 @@ map:
null: org.openapitools.jackson.nullable.JsonNullable
/api/hs/office/debitors/{debitorUUID}:
null: org.openapitools.jackson.nullable.JsonNullable
/api/hs/office/sepamandates/{debitorUUID}:
null: org.openapitools.jackson.nullable.JsonNullable

View File

@@ -0,0 +1,60 @@
components:
schemas:
HsOfficeSepaMandate:
type: object
properties:
uuid:
type: string
format: uuid
debitor:
$ref: './hs-office-debitor-schemas.yaml#/components/schemas/HsOfficeDebitor'
bankAccount:
$ref: './hs-office-bankaccount-schemas.yaml#/components/schemas/HsOfficeBankAccount'
reference:
type: string
validFrom:
type: string
format: date
validTo:
type: string
format: date
HsOfficeSepaMandatePatch:
type: object
properties:
validTo:
type: string
format: date
additionalProperties: false
HsOfficeSepaMandateInsert:
type: object
properties:
debitorUuid:
type: string
format: uuid
nullable: false
bankAccountUuid:
type: string
format: uuid
nullable: false
reference:
type: string
nullable: false
validFrom:
type: string
format: date
nullable: false
validTo:
type: string
format: date
nullable: true
required:
- debitorUuid
- bankAccountUuid
- reference
- validFrom
additionalProperties: false

View File

@@ -0,0 +1,83 @@
get:
tags:
- hs-office-sepaMandates
description: 'Fetch a single SEPA Mandate by its uuid, if visible for the current subject.'
operationId: getSepaMandateByUuid
parameters:
- $ref: './auth.yaml#/components/parameters/currentUser'
- $ref: './auth.yaml#/components/parameters/assumedRoles'
- name: sepaMandateUUID
in: path
required: true
schema:
type: string
format: uuid
description: UUID of the SEPA Mandate to fetch.
responses:
"200":
description: OK
content:
'application/json':
schema:
$ref: './hs-office-sepamandate-schemas.yaml#/components/schemas/HsOfficeSepaMandate'
"401":
$ref: './error-responses.yaml#/components/responses/Unauthorized'
"403":
$ref: './error-responses.yaml#/components/responses/Forbidden'
patch:
tags:
- hs-office-sepaMandates
description: 'Updates a single SEPA Mandate by its uuid, if permitted for the current subject.'
operationId: patchSepaMandate
parameters:
- $ref: './auth.yaml#/components/parameters/currentUser'
- $ref: './auth.yaml#/components/parameters/assumedRoles'
- name: sepaMandateUUID
in: path
required: true
schema:
type: string
format: uuid
requestBody:
content:
'application/json':
schema:
$ref: './hs-office-sepamandate-schemas.yaml#/components/schemas/HsOfficeSepaMandatePatch'
responses:
"200":
description: OK
content:
'application/json':
schema:
$ref: './hs-office-sepamandate-schemas.yaml#/components/schemas/HsOfficeSepaMandate'
"401":
$ref: './error-responses.yaml#/components/responses/Unauthorized'
"403":
$ref: './error-responses.yaml#/components/responses/Forbidden'
delete:
tags:
- hs-office-sepaMandates
description: 'Delete a single SEPA Mandate by its uuid, if permitted for the current subject.'
operationId: deleteSepaMandateByUuid
parameters:
- $ref: './auth.yaml#/components/parameters/currentUser'
- $ref: './auth.yaml#/components/parameters/assumedRoles'
- name: sepaMandateUUID
in: path
required: true
schema:
type: string
format: uuid
description: UUID of the sepaMandate to delete.
responses:
"204":
description: No Content
"401":
$ref: './error-responses.yaml#/components/responses/Unauthorized'
"403":
$ref: './error-responses.yaml#/components/responses/Forbidden'
"404":
$ref: './error-responses.yaml#/components/responses/NotFound'

View File

@@ -0,0 +1,57 @@
get:
summary: Returns a list of (optionally filtered) SEPA Mandates.
description: Returns the list of (optionally filtered) SEPA Mandates which are visible to the current user or any of it's assumed roles.
tags:
- hs-office-sepaMandates
operationId: listSepaMandatesByIBAN
parameters:
- $ref: './auth.yaml#/components/parameters/currentUser'
- $ref: './auth.yaml#/components/parameters/assumedRoles'
- name: name
in: query
required: false
schema:
type: string
description: (Beginning of) IBAN to filter the results.
responses:
"200":
description: OK
content:
'application/json':
schema:
type: array
items:
$ref: './hs-office-sepamandate-schemas.yaml#/components/schemas/HsOfficeSepaMandate'
"401":
$ref: './error-responses.yaml#/components/responses/Unauthorized'
"403":
$ref: './error-responses.yaml#/components/responses/Forbidden'
post:
summary: Adds a new SEPA Mandate.
tags:
- hs-office-sepaMandates
operationId: addSepaMandate
parameters:
- $ref: './auth.yaml#/components/parameters/currentUser'
- $ref: './auth.yaml#/components/parameters/assumedRoles'
requestBody:
description: A JSON object describing the new SEPA-Mandate.
required: true
content:
application/json:
schema:
$ref: '/hs-office-sepamandate-schemas.yaml#/components/schemas/HsOfficeSepaMandateInsert'
responses:
"201":
description: Created
content:
'application/json':
schema:
$ref: './hs-office-sepamandate-schemas.yaml#/components/schemas/HsOfficeSepaMandate'
"401":
$ref: './error-responses.yaml#/components/responses/Unauthorized'
"403":
$ref: './error-responses.yaml#/components/responses/Forbidden'
"409":
$ref: './error-responses.yaml#/components/responses/Conflict'

View File

@@ -60,3 +60,12 @@ paths:
/api/hs/office/debitors/{debitorUUID}:
$ref: "./hs-office-debitors-with-uuid.yaml"
# SepaMandates
/api/hs/office/sepamandates:
$ref: "./hs-office-sepamandates.yaml"
/api/hs/office/sepamandates/{sepaMandateUUID}:
$ref: "./hs-office-sepamandates-with-uuid.yaml"