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

perf(api): Lookup subscriber preferences with a single database query #7119

Merged
merged 12 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { deepMerge } from '../../utils';
import { GetPreferencesCommand } from './get-preferences.command';
import { GetPreferencesResponseDto } from './get-preferences.dto';
import { InstrumentUsecase } from '../../instrumentation';
import { MergePreferences } from '../merge-preferences/merge-preferences.usecase';
import { MergePreferencesCommand } from '../merge-preferences/merge-preferences.command';

class PreferencesNotFoundException extends BadRequestException {
constructor(featureFlagCommand: GetPreferencesCommand) {
Expand All @@ -32,7 +34,11 @@ export class GetPreferences {
throw new PreferencesNotFoundException(command);
}

const mergedPreferences = this.mergePreferences(items, command.templateId);
const mergedPreferences = MergePreferences.merge(
MergePreferencesCommand.create({
preferences: items,
}),
);

if (!mergedPreferences.preferences) {
throw new PreferencesNotFoundException(command);
Expand Down Expand Up @@ -99,172 +105,6 @@ export class GetPreferences {
return mappedPreferences;
}

private mergePreferences(
items: PreferencesEntity[],
workflowId?: string,
): GetPreferencesResponseDto {
const workflowResourcePreferences =
this.getWorkflowResourcePreferences(items);
const workflowUserPreferences = this.getWorkflowUserPreferences(items);

const workflowPreferences = deepMerge(
[workflowResourcePreferences, workflowUserPreferences]
.filter((preference) => preference !== undefined)
.map((item) => item.preferences),
) as WorkflowPreferences;

const subscriberGlobalPreferences =
this.getSubscriberGlobalPreferences(items);
const subscriberWorkflowPreferences = this.getSubscriberWorkflowPreferences(
items,
workflowId,
);

const subscriberPreferences = deepMerge(
[subscriberGlobalPreferences, subscriberWorkflowPreferences]
.filter((preference) => preference !== undefined)
.map((item) => item.preferences),
);

/**
* Order is important here because we like the workflowPreferences (that comes from the bridge)
* to be overridden by any other preferences and then we have preferences defined in dashboard and
* then subscribers global preferences and the once that should be used if it says other then anything before it
* we use subscribers workflow preferences
*/
const preferencesEntities = [
workflowResourcePreferences,
workflowUserPreferences,
subscriberGlobalPreferences,
subscriberWorkflowPreferences,
];
const source = Object.values(PreferencesTypeEnum).reduce(
(acc, type) => {
const preference = items.find((item) => item.type === type);
if (preference) {
acc[type] = preference.preferences as WorkflowPreferences;
} else {
acc[type] = null;
}

return acc;
},
{} as GetPreferencesResponseDto['source'],
);
const preferences = preferencesEntities
.filter((preference) => preference !== undefined)
.map((item) => item.preferences);

// ensure we don't merge on an empty list
if (preferences.length === 0) {
return { preferences: undefined, type: undefined, source };
}

const readOnlyFlag = workflowPreferences?.all?.readOnly;

// Determine the most specific preference applied
let mostSpecificPreference: PreferencesTypeEnum | undefined;
if (subscriberWorkflowPreferences && !readOnlyFlag) {
mostSpecificPreference = PreferencesTypeEnum.SUBSCRIBER_WORKFLOW;
} else if (subscriberGlobalPreferences && !readOnlyFlag) {
mostSpecificPreference = PreferencesTypeEnum.SUBSCRIBER_GLOBAL;
} else if (workflowUserPreferences) {
mostSpecificPreference = PreferencesTypeEnum.USER_WORKFLOW;
} else if (workflowResourcePreferences) {
mostSpecificPreference = PreferencesTypeEnum.WORKFLOW_RESOURCE;
}

// If workflowPreferences have readOnly flag set to true, disregard subscriber preferences
if (readOnlyFlag) {
return {
preferences: workflowPreferences,
type: mostSpecificPreference,
source,
};
}

/**
* Order is (almost exactly) reversed of that above because 'readOnly' should be prioritized
* by the Dashboard (userPreferences) the most.
*/
const orderedPreferencesForReadOnly = [
subscriberWorkflowPreferences,
subscriberGlobalPreferences,
workflowResourcePreferences,
workflowUserPreferences,
]
.filter((preference) => preference !== undefined)
.map((item) => item.preferences);

const readOnlyPreferences = orderedPreferencesForReadOnly.map(
({ all }) => ({
all: { readOnly: all?.readOnly || false },
}),
) as WorkflowPreferences[];

const readOnlyPreference = deepMerge([...readOnlyPreferences]);

if (Object.keys(subscriberPreferences).length === 0) {
return {
preferences: workflowPreferences,
type: mostSpecificPreference,
source,
};
}
// if the workflow should be readonly, we return the resource preferences default value for workflow.
if (readOnlyPreference?.all?.readOnly) {
subscriberPreferences.all.enabled = workflowPreferences?.all?.enabled;
}

// making sure we respond with correct readonly values.
const mergedPreferences = deepMerge([
workflowPreferences,
subscriberPreferences,
readOnlyPreference,
]) as WorkflowPreferences;

return {
preferences: mergedPreferences,
type: mostSpecificPreference,
source,
};
}

private getSubscriberWorkflowPreferences(
items: PreferencesEntity[],
templateId: string,
) {
return items.find(
(item) =>
item.type === PreferencesTypeEnum.SUBSCRIBER_WORKFLOW &&
item._templateId === templateId,
);
}

private getSubscriberGlobalPreferences(
items: PreferencesEntity[],
): PreferencesEntity | undefined {
return items.find(
(item) => item.type === PreferencesTypeEnum.SUBSCRIBER_GLOBAL,
);
}

private getWorkflowUserPreferences(
items: PreferencesEntity[],
): PreferencesEntity | undefined {
return items.find(
(item) => item.type === PreferencesTypeEnum.USER_WORKFLOW,
);
}

private getWorkflowResourcePreferences(
items: PreferencesEntity[],
): PreferencesEntity | undefined {
return items.find(
(item) => item.type === PreferencesTypeEnum.WORKFLOW_RESOURCE,
);
}

private async getPreferencesFromDb(
command: GetPreferencesCommand,
): Promise<PreferencesEntity[]> {
Expand Down
Loading
Loading