Skip to content

Commit

Permalink
[MODAUD-174] - Consume piece change events and implement endpoints (#155
Browse files Browse the repository at this point in the history
)

* [MODAUD-174] - Consume piece change events and implement endpoints

* [MODAUD-174] - Fixed unit tests

* [MODAUD-174] - Fixed Code smell

* [MODAUD-174] - Fixed Code smell

* [MODAUD-174] - Fixed Unit tests

* [MODAUD-174] - Minor improvements

* [MODAUD-174] - Implements kafka handler and creating table

* [MODAUD-174] - Increased code coverage

* [MODAUD-174] - Increased code coverage

* [MODAUD-174] - Minor improvements

* [MODAUD-174] - Minor improvements

* [MODAUD-174] - Minor improvements

* [MODAUD-174] - Minor improvements

* [MODAUD-174] - Minor improvements

* [MODAUD-174] - Minor improvements

* [MODAUD-174] - Implemented uniq-status api

* [MODAUD-174] - Implemented status change history feature and cleaned code

* [MODAUD-174] - Implemented status change history feature and cleaned code

* [MODAUD-174] - Implemented status change history feature and cleaned code

* [MODAUD-174] - draft changes

* [MODAUD-174] - Fixed error

* [MODAUD-174] - minor improvements

* [MODAUD-174] - minor improvements

* [MODAUD-174] - minor improvements

* [MODAUD-174] - minor improvements

* [MODAUD-174] - minor improvements

* [MODAUD-174] - Fixed unit tests

* [MODAUD-174] - Fixed unit tests

* [MODAUD-174] - Fixed unit tests

* [MODAUD-174] - Fixed unit tests

* [MODAUD-174] - draft changes

* [MODAUD-174] - Fixed unit tests

* [MODAUD-174] - Minor improvements

* [MODAUD-174] - Minor improvements

* [MODAUD-174] - Minor improvements

* [MODAUD-174] - Improved code by changing field injection to constructor injection and changed method name

* [MODAUD-174] - Fixed unit tests

* [MODAUD-174] - Fixed unit tests

* [MODAUD-174] - Fixed sql query

* [MODAUD-174] - Extracted duplicate methods

* [MODAUD-174] - Added java docs

* [MODAUD-174] - Improved java integration test

* [MODAUD-174] - Extracted handleFauilures and optimized codebase

* [MODAUD-174] - Fixed sql query

* [MODAUD-174] - Fixed sql query

* [MODAUD-174] - Minor improvement

* [MODAUD-174] - Improved AuditDataAcquisitionImpl

* [MODAUD-174] - Improved AuditDataAcquisitionImpl and changed debug to error level in catch block

* [MODAUD-174] - Fixed ZoneOffset

* [MODAUD-174] - Improved log.error message

* [MODAUD-174] - Minor improvements
  • Loading branch information
azizbekxm authored Nov 16, 2023
1 parent cddd4bd commit fb5889a
Show file tree
Hide file tree
Showing 37 changed files with 1,269 additions and 336 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ nbproject/
.settings/
.classpath
/bin/
/src/main/resources/postgres-conf.json
32 changes: 31 additions & 1 deletion descriptors/ModuleDescriptor-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,30 @@
}
]
},
{
"id": "acquisition-piece-events",
"version": "1.0",
"handlers": [
{
"methods": [
"GET"
],
"pathPattern": "/audit-data/acquisition/piece/{id}",
"permissionsRequired": [
"acquisition.piece.events.get"
]
},
{
"methods": [
"GET"
],
"pathPattern": "/audit-data/acquisition/piece/{id}/status-change-history",
"permissionsRequired": [
"acquisition.piece.events.get"
]
}
]
},
{
"id": "circulation-logs",
"version": "1.2",
Expand Down Expand Up @@ -223,6 +247,11 @@
"displayName": "Acquisition order-line events - get order-line change events",
"description": "Get order-line change events"
},
{
"permissionName": "acquisition.piece.events.get",
"displayName": "Acquisition piece events - get piece change events",
"description": "Get piece change events"
},
{
"permissionName": "audit.all",
"displayName": "Audit - all permissions",
Expand All @@ -235,7 +264,8 @@
"audit.item.delete",
"circulation-logs.collection.get",
"acquisition.order.events.get",
"acquisition.order-line.events.get"
"acquisition.order-line.events.get",
"acquisition.piece.events.get"
]
}
],
Expand Down
4 changes: 2 additions & 2 deletions mod-audit-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>mod-audit-server</artifactId>
<version>2.8.1-SNAPSHOT</version>
<version>2.9.0-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
<artifactId>mod-audit</artifactId>
<groupId>org.folio</groupId>
<version>2.8.1-SNAPSHOT</version>
<version>2.9.0-SNAPSHOT</version>
</parent>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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.PieceAuditEvent;
import org.folio.rest.jaxrs.model.PieceAuditEventCollection;

