adamsaghy commented on code in PR #4222: URL: https://github.com/apache/fineract/pull/4222#discussion_r1886741181
########## fineract-loan/src/main/java/org/apache/fineract/portfolio/interestpauses/service/InterestPauseReadPlatformServiceImpl.java: ########## @@ -0,0 +1,156 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.portfolio.interestpauses.service; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.util.List; +import java.util.Locale; +import lombok.RequiredArgsConstructor; +import org.apache.fineract.infrastructure.core.domain.ExternalId; +import org.apache.fineract.infrastructure.core.service.DateUtils; +import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext; +import org.apache.fineract.portfolio.interestpauses.data.InterestPauseData; +import org.apache.fineract.portfolio.loanaccount.data.LoanTermVariationsData; +import org.apache.fineract.portfolio.loanaccount.domain.Loan; +import org.apache.fineract.portfolio.loanaccount.domain.LoanRepository; +import org.apache.fineract.portfolio.loanaccount.domain.LoanTermVariationType; +import org.apache.fineract.portfolio.loanaccount.domain.LoanTermVariations; +import org.apache.fineract.portfolio.loanaccount.rescheduleloan.domain.LoanTermVariationsRepository; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class InterestPauseReadPlatformServiceImpl implements InterestPauseReadPlatformService { + + private final PlatformSecurityContext context; + private final LoanTermVariationsRepository loanTermVariationsRepository; + private final LoanRepository loanRepository; + + @Override + @Transactional(readOnly = true) + public List<InterestPauseData> retrieveInterestPauses(Long loanId) { + this.context.authenticatedUser(); + + List<LoanTermVariationsData> variations = this.loanTermVariationsRepository.findLoanTermVariationsByLoanIdAndTermType(loanId, + LoanTermVariationType.INTEREST_PAUSE.getValue()); + + return mapToInterestPauseData(variations); + } + + @Override + @Transactional(readOnly = true) + public List<InterestPauseData> retrieveInterestPausesByExternalId(String loanExternalId) { + this.context.authenticatedUser(); + + List<LoanTermVariationsData> variations = this.loanTermVariationsRepository.findLoanTermVariationsByExternalLoanIdAndTermType( + new ExternalId(loanExternalId), LoanTermVariationType.INTEREST_PAUSE.getValue()); + + return mapToInterestPauseData(variations); + } + + @Override + @Transactional + public Long createInterestPause(ExternalId loanExternalId, String startDate, String endDate, String dateFormat, String locale) { + this.context.authenticatedUser(); + + Long loanId = loanRepository.findIdByExternalId(loanExternalId); + if (loanId == null) { + throw new IllegalArgumentException("Loan not found with External ID: " + loanExternalId); + } + + Loan loan = loanRepository.findById(loanId).orElseThrow(() -> new IllegalArgumentException("Loan not found with ID: " + loanId)); + + validateInterestPauseDates(loan, startDate, endDate, dateFormat, locale); + + LocalDate startLocalDate = parseAndValidateDate(startDate, dateFormat, locale); + LocalDate endLocalDate = parseAndValidateDate(endDate, dateFormat, locale); + + LoanTermVariations variation = new LoanTermVariations(LoanTermVariationType.INTEREST_PAUSE.getValue(), startLocalDate, null, + endLocalDate, false, loan); + + LoanTermVariations savedVariation = loanTermVariationsRepository.save(variation); + loanTermVariationsRepository.flush(); + + return savedVariation.getId(); + } + + @Override + @Transactional + public Long createInterestPauseByLoanId(Long loanId, String startDate, String endDate, String dateFormat, String locale) { + this.context.authenticatedUser(); + + final Loan loan = loanRepository.findById(loanId) + .orElseThrow(() -> new IllegalArgumentException("Loan not found with ID: " + loanId)); + + validateInterestPauseDates(loan, startDate, endDate, dateFormat, locale); + + LocalDate startLocalDate = parseAndValidateDate(startDate, dateFormat, locale); + LocalDate endLocalDate = parseAndValidateDate(endDate, dateFormat, locale); + + LoanTermVariations variation = new LoanTermVariations(LoanTermVariationType.INTEREST_PAUSE.getValue(), startLocalDate, null, + endLocalDate, false, loan); + + LoanTermVariations savedVariation = loanTermVariationsRepository.save(variation); + loanTermVariationsRepository.flush(); + + return savedVariation.getId(); + } + + private void validateInterestPauseDates(Loan loan, String startDateStr, String endDateStr, String dateFormat, String locale) { + if (startDateStr == null || endDateStr == null) { + throw new IllegalArgumentException("Both `startDate` and `endDate` parameters are mandatory."); Review Comment: ``` validateOrThrow("interest-pause", baseDataValidator -> {baseDataValidator.reset().parameter(<parameter_name>).value(<parameter_value>).notNull(); ``` might be better here! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
