add transactions to email body
This commit is contained in:
parent
ab1b7c464f
commit
94267c47cf
2 changed files with 112 additions and 10 deletions
64
database.go
64
database.go
|
|
@ -313,7 +313,8 @@ func db_get_card_from_card_id(card_id int) (*card, error) {
|
||||||
|
|
||||||
sqlStatement := `SELECT card_id, k2_cmac_key, uid, ` +
|
sqlStatement := `SELECT card_id, k2_cmac_key, uid, ` +
|
||||||
`last_counter_value, lnurlw_request_timeout_sec, ` +
|
`last_counter_value, lnurlw_request_timeout_sec, ` +
|
||||||
` lnurlw_enable, tx_limit_sats, day_limit_sats, email_enable, email_address` +
|
`lnurlw_enable, tx_limit_sats, day_limit_sats, ` +
|
||||||
|
`email_enable, email_address, card_name ` +
|
||||||
`FROM cards WHERE card_id=$1;`
|
`FROM cards WHERE card_id=$1;`
|
||||||
row := db.QueryRow(sqlStatement, card_id)
|
row := db.QueryRow(sqlStatement, card_id)
|
||||||
err = row.Scan(
|
err = row.Scan(
|
||||||
|
|
@ -326,7 +327,8 @@ func db_get_card_from_card_id(card_id int) (*card, error) {
|
||||||
&c.tx_limit_sats,
|
&c.tx_limit_sats,
|
||||||
&c.day_limit_sats,
|
&c.day_limit_sats,
|
||||||
&c.email_enable,
|
&c.email_enable,
|
||||||
&c.email_address)
|
&c.email_address,
|
||||||
|
&c.card_name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &c, err
|
return &c, err
|
||||||
}
|
}
|
||||||
|
|
@ -593,8 +595,62 @@ func db_get_card_totals(card_id int) (int, error) {
|
||||||
return day_total_sats, nil
|
return day_total_sats, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO:
|
func db_get_card_txs(card_id int) ([]transaction, error) {
|
||||||
//func db_get_card_txs(card_id int) ([]transaction, error) {
|
// open the database
|
||||||
|
|
||||||
|
db, err := db_open()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
// query the database
|
||||||
|
|
||||||
|
//TODO: LIMIT 10 + COUNT
|
||||||
|
sqlStatement := `SELECT card_id, ` +
|
||||||
|
`card_payments.card_payment_id AS tx_id, 'payment' AS tx_type, ` +
|
||||||
|
`amount_msats as tx_amount_msats, ` +
|
||||||
|
`TO_CHAR(payment_status_time, 'DD/MM/YYYY HH:MI:SS') AS tx_time ` +
|
||||||
|
`FROM card_payments WHERE card_id = $1 AND payment_status != 'FAILED' ` +
|
||||||
|
`AND amount_msats != 0 UNION SELECT card_id, card_receipts.card_receipt_id AS tx_id, ` +
|
||||||
|
`'receipt' AS tx_type, amount_msats as tx_amount_msats, ` +
|
||||||
|
`TO_CHAR(receipt_status_time, 'DD/MM/YYYY HH:MI:SS') AS tx_time ` +
|
||||||
|
`FROM card_receipts WHERE card_id = $1 ` +
|
||||||
|
`AND receipt_status = 'SETTLED' ORDER BY tx_time DESC`
|
||||||
|
|
||||||
|
rows, err := db.Query(sqlStatement, card_id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
// prepare the results
|
||||||
|
|
||||||
|
var transactions []transaction
|
||||||
|
|
||||||
|
// Loop through rows, using Scan to assign column data to struct fields.
|
||||||
|
for rows.Next() {
|
||||||
|
var t transaction
|
||||||
|
err := rows.Scan(&t.card_id, &t.tx_id, &t.tx_type, &t.tx_amount_msats, &t.tx_time)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return transactions, err
|
||||||
|
}
|
||||||
|
transactions = append(transactions, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = rows.Err()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return transactions, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return transactions, nil
|
||||||
|
}
|
||||||
|
|
||||||
func db_get_card_total(card_id int) (int, error) {
|
func db_get_card_total(card_id int) (int, error) {
|
||||||
|
|
||||||
|
|
|
||||||
52
email.go
52
email.go
|
|
@ -9,19 +9,65 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func send_balance_email(recipient_email string, card_id int) {
|
func send_balance_email(recipient_email string, card_id int) {
|
||||||
|
|
||||||
|
c, err := db_get_card_from_card_id(card_id)
|
||||||
|
if err != nil {
|
||||||
|
log.Warn(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
card_total_sats, err := db_get_card_total(card_id)
|
card_total_sats, err := db_get_card_total(card_id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn(err.Error())
|
log.Warn(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
txs, err := db_get_card_txs(card_id)
|
||||||
|
if err != nil {
|
||||||
|
log.Warn(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
subject := c.card_name + " balance = " + strconv.Itoa(card_total_sats) + " sats"
|
||||||
|
|
||||||
|
// add transactions to the email body
|
||||||
|
|
||||||
|
var html_body_sb strings.Builder
|
||||||
|
var text_body_sb strings.Builder
|
||||||
|
|
||||||
|
html_body_sb.WriteString("<!DOCTYPE html><html><head><style> table, " +
|
||||||
|
"th, td { border: 1px solid black; border-collapse: collapse; } " +
|
||||||
|
"</style></head><body>")
|
||||||
|
|
||||||
|
html_body_sb.WriteString("<h3>transactions</h3><table><tr><th>date</th><th>action</th><th>amount</th>")
|
||||||
|
text_body_sb.WriteString("transactions\n\n")
|
||||||
|
|
||||||
|
for _, tx := range txs {
|
||||||
|
|
||||||
|
html_body_sb.WriteString(
|
||||||
|
"<tr>" +
|
||||||
|
"<td>" + tx.tx_time + "</td>" +
|
||||||
|
"<td>" + tx.tx_type + "</td>" +
|
||||||
|
"<td style='text-align:right'>" + strconv.Itoa(tx.tx_amount_msats / 1000) + "</td>" +
|
||||||
|
"</tr>")
|
||||||
|
|
||||||
|
text_body_sb.WriteString(tx.tx_type +
|
||||||
|
" " + strconv.Itoa(tx.tx_amount_msats / 1000))
|
||||||
|
}
|
||||||
|
|
||||||
|
html_body_sb.WriteString("</table></body></html>")
|
||||||
|
|
||||||
|
html_body := html_body_sb.String()
|
||||||
|
text_body := text_body_sb.String()
|
||||||
|
|
||||||
send_email(recipient_email,
|
send_email(recipient_email,
|
||||||
"bolt card balance: " + strconv.Itoa(card_total_sats) + " sats",
|
subject,
|
||||||
"html body",
|
html_body,
|
||||||
"text body")
|
text_body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/ses-example-send-email.html
|
// https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/ses-example-send-email.html
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue