group TransactionSqlDao: EntitySqlDao;

tableName() ::= "payment_transactions"

historyTableName() ::= "payment_transaction_history"

defaultOrderBy(prefix) ::= <<
order by <prefix>effective_date ASC, <recordIdField(prefix)> ASC
>>

tableFields(prefix) ::= <<
  <prefix>attempt_id
, <prefix>transaction_external_key
, <prefix>transaction_type
, <prefix>effective_date
, <prefix>transaction_status
, <prefix>amount
, <prefix>currency
, <prefix>processed_amount
, <prefix>processed_currency
, <prefix>payment_id
, <prefix>gateway_error_code
, <prefix>gateway_error_msg
, <prefix>created_by
, <prefix>created_date
, <prefix>updated_by
, <prefix>updated_date
>>

tableValues() ::= <<
  :attemptId
, :transactionExternalKey
, :transactionType
, :effectiveDate
, :transactionStatus
, :amount
, :currency
, :processedAmount
, :processedCurrency
, :paymentId
, :gatewayErrorCode
, :gatewayErrorMsg
, :createdBy
, :createdDate
, :updatedBy
, :updatedDate
>>

getPaymentTransactionsByExternalKey() ::= <<
select
<allTableFields("")>
from <tableName()>
where transaction_external_key = :transactionExternalKey
<AND_CHECK_TENANT()>
<defaultOrderBy()>
;
>>


updateTransactionStatus() ::= <<
update <tableName()>
set transaction_status = :transactionStatus
, processed_amount = :processedAmount
, processed_currency = :processedCurrency
, gateway_error_code = :gatewayErrorCode
, gateway_error_msg = :gatewayErrorMsg
, updated_by = :updatedBy
, updated_date = :createdDate
where id = :id
<AND_CHECK_TENANT()>
;
>>

getByPaymentId() ::= <<
select <allTableFields()>
from <tableName()>
where payment_id = :paymentId
<AND_CHECK_TENANT()>
<defaultOrderBy()>
;
>>


/* Does not include AND_CHECK_TENANT() since this is a global operation */
getByTransactionStatusPriorDate() ::= <<
select <allTableFields()>
from <tableName()>
where transaction_status = :transactionStatus
and created_date \< :beforeCreatedDate
<defaultOrderBy()>
;
>>


failOldPendingTransactions(ids) ::= <<
update <tableName()>
set transaction_status = :newTransactionStatus
, updated_by = :updatedBy
, updated_date = :createdDate
where <idField("")> in (<ids: {id | :id_<i0>}; separator="," >)
<defaultOrderBy()>
;
>>