Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Add idempotent subscriber credential update operation #5211

Merged
merged 3 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions apps/api/src/app/subscribers/subscribers.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,35 @@ export class SubscribersController {
credentials: body.credentials,
integrationIdentifier: body.integrationIdentifier,
oauthHandler: OAuthHandlerEnum.EXTERNAL,
isIdempotentOperation: true,
})
);
}

@Patch('/:subscriberId/credentials')
@ExternalApiAccessible()
@UseGuards(UserAuthGuard)
@ApiResponse(SubscriberResponseDto)
@ApiOperation({
summary: 'Modify subscriber credentials',
description: `Subscriber credentials associated to the delivery methods such as slack and push tokens.
This endpoint appends provided credentials and deviceTokens to the existing ones.`,
})
async modifySubscriberChannel(
@UserSession() user: IJwtPayload,
@Param('subscriberId') subscriberId: string,
@Body() body: UpdateSubscriberChannelRequestDto
): Promise<SubscriberResponseDto> {
return await this.updateSubscriberChannelUsecase.execute(
UpdateSubscriberChannelCommand.create({
environmentId: user.environmentId,
organizationId: user.organizationId,
subscriberId,
providerId: body.providerId,
credentials: body.credentials,
integrationIdentifier: body.integrationIdentifier,
oauthHandler: OAuthHandlerEnum.EXTERNAL,
isIdempotentOperation: false,
})
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class ChatOauthCallback {
integrationIdentifier: command.integrationIdentifier,
credentials: subscriberCredentials,
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: false,
})
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('Delete subscriber provider credentials', function () {
providerId: ChatProviderIdEnum.Discord,
credentials: { webhookUrl: 'newWebhookUrl' },
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: false,
})
);

Expand All @@ -52,6 +53,7 @@ describe('Delete subscriber provider credentials', function () {
providerId: PushProviderIdEnum.FCM,
credentials: { deviceTokens: fcmTokens },
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: false,
})
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IsNotEmpty, IsOptional, IsString, ValidateNested } from 'class-validator';
import { IsBoolean, IsNotEmpty, IsOptional, IsString, ValidateNested } from 'class-validator';
import { EnvironmentCommand } from '../../../shared/commands/project.command';
import { ChatProviderIdEnum, PushProviderIdEnum } from '@novu/shared';
import { ChannelCredentials, SubscriberChannel } from '../../../shared/dtos/subscriber-channel';
Expand Down Expand Up @@ -33,4 +33,7 @@ export class UpdateSubscriberChannelCommand extends EnvironmentCommand implement
@IsOptional()
@IsString()
integrationIdentifier?: string;

@IsBoolean()
isIdempotentOperation: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('Update Subscriber channel credentials', function () {
providerId: subscriberChannel.providerId,
credentials: subscriberChannel.credentials,
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: true,
})
);

Expand Down Expand Up @@ -70,6 +71,7 @@ describe('Update Subscriber channel credentials', function () {
providerId: ChatProviderIdEnum.Discord,
credentials: { webhookUrl: 'webhookUrl' },
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: true,
})
);

Expand All @@ -85,6 +87,7 @@ describe('Update Subscriber channel credentials', function () {
providerId: newSlackSubscribersChannel.providerId,
credentials: newSlackSubscribersChannel.credentials,
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: true,
})
);

Expand Down Expand Up @@ -122,6 +125,7 @@ describe('Update Subscriber channel credentials', function () {
providerId: newSlackCredentials.providerId,
credentials: newSlackCredentials.credentials,
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: true,
})
);

Expand Down Expand Up @@ -163,6 +167,7 @@ describe('Update Subscriber channel credentials', function () {
providerId: ChatProviderIdEnum.Slack,
credentials: { webhookUrl },
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: true,
})
);

Expand All @@ -178,7 +183,7 @@ describe('Update Subscriber channel credentials', function () {
expect(updatedChannel?.credentials.webhookUrl).to.equal(webhookUrl);
});

