-
Notifications
You must be signed in to change notification settings - Fork 12
[MEX-748] Implement Claim Rewards notifications in xPortal #1598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
claudiulataretu
merged 24 commits into
feat/push-notifications-system
from
MEX-748-push-notifications
May 15, 2025
Merged
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
805494f
MEX-748 Implement Claim Rewards notifications in xPortal
EmanuelMiron bc73efe
MEX-748 Fix issue on failed req
EmanuelMiron e33d070
MEX-748 Make the module more generic
EmanuelMiron c726d97
MEX-748 Added push_notifications_aip_url in env.example
EmanuelMiron 5701b74
MEX-748 Moved options to config from env
EmanuelMiron 9daa642
MEX-748 Use ApiService instead of axios directly
EmanuelMiron c9f750a
MEX-748 Added Fixes after review
EmanuelMiron 7454f00
MEX-748 Fix feesCollector startEpoch logic
EmanuelMiron 49f1ca4
MEX-748 Fix Cron interval for feesCollector Rewards Notifications
EmanuelMiron ff38557
MEX-748 Added Logs to PushNotification Service
EmanuelMiron 7084897
MEX-748 Added try catch block for the sendPushNotifications
EmanuelMiron c534c44
MEX-748 Implement RedLock for Push Notifications as a decorator
EmanuelMiron ff5aee7
Merge branch 'development' into MEX-748-push-notifications
EmanuelMiron 3f19dad
MEX-748 Added Logs back
EmanuelMiron 5f3c8f1
MEX-748 Added error logging on sendPushNotifications
EmanuelMiron 6ea6bdd
MEX-748 Moved withLockAndRetry to generic utils
EmanuelMiron 171b388
MEX-748: Removed redundant try/catch blocks
EmanuelMiron 47aa1ca
MEX-748 Moved LockAndRetry decorator
EmanuelMiron 934afbc
MEX-748 Other small fixes
EmanuelMiron 945d960
MEX-748 Place failedNotifications on the common redis instance
EmanuelMiron dc8e57b
MEX-748 Fix srem issue
EmanuelMiron 9a2131e
MEX-748 Integrate devnet energy snapshot index
EmanuelMiron e8d35ca
Merge branch 'development' into MEX-748-push-notifications
EmanuelMiron afcd632
MEX-748 Dynamic Cron based on environment
EmanuelMiron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/modules/push-notifications/crons/push.notifications.energy.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { | ||
| AccountType, | ||
| NotificationType, | ||
| } from '../models/push.notifications.types'; | ||
| import { Injectable, Inject } from '@nestjs/common'; | ||
| import { Cron, CronExpression } from '@nestjs/schedule'; | ||
| import { PushNotificationsService } from '../services/push.notifications.service'; | ||
| import { EnergyAbiService } from 'src/modules/energy/services/energy.abi.service'; | ||
| import { ContextGetterService } from 'src/services/context/context.getter.service'; | ||
| import { ElasticAccountsEnergyService } from 'src/services/elastic-search/services/es.accounts.energy.service'; | ||
| import { pushNotificationsConfig, scAddress } from 'src/config'; | ||
| import { WeekTimekeepingAbiService } from 'src/submodules/week-timekeeping/services/week-timekeeping.abi.service'; | ||
| import { WINSTON_MODULE_PROVIDER } from 'nest-winston'; | ||
| import { Logger } from 'winston'; | ||
| import { RedlockService } from '@multiversx/sdk-nestjs-cache'; | ||
| import { LockAndRetry } from '../decorators/lock.retry.decorator'; | ||
|
|
||
| @Injectable() | ||
| export class PushNotificationsEnergyCron { | ||
| constructor( | ||
| private readonly energyAbiService: EnergyAbiService, | ||
| private readonly contextGetter: ContextGetterService, | ||
| private readonly pushNotificationsService: PushNotificationsService, | ||
| private readonly accountsEnergyElasticService: ElasticAccountsEnergyService, | ||
| private readonly weekTimekeepingAbi: WeekTimekeepingAbiService, | ||
| private readonly redLockService: RedlockService, | ||
| @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger, | ||
| ) {} | ||
|
|
||
| @Cron(CronExpression.EVERY_DAY_AT_NOON) | ||
EmanuelMiron marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @LockAndRetry({ | ||
| lockKey: 'pushNotifications', | ||
| lockName: 'feesCollector', | ||
| }) | ||
| async feesCollectorRewardsCron() { | ||
| const currentEpoch = await this.contextGetter.getCurrentEpoch(); | ||
| const firstWeekStartEpoch = | ||
| await this.weekTimekeepingAbi.firstWeekStartEpoch( | ||
| String(scAddress.feesCollector), | ||
| ); | ||
|
|
||
| if ((currentEpoch - firstWeekStartEpoch) % 7 !== 0) { | ||
| return; | ||
| } | ||
|
|
||
| const isDevnet = process.env.NODE_ENV === 'devnet'; | ||
|
|
||
| if (isDevnet) { | ||
| const addresses = await this.energyAbiService.getUsersWithEnergy(); | ||
|
|
||
| await this.pushNotificationsService.sendNotificationsInBatches( | ||
| addresses, | ||
| pushNotificationsConfig[ | ||
| NotificationType.FEES_COLLECTOR_REWARDS | ||
| ], | ||
| NotificationType.FEES_COLLECTOR_REWARDS, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| await this.accountsEnergyElasticService.getAccountsByEnergyAmount( | ||
| currentEpoch - 1, | ||
| 'gt', | ||
| async (items: AccountType[]) => { | ||
| const addresses = items.map( | ||
| (item: AccountType) => item.address, | ||
| ); | ||
|
|
||
| await this.pushNotificationsService.sendNotificationsInBatches( | ||
| addresses, | ||
| pushNotificationsConfig[ | ||
| NotificationType.FEES_COLLECTOR_REWARDS | ||
| ], | ||
| NotificationType.FEES_COLLECTOR_REWARDS, | ||
| ); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| @Cron('0 12 */2 * *') // Every 2 days at noon | ||
| @LockAndRetry({ | ||
| lockKey: 'pushNotifications', | ||
| lockName: 'negativeEnergy', | ||
| }) | ||
| async negativeEnergyNotificationsCron() { | ||
| const currentEpoch = await this.contextGetter.getCurrentEpoch(); | ||
|
|
||
| await this.accountsEnergyElasticService.getAccountsByEnergyAmount( | ||
| currentEpoch - 1, | ||
| 'lt', | ||
| async (items: AccountType[]) => { | ||
| const addresses = items.map( | ||
| (item: AccountType) => item.address, | ||
| ); | ||
|
|
||
| await this.pushNotificationsService.sendNotificationsInBatches( | ||
| addresses, | ||
| pushNotificationsConfig[NotificationType.NEGATIVE_ENERGY], | ||
| NotificationType.NEGATIVE_ENERGY, | ||
| ); | ||
| this.logger.log( | ||
| `Sent ${addresses.length} negative energy notifications`, | ||
| 'PushNotificationsEnergyCron', | ||
| ); | ||
| }, | ||
| 0, | ||
| ); | ||
| } | ||
|
|
||
| @Cron(CronExpression.EVERY_10_MINUTES) | ||
| @LockAndRetry({ | ||
| lockKey: 'pushNotifications', | ||
| lockName: 'retryFailed', | ||
| }) | ||
| async retryFailedNotificationsCron() { | ||
| const notificationTypes = Object.values(NotificationType); | ||
|
|
||
| for (const notificationType of notificationTypes) { | ||
| await this.pushNotificationsService.retryFailedNotifications( | ||
| notificationType, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
src/modules/push-notifications/decorators/lock.retry.decorator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { RedlockService } from '@multiversx/sdk-nestjs-cache'; | ||
| import { Logger } from 'winston'; | ||
| import { withLockAndRetry } from '../utils/lock.retry.utils'; | ||
|
|
||
| export function LockAndRetry(options: { | ||
EmanuelMiron marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| lockKey: string; | ||
| lockName: string; | ||
| keyExpiration?: number; | ||
| maxLockRetries?: number; | ||
| lockRetryInterval?: number; | ||
| maxOperationRetries?: number; | ||
| operationRetryInterval?: number; | ||
| }) { | ||
| return function ( | ||
| target: any, | ||
| propertyKey: string, | ||
| descriptor: PropertyDescriptor, | ||
| ) { | ||
| const originalMethod = descriptor.value; | ||
|
|
||
| descriptor.value = async function (...args: any[]) { | ||
| const redLockService = this.redLockService as RedlockService; | ||
| const logger = this.logger as Logger; | ||
|
|
||
| if (!redLockService || !logger) { | ||
| throw new Error( | ||
| 'Class must have redLockService and logger properties', | ||
| ); | ||
| } | ||
|
|
||
| await withLockAndRetry( | ||
| redLockService, | ||
| logger, | ||
| options, | ||
| async () => { | ||
| await originalMethod.apply(this, args); | ||
| }, | ||
| ); | ||
| }; | ||
|
|
||
| return descriptor; | ||
| }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.