From 9ea584e7bc9d60e7d9ef80bc48283f3d59e73a44 Mon Sep 17 00:00:00 2001 From: Mike Audi Date: Mon, 2 Oct 2023 18:49:11 -0500 Subject: [PATCH] fix: add payout on criteria met. --- src/components/sheet/sheet-home.vue | 9 +++++---- src/service/publish/index.ts | 29 +++++++++++++++++++++++++++ src/service/tiki/index.ts | 1 + src/service/tiki/internal-handlers.ts | 20 ++++++++++++++++-- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/components/sheet/sheet-home.vue b/src/components/sheet/sheet-home.vue index 9aaa86b5..ff2fa39d 100644 --- a/src/components/sheet/sheet-home.vue +++ b/src/components/sheet/sheet-home.vue @@ -11,8 +11,7 @@ import BulletList from "@/components/bullet/bullet-list.vue"; import { ButtonTextState } from "@/components/button/button-text-state"; import { BulletState } from "@/components/bullet/bullet-state"; import type { TikiService } from "@/service"; -import { inject } from "vue"; -import { getWeek } from "@/utils/week"; +import { inject, ref } from "vue"; defineEmits(["close", "learn", "retailer", "gmail", "withdraw"]); const tiki: TikiService | undefined = inject("Tiki"); @@ -25,6 +24,8 @@ const syncState = (): BulletState => : BulletState.P0; const receiptState = (): BulletState => tiki!.store.receipt.status(); +const balance = ref(0); +tiki?.publish.balance().then((amount) => (balance.value = amount)); diff --git a/src/service/publish/index.ts b/src/service/publish/index.ts index f0b65e8f..92e2f7dc 100644 --- a/src/service/publish/index.ts +++ b/src/service/publish/index.ts @@ -142,6 +142,18 @@ export class ServicePublish { } } + async getReceipts(payables?: PayableRecord[]): Promise { + const receipts: ReceiptRecord[] = []; + if (!payables) payables = await this.getPayables(); + for (const payable of payables) { + const records: ReceiptRecord[] = await this.plugin.getReceipts( + payable.id, + ); + receipts.push(...records); + } + return receipts; + } + async publish(receipt: Receipt): Promise { const license = await this.getLicense(); if (!license) throw Error("Publish requires a valid data license."); @@ -163,6 +175,23 @@ export class ServicePublish { } } + async balance(): Promise { + let balance: number = 0; + const payables: PayableRecord[] = (await this.getPayables()).filter( + (payable) => payable.type === this.config.payableType, + ); + payables.forEach((payable) => (balance += Number(payable.amount))); + if (payables.length > 0) { + const receipts: ReceiptRecord[] = await this.getReceipts(payables); + receipts.forEach((receipt) => { + const amount: number = Number(receipt.amount); + if (balance - amount > 0) balance -= amount; + else balance = 0; + }); + } + return balance; + } + logout(): void { this._id = undefined; this._license = undefined; diff --git a/src/service/tiki/index.ts b/src/service/tiki/index.ts index fbf63847..7dfb041f 100644 --- a/src/service/tiki/index.ts +++ b/src/service/tiki/index.ts @@ -82,6 +82,7 @@ export class TikiService { this.capture.scan(); }); await this.store.sync.add(); + await this.internalHandlers.checkPayout(); } /** diff --git a/src/service/tiki/internal-handlers.ts b/src/service/tiki/internal-handlers.ts index 12e15d33..3fc100fe 100644 --- a/src/service/tiki/internal-handlers.ts +++ b/src/service/tiki/internal-handlers.ts @@ -3,8 +3,7 @@ * MIT license. See LICENSE file in root directory. */ -import type { ServiceCapture } from "@/service/capture"; -import type { AccountStatus } from "@/service/capture"; +import type { AccountStatus, ServiceCapture } from "@/service/capture"; import type { ServiceStore } from "@/service/store"; import { type Account, @@ -12,6 +11,7 @@ import { type Receipt, } from "@mytiki/capture-receipt-capacitor"; import type { ServicePublish } from "@/service"; +import { BulletState } from "@/components/bullet/bullet-state"; export class InternalHandlers { private readonly capture: ServiceCapture; @@ -64,4 +64,20 @@ export class InternalHandlers { this.publish.publish(receipt).catch((error) => console.error(error)); } } + + async checkPayout(): Promise { + const gmail = this.store.gmail.get(); + const retailer = this.store.retailer.get(); + const sync = this.store.sync.countWeeks(); + const receipts = this.store.receipt.count(); + if ( + gmail.value === BulletState.P100 && + retailer.value === BulletState.P100 && + sync >= 4 && + receipts >= 5 + ) { + await this.publish.createPayable(1); + await this.store.reset(); + } + } }