Skip to content

Commit

Permalink
REFACTOR delete unsubscribe reference using existing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
john-tco committed Oct 24, 2023
1 parent 1ab853b commit f1ec7a1
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 12 deletions.
21 changes: 20 additions & 1 deletion src/newsletter/newsletter.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 9 additions & 3 deletions src/newsletter/newsletter.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
})
Expand Down
3 changes: 2 additions & 1 deletion src/notifications/v2/notifications.savedSearch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/notifications/v2/notifications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions src/saved_search/saved_search.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Param,
Patch,
Post,
Query,
} from '@nestjs/common';
import { UserService } from '../user/user.service';
import { DeleteResult } from 'typeorm';
Expand All @@ -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')
Expand Down Expand Up @@ -61,12 +64,25 @@ export class SavedSearchController {
async delete(
@Param('id') savedSearchId: number,
@Body() body: { email: string },
@Query() query: { unsubscribeReference?: string },
): Promise<DeleteResult> {
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;
}
}
6 changes: 4 additions & 2 deletions src/saved_search/saved_search.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
})
Expand Down
20 changes: 19 additions & 1 deletion src/subscription/subscription.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { UnsubscribeService } from './../notifications/v2/unsubscribe/unsubscribe.service';
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Query,
Res,
} from '@nestjs/common';
import { Response } from 'express';
Expand All @@ -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<Subscription> {
const result = await this.subscriptionService.create(dto);
Expand Down Expand Up @@ -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<void> {
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();
Expand Down
8 changes: 5 additions & 3 deletions src/subscription/subscription.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
})
Expand Down

0 comments on commit f1ec7a1

Please sign in to comment.