Skip to content

Commit

Permalink
feat(notice): cancel delete recently sent notices recorded in redis…
Browse files Browse the repository at this point in the history
… by lambda

related PR thematters/lambda-handlers#152
  • Loading branch information
gary02 committed Jul 31, 2024
1 parent 3fea6c5 commit ae9a724
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
35 changes: 26 additions & 9 deletions src/connectors/notificationService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@ import type {
import { MONTH, NOTICE_TYPE, QUEUE_URL, CACHE_TTL } from 'common/enums'
import { isTest } from 'common/environment'
import { getLogger } from 'common/logger'
import { aws } from 'connectors'
import { aws, AtomService } from 'connectors'

import { mail } from './mail'

const logger = getLogger('service-notification')

const SKIP_NOTICE_FLAG_PREFIX = 'skip-notice'
const DELETE_NOTICE_KEY_PREFIX = 'delete-notice'

export class NotificationService {
public mail: typeof mail
private connections: Connections
private aws: typeof aws
private models: AtomService

public constructor(connections: Connections) {
this.connections = connections
this.mail = mail
this.aws = aws
this.models = new AtomService(this.connections)
}

public trigger = async (params: NotificationParams) => {
Expand All @@ -37,9 +40,10 @@ export class NotificationService {
logger.info(`triggered notification params: ${JSON.stringify(params)}`)

if ('tag' in params) {
params.tag = `${SKIP_NOTICE_FLAG_PREFIX}:${params.tag}`
// delete skip flag when sending this notice again
await this.connections.redis.del(params.tag)
await this.connections.redis.del(
`${SKIP_NOTICE_FLAG_PREFIX}:${params.tag}`
)
}
try {
await this.aws.sqsSendMessage({
Expand All @@ -52,18 +56,24 @@ export class NotificationService {
}

/**
* Mark a notice tag to be skipped.
*
* In Lambda, we will check if a flag exists for a given tag,
* if so, we will skip processing this notice with the tag.
* Mark a notice tag for skipping processing in Lambda function;
* And delete all recently sent notices recorded by Lambda
*/
public cancel = async (tag: string) =>
this.connections.redis.set(
public cancel = async (tag: string) => {
const redis = this.connections.redis
// set skip flag for this tag
await redis.set(
`${SKIP_NOTICE_FLAG_PREFIX}:${tag}`,
'1',
'EX',
CACHE_TTL.NOTICE
)
// delete all recently sent notices of this tag
const deleteKey = `${DELETE_NOTICE_KEY_PREFIX}:${tag}`
const noticeIds = await redis.smembers(deleteKey)
await Promise.all(noticeIds.map((id: string) => this.deleteNotice(id)))
await redis.del(deleteKey)
}

public markAllNoticesAsRead = async (userId: string) => {
const knex = this.connections.knex
Expand Down Expand Up @@ -252,4 +262,11 @@ export class NotificationService {
.where({ noticeId })
return actors
}

private deleteNotice = async (id: string) =>
this.models.update({
table: 'notice',
where: { id },
data: { deleted: true },
})
}
3 changes: 2 additions & 1 deletion src/definitions/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import type {
SearchHistory,
} from './misc'
import type { Moment, MomentAsset } from './moment'
import type { Notice } from './notification'
import type { UserOauthLikecoinDB } from './oauth'
import type {
BlockchainSyncRecord,
Expand Down Expand Up @@ -233,6 +234,7 @@ export type TableTypeMap = {
moment_asset: MomentAsset
matters_choice: MattersChoice
matters_choice_topic: MattersChoiceTopic
notice: Notice
payout_account: PayoutAccount
punish_record: PunishRecord
recommended_articles_from_read_tags_materialized: RecommendedArticlesFromReadTagsMaterialized
Expand Down Expand Up @@ -263,7 +265,6 @@ type OtherTable =
| 'feedback'
| 'log_record'
| 'matters_choice_tag'
| 'notice'
| 'oauth_access_token'
| 'oauth_authorization_code'
| 'oauth_client'
Expand Down

0 comments on commit ae9a724

Please sign in to comment.