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

Commit

Permalink
fix: add payout on criteria met.
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-audi committed Oct 2, 2023
1 parent 1d97d36 commit 9ea584e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/components/sheet/sheet-home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -25,6 +24,8 @@ const syncState = (): BulletState =>
: BulletState.P0;

const receiptState = (): BulletState => tiki!.store.receipt.status();
const balance = ref<number>(0);
tiki?.publish.balance().then((amount) => (balance.value = amount));
</script>

<template>
Expand Down Expand Up @@ -78,9 +79,9 @@ const receiptState = (): BulletState => tiki!.store.receipt.status();
/>
</div>
<button-text
text="$1 Cash Out"
:text="balance > 0 ? `$${balance} Cash Out` : 'No Balance, Yet'"
class="cash"
:state="ButtonTextState.DISABLED"
:state="balance > 0 ? ButtonTextState.STANDARD : ButtonTextState.DISABLED"
@click="$emit('withdraw')"
/>
</template>
Expand Down
29 changes: 29 additions & 0 deletions src/service/publish/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ export class ServicePublish {
}
}

async getReceipts(payables?: PayableRecord[]): Promise<ReceiptRecord[]> {
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<void> {
const license = await this.getLicense();
if (!license) throw Error("Publish requires a valid data license.");
Expand All @@ -163,6 +175,23 @@ export class ServicePublish {
}
}

async balance(): Promise<number> {
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;
Expand Down
1 change: 1 addition & 0 deletions src/service/tiki/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class TikiService {
this.capture.scan();
});
await this.store.sync.add();
await this.internalHandlers.checkPayout();
}

/**
Expand Down
20 changes: 18 additions & 2 deletions src/service/tiki/internal-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
* 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,
GMAIL,
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;
Expand Down Expand Up @@ -64,4 +64,20 @@ export class InternalHandlers {
this.publish.publish(receipt).catch((error) => console.error(error));
}
}

async checkPayout(): Promise<void> {
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();
}
}
}

0 comments on commit 9ea584e

Please sign in to comment.