From ae9a72441ac0c3115a7fe51bee02fc08b54f2c0f Mon Sep 17 00:00:00 2001 From: j <13580441+gary02@users.noreply.github.com> Date: Wed, 31 Jul 2024 14:03:12 +0800 Subject: [PATCH] feat(notice): `cancel` delete recently sent notices recorded in redis by lambda related PR https://github.com/thematters/lambda-handlers/pull/152 --- src/connectors/notificationService/index.ts | 35 +++++++++++++++------ src/definitions/index.d.ts | 3 +- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/connectors/notificationService/index.ts b/src/connectors/notificationService/index.ts index 3bfd28fca..bee09cf65 100644 --- a/src/connectors/notificationService/index.ts +++ b/src/connectors/notificationService/index.ts @@ -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) => { @@ -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({ @@ -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 @@ -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 }, + }) } diff --git a/src/definitions/index.d.ts b/src/definitions/index.d.ts index 4c4cca5af..8545aab82 100644 --- a/src/definitions/index.d.ts +++ b/src/definitions/index.d.ts @@ -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, @@ -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 @@ -263,7 +265,6 @@ type OtherTable = | 'feedback' | 'log_record' | 'matters_choice_tag' - | 'notice' | 'oauth_access_token' | 'oauth_authorization_code' | 'oauth_client'