|
19 | 19 | package org.apache.fineract.portfolio.note.service; |
20 | 20 |
|
21 | 21 | import lombok.extern.slf4j.Slf4j; |
22 | | -import org.apache.commons.lang3.Strings; |
23 | 22 | import org.apache.commons.lang3.tuple.Pair; |
| 23 | +import org.apache.fineract.infrastructure.core.api.JsonCommand; |
| 24 | +import org.apache.fineract.infrastructure.core.data.CommandProcessingResult; |
| 25 | +import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder; |
24 | 26 | import org.apache.fineract.portfolio.client.domain.ClientRepositoryWrapper; |
25 | 27 | import org.apache.fineract.portfolio.group.domain.GroupRepository; |
26 | 28 | import org.apache.fineract.portfolio.group.exception.GroupNotFoundException; |
| 29 | +import org.apache.fineract.portfolio.loanaccount.domain.Loan; |
27 | 30 | import org.apache.fineract.portfolio.loanaccount.domain.LoanRepositoryWrapper; |
| 31 | +import org.apache.fineract.portfolio.loanaccount.domain.LoanTransaction; |
28 | 32 | import org.apache.fineract.portfolio.loanaccount.domain.LoanTransactionRepository; |
29 | 33 | import org.apache.fineract.portfolio.loanaccount.exception.LoanTransactionNotFoundException; |
30 | 34 | import org.apache.fineract.portfolio.note.data.NoteCreateRequest; |
|
38 | 42 | import org.apache.fineract.portfolio.note.domain.NoteType; |
39 | 43 | import org.apache.fineract.portfolio.note.exception.NoteNotFoundException; |
40 | 44 | import org.apache.fineract.portfolio.note.exception.NoteResourceNotSupportedException; |
| 45 | +import org.apache.fineract.portfolio.savings.domain.SavingsAccount; |
41 | 46 | import org.apache.fineract.portfolio.savings.domain.SavingsAccountRepository; |
42 | 47 | import org.apache.fineract.portfolio.savings.exception.SavingsAccountNotFoundException; |
43 | 48 |
|
@@ -110,16 +115,77 @@ public NoteCreateResponse createNote(final NoteCreateRequest request) { |
110 | 115 |
|
111 | 116 | @Override |
112 | 117 | public NoteUpdateResponse updateNote(final NoteUpdateRequest request) { |
113 | | - final var result = getNote(request.getType(), request.getResourceId(), request.getId()); |
114 | | - final var note = result.getLeft(); |
115 | | - final var response = NoteUpdateResponse.builder().officeId(result.getRight()).resourceId(request.getResourceId()); |
| 118 | + final Note noteForUpdate = this.noteRepository.findById(request.getId()) |
| 119 | + .orElseThrow(() -> new NoteNotFoundException(request.getId(), request.getResourceId(), |
| 120 | + request.getType().name().toLowerCase(java.util.Locale.ROOT))); |
116 | 121 |
|
117 | | - if (!Strings.CI.equals(note.getNote(), request.getNote())) { |
118 | | - response.changes(note.update(request.getNote())); |
119 | | - noteRepository.saveAndFlush(note); |
| 122 | + noteForUpdate.update(request.getNote()); |
| 123 | + this.noteRepository.saveAndFlush(noteForUpdate); |
| 124 | + |
| 125 | + return NoteUpdateResponse.builder().build(); |
| 126 | + } |
| 127 | + |
| 128 | + private CommandProcessingResult updateLoanNote(final JsonCommand command) { |
| 129 | + final Long resourceId = command.subentityId(); |
| 130 | + final Long noteId = command.entityId(); |
| 131 | + final NoteType type = NoteType.LOAN; |
| 132 | + |
| 133 | + final Loan loan = this.loanRepository.findOneWithNotFoundDetection(resourceId); |
| 134 | + final Note noteForUpdate = this.noteRepository.findByLoanAndId(loan, noteId); |
| 135 | + |
| 136 | + if (noteForUpdate == null) { |
| 137 | + throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase(java.util.Locale.ROOT)); |
| 138 | + } |
| 139 | + |
| 140 | + final String noteContent = command.stringValueOfParameterNamed("note"); |
| 141 | + noteForUpdate.update(noteContent); |
| 142 | + this.noteRepository.saveAndFlush(noteForUpdate); |
| 143 | + |
| 144 | + return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(noteForUpdate.getId()) |
| 145 | + .withLoanId(loan.getId()).withOfficeId(loan.getOfficeId()).build(); |
| 146 | + } |
| 147 | + |
| 148 | + private CommandProcessingResult updateLoanTransactionNote(final JsonCommand command) { |
| 149 | + final Long resourceId = command.subentityId(); |
| 150 | + final Long noteId = command.entityId(); |
| 151 | + final NoteType type = NoteType.LOAN_TRANSACTION; |
| 152 | + |
| 153 | + final LoanTransaction loanTransaction = this.loanTransactionRepository.findById(resourceId) |
| 154 | + .orElseThrow(() -> new LoanTransactionNotFoundException(resourceId)); |
| 155 | + final Loan loan = loanTransaction.getLoan(); |
| 156 | + |
| 157 | + final Note noteForUpdate = this.noteRepository.findByLoanAndId(loan, noteId); |
| 158 | + if (noteForUpdate == null) { |
| 159 | + throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase(java.util.Locale.ROOT)); |
120 | 160 | } |
121 | 161 |
|
122 | | - return response.build(); |
| 162 | + final String noteContent = command.stringValueOfParameterNamed("note"); |
| 163 | + noteForUpdate.update(noteContent); |
| 164 | + this.noteRepository.saveAndFlush(noteForUpdate); |
| 165 | + |
| 166 | + return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(noteForUpdate.getId()) |
| 167 | + .withLoanId(loan.getId()).withOfficeId(loan.getOfficeId()).build(); |
| 168 | + } |
| 169 | + |
| 170 | + private CommandProcessingResult updateSavingAccountNote(final JsonCommand command) { |
| 171 | + final Long resourceId = command.subentityId(); |
| 172 | + final Long noteId = command.entityId(); |
| 173 | + final NoteType type = NoteType.SAVING_ACCOUNT; |
| 174 | + |
| 175 | + final SavingsAccount savingAccount = this.savingsAccountRepository.findById(resourceId) |
| 176 | + .orElseThrow(() -> new SavingsAccountNotFoundException(resourceId)); |
| 177 | + |
| 178 | + final Note noteForUpdate = this.noteRepository.findBySavingsAccountAndId(savingAccount, noteId); |
| 179 | + if (noteForUpdate == null) { |
| 180 | + throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase(java.util.Locale.ROOT)); |
| 181 | + } |
| 182 | + |
| 183 | + final String noteContent = command.stringValueOfParameterNamed("note"); |
| 184 | + noteForUpdate.update(noteContent); |
| 185 | + this.noteRepository.saveAndFlush(noteForUpdate); |
| 186 | + |
| 187 | + return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(noteForUpdate.getId()) |
| 188 | + .withSavingsId(savingAccount.getId()).withOfficeId(savingAccount.officeId()).build(); |
123 | 189 | } |
124 | 190 |
|
125 | 191 | @Override |
@@ -173,11 +239,9 @@ private Pair<Note, Long> getNote(NoteType type, Long resourceId, Long noteId) { |
173 | 239 | log.error("Not yet implemented: {}", type); |
174 | 240 | break; |
175 | 241 | } |
176 | | - |
177 | 242 | if (note == null) { |
178 | | - throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase()); |
| 243 | + throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase(java.util.Locale.ROOT)); |
179 | 244 | } |
180 | | - |
181 | 245 | return Pair.of(note, officeId); |
182 | 246 | } |
183 | 247 | } |
0 commit comments