1

I created a method in my API that receives a list of objects as it's parameter to add and validate multiple objects.

But, I'm thinking about potential validation errors, what are the best approaches to make the response includes errors for any object that errors can occurs in it.

for example:

If 3 of the objects doesn't have phone numbers or one object doesn't have an SSN, how can I trigger these errors and display them in my response?

myDTO:

public class InvoiceDTO {

  @NotNull(message = "PersonId can't be null")
  @PersonId
  private String personId;

  @NotNull(message = "invoiceDate can't be null")
  // @DateTimeFormat(iso = DateTimeFormatter.ofPattern("yyyy-MM-dd"))
  @PastOrPresent
  @JsonFormat(pattern = "yyyy-MM-dd")
  private LocalDate invoiceDate;

  @NotNull(message = "invoiceNumber can't be null")
  private String invoiceNumber;

  @NotNull(message = "phone number can't be null")
  private Long phoneNumber;

  @NotNull(message = "invoiceAmount can't be null")
  private Double invoiceAmount; //how much does the client ows the org

  @NotNull(message = "invoiceNumber can't be null")
  private String accountNumber;

  private int msdin; //Type of subscription
}

Controller:

 @PostMapping("/invoices")
  private ResponseEntity<Response> addNewInvoices(@RequestBody @Valid List<InvoiceDTO> invoiceDTO) {

    //Todo find a way to through exceptions for every object if validations errors occurs
    ResponseEntity<Response> response = null;
    for (InvoiceDTO invoices : invoiceDTO) {
      Optional<Organization> organization = this.organizationService.getOrganizationFromRequest();
      Optional<Madeen> madeen = madeenRepository.findByPersonId(invoices.getPersonId());


      if (!madeen.isPresent()) {
        Madeen newMadeen = new Madeen();
        newMadeen.setCreatedDate(LocalDate.now());
        newMadeen.setPersonId(invoices.getPersonId());
        newMadeen.setStatus(Status.NOT_MADEEN);
        newMadeen.setMadeenTimeTracker(null);
        newMadeen.setOrganization(organization.get());
        madeenRepository.save(newMadeen);
      }

      log.info("Add new Invoice : {}", invoiceDTO);
      String tcn = UUID.randomUUID().toString();
      AuditLog auditLog = new AuditLog();
      auditLog.setTcn(tcn);
      auditLog.setRequestBody(invoiceDTO.toString());
      response = null;


      //Add the new amount of the invoice to an existing debts
      Optional<Organization> organization1 = this.organizationService.getOrganizationFromRequest();
      Optional<Debts> debts = debtsRepository.findDebtsByPersonIdAndOrganization_id(invoices.getPersonId(), organization1.get().getId());
      Optional<Madeen> madeenOptional = madeenRepository.findByPersonId(invoices.getPersonId());


      if (!debts.isPresent()) {
        Debts newDebt = new Debts();
        newDebt.setPersonId(invoices.getPersonId());
        newDebt.setCreatedDate(LocalDate.now());
        newDebt.setUpdatedDate(invoices.getInvoiceDate());
        newDebt.setDebtAmount(invoices.getInvoiceAmount());
        newDebt.setOrganization(organization1.get());
        newDebt.setMadeenId(madeenOptional.get());
        debtsRepository.save(newDebt);
      }

      Optional<Debts> updatedDebts = debtsRepository.findDebtsByPersonIdAndOrganization_id(invoices.getPersonId(), organization1.get().getId());
      Madeen madeenCheck = madeenOptional.get();
      Debts existingDebts = updatedDebts.get();

      if (debts.isPresent()) {
        existingDebts.setDebtAmount(existingDebts.getDebtAmount() + invoices.getInvoiceAmount());
        existingDebts.setUpdatedDate(invoices.getInvoiceDate());
        debtsRepository.save(existingDebts);
      }

      if (existingDebts.getDebtAmount() < -1) {
        madeenCheck.setStatus(Status.STUMBLED);
        madeenRepository.save(madeenCheck);
      }
      //Calculate the amount of debts
      List<Debts> findAllDebts = debtsRepository.findDebtsByPersonId(invoices.getPersonId());
      double totalDebts = 0; //find how much is it the totalDebts among all organization;
      for (Debts item : findAllDebts) {
        totalDebts = totalDebts + item.getDebtAmount();
      }

      if (totalDebts <= -750 && madeenCheck.getMadeenTimeTracker() == null) {
        madeenCheck.setMadeenTimeTracker(LocalDate.now());
        madeenRepository.save(madeenCheck);
      }

      if (totalDebts > -750) {
        madeenCheck.setMadeenTimeTracker(null);
        madeenRepository.save(madeenCheck);
      }

      if (madeenCheck.getMadeenTimeTracker() != null) {
        LocalDate sixtyDaysFromLastInvoice = madeenCheck.getMadeenTimeTracker().plus(Period.ofDays(60));
        if (totalDebts <= -750 && LocalDate.now().isAfter(sixtyDaysFromLastInvoice)) {
          madeenCheck.setStatus(Status.MADEEN);
          madeenRepository.save(madeenCheck);
        }
      }

      if (totalDebts >= 0) {
        madeenCheck.setStatus(Status.NOT_MADEEN);
        madeenRepository.save(madeenCheck);

      }
    }
    return response;
  }

DarkBot
  • 43
  • 7

0 Answers0