|
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,78 +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 |
|
| 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(); |
117 | 131 | final NoteType type = NoteType.LOAN; |
118 | 132 |
|
119 | 133 | final Loan loan = this.loanRepository.findOneWithNotFoundDetection(resourceId); |
120 | 134 | final Note noteForUpdate = this.noteRepository.findByLoanAndId(loan, noteId); |
| 135 | + |
121 | 136 | if (noteForUpdate == null) { |
122 | 137 | throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase(java.util.Locale.ROOT)); |
123 | 138 | } |
124 | 139 |
|
125 | | - final Map<String, Object> changes = noteForUpdate.update(command); |
126 | | - |
127 | | - if (!changes.isEmpty()) { |
128 | | - this.noteRepository.saveAndFlush(noteForUpdate); |
129 | | - } |
| 140 | + final String noteContent = command.stringValueOfParameterNamed("note"); |
| 141 | + noteForUpdate.update(noteContent); |
| 142 | + this.noteRepository.saveAndFlush(noteForUpdate); |
130 | 143 |
|
131 | 144 | return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(noteForUpdate.getId()) |
132 | | - .withLoanId(loan.getId()).withOfficeId(loan.getOfficeId()).with(changes).build(); |
| 145 | + .withLoanId(loan.getId()).withOfficeId(loan.getOfficeId()).build(); |
133 | 146 | } |
134 | 147 |
|
135 | 148 | private CommandProcessingResult updateLoanTransactionNote(final JsonCommand command) { |
136 | | - |
137 | 149 | final Long resourceId = command.subentityId(); |
138 | 150 | final Long noteId = command.entityId(); |
139 | | - |
140 | 151 | final NoteType type = NoteType.LOAN_TRANSACTION; |
141 | 152 |
|
142 | 153 | final LoanTransaction loanTransaction = this.loanTransactionRepository.findById(resourceId) |
143 | 154 | .orElseThrow(() -> new LoanTransactionNotFoundException(resourceId)); |
144 | 155 | final Loan loan = loanTransaction.getLoan(); |
145 | 156 |
|
146 | | - final Note noteForUpdate = this.noteRepository.findByLoanTransactionAndId(loanTransaction, noteId); |
147 | | - |
| 157 | + final Note noteForUpdate = this.noteRepository.findByLoanAndId(loan, noteId); |
148 | 158 | if (noteForUpdate == null) { |
149 | 159 | throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase(java.util.Locale.ROOT)); |
150 | 160 | } |
151 | 161 |
|
152 | | - final Map<String, Object> changes = noteForUpdate.update(command); |
153 | | - |
154 | | - if (!changes.isEmpty()) { |
155 | | - this.noteRepository.saveAndFlush(noteForUpdate); |
156 | | - } |
| 162 | + final String noteContent = command.stringValueOfParameterNamed("note"); |
| 163 | + noteForUpdate.update(noteContent); |
| 164 | + this.noteRepository.saveAndFlush(noteForUpdate); |
157 | 165 |
|
158 | 166 | return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(noteForUpdate.getId()) |
159 | | - .withLoanId(loan.getId()).withOfficeId(loan.getOfficeId()).with(changes).build(); |
| 167 | + .withLoanId(loan.getId()).withOfficeId(loan.getOfficeId()).build(); |
160 | 168 | } |
161 | 169 |
|
162 | 170 | private CommandProcessingResult updateSavingAccountNote(final JsonCommand command) { |
163 | | - final Long resourceId = command.getSavingsId(); |
| 171 | + final Long resourceId = command.subentityId(); |
164 | 172 | final Long noteId = command.entityId(); |
165 | 173 | final NoteType type = NoteType.SAVING_ACCOUNT; |
| 174 | + |
166 | 175 | final SavingsAccount savingAccount = this.savingsAccountRepository.findById(resourceId) |
167 | 176 | .orElseThrow(() -> new SavingsAccountNotFoundException(resourceId)); |
168 | 177 |
|
169 | 178 | final Note noteForUpdate = this.noteRepository.findBySavingsAccountAndId(savingAccount, noteId); |
170 | 179 | if (noteForUpdate == null) { |
171 | 180 | throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase(java.util.Locale.ROOT)); |
172 | 181 | } |
173 | | - final Map<String, Object> changes = noteForUpdate.update(command); |
174 | | - if (!changes.isEmpty()) { |
175 | | - this.noteRepository.saveAndFlush(noteForUpdate); |
176 | | - } |
177 | 182 |
|
178 | | - return new CommandProcessingResultBuilder() // |
179 | | - .withCommandId(command.commandId()) // |
180 | | - .withEntityId(noteForUpdate.getId()) // |
181 | | - .withOfficeId(savingAccount.getClient().getOffice().getId()) // |
182 | | - .withSavingsId(savingAccount.getId()) // |
183 | | - .with(changes) // |
184 | | - .build(); |
| 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(); |
185 | 189 | } |
186 | 190 |
|
187 | 191 | @Override |
@@ -235,10 +239,9 @@ private Pair<Note, Long> getNote(NoteType type, Long resourceId, Long noteId) { |
235 | 239 | log.error("Not yet implemented: {}", type); |
236 | 240 | break; |
237 | 241 | } |
238 | | - if (noteForUpdate == null) { |
| 242 | + if (note == null) { |
239 | 243 | throw new NoteNotFoundException(noteId, resourceId, type.name().toLowerCase(java.util.Locale.ROOT)); |
240 | 244 | } |
241 | | - |
242 | 245 | return Pair.of(note, officeId); |
243 | 246 | } |
244 | 247 | } |
0 commit comments