Skip to content

Commit

Permalink
feat: network in email notification (#1474)
Browse files Browse the repository at this point in the history
Signed-off-by: Hristiyan <[email protected]>
Co-authored-by: John Bair <[email protected]>
  • Loading branch information
icoxxx and jbair06 authored Jan 22, 2025
1 parent 37d5d7e commit a9ab092
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
notifySyncIndicators,
notifyTransactionAction,
notifyWaitingForSignatures,
getNetwork,
} from '@app/common';
import {
NotificationType,
Expand Down Expand Up @@ -294,6 +295,7 @@ describe('TransactionStatusService', () => {
creatorKey: {
userId: 23,
},
mirrorNetwork: 'testnet',
},
{
id: 2,
Expand All @@ -314,6 +316,8 @@ describe('TransactionStatusService', () => {

await service.updateTransactions(new Date(), new Date());

const networkString = getNetwork(transactions[0] as Transaction);

expect(transactionRepo.update).toHaveBeenNthCalledWith(
1,
{
Expand Down Expand Up @@ -342,7 +346,7 @@ describe('TransactionStatusService', () => {
entityId: transactions[0].id,
type: NotificationType.TRANSACTION_READY_FOR_EXECUTION,
actorId: null,
content: `Transaction ${transactions[0].transactionId} is ready for execution`,
content: `Transaction is ready for execution!\nTransaction ID: ${transactions[0].transactionId}\nNetwork: ${networkString}`,
userIds: [transactions[0].creatorKey?.userId],
});
expect(notifySyncIndicators).toHaveBeenCalledWith(
Expand Down Expand Up @@ -405,6 +409,7 @@ describe('TransactionStatusService', () => {
creatorKey: {
userId: 23,
},
mirrorNetwork: 'testnet',
};
transactionRepo.findOne.mockResolvedValue(transaction as Transaction);

Expand All @@ -413,6 +418,8 @@ describe('TransactionStatusService', () => {

await service.updateTransactionStatus({ id: transaction.id });

const networkString = getNetwork(transaction as Transaction);

expect(transactionRepo.update).toHaveBeenNthCalledWith(
1,
{
Expand All @@ -431,7 +438,7 @@ describe('TransactionStatusService', () => {
entityId: transaction.id,
type: NotificationType.TRANSACTION_READY_FOR_EXECUTION,
actorId: null,
content: `Transaction ${transaction.transactionId} is ready for execution`,
content: `Transaction is ready for execution!\nTransaction ID: ${transaction.transactionId}\nNetwork: ${networkString}`,
userIds: [transaction.creatorKey?.userId],
});
expect(notifyTransactionAction).toHaveBeenCalledWith(notificationsService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { NotificationType, Transaction, TransactionGroup, TransactionStatus } from '@entities';

import { ExecuteService } from '../execute/execute.service';
import { getNetwork } from '@app/common';

@Injectable()
export class TransactionStatusService {
Expand Down Expand Up @@ -238,13 +239,13 @@ export class TransactionStatusService {

private emitNotificationEvents(transaction: Transaction, newStatus: TransactionStatus) {
notifySyncIndicators(this.notificationsService, transaction.id, newStatus);

const networkString = getNetwork(transaction);
if (newStatus === TransactionStatus.WAITING_FOR_EXECUTION) {
this.notificationsService.emit<undefined, NotifyGeneralDto>(NOTIFY_GENERAL, {
entityId: transaction.id,
type: NotificationType.TRANSACTION_READY_FOR_EXECUTION,
actorId: null,
content: `Transaction ${transaction.transactionId} is ready for execution`,
content: `Transaction is ready for execution!\nTransaction ID: ${transaction.transactionId}\nNetwork: ${networkString}`,
userIds: [transaction.creatorKey?.userId],
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
MirrorNodeService,
NotifyForTransactionDto,
NotifyGeneralDto,
getNetwork,
} from '@app/common';
import {
Notification,
Expand Down Expand Up @@ -648,6 +649,7 @@ describe('ReceiverService', () => {
signedTransactions: [],
createdTransactions: [],
},
mirrorNetwork: 'testnet',
};
entityManager.findOne
.calledWith(Transaction, expect.anything())
Expand All @@ -666,6 +668,8 @@ describe('ReceiverService', () => {

await service.notifyTransactionRequiredSigners(dto);

const networkString = getNetwork(transaction as Transaction);

expect(entityManager.findOne).toHaveBeenCalledWith(Transaction, {
where: { id: dto.transactionId },
relations: { creatorKey: true },
Expand All @@ -674,7 +678,7 @@ describe('ReceiverService', () => {
expect(notifyGeneral).toHaveBeenCalledWith({
userIds: usersIdsRequiredToSign.filter(id => id !== creatorId),
type: NotificationType.TRANSACTION_WAITING_FOR_SIGNATURES,
content: `A new transaction requires your review and signature. Please visit the Hedera Transaction Tool and locate the transaction using the following ID: ${transaction.transactionId}.`,
content: `A new transaction requires your review and signature. Please visit the Hedera Transaction Tool and locate the transaction.\nTransaction ID: ${transaction.transactionId}\nNetwork: ${networkString}`,
entityId: 1,
actorId: null,
});
Expand Down
4 changes: 3 additions & 1 deletion back-end/apps/notifications/src/receiver/receiver.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { InjectEntityManager } from '@nestjs/typeorm';
import { EntityManager, In } from 'typeorm';

import {
getNetwork,
keysRequiredToSign,
MirrorNodeService,
NotifyForTransactionDto,
Expand Down Expand Up @@ -92,14 +93,15 @@ export class ReceiverService {
});

if (!transaction) throw new Error('Transaction not found');
const networkString = getNetwork(transaction);

/* Get users required to sign */
const userIds = await this.getUsersIdsRequiredToSign(this.entityManager, transaction);

/* Notify */
await this.notifyGeneral({
type: NotificationType.TRANSACTION_WAITING_FOR_SIGNATURES,
content: `A new transaction requires your review and signature. Please visit the Hedera Transaction Tool and locate the transaction using the following ID: ${transaction.transactionId}.`,
content: `A new transaction requires your review and signature. Please visit the Hedera Transaction Tool and locate the transaction.\nTransaction ID: ${transaction.transactionId}\nNetwork: ${networkString}`,
entityId: transaction.id,
actorId: null,
userIds: userIds.filter(id => id !== transaction.creatorKey?.userId),
Expand Down
10 changes: 10 additions & 0 deletions back-end/libs/common/src/utils/transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,13 @@ export const userKeysRequiredToSign = async (

return userKeysRequiredToSign.map(k => k.id);
};

export const getNetwork = (transaction: Transaction) => {
const network = transaction.mirrorNetwork;
const defaultNetworks = ['mainnet', 'testnet', 'previewnet', 'local-node'];
const isCustom = !defaultNetworks.includes(network);
const networkString = isCustom
? network
: network.charAt(0).toUpperCase() + network.slice(1).toLowerCase();
return networkString;
};

0 comments on commit a9ab092

Please sign in to comment.