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 #357 from tiki/fix/update-state-logic-start-date
Browse files Browse the repository at this point in the history
Fix/update state logic start date
  • Loading branch information
MiroBenicio authored Oct 26, 2023
2 parents fec131a + 8ab4145 commit 377603b
Show file tree
Hide file tree
Showing 15 changed files with 25,246 additions and 44 deletions.
16 changes: 16 additions & 0 deletions __tests__/__fixtures__/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Config } from '../../src/config'

export default new Config({
company: {
name: "testCompany",
jurisdiction: "Nashville - TN",
terms: "test terms",
privacy: "https://privacy.test"
},
key: {
publishingId: "e12f5b7b-6b48-4503-8b39-28e4995b5f88",
ios: "testIos",
android: "testAndroid",
product: "testProduct"
}
})
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { StateAccount } from "../src/service/store/state/state-account";
import { Store } from "../src/service/store";

import connectGmail from "./__fixtures__/connectGmail";
import connectRetailer from "./__fixtures__/connectRetailer";
import fiveReceipts from "./__fixtures__/fiveReceipts";
import fourWeeks from "./__fixtures__/fourWeeks";
import mockCheckPayout from "./__mocks__/mockCheckPayout";
import { test, expect } from 'vitest'

test("User disconnect Gmail", async () => {
const store = new Store();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

import { StateAccount } from "../src/service/store/state/state-account"
import { Store } from "../src/service/store"

import connectRetailer from "./__fixtures__/connectRetailer"
import connectGmail from "./__fixtures__/connectGmail";
import fiveReceipts from "./__fixtures__/fiveReceipts";
import fourWeeks from "./__fixtures__/fourWeeks";
import mockCheckPayout from "./__mocks__/mockCheckPayout";
import { test, expect } from 'vitest'

test("User disconnect Gmail", async () => {
const store = new Store()
Expand Down
56 changes: 56 additions & 0 deletions __tests__/weekBuffer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { accountTypes } from '@mytiki/capture-receipt-capacitor';
import { Store } from '../src/service/store'
import { vi, test, describe, expect, beforeEach, afterEach } from 'vitest'
import { StateAccount } from '../src/service/store/state/state-account';
import { BulletState } from '../src/components/bullet/bullet-state';

describe('Reward payment tests', () => {

beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
vi.useRealTimers()
})

test("End of the month and reset", async () => {
const store = new Store();
await store.initialize();
store.gmail.update([{
username: "[email protected]",
type: accountTypes.index.get("GMAIL")!,
isVerified: true
}])
store.retailer.update([{
username: "[email protected]",
type: accountTypes.index.get("AMAZON")!,
isVerified: true
}])
await store.receipt.add("test1");
await store.receipt.add("test2");
await store.receipt.add("test3");
await store.receipt.add("test4");
await store.receipt.add("test5");
store.sync.add();
plus1week();
store.sync.add();
plus1week();
store.sync.add();
plus1week();
store.sync.add();
expect(store.sync.status()).toBe(BulletState.P100)
expect(store.receipt.status.bulletState).toBe(BulletState.P100)
expect((store.gmail as StateAccount).get().value).toBe(BulletState.P100)
expect((store.retailer as StateAccount).get().value).toBe(BulletState.P100)
plus1week()
expect(store.sync.status()).toBe(BulletState.P0)
})

});

const plus1week = () => {
const date = new Date();
date.setDate(date.getDate() + 7);
vi.setSystemTime(date);
}
8 changes: 6 additions & 2 deletions example/vue2/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
/** @type {import('ts-jest').JestConfigWithTsJest} */
const { createViteJestPreset } = require('vite-jest/preset');

module.exports = {
...createViteJestPreset(),
preset: 'ts-jest',
testEnvironment: 'jsdom',
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
},
setupFiles: ["<rootDir>/__tests__/__mocks__/localStorageMock.js"],
verbose: true,
globals: {
crypto: {
getRandomValues: (arr) => require("crypto").randomBytes(arr.length),
},
setupFiles: ["<rootDir>/__tests__/__mocks__/localStorageMock.js"],
verbose: true,
Expand All @@ -13,4 +22,5 @@
},
},
modulePathIgnorePatterns: ["<rootDir>/__tests__/__mocks__/*", "<rootDir>/__tests__/__fixtures__/*"]
}
}
}
4 changes: 3 additions & 1 deletion src/components/sheet/add/sheet-add-gmail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ BulletState,
} from "@/components";
import { type Account, GMAIL } from "@mytiki/capture-receipt-capacitor";
import { computed, inject, ref } from "vue";
import { type Capture, type Store } from "@/service";
import { type TikiService, type Capture, type Store } from "@/service";
import { InjectKey } from "@/utils";

const emit = defineEmits(["close", "back"]);
const capture: Capture = inject(InjectKey.capture)!;
const store: Store = inject(InjectKey.store)!;
const tiki: TikiService | undefined = inject("Tiki");

