Skip to content

Commit

Permalink
Merge pull request #1358 from hngprojects/feat/unsubscribe-newsletter
Browse files Browse the repository at this point in the history
refactor(http): rename statusCode to status_code and prevent email exposure in response
  • Loading branch information
AdeGneus authored Mar 2, 2025
2 parents 8f3b464 + b266f4e commit 3acd2f5
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { initializeDataSource } from '@database/data-source';
import { SeedingService } from '@database/seeding/seeding.service';
import { ResponseInterceptor } from '@shared/inteceptors/response.interceptor';
import { Request, Response } from 'express';
import { HttpExceptionFilter } from '@shared/helpers/http-exception-filter';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule, { bufferLogs: true });

Expand All @@ -34,6 +35,7 @@ async function bootstrap() {
app.enableCors();
app.setGlobalPrefix('api/v1', { exclude: ['/', 'health', 'api', 'api/v1', 'api/docs', 'probe'] });
app.useGlobalInterceptors(new ResponseInterceptor());
app.useGlobalFilters(new HttpExceptionFilter());

const options = new DocumentBuilder()
.setTitle('HNG Boilerplate')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ export class NewsletterSubscriptionService {
const subscription = await this.newsletterSubscriptionRepository.findOne({ where: { email } });

if (!subscription) {
throw new NotFoundException(`Email ${email} not found in the subscription list`);
throw new NotFoundException('Email not found in the subscription list');
}

subscription.isUnsubscribed = true;
await this.newsletterSubscriptionRepository.save(subscription);

return { message: `Email ${email} has been unsubscribed successfully` };
return { message: 'Email has been unsubscribed successfully' };
}
async resubscribe(dto: ResubscribeNewsletterDto): Promise<{ message: string }> {
const { id, email } = dto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ describe('NewsletterService', () => {
const email = '[email protected]';
const mockSubscription = { id: '1', email, isUnsubscribed: false } as NewsletterSubscription;

// Mock `findOne` to return a subscribed user
jest.spyOn(repository, 'findOne').mockResolvedValue(mockSubscription);
// Mock `save` to return updated object
jest.spyOn(repository, 'save').mockImplementation(
async sub =>
({
Expand All @@ -169,21 +167,18 @@ describe('NewsletterService', () => {
);

const result = await service.unsubscribe(email);
expect(result).toEqual({ message: `Email ${email} has been unsubscribed successfully` });
expect(result).toEqual({ message: 'Email has been unsubscribed successfully' });

// Ensure `isUnsubscribed` was updated
expect(repository.save).toHaveBeenCalledWith(expect.objectContaining({ isUnsubscribed: true }));
});

it('should throw NotFoundException if email is not found', async () => {
const email = '[email protected]';

// Mock `findOne` to return null
jest.spyOn(repository, 'findOne').mockResolvedValue(null);

// Expecting an error
await expect(service.unsubscribe(email)).rejects.toThrow(
new NotFoundException(`Email ${email} not found in the subscription list`)
new NotFoundException('Email not found in the subscription list')
);

expect(repository.findOne).toHaveBeenCalledWith({ where: { email } });
Expand Down
2 changes: 1 addition & 1 deletion src/shared/helpers/http-exception-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class HttpExceptionFilter implements ExceptionFilter {
const error = typeof exceptionResponse === 'string' ? '' : (exceptionResponse as ExceptionResponse).error;

response.status(status).json({
status,
status_code: status,
error: error,
message: errorMessage,
});
Expand Down

0 comments on commit 3acd2f5

Please sign in to comment.