mirror of
https://github.com/digital-asset/daml.git
synced 2024-09-20 01:07:18 +03:00
Refactor response preparation for flat transactions (#5187)
* Refactor response preparation for flat transactions This will allow to share code across endpoints changelog_begin changelog_end * Address https://github.com/digital-asset/daml/pull/5187#discussion_r397996322
This commit is contained in:
parent
ef6c09b1bd
commit
1a229ebb7b
@ -18,7 +18,8 @@ import com.digitalasset.ledger.api.v1.transaction.{
|
||||
}
|
||||
import com.digitalasset.ledger.api.v1.transaction_service.{
|
||||
GetFlatTransactionResponse,
|
||||
GetTransactionResponse
|
||||
GetTransactionResponse,
|
||||
GetTransactionsResponse,
|
||||
}
|
||||
import com.digitalasset.ledger.api.v1.value.Identifier
|
||||
import com.digitalasset.platform.ApiOffset
|
||||
@ -59,29 +60,34 @@ private[events] trait EventsTable {
|
||||
private def instantToTimestamp(t: Instant): Timestamp =
|
||||
Timestamp(seconds = t.getEpochSecond, nanos = t.getNano)
|
||||
|
||||
def toFlatTransaction(events: List[Entry[Event]]): Option[GetFlatTransactionResponse] = {
|
||||
private def flatTransaction[R](makeResponse: ApiTransaction => R)(
|
||||
events: Seq[Entry[Event]],
|
||||
): Option[R] =
|
||||
events.headOption.flatMap { first =>
|
||||
val flatEvents =
|
||||
TransactionConversion.removeTransient(events.iterator.map(_.event).toVector)
|
||||
if (flatEvents.nonEmpty || first.commandId.nonEmpty)
|
||||
Some(
|
||||
GetFlatTransactionResponse(
|
||||
transaction = Some(
|
||||
ApiTransaction(
|
||||
transactionId = first.transactionId,
|
||||
commandId = first.commandId,
|
||||
effectiveAt = Some(instantToTimestamp(first.ledgerEffectiveTime)),
|
||||
workflowId = first.workflowId,
|
||||
offset = ApiOffset.toApiString(first.eventOffset),
|
||||
events = flatEvents,
|
||||
)
|
||||
makeResponse(
|
||||
ApiTransaction(
|
||||
transactionId = first.transactionId,
|
||||
commandId = first.commandId,
|
||||
effectiveAt = Some(instantToTimestamp(first.ledgerEffectiveTime)),
|
||||
workflowId = first.workflowId,
|
||||
offset = ApiOffset.toApiString(first.eventOffset),
|
||||
events = flatEvents,
|
||||
)
|
||||
)
|
||||
)
|
||||
else None
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
def toGetTransactionsResponse(events: Vector[Entry[Event]]): Seq[GetTransactionsResponse] =
|
||||
flatTransaction(tx => GetTransactionsResponse(Seq(tx)))(events).toList
|
||||
|
||||
def toGetFlatTransactionResponse(
|
||||
events: List[Entry[Event]]): Option[GetFlatTransactionResponse] =
|
||||
flatTransaction(tx => GetFlatTransactionResponse(Some(tx)))(events)
|
||||
|
||||
def toTransactionTree(events: List[Entry[TreeEvent]]): Option[GetTransactionResponse] =
|
||||
events.headOption.map(
|
||||
|
@ -32,8 +32,9 @@ private[dao] object TransactionsReader {
|
||||
) { implicit connection =>
|
||||
query.as(EventsTable.flatEventParser.*)
|
||||
}
|
||||
.map(EventsTable.Entry.toFlatTransaction)(executionContext)
|
||||
.map(EventsTable.Entry.toGetFlatTransactionResponse)(executionContext)
|
||||
}
|
||||
|
||||
override def lookupTransactionTreeById(
|
||||
transactionId: TransactionId,
|
||||
requestingParties: Set[Party],
|
||||
|
Loading…
Reference in New Issue
Block a user