Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #279 from tiki/fix/filter-erroneous-receipts
Browse files Browse the repository at this point in the history
fix: filter out erroneous receipts
  • Loading branch information
mike-audi authored Oct 4, 2023
2 parents 77fa3f2 + 2ce839f commit f1a3323
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
38 changes: 37 additions & 1 deletion src/service/capture/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export class ServiceCapture {
return new Promise<void>((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(),
Expand All @@ -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";
32 changes: 16 additions & 16 deletions src/service/publish/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,24 @@ export class ServicePublish {
return receipts;
}

async publish(_receipt: Receipt): Promise<void> {
async publish(receipt: Receipt): Promise<void> {
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<number> {
Expand Down
7 changes: 1 addition & 6 deletions src/service/store/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,8 @@ export class Repository {

async clear(): Promise<void> {
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}`;
Expand Down

0 comments on commit f1a3323

Please sign in to comment.