From 2ce839f0dc232f038f882f47ef9a017850dfa31d Mon Sep 17 00:00:00 2001 From: Mike Audi Date: Tue, 3 Oct 2023 21:30:29 -0500 Subject: [PATCH] fix: filters out erroneous receipts filter happens BEFORE reward issuance. --- src/service/capture/index.ts | 38 ++++++++++++++++++++++++++++++++- src/service/publish/index.ts | 32 +++++++++++++-------------- src/service/store/repository.ts | 7 +----- 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/src/service/capture/index.ts b/src/service/capture/index.ts index 1ec12af6..afb337c6 100644 --- a/src/service/capture/index.ts +++ b/src/service/capture/index.ts @@ -75,7 +75,9 @@ export class ServiceCapture { return new Promise((resolve, reject) => { this.plugin.scan( (receipt: Receipt) => { - this._onReceiptListeners.forEach((listener) => listener(receipt)); + this._onReceiptListeners.forEach((listener) => { + if (this.guardReceipt(receipt)) listener(receipt); + }); }, 7, (): void => resolve(), @@ -102,6 +104,40 @@ export class ServiceCapture { private accountKey = (account: Account): string => account.type.id + ":" + account.username; + + private guardReceipt(receipt: Receipt): boolean { + if (receipt.blinkReceiptId == undefined) { + console.debug(`Invalid receipt: No blinkReceiptId.`); + return false; + } + if ( + receipt.retailerId.id == undefined || + receipt.retailerId.bannerId == undefined + ) { + console.debug( + `Invalid receipt: Invalid retailerId: ${JSON.stringify( + receipt.retailerId, + )}`, + ); + return false; + } + if (receipt.total == undefined) { + console.debug(`Invalid receipt: No total.`); + return false; + } + if ( + receipt.receiptDateTime == undefined || + receipt.receiptDate?.value == undefined + ) { + console.debug( + `Invalid receipt: No receipt date — receiptDateTime: ${JSON.stringify( + receipt.receiptDateTime, + )} receiptDate: ${JSON.stringify(receipt.receiptDate)}`, + ); + return false; + } + return true; + } } export * from "./account-status"; diff --git a/src/service/publish/index.ts b/src/service/publish/index.ts index 205b6df0..eb0740cb 100644 --- a/src/service/publish/index.ts +++ b/src/service/publish/index.ts @@ -154,24 +154,24 @@ export class ServicePublish { return receipts; } - async publish(_receipt: Receipt): Promise { + async publish(receipt: Receipt): Promise { const license = await this.getLicense(); if (!license) throw Error("Publish requires a valid data license."); - const _jwt: Jwt = await this.plugin.token(); - // const rsp = await fetch( - // "https://ingest.mytiki.com/api/latest/microblink-receipt", - // { - // method: "POST", - // headers: { - // Authorization: `Bearer ${jwt.accessToken}`, - // }, - // body: JSON.stringify(receipt), - // }, - // ); - // if (!rsp.ok) { - // const body = await rsp.text(); - // console.debug(`Unsupported receipt. Skipping. ${body}`); - // } + const jwt: Jwt = await this.plugin.token(); + const rsp = await fetch( + "https://ingest.mytiki.com/api/latest/microblink-receipt", + { + method: "POST", + headers: { + Authorization: `Bearer ${jwt.accessToken}`, + }, + body: JSON.stringify(receipt), + }, + ); + if (!rsp.ok) { + const body = await rsp.text(); + console.warn(`Failed to upload receipt. Skipping. ${body}`); + } } async balance(): Promise { diff --git a/src/service/store/repository.ts b/src/service/store/repository.ts index ddfde582..9305df28 100644 --- a/src/service/store/repository.ts +++ b/src/service/store/repository.ts @@ -30,13 +30,8 @@ export class Repository { async clear(): Promise { const { keys } = await Preferences.keys(); - console.log(`KEYS: ${keys}`); const filtered = keys.filter((key) => key.startsWith(this.prefix)); - console.log(`FILTERED KEYS: ${keys}`); - for (const key of filtered) { - console.log(`REMOVING KEY: ${key}`); - await Preferences.remove({ key: key }); - } + for (const key of filtered) await Preferences.remove({ key: key }); } private toKey = (key: string): string => `${this.prefix}.${key}`;