Skip to content
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 9 commits into from
Nov 4, 2024
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.10.1-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
<artifactId>mod-audit</artifactId>
<groupId>org.folio</groupId>
<version>2.10.1-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
</parent>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,45 +58,40 @@ public Future<RowSet<Row>> save(InvoiceAuditEvent invoiceAuditEvent, String tena
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId);
SerhiiNosko marked this conversation as resolved.
Show resolved Hide resolved
String logTable = formatDBTableName(tenantId, TABLE_NAME);
String query = format(INSERT_SQL, logTable);
makeSaveCall(promise, query, invoiceAuditEvent, tenantId);
LOGGER.info("save:: Saved Invoice AuditEvent with tenant id : {}", tenantId);
return promise.future();
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);
Promise<RowSet<Row>> promise = Promise.promise();
try {
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId);
String logTable = formatDBTableName(tenantId, TABLE_NAME);
String query = format(GET_BY_INVOICE_ID_SQL, logTable, logTable, format(ORDER_BY_PATTERN, sortBy, sortInvoice));
Tuple queryParams = Tuple.of(UUID.fromString(invoiceId), limit, offset);
pgClientFactory.createInstance(tenantId).selectRead(query, queryParams, promise);
} catch (Exception e) {
LOGGER.warn("Error getting invoice audit events by invoice id: {}", invoiceId, e);
promise.fail(e);
}
LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId);
String logTable = formatDBTableName(tenantId, TABLE_NAME);
String query = format(GET_BY_INVOICE_ID_SQL, logTable, logTable, format(ORDER_BY_PATTERN, sortBy, sortInvoice));
Tuple queryParams = Tuple.of(UUID.fromString(invoiceId), limit, offset);
pgClientFactory.createInstance(tenantId).selectRead(query, queryParams, promise);
LOGGER.info("getAuditEventsByInvoiceId:: Retrieved AuditEvent with invoice id : {}", invoiceId);
return promise.future().map(rowSet -> rowSet.rowCount() == 0 ? new InvoiceAuditEventCollection().withTotalItems(0)
SerhiiNosko marked this conversation as resolved.
Show resolved Hide resolved
: mapRowToListOfInvoiceEvent(rowSet));
}

private void makeSaveCall(Promise<RowSet<Row>> promise, String query, InvoiceAuditEvent invoiceAuditEvent, String tenantId) {
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 {
LOGGER.info("makeSaveCall:: Trying to make save call with query : {} and tenant id : {}", query, tenantId);
pgClientFactory.createInstance(tenantId).execute(query, Tuple.of(invoiceAuditEvent.getId(),
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())), promise);
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);
promise.fail(e);
return Future.failedFuture(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
{
"run": "after",
"snippetPath": "acquisition/create_acquisition_invoice_log_table.sql",
"fromModuleVersion": "mod-audit-2.10.1"
"fromModuleVersion": "mod-audit-3.0.0"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import static org.folio.utils.EntityUtils.createInvoiceAuditEvent;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.util.List;
import java.util.UUID;
Expand All @@ -13,6 +17,7 @@
import io.vertx.pgclient.PgException;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import io.vertx.sqlclient.Tuple;
import org.folio.CopilotGenerated;
import org.folio.dao.acquisition.impl.InvoiceEventsDaoImpl;
import org.folio.rest.jaxrs.model.InvoiceAuditEvent;
Expand Down Expand Up @@ -43,7 +48,10 @@ void shouldCreateEventProcessed() {
var invoiceAuditEvent = createInvoiceAuditEvent(UUID.randomUUID().toString());

Future<RowSet<Row>> saveFuture = invoiceEventDao.save(invoiceAuditEvent, TENANT_ID);
saveFuture.onComplete(ar -> assertTrue(ar.succeeded()));
saveFuture.onComplete(ar -> {
assertTrue(ar.succeeded());
verify(postgresClientFactory, times(1)).createInstance(TENANT_ID).execute(anyString(), any(Tuple.class));
});
}

@Test
Expand All @@ -57,6 +65,7 @@ void shouldThrowConstraintViolation() {
assertTrue(re.failed());
assertTrue(re.cause() instanceof PgException);
assertEquals("ERROR: duplicate key value violates unique constraint \"acquisition_invoice_log_pkey\" (23505)", re.cause().getMessage());
verify(postgresClientFactory, times(1)).createInstance(TENANT_ID).execute(anyString(), any(Tuple.class));
});
});
}
Expand All @@ -75,6 +84,7 @@ void shouldGetCreatedEvent() {

assertEquals(invoiceAuditEventList.get(0).getId(), id);
assertEquals(InvoiceAuditEvent.Action.CREATE.value(), invoiceAuditEventList.get(0).getAction().value());
verify(postgresClientFactory, times(1)).createInstance(TENANT_ID).selectRead(anyString(), any(Tuple.class), any());
});
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.folio</groupId>
<artifactId>mod-audit</artifactId>
<version>2.10.1-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<licenses>
Expand Down