feat: parse and store createdAt from time in PaymentRecord

This commit is contained in:
2026-06-03 00:15:13 +02:00
parent 05562f847d
commit 5dd2edce53
6 changed files with 66 additions and 21 deletions
@@ -12,9 +12,9 @@ import androidx.sqlite.db.SupportSQLiteDatabase
entities = [
PaymentRecordEntity::class,
LnurlCacheEntity::class,
NodeAliasCacheEntity::class, // ← added
NodeAliasCacheEntity::class,
],
version = 3, // ← bumped
version = 4, // ← bumped
exportSchema = false
)
@TypeConverters(PaymentConverters::class)
@@ -22,7 +22,7 @@ abstract class AppDatabase : RoomDatabase() {
abstract fun paymentDao(): PaymentDao
abstract fun lnurlCacheDao(): LnurlCacheDao
abstract fun nodeAliasCacheDao(): NodeAliasCacheDao // ← added
abstract fun nodeAliasCacheDao(): NodeAliasCacheDao
companion object {
@Volatile private var INSTANCE: AppDatabase? = null
@@ -42,7 +42,7 @@ abstract class AppDatabase : RoomDatabase() {
}
}
private val MIGRATION_2_3 = object : Migration(2, 3) { // ← added
private val MIGRATION_2_3 = object : Migration(2, 3) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL(
"""
@@ -57,6 +57,19 @@ abstract class AppDatabase : RoomDatabase() {
}
}
private val MIGRATION_3_4 = object : Migration(3, 4) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE payment_records ADD COLUMN createdAt INTEGER")
db.execSQL(
"""
UPDATE payment_records
SET createdAt = CAST(time AS INTEGER)
WHERE CAST(time AS INTEGER) > 1000000000
""".trimIndent()
)
}
}
fun getInstance(context: Context): AppDatabase =
INSTANCE ?: synchronized(this) {
INSTANCE ?: Room.databaseBuilder(
@@ -64,7 +77,7 @@ abstract class AppDatabase : RoomDatabase() {
AppDatabase::class.java,
"gudari_wallet.db"
)
.addMigrations(MIGRATION_1_2, MIGRATION_2_3) // ← added
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4) // ← added
.build()
.also { INSTANCE = it }
}
@@ -14,6 +14,7 @@ data class PaymentRecordEntity(
val feeMsat: Long,
val memo: String?,
val time: String,
val createdAt: Long? = null,
val status: String,
val bolt11: String?,
val pending: Boolean,
@@ -1,8 +1,27 @@
package com.bitcointxoko.gudariwallet.data.db
import android.util.Log
import com.bitcointxoko.gudariwallet.api.PaymentRecord
import com.bitcointxoko.gudariwallet.data.db.PaymentRecordEntity
private const val TAG = "PaymentMappers"
// ── Parse helper — runs once at write time, never again ──────────────────────
private fun parseCreatedAt(time: String): Long? {
val result = time.toLongOrNull()
?: runCatching {
java.time.OffsetDateTime.parse(time).toEpochSecond()
}.getOrNull()
if (result == null) {
Log.w(TAG, "parseCreatedAt: could not parse time string \"$time\" — createdAt will be null")
}
return result
}
// ── PaymentRecord → PaymentRecordEntity ──────────────────────────────────────
fun PaymentRecord.toEntity(savedAt: Long): PaymentRecordEntity =
PaymentRecordEntity(
checkingId = checkingId,
@@ -11,6 +30,7 @@ fun PaymentRecord.toEntity(savedAt: Long): PaymentRecordEntity =
feeMsat = feeMsat,
memo = memo,
time = time,
createdAt = parseCreatedAt(time), // ← NEW
status = status,
bolt11 = bolt11,
pending = pending,
@@ -19,6 +39,8 @@ fun PaymentRecord.toEntity(savedAt: Long): PaymentRecordEntity =
savedAt = savedAt
)
// ── PaymentRecordEntity → PaymentRecord ──────────────────────────────────────
fun PaymentRecordEntity.toDomain(): PaymentRecord =
PaymentRecord(
checkingId = checkingId,
@@ -27,6 +49,7 @@ fun PaymentRecordEntity.toDomain(): PaymentRecord =
feeMsat = feeMsat,
memo = memo,
time = time,
createdAt = createdAt, // ← NEW
status = status,
bolt11 = bolt11,
pending = pending,