Merge pull request #73 from Zod-Alkhair/feature/notification-system-fcm-registration

add eveint lestiner to the parent
This commit is contained in:
Majdalkilany0
2026-01-06 14:52:52 +03:00
committed by GitHub

View File

@ -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<void> {
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