public interface PieceEventsDao {

/**
* Saves pieceAuditEvent entity to DB
*
* @param pieceAuditEvent pieceAuditEvent entity to save
* @param tenantId tenant id
* @return future with created row
*/
Future<RowSet<Row>> save(PieceAuditEvent pieceAuditEvent, String tenantId);

/**
* Searches for piece audit events by id
*
* @param pieceId piece id
* @param sortBy sort by
* @param sortOrder sort order
* @param limit limit
* @param offset offset
* @param tenantId tenant id
* @return future with PieceAuditEventCollection
*/
Future<PieceAuditEventCollection> getAuditEventsByPieceId(String pieceId, String sortBy, String sortOrder,
int limit, int offset, String tenantId);

/**
* Searches for piece audit events with status changes by piece id
* @param pieceId piece id
* @param sortBy sort by
* @param sortOrder sort order
* @param limit limit
* @param offset offset
* @param tenantId tenant id
* @return future with PieceAuditEventCollection
*/
Future<PieceAuditEventCollection> getAuditEventsWithStatusChangesByPieceId(String pieceId, String sortBy, String sortOrder,
int limit, int offset, String tenantId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package org.folio.dao.acquisition.impl;

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.MODIFIED_CONTENT_FIELD;
import static org.folio.util.AuditEventDBConstants.ORDER_BY_PATTERN;
import static org.folio.util.AuditEventDBConstants.ORDER_ID_FIELD;
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;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.UUID;

import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonObject;
Expand All @@ -12,18 +30,8 @@
import org.folio.rest.jaxrs.model.OrderAuditEvent;
import org.folio.rest.jaxrs.model.OrderAuditEventCollection;
import org.folio.util.PostgresClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.UUID;

import static java.lang.String.format;
import static org.folio.rest.persist.PostgresClient.convertToPsqlStandard;
import static org.folio.util.OrderAuditEventDBConstants.*;

@Repository
public class OrderEventsDaoImpl implements OrderEventsDao {

Expand All @@ -37,10 +45,8 @@ public class OrderEventsDaoImpl implements OrderEventsDao {
public static final String INSERT_SQL = "INSERT INTO %s (id, action, order_id, user_id, event_date, action_date, modified_content_snapshot)" +
" VALUES ($1, $2, $3, $4, $5, $6, $7)";

@Autowired
private final PostgresClientFactory pgClientFactory;

@Autowired
public OrderEventsDaoImpl(PostgresClientFactory pgClientFactory) {
this.pgClientFactory = pgClientFactory;
}
Expand All @@ -49,10 +55,9 @@ public OrderEventsDaoImpl(PostgresClientFactory pgClientFactory) {
public Future<RowSet<Row>> save(OrderAuditEvent orderAuditEvent, String tenantId) {
LOGGER.debug("save:: Saving Order AuditEvent with tenant id : {}", tenantId);
Promise<RowSet<Row>> promise = Promise.promise();
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId);
String logTable = formatDBTableName(tenantId, TABLE_NAME);

String query = format(INSERT_SQL, logTable);

makeSaveCall(promise, query, orderAuditEvent, tenantId);
LOGGER.info("save:: Saved Order AuditEvent with tenant id : {}", tenantId);
return promise.future();
Expand All @@ -63,7 +68,7 @@ public Future<OrderAuditEventCollection> getAuditEventsByOrderId(String orderId,
LOGGER.debug("getAuditEventsByOrderId:: Retrieving AuditEvent with order id : {}", orderId);
Promise<RowSet<Row>> promise = Promise.promise();
try {
LOGGER.info("getAuditEventsByOrderId:: Trying to Retrieve AuditEvent with order id : {}", orderId);
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId);
String logTable = formatDBTableName(tenantId, TABLE_NAME);
String query = format(GET_BY_ORDER_ID_SQL, logTable, logTable, format(ORDER_BY_PATTERN, sortBy, sortOrder));
Tuple queryParams = Tuple.of(UUID.fromString(orderId), limit, offset);
Expand All @@ -85,11 +90,11 @@ private void makeSaveCall(Promise<RowSet<Row>> promise, String query, OrderAudit
orderAuditEvent.getAction(),
orderAuditEvent.getOrderId(),
orderAuditEvent.getUserId(),
LocalDateTime.ofInstant(orderAuditEvent.getEventDate().toInstant(), ZoneOffset.UTC),
LocalDateTime.ofInstant(orderAuditEvent.getActionDate().toInstant(), ZoneOffset.UTC),
LocalDateTime.ofInstant(orderAuditEvent.getEventDate().toInstant(), ZoneId.systemDefault()),
LocalDateTime.ofInstant(orderAuditEvent.getActionDate().toInstant(), ZoneId.systemDefault()),
JsonObject.mapFrom(orderAuditEvent.getOrderSnapshot())), promise);
} catch (Exception e) {
LOGGER.warn("Failed to save record with id: {} for order id: {} in to table {}",
LOGGER.error("Failed to save record with id: {} for order id: {} in to table {}",
orderAuditEvent.getId(), orderAuditEvent.getOrderId(), TABLE_NAME, e);
promise.fail(e);
}
Expand All @@ -102,7 +107,7 @@ private OrderAuditEventCollection mapRowToListOfOrderEvent(RowSet<Row> rowSet) {
orderAuditEventCollection.getOrderAuditEvents().add(mapRowToOrderEvent(row));
orderAuditEventCollection.setTotalItems(row.getInteger(TOTAL_RECORDS_FIELD));
});
LOGGER.info("mapRowToListOfOrderEvent:: Mapped row to List of Order Events");
LOGGER.debug("mapRowToListOfOrderEvent:: Mapped row to List of Order Events");
return orderAuditEventCollection;
}

Expand All @@ -119,8 +124,4 @@ private OrderAuditEvent mapRowToOrderEvent(Row row) {
.withOrderSnapshot(JsonObject.mapFrom(row.getValue(MODIFIED_CONTENT_FIELD)));
}

private String formatDBTableName(String tenantId, String table) {
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId);
return format("%s.%s", convertToPsqlStandard(tenantId), table);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package org.folio.dao.acquisition.impl;

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.MODIFIED_CONTENT_FIELD;
import static org.folio.util.AuditEventDBConstants.ORDER_BY_PATTERN;
import static org.folio.util.AuditEventDBConstants.ORDER_ID_FIELD;
import static org.folio.util.AuditEventDBConstants.ORDER_LINE_ID_FIELD;
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;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.UUID;

import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonObject;
Expand All @@ -12,18 +31,8 @@
import org.folio.rest.jaxrs.model.OrderLineAuditEvent;
import org.folio.rest.jaxrs.model.OrderLineAuditEventCollection;
import org.folio.util.PostgresClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.UUID;

import static java.lang.String.format;
import static org.folio.rest.persist.PostgresClient.convertToPsqlStandard;
import static org.folio.util.OrderAuditEventDBConstants.*;

@Repository
public class OrderLineEventsDaoImpl implements OrderLineEventsDao {

Expand All @@ -38,10 +47,8 @@ public class OrderLineEventsDaoImpl implements OrderLineEventsDao {
private static final String INSERT_SQL = "INSERT INTO %s (id, action, order_id, order_line_id, user_id, event_date, action_date, modified_content_snapshot) " +
"VALUES ($1, $2, $3, $4, $5, $6, $7, $8)";

@Autowired
private final PostgresClientFactory pgClientFactory;

@Autowired
public OrderLineEventsDaoImpl(PostgresClientFactory pgClientFactory) {
this.pgClientFactory = pgClientFactory;
}
Expand All @@ -50,12 +57,10 @@ public OrderLineEventsDaoImpl(PostgresClientFactory pgClientFactory) {
public Future<RowSet<Row>> save(OrderLineAuditEvent orderLineAuditEvent, String tenantId) {
LOGGER.debug("save:: Saving OrderLine AuditEvent with tenant id : {}", tenantId);
Promise<RowSet<Row>> promise = Promise.promise();
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId);
String logTable = formatDBTableName(tenantId, TABLE_NAME);

String query = format(INSERT_SQL, logTable);

makeSaveCall(promise, query, orderLineAuditEvent, tenantId);

LOGGER.info("save:: Saved OrderLine AuditEvent with tenant id : {}", tenantId);
return promise.future();
}
Expand All @@ -65,7 +70,7 @@ public Future<OrderLineAuditEventCollection> getAuditEventsByOrderLineId(String
LOGGER.debug("getAuditEventsByOrderLineId:: Retrieving AuditEvent with order line id : {}", orderLineId);
Promise<RowSet<Row>> promise = Promise.promise();
try {
LOGGER.info("getAuditEventsByOrderLineId:: Trying to Retrieve AuditEvent with order line id : {}", orderLineId);
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId);
String logTable = formatDBTableName(tenantId, TABLE_NAME);
String query = format(GET_BY_ORDER_LINE_ID_SQL, logTable, logTable, format(ORDER_BY_PATTERN, sortBy, sortOrder));
Tuple queryParams = Tuple.of(UUID.fromString(orderLineId), limit, offset);
Expand All @@ -88,11 +93,11 @@ private void makeSaveCall(Promise<RowSet<Row>> promise, String query, OrderLineA
orderLineAuditEvent.getOrderId(),
orderLineAuditEvent.getOrderLineId(),
orderLineAuditEvent.getUserId(),
LocalDateTime.ofInstant(orderLineAuditEvent.getEventDate().toInstant(), ZoneOffset.UTC),
LocalDateTime.ofInstant(orderLineAuditEvent.getActionDate().toInstant(), ZoneOffset.UTC),
LocalDateTime.ofInstant(orderLineAuditEvent.getEventDate().toInstant(), ZoneId.systemDefault()),
LocalDateTime.ofInstant(orderLineAuditEvent.getActionDate().toInstant(), ZoneId.systemDefault()),
JsonObject.mapFrom(orderLineAuditEvent.getOrderLineSnapshot())), promise);
} catch (Exception e) {
LOGGER.warn("Failed to save record with id: {} for order line id: {} in to table {}",
LOGGER.error("Failed to save record with id: {} for order line id: {} in to table {}",
orderLineAuditEvent.getId(), orderLineAuditEvent.getOrderLineId(), TABLE_NAME, e);
promise.fail(e);
}
Expand All @@ -105,7 +110,7 @@ private OrderLineAuditEventCollection mapRowToListOfOrderLineEvent(RowSet<Row> r
orderLineAuditEventCollection.getOrderLineAuditEvents().add(mapRowToOrderLineEvent(row));
orderLineAuditEventCollection.setTotalItems(row.getInteger(TOTAL_RECORDS_FIELD));
});
LOGGER.info("mapRowToListOfOrderLineEvent:: Mapped row to List of Order Line Events");
LOGGER.debug("mapRowToListOfOrderLineEvent:: Mapped row to List of Order Line Events");
return orderLineAuditEventCollection;
}

Expand All @@ -122,9 +127,4 @@ private OrderLineAuditEvent mapRowToOrderLineEvent(Row row) {
.withActionDate(Date.from(row.getLocalDateTime(ACTION_DATE_FIELD).toInstant(ZoneOffset.UTC)))
.withOrderLineSnapshot(JsonObject.mapFrom(row.getValue(MODIFIED_CONTENT_FIELD)));
}

private String formatDBTableName(String tenantId, String table) {
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId);
return format("%s.%s", convertToPsqlStandard(tenantId), table);
}
}
Loading

0 comments on commit fb5889a

Please sign in to comment.