it('should not add duplicated token ', async function () {
it('should not add duplicated token when the operation IS idempotent', async function () {
const subscriberService = new SubscribersService(session.organization._id, session.environment._id);
const subscriber = await subscriberService.createSubscriber();

Expand All @@ -195,6 +200,42 @@ describe('Update Subscriber channel credentials', function () {
providerId: fcmCredentials.providerId,
credentials: fcmCredentials.credentials,
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: true,
})
);

let updatedSubscriber = await subscriberRepository.findOne({
_id: subscriber._id,
_environmentId: subscriber._environmentId,
});

const addedFcmToken = updatedSubscriber?.channels?.find(
(channel) => channel.providerId === fcmCredentials.providerId
);

expect(addedFcmToken?.providerId).to.equal(PushProviderIdEnum.FCM);
expect(addedFcmToken?.credentials?.deviceTokens?.length).to.equal(1);
expect(addedFcmToken?.credentials?.deviceTokens).to.deep.equal(['token_1']);
});

it('should not add duplicated token when the operation IS NOT idempotent', async function () {
const subscriberService = new SubscribersService(session.organization._id, session.environment._id);
const subscriber = await subscriberService.createSubscriber();

const fcmCredentials = {
providerId: PushProviderIdEnum.FCM,
credentials: { deviceTokens: ['token_1', 'token_1'] },
};

await updateSubscriberChannelUsecase.execute(
UpdateSubscriberChannelCommand.create({
organizationId: subscriber._organizationId,
subscriberId: subscriber.subscriberId,
environmentId: session.environment._id,
providerId: fcmCredentials.providerId,
credentials: fcmCredentials.credentials,
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: false,
})
);

Expand All @@ -212,7 +253,7 @@ describe('Update Subscriber channel credentials', function () {
expect(addedFcmToken?.credentials?.deviceTokens).to.deep.equal(['identifier', 'token_1']);
});

it('should update deviceTokens with empty array', async function () {
it('should append to existing device token array when the operation IS NOT idempotent', async function () {
const subscriberService = new SubscribersService(session.organization._id, session.environment._id);
const subscriber = await subscriberService.createSubscriber();

Expand All @@ -229,6 +270,7 @@ describe('Update Subscriber channel credentials', function () {
providerId: fcmCredentials.providerId,
credentials: fcmCredentials.credentials,
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: false,
})
);

Expand All @@ -241,8 +283,43 @@ describe('Update Subscriber channel credentials', function () {
(channel) => channel.providerId === fcmCredentials.providerId
);

expect(addedFcmToken?.providerId).to.equal(PushProviderIdEnum.FCM);
expect(addedFcmToken?.credentials?.deviceTokens?.length).to.equal(2);
expect(addedFcmToken?.credentials?.deviceTokens).to.deep.equal(['identifier', 'token_1']);
});

it('should update deviceTokens with empty array', async function () {
const subscriberService = new SubscribersService(session.organization._id, session.environment._id);
const subscriber = await subscriberService.createSubscriber();

const fcmCredentials = {
providerId: PushProviderIdEnum.FCM,
credentials: { deviceTokens: ['token_1'] },
};

await updateSubscriberChannelUsecase.execute(
UpdateSubscriberChannelCommand.create({
organizationId: subscriber._organizationId,
subscriberId: subscriber.subscriberId,
environmentId: session.environment._id,
providerId: fcmCredentials.providerId,
credentials: fcmCredentials.credentials,
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: true,
})
);

let updatedSubscriber = await subscriberRepository.findOne({
_id: subscriber._id,
_environmentId: subscriber._environmentId,
});

const addedFcmToken = updatedSubscriber?.channels?.find(
(channel) => channel.providerId === fcmCredentials.providerId
);

expect(addedFcmToken?.credentials?.deviceTokens?.length).to.equal(1);
expect(addedFcmToken?.credentials?.deviceTokens).to.deep.equal(['token_1']);

await updateSubscriberChannelUsecase.execute(
UpdateSubscriberChannelCommand.create({
Expand All @@ -252,6 +329,7 @@ describe('Update Subscriber channel credentials', function () {
providerId: fcmCredentials.providerId,
credentials: { deviceTokens: [] },
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: true,
})
);

Expand Down Expand Up @@ -280,6 +358,7 @@ describe('Update Subscriber channel credentials', function () {
providerId: PushProviderIdEnum.FCM,
credentials: { deviceTokens: ['token_1'] },
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: true,
})
);

Expand All @@ -290,8 +369,8 @@ describe('Update Subscriber channel credentials', function () {

let updateToken = updatedSubscriber?.channels?.find((channel) => channel.providerId === PushProviderIdEnum.FCM);

expect(updateToken?.credentials?.deviceTokens?.length).to.equal(2);
expect(updateToken?.credentials?.deviceTokens).to.deep.equal(['identifier', 'token_1']);
expect(updateToken?.credentials?.deviceTokens?.length).to.equal(1);
expect(updateToken?.credentials?.deviceTokens).to.deep.equal(['token_1']);

await updateSubscriberChannelUsecase.execute(
UpdateSubscriberChannelCommand.create({
Expand All @@ -301,6 +380,7 @@ describe('Update Subscriber channel credentials', function () {
providerId: PushProviderIdEnum.FCM,
credentials: { deviceTokens: ['token_1', 'token_2', 'token_2', 'token_3'] },
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: true,
})
);

Expand All @@ -311,8 +391,8 @@ describe('Update Subscriber channel credentials', function () {

updateToken = updatedSubscriber?.channels?.find((channel) => channel.providerId === PushProviderIdEnum.FCM);

expect(updateToken?.credentials?.deviceTokens?.length).to.equal(4);
expect(updateToken?.credentials?.deviceTokens).to.deep.equal(['identifier', 'token_1', 'token_2', 'token_3']);
expect(updateToken?.credentials?.deviceTokens?.length).to.equal(3);
expect(updateToken?.credentials?.deviceTokens).to.deep.equal(['token_1', 'token_2', 'token_3']);

await updateSubscriberChannelUsecase.execute(
UpdateSubscriberChannelCommand.create({
Expand All @@ -322,6 +402,7 @@ describe('Update Subscriber channel credentials', function () {
providerId: PushProviderIdEnum.FCM,
credentials: { deviceTokens: ['token_555'] },
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: true,
})
);

Expand All @@ -332,14 +413,8 @@ describe('Update Subscriber channel credentials', function () {

updateToken = updatedSubscriber?.channels?.find((channel) => channel.providerId === PushProviderIdEnum.FCM);

expect(updateToken?.credentials?.deviceTokens?.length).to.equal(5);
expect(updateToken?.credentials?.deviceTokens).to.deep.equal([
'identifier',
'token_1',
'token_2',
'token_3',
'token_555',
]);
expect(updateToken?.credentials?.deviceTokens?.length).to.equal(1);
expect(updateToken?.credentials?.deviceTokens).to.deep.equal(['token_555']);
});

it('should update deviceTokens without duplication on channel creation (addChannelToSubscriber)', async function () {
Expand All @@ -362,6 +437,7 @@ describe('Update Subscriber channel credentials', function () {
providerId: PushProviderIdEnum.FCM,
credentials: { deviceTokens: ['token_1', 'token_1', 'token_1'] },
oauthHandler: OAuthHandlerEnum.NOVU,
isIdempotentOperation: true,
})
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export class UpdateSubscriberChannel {
command.environmentId,
existingChannel,
updatePayload,
foundSubscriber
foundSubscriber,
command.isIdempotentOperation
);
} else {
await this.addChannelToSubscriber(updatePayload, foundIntegration, command, foundSubscriber);
Expand Down Expand Up @@ -110,7 +111,8 @@ export class UpdateSubscriberChannel {
environmentId: string,
existingChannel: IChannelSettings,
updatePayload: Partial<IChannelSettings>,
foundSubscriber: SubscriberEntity
foundSubscriber: SubscriberEntity,
isIdempotentOperation: boolean
) {
const equal = isEqual(existingChannel.credentials, updatePayload.credentials);

Expand All @@ -121,10 +123,14 @@ export class UpdateSubscriberChannel {
let deviceTokens: string[] = [];

if (updatePayload.credentials?.deviceTokens) {
deviceTokens = this.unionDeviceTokens(
existingChannel.credentials.deviceTokens ?? [],
updatePayload.credentials.deviceTokens
);
if (isIdempotentOperation) {
deviceTokens = this.unionDeviceTokens([], updatePayload.credentials.deviceTokens);
} else {
deviceTokens = this.unionDeviceTokens(
existingChannel.credentials.deviceTokens ?? [],
updatePayload.credentials.deviceTokens
);
}
}

await this.invalidateCache.invalidateByKey({
Expand Down
Loading