Skip to content

Commit

Permalink
fixup: connections and tests between fees and payments
Browse files Browse the repository at this point in the history
  • Loading branch information
hei-ryan committed Mar 17, 2022
1 parent 88467cb commit 57d214e
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 111 deletions.
11 changes: 3 additions & 8 deletions doc/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,6 @@ paths:
$ref: '#/components/responses/429'
'500':
$ref: '#/components/responses/500'

/students/{student_id}/fees/{fee_id}/payments:
get:
tags:
Expand Down Expand Up @@ -723,24 +722,20 @@ paths:
$ref: '#/components/responses/429'
'500':
$ref: '#/components/responses/500'
/fees/{fee_id}/payments:
post:
tags:
- Paying
summary: Create student payments
parameters:
- name: student_id
in: path
required: true
schema:
type: string
- name: fee_id
in: path
required: true
schema:
type: string
operationId: createStudentPayments
requestBody:
description: Student fees to create
description: Student payments to create
required: true
content:
application/json:
Expand All @@ -767,7 +762,7 @@ paths:
$ref: '#/components/responses/429'
'500':
$ref: '#/components/responses/500'
/students/{student_id}/fees/payments:
/students/{student_id}/payments:
get:
tags:
- Paying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ public class PaymentController {
private final PaymentService paymentService;
private final PaymentMapper paymentMapper;

@PostMapping("/students/{studentId}/fees/{feeId}/payments")
public List<Payment> createPayments(
@PathVariable String studentId,
@PathVariable String feeId,
@PostMapping("/fees/{feeId}/payments")
public List<Payment> createPayments(@PathVariable String feeId,
@RequestBody List<CreatePayment> toCreate) {
return paymentService
.saveAll(studentId, feeId, toCreate.stream()
.saveAll(feeId, toCreate.stream()
.map(paymentMapper::toDomainPayment)
.collect(toUnmodifiableList()))
.stream()
Expand All @@ -36,14 +34,13 @@ public List<Payment> createPayments(
}

@GetMapping("/students/{studentId}/fees/{feeId}/payments")
public List<Payment> getFeePaymentsByStudentId(
@PathVariable String studentId, @PathVariable String feeId) {
return paymentService.getByStudentIdAndFeeId(studentId, feeId).stream()
public List<Payment> getFeePaymentsByStudentId(@PathVariable String feeId) {
return paymentService.getByFeeId(feeId).stream()
.map(paymentMapper::toRestPayment)
.collect(toUnmodifiableList());
}

@GetMapping("/students/{studentId}/fees/payments")
@GetMapping("/students/{studentId}/payments")
public List<Payment> getPaymentsByStudentId(@PathVariable String studentId) {
return paymentService.getByStudentId(studentId).stream()
.map(paymentMapper::toRestPayment)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package school.hei.haapi.endpoint.rest.mapper;

import java.util.Objects;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import school.hei.haapi.endpoint.rest.model.CreateFee;
import school.hei.haapi.endpoint.rest.model.Fee;
import school.hei.haapi.model.exception.BadRequestException;
import school.hei.haapi.service.UserService;

@Component
@AllArgsConstructor
public class FeeMapper {
private final UserService userService;

public Fee toRestFee(school.hei.haapi.model.Fee fee) {
return new Fee()
.id(fee.getId())
.studentId(fee.getStudentId())
.studentId(fee.getStudent().getId())
.status(fee.getStatus())
.type(fee.getType())
.totalAmount(fee.getTotalAmount())
Expand All @@ -26,7 +31,7 @@ public school.hei.haapi.model.Fee toDomainFee(CreateFee createFee) {
throw new BadRequestException("Total amount is mandatory");
}
return school.hei.haapi.model.Fee.builder()
.studentId(createFee.getStudentId())
.student(userService.getById(createFee.getStudentId()))
.type(toDomainFeeType(Objects.requireNonNull(createFee.getType())))
.totalAmount(createFee.getTotalAmount())
.comment(createFee.getComment())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
package school.hei.haapi.endpoint.rest.mapper;

import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import school.hei.haapi.endpoint.rest.model.CreatePayment;
import school.hei.haapi.endpoint.rest.model.Payment;
import school.hei.haapi.model.exception.BadRequestException;
import school.hei.haapi.service.FeeService;

@Component
@AllArgsConstructor
public class PaymentMapper {
private final FeeService feeService;

public Payment toRestPayment(school.hei.haapi.model.Payment payment) {
return new Payment()
.id(payment.getId())
.feeId(payment.getFeeId())
.feeId(payment.getFee().getId())
.type(payment.getType())
.amount(payment.getAmount())
.comment(payment.getComment())
.creationDatetime(payment.getCreationDatetime());
}

public school.hei.haapi.model.Payment toDomainPayment(CreatePayment createPayment) {
if (createPayment.getAmount() == null) {
throw new BadRequestException("Amount is mandatory");
}
return school.hei.haapi.model.Payment.builder()
.feeId(createPayment.getFeeId())
.fee(feeService.getById(createPayment.getFeeId()))
.type(toDomainPaymentType(createPayment.getType()))
.amount(createPayment.getAmount())
.comment(createPayment.getComment())
.build();
}

private Payment.TypeEnum toDomainPaymentType(CreatePayment.TypeEnum createPaymentType) {
String feeType = createPaymentType.toString();
if (feeType.equals(Payment.TypeEnum.CASH.toString())) {
String paymentType = createPaymentType.toString();
if (paymentType.equals(Payment.TypeEnum.CASH.toString())) {
return Payment.TypeEnum.CASH;
} else if (feeType.equals(Payment.TypeEnum.SCOLARSHIP.toString())) {
} else if (paymentType.equals(Payment.TypeEnum.SCOLARSHIP.toString())) {
return Payment.TypeEnum.SCOLARSHIP;
} else if (feeType.equals(Payment.TypeEnum.FIX.toString())) {
} else if (paymentType.equals(Payment.TypeEnum.FIX.toString())) {
return Payment.TypeEnum.FIX;
}
throw new BadRequestException("Payment type must be valid");
throw new BadRequestException("Unexpected paymentFee: " + paymentType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ protected void configure(HttpSecurity http) throws Exception {
.antMatchers(GET, "/students/*/fees/*").hasAnyRole(MANAGER.getRole())
.antMatchers(POST, "/students/*/fees").hasAnyRole(MANAGER.getRole())
.requestMatchers(new SelfMatcher(GET, "/students/*/fees")).hasAnyRole(STUDENT.getRole())
.antMatchers(GET, "/students/*/fees").hasAnyRole(MANAGER.getRole())
.requestMatchers(new SelfMatcher(GET, "/students/*/fees/*/payments")).hasAnyRole(STUDENT.getRole())
.antMatchers(GET, "/students/*/fees/*/payments").hasAnyRole(MANAGER.getRole())
.antMatchers(POST, "/students/*/fees/*/payments").hasAnyRole(MANAGER.getRole())
.antMatchers(GET, "/students/*/fees").hasAnyRole(MANAGER.getRole())
.antMatchers(POST, "/fees/*/payments").hasAnyRole(MANAGER.getRole())
.requestMatchers(new SelfMatcher(GET, "/students/*/payments")).hasAnyRole(STUDENT.getRole())
.antMatchers(GET, "/students/*/payments").hasAnyRole(MANAGER.getRole())
.requestMatchers(new SelfMatcher(GET, "/students/*")).hasAnyRole(STUDENT.getRole())
.antMatchers(GET, "/students/*").hasAnyRole(TEACHER.getRole(), MANAGER.getRole())
.antMatchers(PUT, "/students/**").hasAnyRole(MANAGER.getRole())
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/school/hei/haapi/model/Fee.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import java.io.Serializable;
import java.time.Instant;
import java.util.List;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Enumerated;
import javax.persistence.EnumType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -38,11 +42,13 @@ public class Fee implements Serializable {
@GeneratedValue(strategy = IDENTITY)
private String id;

@Column(name = "user_id")
private String studentId;
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private User student;

@Type(type = "pgsql_enum")
@Enumerated(EnumType.STRING)
@Transient
private school.hei.haapi.endpoint.rest.model.Fee.StatusEnum status;

@Type(type = "pgsql_enum")
Expand All @@ -51,13 +57,17 @@ public class Fee implements Serializable {

private int totalAmount;

@Transient
private int remainingAmount;

private String comment;
@CreationTimestamp private Instant creationDatetime;

private Instant dueDatetime;

@OneToMany(mappedBy = "fee")
private List<Payment> paymentList;

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -70,7 +80,7 @@ public boolean equals(Object o) {
return totalAmount == fee.totalAmount
&& remainingAmount == fee.remainingAmount
&& Objects.equals(id, fee.id)
&& Objects.equals(studentId, fee.studentId)
&& Objects.equals(student.getId(), fee.student.getId())
&& status == fee.status
&& type == fee.type
&& Objects.equals(creationDatetime, fee.creationDatetime)
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/school/hei/haapi/model/Payment.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -37,7 +39,9 @@ public class Payment implements Serializable {
@GeneratedValue(strategy = IDENTITY)
private String id;

private String feeId;
@ManyToOne
@JoinColumn(name = "fee_id", nullable = false)
private Fee fee;

@Type(type = "pgsql_enum")
@Enumerated(EnumType.STRING)
Expand All @@ -58,7 +62,7 @@ public boolean equals(Object o) {
Payment payment = (Payment) o;
return amount == payment.amount
&& Objects.equals(id, payment.id)
&& Objects.equals(feeId, payment.feeId)
&& Objects.equals(fee.getId(), payment.getFee().getId())
&& type == payment.type
&& creationDatetime.compareTo(payment.creationDatetime) == 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void accept(List<Fee> fees) {

@Override public void accept(Fee fee) {
Set<String> violationMessages = new HashSet<>();
if (fee.getStudentId() == null) {
if (fee.getStudent() == null) {
violationMessages.add("Student is mandatory");
}
if (fee.getDueDatetime() == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package school.hei.haapi.model.validator;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import school.hei.haapi.model.Payment;
import school.hei.haapi.model.exception.BadRequestException;

@Component
@AllArgsConstructor
public class PaymentValidator implements Consumer<Payment> {

public void accept(List<Payment> payments) {
payments.forEach(this::accept);
}

@Override public void accept(Payment payment) {
Set<String> violationMessages = new HashSet<>();
if (payment.getFee() == null) {
violationMessages.add("Fee is mandatory");
}
if (payment.getAmount() < 0) {
violationMessages.add("Amount must be positive");
}
if (!violationMessages.isEmpty()) {
String formattedViolationMessages = violationMessages.stream()
.map(String::toString)
.collect(Collectors.joining(". "));
throw new BadRequestException(formattedViolationMessages);
}
}
}

0 comments on commit 57d214e

Please sign in to comment.