Skip to content

Commit

Permalink
Schedule reminder on revenue share start and purge on manual close
Browse files Browse the repository at this point in the history
  • Loading branch information
ikprk committed Aug 6, 2024
1 parent 66e84d5 commit 428d947
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/mappings/token/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
CreatorTokenMarketStarted,
CreatorTokenMarketStartedEventData,
CreatorTokenRevenueShareEnded,
CreatorTokenRevenueShareEndedReminder,
CreatorTokenRevenueSharePlanned,
CreatorTokenRevenueShareStarted,
CreatorTokenRevenueSplitIssuedEventData,
Expand All @@ -41,10 +42,12 @@ import {
VestedSale,
VestingSchedule,
} from '../../model'
import { BLOCKS_PER_DAY } from '../../server-extension/resolvers/CreatorToken'
import { getCurrentBlockHeight } from '../../utils/blockHeight'
import { EventHandlerContext } from '../../utils/events'
import { criticalError } from '../../utils/misc'
import { addNotification } from '../../utils/notification'
import { removeFutureNotification, removeNotification } from '../../utils/notification/helpers'

Check warning on line 50 in src/mappings/token/index.ts

View workflow job for this annotation

GitHub Actions / Local build, linting and formatting (ubuntu-latest, 18.x)

'removeNotification' is defined but never used. Allowed unused vars must match /^_/u
import { getChannelOwnerAccount, notifyChannelFollowers, parseChannelTitle } from '../content/utils'
import { deserializeMetadata, genericEventFields } from '../utils'
import {
Expand Down Expand Up @@ -695,6 +698,26 @@ export async function processRevenueSplitIssuedEvent({
event,
endsAt // schedule for end block
)

// This event should have dispatch block of ending block plus about 3 days
// If user closes the share before its execution, this notification will be removing during share closing mapping
const revenueSharedEndedReminderNotification = new CreatorTokenRevenueShareEndedReminder({
revenueShareId: revenueShare.id,
channelTitle: parseChannelTitle(channel),
channelId: channel.id,
tokenSymbol: parseCreatorTokenSymbol(token),
tokenId: tokenId.toString(),
})

const channelOwnerAccount = await getChannelOwnerAccount(overlay, channel)
await addNotification(
overlay,
channelOwnerAccount,
new ChannelRecipient({ channel: channel.id }),
revenueSharedEndedReminderNotification,
undefined,
endsAt + BLOCKS_PER_DAY * 3 // schedule for end block
)
}

export async function processMemberJoinedWhitelistEvent({
Expand Down Expand Up @@ -799,6 +822,10 @@ export async function processRevenueSplitFinalizedEvent({
.getByIdOrFail(token.currentRevenueShareId!)
revenueShare.finalized = true
token.currentRevenueShareId = null

await removeFutureNotification(overlay.getEm(), 'CreatorTokenRevenueShareEndedReminder', 1, {
tokenId: token.id,
})
}

export async function processUserParticipatedInSplitEvent({
Expand Down
45 changes: 44 additions & 1 deletion src/utils/notification/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Flat } from 'lodash'
import { EntityManager } from 'typeorm'
import { EntityManager, FindOptionsWhere } from 'typeorm'
import {
Account,
AccountNotificationPreferences,
Expand Down Expand Up @@ -72,6 +72,7 @@ export function defaultNotificationPreferences(): AccountNotificationPreferences
crtRevenueShareStarted: notificationPrefAllTrue(),
crtRevenueSharePlanned: notificationPrefAllTrue(),
crtRevenueShareEnded: notificationPrefAllTrue(),
crtRevenueShareEndedReminder: notificationPrefAllTrue(),
})
}

Expand Down Expand Up @@ -144,6 +145,9 @@ export function preferencesForNotification(
return preferences.crtRevenueSharePlanned
case 'CreatorTokenRevenueShareEnded':
return preferences.crtRevenueShareEnded
case 'CreatorTokenRevenueShareEndedReminder':
return preferences.crtRevenueShareEnded

default: // all the remaining notifications (v2 scope) are not enabled by default
return new NotificationPreference({ inAppEnabled: false, emailEnabled: false })
}
Expand Down Expand Up @@ -299,6 +303,45 @@ export const addNotification = async (
}
}

export async function removeFutureNotification(
em: EntityManager,
notificationType: Notification['notificationType']['isTypeOf'],
currentBlockHeight: number,
filters: Record<string, string | number>
) {
const result: { id: string }[] = await em.query(
`
SELECT
id
FROM
public.notification
WHERE
notification_type ->> 'isTypeOf'=$1
AND
dispatch_block > $2
${Object.keys(filters).map((key, idx) => {
return `
AND
notification_type ->> '${key}'=$${idx + 3}
`
})}
`,
[notificationType, currentBlockHeight, ...Object.values(filters)]
)
const notificationToRemove = result[0]

if (!notificationToRemove) {
return
}

await em.getRepository(Notification).delete({
id: notificationToRemove.id,
})
await em.getRepository(NotificationEmailDelivery).delete({
notificationId: notificationToRemove.id,
})
}

async function saveNextNotificationId(
em: EntityManager,
nextNotificationId: number,
Expand Down

0 comments on commit 428d947

Please sign in to comment.