-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1335 from darionnuel/feat/resubscribe-newsletter
feat: Create an Endpoint to Resubscribe to Newsletter
- Loading branch information
Showing
5 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/modules/newsletter-subscription/dto/resubscribe-newsletter.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { IsEmail, IsNotEmpty, IsOptional, IsUUID } from 'class-validator'; | ||
|
||
export class ResubscribeNewsletterDto { | ||
@IsOptional() | ||
@IsUUID() | ||
id?: string; | ||
|
||
@IsOptional() | ||
@IsEmail() | ||
email?: string; | ||
|
||
@IsNotEmpty() | ||
validateUserIdentifier?() { | ||
if (!this.id && !this.email) { | ||
throw new Error('Either userId or email must be provided.'); | ||
} | ||
} | ||
} |
16 changes: 14 additions & 2 deletions
16
src/modules/newsletter-subscription/entities/newsletter-subscription.entity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
import { AbstractBaseEntity } from '../../../entities/base.entity'; | ||
import { Column, DeleteDateColumn, Entity } from 'typeorm'; | ||
import { Column, DeleteDateColumn, Entity, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm'; | ||
|
||
@Entity('newsletters') | ||
export class NewsletterSubscription extends AbstractBaseEntity { | ||
@Column({ unique: true }) | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string; | ||
|
||
@Column({ unique: true, nullable: true }) | ||
email: string; | ||
|
||
@CreateDateColumn() | ||
createdAt: Date; | ||
|
||
@UpdateDateColumn() | ||
updatedAt: Date; | ||
|
||
@DeleteDateColumn() | ||
deletedAt: Date; | ||
|
||
@Column({ default: false }) | ||
isUnsubscribed: boolean; | ||
|
||
@Column({ default: 'inactive' }) // inactive, active, unsubscribed | ||
status: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,4 +189,38 @@ describe('NewsletterService', () => { | |
expect(repository.findOne).toHaveBeenCalledWith({ where: { email } }); | ||
}); | ||
}); | ||
|
||
describe('resubscribe', () => { | ||
it('should successfully resubscribe a user who was unsubscribed', async () => { | ||
const dto = { email: '[email protected]' }; | ||
const mockSubscription = { | ||
id: '1', | ||
email: '[email protected]', | ||
status: 'unsubscribed', | ||
} as NewsletterSubscription; | ||
|
||
jest.spyOn(repository, 'findOne').mockResolvedValue(mockSubscription); | ||
jest.spyOn(repository, 'save').mockResolvedValue({ ...mockSubscription, status: 'active' }); | ||
|
||
const result = await service.resubscribe(dto); | ||
|
||
expect(result).toEqual({ message: 'Successfully resubscribed to the newsletter.' }); | ||
expect(repository.findOne).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
where: expect.arrayContaining([expect.objectContaining({ email: dto.email })]), | ||
}) | ||
); | ||
expect(repository.save).toHaveBeenCalledWith(expect.objectContaining({ status: 'active' })); | ||
}); | ||
|
||
it('should throw NotFoundException if user is not found or was never subscribed', async () => { | ||
const dto = { email: '[email protected]' }; | ||
|
||
jest.spyOn(repository, 'findOne').mockResolvedValue(null); | ||
|
||
await expect(service.resubscribe(dto)).rejects.toThrow( | ||
new NotFoundException('User not found or not unsubscribed.') | ||
); | ||
}); | ||
}); | ||
}); |