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

fix(api-service): on global preferences update reset workflow preferences per env #7428

Merged
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
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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fix --> classical

_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
Loading