Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
feat(profiles): implement `IProfileTransactionNotifications#isSyncing…
Browse files Browse the repository at this point in the history
…/markAllAsRead` (#35)
  • Loading branch information
goga-m authored Jul 11, 2021
1 parent 4a9a50a commit f1f4b35
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 14 deletions.
6 changes: 1 addition & 5 deletions packages/sdk-lsk/source/networks/lsk.testnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ const network: Networks.NetworkManifest = {
importMethods,
featureFlags: {
...featureFlags,
Transaction: [
"delegateRegistration",
"transfer",
"vote",
],
Transaction: ["delegateRegistration", "transfer", "vote"],
},
explorer: {
block: "block/{0}",
Expand Down
14 changes: 14 additions & 0 deletions packages/sdk-profiles/source/notification.repository.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,20 @@ export interface IProfileTransactionNotifications {
* @memberof IProfileTransactionNotifications
*/
transaction(transactionId: string): ExtendedConfirmedTransactionData | undefined;

/**
* Check if the notification transactions are syncing
*
* @memberof IProfileTransactionNotifications
*/
isSyncing(): boolean;

/**
* Mark all notifications as read
*
* @memberof IProfileTransactionNotifications
*/
markAllAsRead(): void;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Profile } from "./profile";

import { NotificationRepository } from "./notification.repository";
import { ProfileTransactionNotifications } from "./notification.transactions.service";
import { IProfileTransactionNotifications } from "./notification.repository.contract";
import { INotificationTypes, IProfileTransactionNotifications } from "./notification.repository.contract";
import { IProfile } from "./profile.contract";
import { ExtendedConfirmedTransactionData } from "./transaction.dto";
const NotificationTransactionFixtures = require("../test/fixtures/client/notification-transactions.json");
Expand Down Expand Up @@ -106,7 +106,7 @@ test("#markAsRead", async () => {
test("#transactions", async () => {
await subject.sync({ limit: 20 });

expect(subject.transactions()).toHaveLength(3);
expect(subject.transactions()).toHaveLength(2);
expect(subject.transactions(1)).toHaveLength(1);
});

Expand All @@ -128,11 +128,29 @@ test("should handle undefined timestamp", async () => {
await subject.sync();
expect(subject.recent(10)).toHaveLength(2);
expect(subject.recent()).toHaveLength(2);
expect(subject.transactions()).toHaveLength(3);
expect(subject.transactions()).toHaveLength(2);

jest.restoreAllMocks();

await subject.sync();
expect(subject.recent(10)).toHaveLength(2);
expect(subject.recent()).toHaveLength(2);
});

test("#markAllAsRead", async () => {
await subject.sync({ limit: 20 });
notificationsRepository.push({
type: INotificationTypes.Release,
meta: { version: "3.0.0" },
});

expect(notificationsRepository.unread()).toHaveLength(3);
expect(subject.recent()).toHaveLength(2);
subject.markAllAsRead();
expect(notificationsRepository.unread()).toHaveLength(1);
});

test("#isSyncing", async () => {
expect(subject.isSyncing()).toBeFalse();
await subject.sync({ limit: 20 });
});
40 changes: 34 additions & 6 deletions packages/sdk-profiles/source/notification.transactions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ export class ProfileTransactionNotifications implements IProfileTransactionNotif
readonly #defaultLimit: number;

#transactions: ExtendedConfirmedTransactionData[];
#isSyncing: boolean;

public constructor(profile: IProfile, notificationRepository: INotificationRepository) {
this.#defaultLimit = 10;
this.#profile = profile;
this.#allowedTypes = ["transfer", "multiPayment"];
this.#notifications = notificationRepository;
this.#transactions = [];
this.#isSyncing = false;
}

private format = (
Expand Down Expand Up @@ -60,11 +62,21 @@ export class ProfileTransactionNotifications implements IProfileTransactionNotif
return unseen;
};

private hasStoredTransaction = (transactionId: string) => {
return !!this.transactions().find((storedTransaction) => storedTransaction.id() === transactionId);
};

private storeTransactions = (transactions: ExtendedConfirmedTransactionData[]) => {
for (const transaction of transactions) {
if (this.has(transaction.id())) {
this.#transactions.push(transaction);
if (this.hasStoredTransaction(transaction.id())) {
continue;
}

if (!this.has(transaction.id())) {
continue;
}

this.#transactions.push(transaction);
}
};

Expand Down Expand Up @@ -106,18 +118,27 @@ export class ProfileTransactionNotifications implements IProfileTransactionNotif
this.#notifications.markAsRead(notification.id);
};

/** {@inheritDoc IProfileTransactionNotifications.markAllAsRead} */
public markAllAsRead = () => {
const unread = this.#notifications.unread();

for (const notification of unread) {
if (notification.type === INotificationTypes.Transaction) {
this.#notifications.markAsRead(notification.id);
}
}
};

/** {@inheritDoc IProfileTransactionNotifications.sync} */
public sync = async (queryInput?: AggregateQuery) => {
const query = {
addresses: this.#profile
.wallets()
.values()
.map((wallet) => wallet.address()),
cursor: 1,
limit: this.#defaultLimit,
...(queryInput && queryInput),
};

this.#isSyncing = true;

const transactions = await this.#profile.transactionAggregate().received(query);
const unseen = this.filterUnseen(transactions.items());

Expand All @@ -126,6 +147,8 @@ export class ProfileTransactionNotifications implements IProfileTransactionNotif
}

this.storeTransactions(transactions.items());

this.#isSyncing = false;
};

/** {@inheritDoc IProfileTransactionNotifications.transactions} */
Expand All @@ -139,4 +162,9 @@ export class ProfileTransactionNotifications implements IProfileTransactionNotif
public transaction = (transactionId: string) => {
return this.transactions().find((transaction) => transaction.id() === transactionId);
};

/** {@inheritDoc IProfileTransactionNotifications.isSyncing} */
public isSyncing = (): boolean => {
return this.#isSyncing;
};
}

0 comments on commit f1f4b35

Please sign in to comment.