const form = ref<Account>({ username: "", password: "", type: GMAIL });
const error = ref<string>();
Expand All @@ -38,6 +39,7 @@ const submit = async () => {
error.value = "";
try {
await capture.login(form.value);
tiki?.checkLogin("GMAIL")
await store.gmail.set(BulletState.SYNC)
form.value = { username: "", password: "", type: GMAIL };
capture.scan().catch((error) => console.error(error.toString())).finally(async ()=>
Expand Down
4 changes: 3 additions & 1 deletion src/components/sheet/add/sheet-add-retailer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import {
OUTLOOK
} from "@mytiki/capture-receipt-capacitor";
import { ref, inject, computed, onMounted } from "vue";
import { type Capture, type Store } from "@/service";
import { type Capture, type Store, type TikiService } from "@/service";
import { InjectKey } from "@/utils";


const emit = defineEmits(["close", "back"]);
const capture: Capture = inject(InjectKey.capture)!;
const store: Store = inject(InjectKey.store)!;
const tiki: TikiService | undefined = inject("Tiki");

const filtered = accountTypes.index;
filtered.delete(GMAIL.id);
Expand Down Expand Up @@ -60,6 +61,7 @@ const submit = async () => {
error.value = "";
try {
await capture.login(form.value);
tiki?.checkLogin("RETAILER")
await store.retailer.set(BulletState.SYNC)
capture.scan().catch((error) => console.error(error.toString())).finally(async ()=>{
await store.retailer.set(BulletState.P100)
Expand Down
4 changes: 3 additions & 1 deletion src/components/sheet/warn/sheet-warn-account.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { HeaderBack, ButtonText, ButtonTextState } from "@/components";
import { inject, ref, type PropType } from "vue";
import type { Account } from "@mytiki/capture-receipt-capacitor";
import type { Capture } from "@/service";
import type { Capture, TikiService } from "@/service";
import { InjectKey } from "@/utils";

const emit = defineEmits(["remove", "close", "back"]);
Expand All @@ -18,10 +18,12 @@ const props = defineProps({
});

if (props.account === undefined) emit("back");
const tiki: TikiService | undefined = inject("Tiki");
const capture: Capture = inject(InjectKey.capture)!;
const isLoading = ref<boolean>(false);
const remove = async () => {
isLoading.value = true;
tiki?.checkLogout()
await capture.logout(props.account);
isLoading.value = false;
emit("back");
Expand Down
14 changes: 14 additions & 0 deletions src/service/store/state/state-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,32 @@ export class StateSync {
private readonly repository: Repository;
private readonly key: string = "sync";
private state: Set<Date> = new Set<Date>();
private startDate?: Date = undefined
private disconnectDate?: Date = undefined

constructor(repository: Repository) {
this.repository = repository;
}

get = (): Set<Date> => this.state;

getStartDate = (): Date | undefined => this.startDate

getDisconnectDate = (): Date | undefined => this.disconnectDate

async add(date: Date = new Date()): Promise<void> {
this.state.add(date);
return this.repository.write(this.key, this.toString());
}

setStartDate(){
this.startDate = new Date()
}

setDisconnectDate(date: Date | undefined){
this.disconnectDate = date
}

async load(): Promise<void> {
const saved: string | undefined = await this.repository.read(this.key);
if (!saved) return this.reset();
Expand Down
58 changes: 45 additions & 13 deletions src/service/tiki-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,24 @@ export class TikiService {
);
this._isInitialized = true;
this.capture.load().then(async (accounts) => {
if (accounts.length > 0) {
const hasGmail = accounts.find(
(account) => account.type.id === "GMAIL",
);
const hasRetailer = accounts.find(
(account) => account.type.type === "RETAILER",
);
if (hasGmail !== undefined)
await this.store.gmail.set(BulletState.SYNC);
if (hasRetailer !== undefined)
await this.store.retailer.set(BulletState.SYNC);
}
const hasGmail = accounts.find((account) => account.type.id === "GMAIL");
const hasRetailer = accounts.find(
(account) => account.type.type === "RETAILER",
);
if (hasGmail !== undefined) await this.store.gmail.set(BulletState.SYNC);
if (hasRetailer !== undefined)
await this.store.retailer.set(BulletState.SYNC);

this.capture
.scan()
.catch((error) => console.error(error.toString()))
.finally(async () => {
this.store.retailer.update(accounts);
this.store.gmail.update(accounts);
});

this.checkInitialize(hasGmail !== undefined, hasRetailer !== undefined);
});
await this.store.sync.add();
await this.internalHandlers.checkPayout();
}

Expand All @@ -111,6 +108,41 @@ export class TikiService {
this.publish.logout();
}

checkLogin(loginType: "GMAIL" | "RETAILER") {
const startDate = this.store.sync.getStartDate();
const retailerState = this.store.retailer.get();
const gmailState = this.store.gmail.get();
if (startDate !== undefined) return;
if (loginType === "GMAIL" && retailerState.value !== BulletState.P100)
return;
if (loginType === "RETAILER" && gmailState.value !== BulletState.P100)
return;
this.store.sync.setStartDate();
this.store.sync.setDisconnectDate(undefined);
}

checkLogout() {
const startDate = this.store.sync.getStartDate();
if (startDate === undefined) return;
this.store.sync.setDisconnectDate(new Date());
}

async checkInitialize(hasGmail: boolean, hasRetailer: boolean) {
const disconnectDate = this.store.sync.getDisconnectDate();
const startDate = this.store.sync.getStartDate();
if (disconnectDate === undefined && startDate !== undefined)
return await this.store.sync.add();
else {
if (hasGmail && hasRetailer)
return this.store.sync.setDisconnectDate(undefined);
else {
if (disconnectDate?.getTime()! >= 1000 * 60 * 60 * 24 * 7) {
return await this.store.reset();
} else return;
}
}
}

/**
* @ignore
*/
Expand Down
Loading

0 comments on commit 377603b

Please sign in to comment.