Skip to content

Commit

Permalink
fix(api-service): on global preferences update reset workflow prefere…
Browse files Browse the repository at this point in the history
…nces per env (#7428)

Co-authored-by: Sokratis Vidros <[email protected]>
  • Loading branch information
LetItRock and SokratisVidros authored Jan 7, 2025
1 parent a581555 commit e9571c3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
70 changes: 68 additions & 2 deletions apps/api/src/app/subscribers/e2e/update-global-preference.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { ChannelTypeEnum } from '@novu/shared';
import { ChannelTypeEnum, StepTypeEnum } from '@novu/shared';
import { UserSession } from '@novu/testing';
import { expect } from 'chai';

import { updateGlobalPreferences } from './helpers';
import { SubscriberRepository } from '@novu/dal';
import { getPreference, updateGlobalPreferences } from './helpers';

describe('Update Subscribers global preferences - /subscribers/:subscriberId/preferences (PATCH)', function () {
let session: UserSession;
let subscriberRepository: SubscriberRepository;

beforeEach(async () => {
session = new UserSession();
subscriberRepository = new SubscriberRepository();
await session.initialize();
});

Expand Down Expand Up @@ -89,6 +92,69 @@ describe('Update Subscribers global preferences - /subscribers/:subscriberId/pre
});
});

it('should update user global preferences only for the current environment', async function () {
// create a template in dev environment
await session.createTemplate({
steps: [
{
type: StepTypeEnum.IN_APP,
content: 'Hello',
},
],
noFeedId: true,
});

await session.switchToProdEnvironment();
// create a subscriber in prod environment
await subscriberRepository.create({
_environmentId: session.environment._id,
_organizationId: session.organization._id,
subscriberId: session.subscriberId,
});
// create a template in prod environment
await session.createTemplate({
steps: [
{
type: StepTypeEnum.IN_APP,
content: 'Hello',
},
],
noFeedId: true,
});

await session.switchToDevEnvironment();
// update the subscriber global preferences in dev environment
const response = await updateGlobalPreferences(
{
enabled: true,
preferences: [{ type: ChannelTypeEnum.IN_APP, enabled: false }],
},
session
);

expect(response.data.data.preference.enabled).to.eql(true);
expect(response.data.data.preference.channels).to.eql({
[ChannelTypeEnum.EMAIL]: true,
[ChannelTypeEnum.PUSH]: true,
[ChannelTypeEnum.CHAT]: true,
[ChannelTypeEnum.SMS]: true,
[ChannelTypeEnum.IN_APP]: false,
});

// get the subscriber preferences in dev environment
const getDevPreferencesResponse = await getPreference(session);
const devPreferences = getDevPreferencesResponse.data.data;
expect(devPreferences.every((item) => !!item.preference.channels.in_app)).to.be.false;

await session.switchToProdEnvironment();

// get the subscriber preferences in prod environment
session.apiKey = session.environment.apiKeys[0].key;
const getProdPreferencesResponse = await getPreference(session);
const prodPreferences = getProdPreferencesResponse.data.data;
expect(prodPreferences.every((item) => !!item.preference.channels.in_app)).to.be.true;
});

// `enabled` flag is not used anymore. The presence of a preference object means that the subscriber has enabled notifications.
it.skip('should update user global preference and disable the flag for the future channels update', async function () {
const disablePreferenceData = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { UpsertSubscriberGlobalPreferencesCommand } from './upsert-subscriber-gl
import { UpsertSubscriberWorkflowPreferencesCommand } from './upsert-subscriber-workflow-preferences.command';
import { UpsertUserWorkflowPreferencesCommand } from './upsert-user-workflow-preferences.command';
import { deepMerge } from '../../utils';
import { Instrument } from '../../instrumentation';

export type WorkflowPreferencesFull = Omit<PreferencesEntity, 'preferences'> & {
preferences: WorkflowPreferences;
Expand All @@ -34,6 +35,7 @@ type UpsertPreferencesCommand = Omit<
export class UpsertPreferences {
constructor(private preferencesRepository: PreferencesRepository) {}

@Instrument()
public async upsertWorkflowPreferences(
command: UpsertWorkflowPreferencesCommand,
): Promise<WorkflowPreferencesFull> {
Expand All @@ -46,6 +48,7 @@ export class UpsertPreferences {
}) as Promise<WorkflowPreferencesFull>;
}

@Instrument()
public async upsertSubscriberGlobalPreferences(
command: UpsertSubscriberGlobalPreferencesCommand,
) {
Expand Down Expand Up @@ -78,7 +81,7 @@ export class UpsertPreferences {

await this.preferencesRepository.update(
{
_organizationId: command.organizationId,
_environmentId: command.environmentId,
_subscriberId: command._subscriberId,
type: PreferencesTypeEnum.SUBSCRIBER_WORKFLOW,
$or: channelTypes.map((channelType) => ({
Expand All @@ -91,6 +94,7 @@ export class UpsertPreferences {
);
}

@Instrument()
public async upsertSubscriberWorkflowPreferences(
command: UpsertSubscriberWorkflowPreferencesCommand,
) {
Expand All @@ -104,6 +108,7 @@ export class UpsertPreferences {
});
}

@Instrument()
public async upsertUserWorkflowPreferences(
command: UpsertUserWorkflowPreferencesCommand,
): Promise<WorkflowPreferencesFull> {
Expand Down

0 comments on commit e9571c3

Please sign in to comment.