|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.fineract.portfolio.loanaccount.api; |
| 20 | + |
| 21 | +import static org.apache.fineract.infrastructure.core.domain.AuditableFieldsConstants.CREATED_BY; |
| 22 | +import static org.apache.fineract.infrastructure.core.domain.AuditableFieldsConstants.CREATED_DATE; |
| 23 | +import static org.apache.fineract.infrastructure.core.domain.AuditableFieldsConstants.LAST_MODIFIED_BY; |
| 24 | +import static org.apache.fineract.infrastructure.core.domain.AuditableFieldsConstants.LAST_MODIFIED_DATE; |
| 25 | + |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Map; |
| 28 | +import javax.ws.rs.Consumes; |
| 29 | +import javax.ws.rs.GET; |
| 30 | +import javax.ws.rs.Path; |
| 31 | +import javax.ws.rs.PathParam; |
| 32 | +import javax.ws.rs.Produces; |
| 33 | +import javax.ws.rs.core.Context; |
| 34 | +import javax.ws.rs.core.MediaType; |
| 35 | +import javax.ws.rs.core.UriInfo; |
| 36 | +import lombok.RequiredArgsConstructor; |
| 37 | +import lombok.extern.slf4j.Slf4j; |
| 38 | +import org.apache.fineract.infrastructure.core.api.ApiRequestParameterHelper; |
| 39 | +import org.apache.fineract.infrastructure.core.serialization.ApiRequestJsonSerializationSettings; |
| 40 | +import org.apache.fineract.infrastructure.core.serialization.ToApiJsonSerializer; |
| 41 | +import org.apache.fineract.portfolio.loanaccount.domain.Loan; |
| 42 | +import org.apache.fineract.portfolio.loanaccount.domain.LoanRepositoryWrapper; |
| 43 | +import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction; |
| 44 | +import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionRepository; |
| 45 | +import org.springframework.beans.factory.InitializingBean; |
| 46 | +import org.springframework.stereotype.Component; |
| 47 | + |
| 48 | +//@Profile("test") |
| 49 | +@Component |
| 50 | +@Path("/internal/loan") |
| 51 | +@RequiredArgsConstructor |
| 52 | +@Slf4j |
| 53 | +public class InternalLoanInformationApiResource implements InitializingBean { |
| 54 | + |
| 55 | + private final LoanRepositoryWrapper loanRepositoryWrapper; |
| 56 | + private final LoanTransactionRepository loanTransactionRepository; |
| 57 | + private final ToApiJsonSerializer<Map> toApiJsonSerializer; |
| 58 | + private final ApiRequestParameterHelper apiRequestParameterHelper; |
| 59 | + |
| 60 | + @Override |
| 61 | + public void afterPropertiesSet() { |
| 62 | + log.warn("------------------------------------------------------------"); |
| 63 | + log.warn(" "); |
| 64 | + log.warn("DO NOT USE THIS IN PRODUCTION!"); |
| 65 | + log.warn("Internal loan services mode is enabled"); |
| 66 | + log.warn("DO NOT USE THIS IN PRODUCTION!"); |
| 67 | + log.warn(" "); |
| 68 | + log.warn("------------------------------------------------------------"); |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + @GET |
| 73 | + @Path("{loanId}/audit") |
| 74 | + @Consumes({ MediaType.APPLICATION_JSON }) |
| 75 | + @Produces({ MediaType.APPLICATION_JSON }) |
| 76 | + public String getLoanAuditFields(@Context final UriInfo uriInfo, @PathParam("loanId") Long loanId) { |
| 77 | + log.warn("------------------------------------------------------------"); |
| 78 | + log.warn(" "); |
| 79 | + log.warn("Fetching loan with {}", loanId); |
| 80 | + log.warn(" "); |
| 81 | + log.warn("------------------------------------------------------------"); |
| 82 | + |
| 83 | + final Loan loan = loanRepositoryWrapper.findOneWithNotFoundDetection(loanId); |
| 84 | + Map<String, Object> auditFields = new HashMap<>( |
| 85 | + Map.of(CREATED_BY, loan.getCreatedBy().orElse(null), CREATED_DATE, loan.getCreatedDate().orElse(null), LAST_MODIFIED_BY, |
| 86 | + loan.getLastModifiedBy().orElse(null), LAST_MODIFIED_DATE, loan.getLastModifiedDate().orElse(null))); |
| 87 | + final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); |
| 88 | + return this.toApiJsonSerializer.serialize(settings, auditFields); |
| 89 | + } |
| 90 | + |
| 91 | + @GET |
| 92 | + @Path("{loanId}/transaction/{transactionId}/audit") |
| 93 | + @Consumes({ MediaType.APPLICATION_JSON }) |
| 94 | + @Produces({ MediaType.APPLICATION_JSON }) |
| 95 | + public String getLoanTransactionAuditFields(@Context final UriInfo uriInfo, @PathParam("loanId") Long loanId, |
| 96 | + @PathParam("transactionId") Long transactionId) { |
| 97 | + log.warn("------------------------------------------------------------"); |
| 98 | + log.warn(" "); |
| 99 | + log.warn("Fetching loan transaction with loanId {}, transactionId {}", loanId, transactionId); |
| 100 | + log.warn(" "); |
| 101 | + log.warn("------------------------------------------------------------"); |
| 102 | + |
| 103 | + final LoanTransaction transaction = loanTransactionRepository.findById(transactionId).orElseThrow(); |
| 104 | + Map<String, Object> auditFields = new HashMap<>(Map.of(CREATED_BY, transaction.getCreatedBy().orElse(null), CREATED_DATE, |
| 105 | + transaction.getCreatedDate().orElse(null), LAST_MODIFIED_BY, transaction.getLastModifiedBy().orElse(null), |
| 106 | + LAST_MODIFIED_DATE, transaction.getLastModifiedDate().orElse(null))); |
| 107 | + final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); |
| 108 | + return this.toApiJsonSerializer.serialize(settings, auditFields); |
| 109 | + } |
| 110 | +} |
0 commit comments