-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MODAUD-195]. Implement consumer & endpoint for invoice records #175
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a8f2c66
[MODAUD-195]. Implement consumer & endpoint for invoice records
BKadirkhodjaev 87f4c37
[MODAUD-195]. Fix sonar issues
BKadirkhodjaev 0cc1dee
[MODAUD-195]. Add final modified to test constants
BKadirkhodjaev c0a0784
[MODAUD-195]. Add worker pool size env vars to each consumer
BKadirkhodjaev 91c167e
[MODORDERS-1208]. Apply review recommendations
BKadirkhodjaev f51be2c
[MODORDERS-1208]. Remove unused promises
BKadirkhodjaev 35ace2c
[MODORDERS-1208]. Remove all promises & unnecessary loggers
BKadirkhodjaev 4810f3c
[MODAUD-195]. Set additionalProperties=true (open to modifications)
BKadirkhodjaev b616f4d
[MODAUD-195]. Remove duplicated logger
BKadirkhodjaev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
mod-audit-server/src/main/java/org/folio/dao/acquisition/InvoiceEventsDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.folio.dao.acquisition; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.sqlclient.Row; | ||
import io.vertx.sqlclient.RowSet; | ||
import org.folio.rest.jaxrs.model.InvoiceAuditEvent; | ||
import org.folio.rest.jaxrs.model.InvoiceAuditEventCollection; | ||
|
||
public interface InvoiceEventsDao { | ||
|
||
/** | ||
* Saves invoiceAuditEvent entity to DB | ||
* | ||
* @param invoiceAuditEvent InvoiceAuditEvent entity to save | ||
* @param tenantId tenant id | ||
* @return future with created row | ||
*/ | ||
Future<RowSet<Row>> save(InvoiceAuditEvent invoiceAuditEvent, String tenantId); | ||
|
||
/** | ||
* Searches for invoice audit events by id | ||
* | ||
* @param invoiceId invoice id | ||
* @param sortBy sort by | ||
* @param sortInvoice sort invoice | ||
* @param limit limit | ||
* @param offset offset | ||
* @param tenantId tenant id | ||
* @return future with InvoiceAuditEventCollection | ||
*/ | ||
Future<InvoiceAuditEventCollection> getAuditEventsByInvoiceId(String invoiceId, String sortBy, String sortInvoice, int limit, int offset, String tenantId); | ||
} |
113 changes: 113 additions & 0 deletions
113
mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/InvoiceEventsDaoImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package org.folio.dao.acquisition.impl; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.sqlclient.Row; | ||
import io.vertx.sqlclient.RowSet; | ||
import io.vertx.sqlclient.Tuple; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.folio.dao.acquisition.InvoiceEventsDao; | ||
import org.folio.rest.jaxrs.model.InvoiceAuditEvent; | ||
import org.folio.rest.jaxrs.model.InvoiceAuditEventCollection; | ||
import org.folio.util.PostgresClientFactory; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.ZoneOffset; | ||
import java.util.Date; | ||
import java.util.UUID; | ||
|
||
import static java.lang.String.format; | ||
import static org.folio.util.AuditEventDBConstants.ACTION_DATE_FIELD; | ||
import static org.folio.util.AuditEventDBConstants.ACTION_FIELD; | ||
import static org.folio.util.AuditEventDBConstants.EVENT_DATE_FIELD; | ||
import static org.folio.util.AuditEventDBConstants.ID_FIELD; | ||
import static org.folio.util.AuditEventDBConstants.INVOICE_ID_FIELD; | ||
import static org.folio.util.AuditEventDBConstants.MODIFIED_CONTENT_FIELD; | ||
import static org.folio.util.AuditEventDBConstants.ORDER_BY_PATTERN; | ||
import static org.folio.util.AuditEventDBConstants.TOTAL_RECORDS_FIELD; | ||
import static org.folio.util.AuditEventDBConstants.USER_ID_FIELD; | ||
import static org.folio.util.DbUtils.formatDBTableName; | ||
|
||
@Repository | ||
public class InvoiceEventsDaoImpl implements InvoiceEventsDao { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(); | ||
|
||
public static final String TABLE_NAME = "acquisition_invoice_log"; | ||
|
||
public static final String GET_BY_INVOICE_ID_SQL = "SELECT id, action, invoice_id, user_id, event_date, action_date, modified_content_snapshot," + | ||
" (SELECT count(*) AS total_records FROM %s WHERE invoice_id = $1) FROM %s WHERE invoice_id = $1 %s LIMIT $2 OFFSET $3"; | ||
|
||
public static final String INSERT_SQL = "INSERT INTO %s (id, action, invoice_id, user_id, event_date, action_date, modified_content_snapshot)" + | ||
" VALUES ($1, $2, $3, $4, $5, $6, $7)"; | ||
|
||
private final PostgresClientFactory pgClientFactory; | ||
|
||
public InvoiceEventsDaoImpl(PostgresClientFactory pgClientFactory) { | ||
this.pgClientFactory = pgClientFactory; | ||
} | ||
|
||
@Override | ||
public Future<RowSet<Row>> save(InvoiceAuditEvent invoiceAuditEvent, String tenantId) { | ||
LOGGER.debug("save:: Saving Invoice AuditEvent with tenant id : {}", tenantId); | ||
String logTable = formatDBTableName(tenantId, TABLE_NAME); | ||
String query = format(INSERT_SQL, logTable); | ||
return makeSaveCall(query, invoiceAuditEvent, tenantId) | ||
.onSuccess(rows -> LOGGER.info("save:: Saved Invoice AuditEvent with tenant id : {}", tenantId)) | ||
.onFailure(e -> LOGGER.error("Failed to save record with id: {} for invoice id: {} in to table {}", | ||
invoiceAuditEvent.getId(), invoiceAuditEvent.getInvoiceId(), TABLE_NAME, e)); | ||
} | ||
|
||
@Override | ||
public Future<InvoiceAuditEventCollection> getAuditEventsByInvoiceId(String invoiceId, String sortBy, String sortInvoice, int limit, int offset, String tenantId) { | ||
LOGGER.debug("getAuditEventsByInvoiceId:: Retrieving AuditEvent with invoice id : {}", invoiceId); | ||
String logTable = formatDBTableName(tenantId, TABLE_NAME); | ||
String query = format(GET_BY_INVOICE_ID_SQL, logTable, logTable, format(ORDER_BY_PATTERN, sortBy, sortInvoice)); | ||
return pgClientFactory.createInstance(tenantId).execute(query, Tuple.of(UUID.fromString(invoiceId), limit, offset)) | ||
.map(rowSet -> rowSet.rowCount() == 0 ? new InvoiceAuditEventCollection().withTotalItems(0) | ||
: mapRowToListOfInvoiceEvent(rowSet)); | ||
} | ||
|
||
private Future<RowSet<Row>> makeSaveCall(String query, InvoiceAuditEvent invoiceAuditEvent, String tenantId) { | ||
LOGGER.debug("makeSaveCall:: Making save call with query : {} and tenant id : {}", query, tenantId); | ||
try { | ||
return pgClientFactory.createInstance(tenantId).execute(query, Tuple.of(invoiceAuditEvent.getId(), | ||
invoiceAuditEvent.getAction(), | ||
invoiceAuditEvent.getInvoiceId(), | ||
invoiceAuditEvent.getUserId(), | ||
LocalDateTime.ofInstant(invoiceAuditEvent.getEventDate().toInstant(), ZoneId.systemDefault()), | ||
LocalDateTime.ofInstant(invoiceAuditEvent.getActionDate().toInstant(), ZoneId.systemDefault()), | ||
JsonObject.mapFrom(invoiceAuditEvent.getInvoiceSnapshot()))); | ||
} catch (Exception e) { | ||
LOGGER.error("Failed to save record with id: {} for invoice id: {} in to table {}", | ||
invoiceAuditEvent.getId(), invoiceAuditEvent.getInvoiceId(), TABLE_NAME, e); | ||
return Future.failedFuture(e); | ||
} | ||
} | ||
|
||
private InvoiceAuditEventCollection mapRowToListOfInvoiceEvent(RowSet<Row> rowSet) { | ||
LOGGER.debug("mapRowToListOfInvoiceEvent:: Mapping row to List of Invoice Events"); | ||
InvoiceAuditEventCollection invoiceAuditEventCollection = new InvoiceAuditEventCollection(); | ||
rowSet.iterator().forEachRemaining(row -> { | ||
invoiceAuditEventCollection.getInvoiceAuditEvents().add(mapRowToInvoiceEvent(row)); | ||
invoiceAuditEventCollection.setTotalItems(row.getInteger(TOTAL_RECORDS_FIELD)); | ||
}); | ||
LOGGER.debug("mapRowToListOfInvoiceEvent:: Mapped row to List of Invoice Events"); | ||
return invoiceAuditEventCollection; | ||
} | ||
|
||
private InvoiceAuditEvent mapRowToInvoiceEvent(Row row) { | ||
LOGGER.debug("mapRowToInvoiceEvent:: Mapping row to Invoice Event"); | ||
return new InvoiceAuditEvent() | ||
.withId(row.getValue(ID_FIELD).toString()) | ||
.withAction(row.get(InvoiceAuditEvent.Action.class, ACTION_FIELD)) | ||
.withInvoiceId(row.getValue(INVOICE_ID_FIELD).toString()) | ||
.withUserId(row.getValue(USER_ID_FIELD).toString()) | ||
.withEventDate(Date.from(row.getLocalDateTime(EVENT_DATE_FIELD).toInstant(ZoneOffset.UTC))) | ||
.withActionDate(Date.from(row.getLocalDateTime(ACTION_DATE_FIELD).toInstant(ZoneOffset.UTC))) | ||
.withInvoiceSnapshot(JsonObject.mapFrom(row.getValue(MODIFIED_CONTENT_FIELD))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
mod-audit-server/src/main/java/org/folio/services/acquisition/InvoiceAuditEventsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.folio.services.acquisition; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.sqlclient.Row; | ||
import io.vertx.sqlclient.RowSet; | ||
import org.folio.rest.jaxrs.model.InvoiceAuditEvent; | ||
import org.folio.rest.jaxrs.model.InvoiceAuditEventCollection; | ||
|
||
public interface InvoiceAuditEventsService { | ||
|
||
/** | ||
* Saves InvoiceAuditEvent | ||
* | ||
* @param invoiceAuditEvent | ||
* @param tenantId id of tenant | ||
* @return successful future if event has not been processed, or failed future otherwise | ||
*/ | ||
Future<RowSet<Row>> saveInvoiceAuditEvent(InvoiceAuditEvent invoiceAuditEvent, String tenantId); | ||
|
||
/** | ||
* Searches for invoice audit events by invoice id | ||
* | ||
* @param invoiceId invoice id | ||
* @param sortBy sort by | ||
* @param sortInvoice sort invoice | ||
* @param limit limit | ||
* @param offset offset | ||
* @return future with InvoiceAuditEventCollection | ||
*/ | ||
Future<InvoiceAuditEventCollection> getAuditEventsByInvoiceId(String invoiceId, String sortBy, String sortInvoice, int limit, int offset, String tenantId); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to what we did in mod-orders-storage: