From 170aa903c7a4ca02dd48996fbe36ac2affc6b4fe Mon Sep 17 00:00:00 2001 From: Abdalhamid Alhamad Date: Tue, 6 Jan 2026 14:51:44 +0300 Subject: [PATCH] add eveint lestiner to the parent --- .../transaction-notification.listener.ts | 68 ++++++++++++++++++- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/src/common/modules/notification/listeners/transaction-notification.listener.ts b/src/common/modules/notification/listeners/transaction-notification.listener.ts index c57d9c8..758532f 100644 --- a/src/common/modules/notification/listeners/transaction-notification.listener.ts +++ b/src/common/modules/notification/listeners/transaction-notification.listener.ts @@ -48,9 +48,15 @@ export class TransactionNotificationListener { // Notify the transaction owner (child or parent) await this.notifyTransactionOwner(transaction, card, isTopUp, isChildSpending); - // If child spending, also notify parent - if (isChildSpending && !isTopUp) { - await this.notifyParentOfChildSpending(transaction, card); + // If child transaction, also notify parent + if (isChildSpending) { + if (isTopUp) { + // Parent topped up child's card - send confirmation to parent + await this.notifyParentOfTopUp(transaction, card); + } else { + // Child spent money - send spending alert to parent + await this.notifyParentOfChildSpending(transaction, card); + } } this.logger.log( @@ -190,6 +196,62 @@ export class TransactionNotificationListener { } } + /** + * Notify parent when they top up their child's card + * This is a confirmation notification for the parent + */ + private async notifyParentOfTopUp(transaction: Transaction, card: Card): Promise { + try { + this.logger.debug(`Checking for parent to notify about top-up`); + + // Check if child has guardian + const customer = card?.customer; + const parentUser = customer?.junior?.guardian?.customer?.user; + + if (!parentUser) { + this.logger.debug(`No parent found for transaction ${transaction.id}, skipping parent notification`); + return; + } + + // Get child info + const childUser = customer.user; + const childName = childUser?.firstName || 'Your child'; + const amount = transaction.transactionAmount; + const balance = card.account?.balance || 0; + + this.logger.debug( + `Notifying parent (user ${parentUser.id}): Topped up ${childName}'s card with $${amount}` + ); + + // Send notification to parent + await this.notificationFactory.send({ + userId: parentUser.id, + title: '✅ Top-Up Confirmation', + message: `You topped up ${childName}'s card with $${amount.toFixed(2)}. New balance: $${balance.toFixed(2)}`, + scope: NotificationScope.PARENT_TOP_UP_CONFIRMATION, + preferences: this.getUserPreferences(parentUser), + data: { + transactionId: transaction.id, + childId: childUser.id, + childName: childName, + amount: amount.toString(), + balance: balance.toString(), + timestamp: transaction.transactionDate.toISOString(), + type: 'TOP_UP', + action: 'OPEN_TRANSACTION', + }, + }); + + this.logger.log(`✅ Notified parent ${parentUser.id} about top-up`); + } catch (error: any) { + this.logger.error( + `Failed to notify parent of top-up: ${error?.message || 'Unknown error'}`, + error?.stack + ); + // Don't throw - parent notification failure should not break child notification + } + } + /** * Extract user preferences from User entity * Converts User properties to NotificationPreferences interface