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

View File

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

View File

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