Skip to content

Commit

Permalink
chore(application-generic): refactor the get subscriber preferences u…
Browse files Browse the repository at this point in the history
…se-case functions
  • Loading branch information
LetItRock committed Jun 22, 2023
1 parent 0a3e9a3 commit cd04026
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
determineOverrides,
overridePreferences,
filteredPreference,
} from './get-subscriber-template-preference.usecase';
import { ChannelTypeEnum } from '@novu/shared';

describe('determineOverrides', function () {
describe('overridePreferences', function () {
beforeEach(function () {});

it('should be overridden by the subscribers preference', async function () {
Expand All @@ -21,7 +21,7 @@ describe('determineOverrides', function () {
push: false,
};

const { preferences, overrides } = determineOverrides(
const { channels, overrides } = overridePreferences(
{
template: templateChannelPreference,
subscriber: subscriberChannelPreference,
Expand All @@ -43,7 +43,7 @@ describe('determineOverrides', function () {
push: false,
};

expect(preferences).toEqual(expectedPreferenceResult);
expect(channels).toEqual(expectedPreferenceResult);
expect(
overrides.find((override) => override.channel === 'email').source
).toEqual('subscriber');
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('determineOverrides', function () {
};
const subscriberChannelPreference = {};

const { preferences, overrides } = determineOverrides(
const { channels, overrides } = overridePreferences(
{
template: templateChannelPreference,
subscriber: subscriberChannelPreference,
Expand All @@ -93,7 +93,7 @@ describe('determineOverrides', function () {
push: true,
};

expect(preferences).toEqual(expectedPreferenceResult);
expect(channels).toEqual(expectedPreferenceResult);
expect(
overrides.find((override) => override.channel === 'email').source
).toEqual('template');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { GetSubscriberTemplatePreferenceCommand } from './get-subscriber-templat
import { ApiException } from '../../utils/exceptions';
import { CachedEntity, buildSubscriberKey } from '../../services';

const PRIORITY_ORDER = ['template', 'subscriber'];

@Injectable()
export class GetSubscriberTemplatePreference {
constructor(
Expand Down Expand Up @@ -50,24 +52,23 @@ export class GetSubscriberTemplatePreference {
_templateId: command.template._id,
});

const templateConfiguration = mapTemplateConfiguration(command.template);
const templatePreferred = subscriberPreference?.enabled ?? true;
const subscriberChannelPreference = subscriberPreference?.channels;
const templateChannelPreference = command.template.preferenceSettings;

const { channelPreferences, overrides } =
this.getChannelPreferenceAndOverrides(
templateChannelPreference,
subscriberChannelPreference,
initialActiveChannels
);
const { channels, overrides } = overridePreferences(
{
template: templateChannelPreference,
subscriber: subscriberChannelPreference,
},
initialActiveChannels
);

return {
template: templateConfiguration,
template: mapTemplateConfiguration(command.template),
preference: {
enabled: templatePreferred,
channels: channelPreferences,
overrides: overrides,
enabled: subscriberPreference?.enabled ?? true,
channels,
overrides,
},
};
}
Expand All @@ -90,22 +91,6 @@ export class GetSubscriberTemplatePreference {
return initialActiveChannels;
}

private getChannelPreferenceAndOverrides(
templateChannelPreference,
subscriberChannelPreference,
initialActiveChannels: IPreferenceChannels
) {
const { preferences, overrides } = determineOverrides(
{
template: templateChannelPreference,
subscriber: subscriberChannelPreference,
},
initialActiveChannels
);

return { channelPreferences: preferences, overrides };
}

private async queryActiveChannels(
command: GetSubscriberTemplatePreferenceCommand
): Promise<ChannelTypeEnum[]> {
Expand Down Expand Up @@ -189,80 +174,51 @@ function updateOverrideReasons(
}
}

export function determineOverrides(
preferenceSources: Record<'template' | 'subscriber', IPreferenceChannels>,
initialActiveChannels: IPreferenceChannels
) {
const priorityOrder = ['template', 'subscriber'];

let result: {
overrideReasons: IPreferenceOverride[];
preferences: IPreferenceChannels;
} = {
overrideReasons: [],
preferences: { ...initialActiveChannels },
};

result = overridePreferenceBySourcePriority(
priorityOrder,
preferenceSources,
result
);

return {
preferences: result.preferences,
overrides: result.overrideReasons,
};
}

function overridePreference(
oldPreferenceState: {
overrideReasons: IPreferenceOverride[];
preferences: IPreferenceChannels;
overrides: IPreferenceOverride[];
channels: IPreferenceChannels;
},
sourcePreference: IPreferenceChannels,
sourceName: string
) {
const resultPreferences = { ...oldPreferenceState.preferences };
const resultOverrideReasons = [...oldPreferenceState.overrideReasons];
const channels = { ...oldPreferenceState.channels };
const overrides = [...oldPreferenceState.overrides];

for (const [channelName, channelValue] of Object.entries(sourcePreference)) {
if (typeof resultPreferences[channelName] !== 'boolean') continue;
if (typeof channels[channelName] !== 'boolean') continue;

const index = resultOverrideReasons.findIndex(
const index = overrides.findIndex(
(overrideReason) => overrideReason.channel === channelName
);

const isSameReason = resultOverrideReasons[index]?.source !== channelValue;
const isSameReason = overrides[index]?.source !== channelValue;

if (!isSameReason) continue;

resultPreferences[channelName] = channelValue;
updateOverrideReasons(
channelName,
sourceName,
index,
resultOverrideReasons
);
channels[channelName] = channelValue;
updateOverrideReasons(channelName, sourceName, index, overrides);
}

return {
preferences: resultPreferences,
overrideReasons: resultOverrideReasons,
channels,
overrides,
};
}

function overridePreferenceBySourcePriority(
priorityOrder: string[],
export function overridePreferences(
preferenceSources: Record<'template' | 'subscriber', IPreferenceChannels>,
oldPreferenceState: {
overrideReasons: IPreferenceOverride[];
preferences: IPreferenceChannels;
}
initialActiveChannels: IPreferenceChannels
) {
let result = { ...oldPreferenceState };
let result: {
overrides: IPreferenceOverride[];
channels: IPreferenceChannels;
} = {
overrides: [],
channels: { ...initialActiveChannels },
};

for (const sourceName of priorityOrder) {
for (const sourceName of PRIORITY_ORDER) {
const sourcePreference = preferenceSources[
sourceName
] as IPreferenceChannels;
Expand Down

0 comments on commit cd04026

Please sign in to comment.