fix: price data update time serialization

price update time was not properly serialized as a number like the schema was, and led to dates being stored as empty objects. UI code also did not check if date was NaN, and rendered "invalid date" instead.
This commit is contained in:
xenticore
2025-03-29 14:19:38 -04:00
parent 13a8e14284
commit be6206194b
3 changed files with 10 additions and 8 deletions

View File

@@ -94,7 +94,7 @@ describe('Price Service', () => {
test('ItemPriceData.toString() returns formatted string', () => {
const data = new ItemPriceData()
data.sku = mockSku
data.update = mockDate
data.update = mockDate.getTime()
data.ttl = mockTtl
data.keys = 1
data.metal = 10.66
@@ -102,6 +102,8 @@ describe('Price Service', () => {
const result = data.toString()
expect(result).toContain(`Price for ${mockSku}`)
expect(result).toContain(mockDate.toString())
expect(result).toContain(new Date(mockDate.getTime() + mockTtl).toString())
expect(result).toContain(`"keys":${data.keys}`)
expect(result).toContain(`"metal":${data.metal}`)
expect(result).toContain(`"scmPrice":${data.scmPrice}`)

View File

@@ -250,7 +250,7 @@ async function inject() {
}).reverse().forEach((element) => {
priceInfoboxHeadingRow.insertAdjacentElement('afterend', element.row);
})
if(!updateTime) { updateTime = new Date() }
if(!updateTime || !(updateTime instanceof Date) || isNaN(+updateTime)) updateTime = new Date()
// Footer row
const row = document.createElement("tr");

View File

@@ -7,8 +7,8 @@ import { logDebug, log } from "./utils/log"
export class ItemPriceData {
/** Item SKU. */
sku: string
/** Date updated. */
update: Date
/** Date updated, as Unix timestamp. */
update: number
/** TTL in milliseconds. */
ttl: number
/** Price in keys. */
@@ -19,7 +19,7 @@ export class ItemPriceData {
scmPrice: number
toString(): string {
return `Price for ${this.sku}, fetched ${this.update} (expires ${this.update.getTime() + this.ttl})\n` +
return `Price for ${this.sku}, fetched ${new Date(this.update)} (expires ${new Date(this.update + this.ttl)})\n` +
JSON.stringify({
keys: this.keys,
metal: this.metal,
@@ -43,8 +43,8 @@ export async function fetchPrice(token: string, sku: string, update: Date = new
return new Promise(async (resolve, reject) => {
var data: ItemPriceData | null
const cached = await getStorageValue(storage_priceprefix + sku, null)
if (cached != null && 'keys' in cached && 'metal' in cached) {
const cached: ItemPriceData = await getStorageValue(storage_priceprefix + sku, null)
if (cached != null && 'keys' in cached && 'metal' in cached && !isNaN(cached.update)) {
data = cached
}
@@ -55,7 +55,7 @@ export async function fetchPrice(token: string, sku: string, update: Date = new
}
data = new ItemPriceData()
data.sku = sku
data.update = update
data.update = update.getTime()
data.ttl = ttl
try {