diff --git a/src/newsletter/newsletter.controller.ts b/src/newsletter/newsletter.controller.ts index ada419f..8e92eec 100644 --- a/src/newsletter/newsletter.controller.ts +++ b/src/newsletter/newsletter.controller.ts @@ -6,14 +6,19 @@ import { Param, Body, Res, + Query, } from '@nestjs/common'; import { NewsletterService } from './newsletter.service'; import { Response } from 'express'; import { NewsletterType } from './newsletter.entity'; +import { UnsubscribeService } from 'src/notifications/v2/unsubscribe/unsubscribe.service'; @Controller('newsletters') export class NewsletterController { - constructor(private newsletterService: NewsletterService) {} + constructor( + private newsletterService: NewsletterService, + private unsubscribeService: UnsubscribeService, + ) {} @Get() async findAll() { @@ -57,11 +62,25 @@ export class NewsletterController { @Param('plainTextEmailAddress') plainTextEmailAddress: string, @Param('newsletterType') type: NewsletterType, @Res() response: Response, + @Query() query: { unsubscribeReference?: string }, ) { const result = await this.newsletterService.deleteByEmailAddressAndType( plainTextEmailAddress, type, ); + if (query?.unsubscribeReference) { + await this.unsubscribeService + .deleteOneById(query.unsubscribeReference) + .catch((error: unknown) => { + console.error( + `Failed to unsubscribe from unsubscribeReference: + ${ + query.unsubscribeReference + }. error:${JSON.stringify(error)}`, + ); + }); + } + result == 0 ? response.status(404) : response.status(204); response.send(); diff --git a/src/newsletter/newsletter.module.ts b/src/newsletter/newsletter.module.ts index 426e070..100fd92 100644 --- a/src/newsletter/newsletter.module.ts +++ b/src/newsletter/newsletter.module.ts @@ -4,11 +4,17 @@ import { NewsletterController } from './newsletter.controller'; import { Newsletter } from './newsletter.entity'; import { NewsletterService } from './newsletter.service'; import { HashModule } from '../hash/hash.module'; -import { UserModule } from 'src/user/user.module'; +import { UserModule } from '../user/user.module'; +import { Unsubscribe } from '../notifications/v2/unsubscribe/unsubscribe.entity'; +import { UnsubscribeService } from '../notifications/v2/unsubscribe/unsubscribe.service'; @Module({ - imports: [TypeOrmModule.forFeature([Newsletter]), HashModule, UserModule], - providers: [NewsletterService], + imports: [ + TypeOrmModule.forFeature([Newsletter, Unsubscribe]), + HashModule, + UserModule, + ], + providers: [NewsletterService, UnsubscribeService], exports: [NewsletterService], controllers: [NewsletterController], }) diff --git a/src/notifications/v2/notifications.savedSearch.service.ts b/src/notifications/v2/notifications.savedSearch.service.ts index 2576db3..f275782 100644 --- a/src/notifications/v2/notifications.savedSearch.service.ts +++ b/src/notifications/v2/notifications.savedSearch.service.ts @@ -5,7 +5,7 @@ import { EmailService } from '../../email/email.service'; import { GrantService } from '../../grant/grant.service'; import { SavedSearchService } from '../../saved_search/saved_search.service'; import { SavedSearchNotificationService } from '../../saved_search_notification/saved_search_notification.service'; -import { FilterArray, NOTIFICATION_TYPES } from '../notifications.types'; +import { FilterArray } from '../notifications.types'; import { NotificationsHelper, addSearchTerm, @@ -14,6 +14,7 @@ import { } from './notifications.helper'; import { SavedSearchNotification } from '../../saved_search_notification/saved_search_notification.entity'; import { SavedSearch } from '../../saved_search/saved_search.entity'; +import { performance } from 'perf_hooks'; @Injectable() export class SavedSearchNotificationsService { diff --git a/src/notifications/v2/notifications.service.ts b/src/notifications/v2/notifications.service.ts index a4efa2b..0f71cf3 100644 --- a/src/notifications/v2/notifications.service.ts +++ b/src/notifications/v2/notifications.service.ts @@ -16,7 +16,7 @@ export class v2NotificationsService { private schedularRegistry: SchedulerRegistry, ) {} - async processScheduledJob({ timer, type }: ScheduledJob, index: number) { + processScheduledJob({ timer, type }: ScheduledJob, index: number) { const CRON_JOB_MAP = { [ScheduledJobType.GRANT_UPDATED]: this.v2GrantService.processGrantUpdatedNotifications, diff --git a/src/saved_search/saved_search.controller.ts b/src/saved_search/saved_search.controller.ts index ab38df0..0ab02fe 100644 --- a/src/saved_search/saved_search.controller.ts +++ b/src/saved_search/saved_search.controller.ts @@ -6,6 +6,7 @@ import { Param, Patch, Post, + Query, } from '@nestjs/common'; import { UserService } from '../user/user.service'; import { DeleteResult } from 'typeorm'; @@ -14,12 +15,14 @@ import { GetSavedSearchDto } from './get_saved_search.dto'; import { CreateSavedSearchDto } from './saved_search.dto'; import { SavedSearch, SavedSearchStatusType } from './saved_search.entity'; import { SavedSearchService } from './saved_search.service'; +import { UnsubscribeService } from 'src/notifications/v2/unsubscribe/unsubscribe.service'; @Controller('saved-searches') export class SavedSearchController { constructor( private savedSearchService: SavedSearchService, private userService: UserService, + private unsubscribeService: UnsubscribeService, ) {} @Get(':plainTextEmailAddress') @@ -61,12 +64,25 @@ export class SavedSearchController { async delete( @Param('id') savedSearchId: number, @Body() body: { email: string }, + @Query() query: { unsubscribeReference?: string }, ): Promise { const user = await this.userService.findByEmail(body.email); const deleteResult = await this.savedSearchService.delete( savedSearchId, user, ); + if (query?.unsubscribeReference) { + await this.unsubscribeService + .deleteOneById(query.unsubscribeReference) + .catch((error: unknown) => { + console.error( + `Failed to unsubscribe from unsubscribeReference: + ${ + query.unsubscribeReference + }. error:${JSON.stringify(error)}`, + ); + }); + } return deleteResult; } } diff --git a/src/saved_search/saved_search.module.ts b/src/saved_search/saved_search.module.ts index 4ac153c..623026e 100644 --- a/src/saved_search/saved_search.module.ts +++ b/src/saved_search/saved_search.module.ts @@ -4,10 +4,12 @@ import { UserModule } from 'src/user/user.module'; import { SavedSearchController } from './saved_search.controller'; import { SavedSearch } from './saved_search.entity'; import { SavedSearchService } from './saved_search.service'; +import { UnsubscribeService } from '../notifications/v2/unsubscribe/unsubscribe.service'; +import { Unsubscribe } from '../notifications/v2/unsubscribe/unsubscribe.entity'; @Module({ - imports: [TypeOrmModule.forFeature([SavedSearch]), UserModule], - providers: [SavedSearchService], + imports: [TypeOrmModule.forFeature([SavedSearch, Unsubscribe]), UserModule], + providers: [SavedSearchService, UnsubscribeService], exports: [SavedSearchService], controllers: [SavedSearchController], }) diff --git a/src/subscription/subscription.controller.ts b/src/subscription/subscription.controller.ts index 5f726b3..9ba00a2 100644 --- a/src/subscription/subscription.controller.ts +++ b/src/subscription/subscription.controller.ts @@ -1,3 +1,4 @@ +import { UnsubscribeService } from './../notifications/v2/unsubscribe/unsubscribe.service'; import { Body, Controller, @@ -5,6 +6,7 @@ import { Get, Param, Post, + Query, Res, } from '@nestjs/common'; import { Response } from 'express'; @@ -14,7 +16,10 @@ import { SubscriptionService } from './subscription.service'; @Controller('subscriptions') export class SubscriptionController { - constructor(private subscriptionService: SubscriptionService) {} + constructor( + private subscriptionService: SubscriptionService, + private unsubscribeService: UnsubscribeService, + ) {} @Post() async create(@Body() dto: CreateSubscriptionDto): Promise { const result = await this.subscriptionService.create(dto); @@ -46,12 +51,25 @@ export class SubscriptionController { async deleteByEmailAndGrantId( @Param('plainTextEmailAddress') plainTextEmailAddress: string, @Param('grantId') grantId: string, + @Query() query: { unsubscribeReference?: string }, @Res() response: Response, ): Promise { const result = await this.subscriptionService.deleteByEmailAndGrantId( plainTextEmailAddress, grantId, ); + if (query?.unsubscribeReference) { + await this.unsubscribeService + .deleteOneById(query.unsubscribeReference) + .catch((error: unknown) => { + console.error( + `Failed to unsubscribe from unsubscribeReference: + ${ + query.unsubscribeReference + }. error:${JSON.stringify(error)}`, + ); + }); + } result.affected == 0 ? response.status(404) : response.status(204); response.send(); diff --git a/src/subscription/subscription.module.ts b/src/subscription/subscription.module.ts index a4ee947..2c6bae0 100644 --- a/src/subscription/subscription.module.ts +++ b/src/subscription/subscription.module.ts @@ -6,15 +6,17 @@ import { EncryptionModule } from '../encryption/encryption.module'; import { HashModule } from '../hash/hash.module'; import { SubscriptionController } from './subscription.controller'; import { UserModule } from 'src/user/user.module'; +import { Unsubscribe } from '../notifications/v2/unsubscribe/unsubscribe.entity'; +import { UnsubscribeService } from '../notifications/v2/unsubscribe/unsubscribe.service'; @Module({ imports: [ - TypeOrmModule.forFeature([Subscription]), + TypeOrmModule.forFeature([Subscription, Unsubscribe]), EncryptionModule, HashModule, - UserModule + UserModule, ], - providers: [SubscriptionService], + providers: [SubscriptionService, UnsubscribeService], exports: [SubscriptionService], controllers: [SubscriptionController], })