-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-service): add create subscriber functionality with validation (
- Loading branch information
1 parent
d1fdc35
commit e511901
Showing
33 changed files
with
2,231 additions
and
88 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
apps/api/src/app/subscribers-v2/dtos/create-subscriber.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,91 @@ | ||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; | ||
import { IsEmail, IsLocale, IsObject, IsOptional, IsString, IsTimeZone, ValidateIf } from 'class-validator'; | ||
|
||
export class CreateSubscriberRequestDto { | ||
@ApiProperty({ | ||
type: String, | ||
description: 'Unique identifier of the subscriber', | ||
}) | ||
@IsString() | ||
subscriberId: string; | ||
|
||
@ApiPropertyOptional({ | ||
type: String, | ||
description: 'First name of the subscriber', | ||
nullable: true, | ||
}) | ||
@IsOptional() | ||
@ValidateIf((obj) => obj.firstName !== null) | ||
@IsString() | ||
firstName?: string | null; | ||
|
||
@ApiPropertyOptional({ | ||
type: String, | ||
description: 'Last name of the subscriber', | ||
nullable: true, | ||
}) | ||
@IsOptional() | ||
@ValidateIf((obj) => obj.lastName !== null) | ||
@IsString() | ||
lastName?: string | null; | ||
|
||
@ApiPropertyOptional({ | ||
type: String, | ||
description: 'Email address of the subscriber', | ||
nullable: true, | ||
}) | ||
@IsOptional() | ||
@ValidateIf((obj) => obj.email !== null) | ||
@IsEmail() | ||
email?: string | null; | ||
|
||
@ApiPropertyOptional({ | ||
type: String, | ||
description: 'Phone number of the subscriber', | ||
nullable: true, | ||
}) | ||
@IsOptional() | ||
@ValidateIf((obj) => obj.phone !== null) | ||
@IsString() | ||
phone?: string | null; | ||
|
||
@ApiPropertyOptional({ | ||
type: String, | ||
description: 'Avatar URL or identifier', | ||
nullable: true, | ||
}) | ||
@IsOptional() | ||
@ValidateIf((obj) => obj.avatar !== null) | ||
@IsString() | ||
avatar?: string | null; | ||
|
||
@ApiPropertyOptional({ | ||
type: String, | ||
description: 'Timezone of the subscriber', | ||
nullable: true, | ||
}) | ||
@IsOptional() | ||
@ValidateIf((obj) => obj.timezone !== null) | ||
@IsTimeZone() | ||
timezone?: string | null; | ||
|
||
@ApiPropertyOptional({ | ||
type: String, | ||
description: 'Locale of the subscriber', | ||
nullable: true, | ||
}) | ||
@IsOptional() | ||
@ValidateIf((obj) => obj.locale !== null) | ||
@IsLocale() | ||
locale?: string | null; | ||
|
||
@ApiPropertyOptional({ | ||
type: Object, | ||
description: 'Additional custom data for the subscriber', | ||
nullable: true, | ||
}) | ||
@IsOptional() | ||
@ValidateIf((obj) => obj.data !== null) | ||
@IsObject() | ||
data?: Record<string, unknown> | null; | ||
} |
57 changes: 57 additions & 0 deletions
57
apps/api/src/app/subscribers-v2/e2e/create-subscriber.e2e.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,57 @@ | ||
import { expect } from 'chai'; | ||
import { Novu } from '@novu/api'; | ||
import { UserSession } from '@novu/testing'; | ||
import { randomBytes } from 'crypto'; | ||
import { expectSdkExceptionGeneric, initNovuClassSdk } from '../../shared/helpers/e2e/sdk/e2e-sdk.helper'; | ||
|
||
let session: UserSession; | ||
|
||
describe('Create Subscriber - /subscribers (POST) #novu-v2', () => { | ||
let novuClient: Novu; | ||
|
||
beforeEach(async () => { | ||
session = new UserSession(); | ||
await session.initialize(); | ||
novuClient = initNovuClassSdk(session); | ||
}); | ||
|
||
it('should create the subscriber', async () => { | ||
const subscriberId = `test-subscriber-${`${randomBytes(4).toString('hex')}`}`; | ||
const payload = { | ||
subscriberId, | ||
firstName: 'First Name', | ||
lastName: 'Last Name', | ||
locale: 'en_US', | ||
timezone: 'America/New_York', | ||
}; | ||
|
||
const res = await novuClient.subscribers.create(payload, payload.subscriberId); | ||
|
||
const subscriber = res.result; | ||
|
||
expect(subscriber.subscriberId).to.equal(payload.subscriberId); | ||
expect(subscriber.firstName).to.equal(payload.firstName); | ||
expect(subscriber.lastName).to.equal(payload.lastName); | ||
expect(subscriber.locale).to.equal(payload.locale); | ||
expect(subscriber.timezone).to.equal(payload.timezone); | ||
}); | ||
|
||
it('should return 409 if subscriberId already exists', async () => { | ||
const subscriberId = `test-subscriber-${`${randomBytes(4).toString('hex')}`}`; | ||
const payload = { | ||
subscriberId, | ||
firstName: 'First Name', | ||
lastName: 'Last Name', | ||
locale: 'en_US', | ||
timezone: 'America/New_York', | ||
}; | ||
|
||
await novuClient.subscribers.create(payload, payload.subscriberId); | ||
|
||
const { error } = await expectSdkExceptionGeneric(() => | ||
novuClient.subscribers.create(payload, payload.subscriberId) | ||
); | ||
|
||
expect(error?.statusCode).to.equal(409); | ||
}); | ||
}); |
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
10 changes: 10 additions & 0 deletions
10
apps/api/src/app/subscribers-v2/usecases/create-subscriber/create-subscriber.command.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,10 @@ | ||
import { Type } from 'class-transformer'; | ||
import { ValidateNested } from 'class-validator'; | ||
import { EnvironmentCommand } from '../../../shared/commands/project.command'; | ||
import { CreateSubscriberRequestDto } from '../../dtos/create-subscriber.dto'; | ||
|
||
export class CreateSubscriberCommand extends EnvironmentCommand { | ||
@ValidateNested() | ||
@Type(() => CreateSubscriberRequestDto) | ||
createSubscriberRequestDto: CreateSubscriberRequestDto; | ||
} |
30 changes: 30 additions & 0 deletions
30
apps/api/src/app/subscribers-v2/usecases/create-subscriber/create-subscriber.usecase.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,30 @@ | ||
import { Injectable, ConflictException } from '@nestjs/common'; | ||
import { SubscriberRepository } from '@novu/dal'; | ||
import { SubscriberResponseDto } from '../../../subscribers/dtos'; | ||
import { mapSubscriberEntityToDto } from '../list-subscribers/map-subscriber-entity-to.dto'; | ||
import { CreateSubscriberCommand } from './create-subscriber.command'; | ||
|
||
@Injectable() | ||
export class CreateSubscriber { | ||
constructor(private subscriberRepository: SubscriberRepository) {} | ||
|
||
async execute(command: CreateSubscriberCommand): Promise<SubscriberResponseDto> { | ||
const existingSubscriber = await this.subscriberRepository.findOne({ | ||
subscriberId: command.createSubscriberRequestDto.subscriberId, | ||
_environmentId: command.environmentId, | ||
_organizationId: command.organizationId, | ||
}); | ||
|
||
if (existingSubscriber) { | ||
throw new ConflictException(`Subscriber: ${command.createSubscriberRequestDto.subscriberId} already exists`); | ||
} | ||
|
||
const createdSubscriber = await this.subscriberRepository.create({ | ||
...command.createSubscriberRequestDto, | ||
_environmentId: command.environmentId, | ||
_organizationId: command.organizationId, | ||
}); | ||
|
||
return mapSubscriberEntityToDto(createdSubscriber); | ||
} | ||
} |
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
Oops, something went wrong.