Skip to content

Commit

Permalink
refactor: Add DeepRequired type and Instrument decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
rifont committed Nov 28, 2024
1 parent 45c08bf commit 92c346a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 22 deletions.
9 changes: 9 additions & 0 deletions libs/application-generic/src/http/utils.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
*/
export type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };

/**
* Recursively make all properties of type `T` required.
*/
export type DeepRequired<T> = T extends object
? {
[P in keyof T]-?: DeepRequired<T[P]>;
}
: T;

/**
* Transform S to CONSTANT_CASE.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { MergePreferences } from '../merge-preferences/merge-preferences.usecase
import { MergePreferencesCommand } from '../merge-preferences/merge-preferences.command';

export type PreferenceSet = {
workflowResourcePreference: PreferencesEntity & {
workflowResourcePreference?: PreferencesEntity & {
preferences: WorkflowPreferences;
};
workflowUserPreference?: PreferencesEntity & {
Expand Down Expand Up @@ -145,12 +145,8 @@ export class GetPreferences {
}) as Promise<PreferenceSet['subscriberGlobalPreference'] | null>,
]);

if (workflowResourcePreference === null) {
throw new PreferencesNotFoundException(command);
}

return {
workflowResourcePreference,
...(workflowResourcePreference ? { workflowResourcePreference } : {}),
...(workflowUserPreference ? { workflowUserPreference } : {}),
...(subscriberWorkflowPreference ? { subscriberWorkflowPreference } : {}),
...(subscriberGlobalPreference ? { subscriberGlobalPreference } : {}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ApiException } from '../../utils/exceptions';
import { GetPreferences } from '../get-preferences';
import { GetSubscriberPreference } from '../get-subscriber-preference/get-subscriber-preference.usecase';
import { filteredPreference } from '../get-subscriber-template-preference/get-subscriber-template-preference.usecase';
import { InstrumentUsecase } from '../../instrumentation';
import { Instrument, InstrumentUsecase } from '../../instrumentation';

@Injectable()
export class GetSubscriberGlobalPreference {
Expand Down Expand Up @@ -54,6 +54,7 @@ export class GetSubscriberGlobalPreference {
};
}

@Instrument()
private async getSubscriberGlobalPreference(
command: GetSubscriberGlobalPreferenceCommand,
subscriberId: string,
Expand Down Expand Up @@ -95,6 +96,7 @@ export class GetSubscriberGlobalPreference {
};
}

@Instrument()
private async getActiveChannels(
command: GetSubscriberGlobalPreferenceCommand,
): Promise<ChannelTypeEnum[]> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
overridePreferences,
} from '../get-subscriber-template-preference';
import { MergePreferencesCommand } from '../merge-preferences/merge-preferences.command';
import { mapTemplateConfiguration } from '../get-subscriber-template-preference/get-subscriber-template-preference.usecase';

@Injectable()
export class GetSubscriberPreference {
Expand Down Expand Up @@ -183,10 +184,10 @@ export class GetSubscriberPreference {
enabled: true,
overrides,
},
template: {
template: mapTemplateConfiguration({
...workflow,
critical: merged.preferences.all.readOnly,
},
}),
type: PreferencesTypeEnum.SUBSCRIBER_WORKFLOW,
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { GetSubscriberTemplatePreferenceCommand } from './get-subscriber-templat
import { ApiException } from '../../utils/exceptions';
import { buildSubscriberKey, CachedEntity } from '../../services/cache';
import { GetPreferences } from '../get-preferences';
import { InstrumentUsecase } from '../../instrumentation';
import { Instrument, InstrumentUsecase } from '../../instrumentation';

const PRIORITY_ORDER = [
PreferenceOverrideSourceEnum.TEMPLATE,
Expand Down Expand Up @@ -89,6 +89,7 @@ export class GetSubscriberTemplatePreference {
};
}

@Instrument()
private async getSubscriberWorkflowPreference(
command: GetSubscriberTemplatePreferenceCommand,
subscriberId: string,
Expand Down Expand Up @@ -148,6 +149,7 @@ export class GetSubscriberTemplatePreference {
};
}

@Instrument()
private async getWorkflowOverride(
command: GetSubscriberTemplatePreferenceCommand,
) {
Expand All @@ -172,6 +174,7 @@ export class GetSubscriberTemplatePreference {
});
}

@Instrument()
private async getChannels(
command: GetSubscriberTemplatePreferenceCommand,
): Promise<IPreferenceChannels> {
Expand All @@ -196,6 +199,7 @@ export class GetSubscriberTemplatePreference {
return initialChannels;
}

@Instrument()
private async queryActiveChannels(
command: GetSubscriberTemplatePreferenceCommand,
): Promise<ChannelTypeEnum[]> {
Expand Down Expand Up @@ -354,7 +358,7 @@ export const filteredPreference = (
{},
);

function mapTemplateConfiguration(
export function mapTemplateConfiguration(
template: NotificationTemplateEntity,
): ITemplateConfiguration {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BaseCommand } from '../../commands';
import { PreferenceSet } from '../get-preferences/get-preferences.usecase';

export class MergePreferencesCommand extends BaseCommand {
workflowResourcePreference: PreferenceSet['workflowResourcePreference'];
workflowResourcePreference?: PreferenceSet['workflowResourcePreference'];
workflowUserPreference?: PreferenceSet['workflowUserPreference'];
subscriberGlobalPreference?: PreferenceSet['subscriberGlobalPreference'];
subscriberWorkflowPreference?: PreferenceSet['subscriberWorkflowPreference'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ const testCases: TestCase[] = [
expectedType: PreferencesTypeEnum.WORKFLOW_RESOURCE,
readOnly: false,
},
{
comment: 'Subscriber global only',
types: [PreferencesTypeEnum.SUBSCRIBER_GLOBAL],
expectedType: PreferencesTypeEnum.SUBSCRIBER_GLOBAL,
readOnly: false,
},
{
comment: 'Subscriber workflow overrides workflow resource',
types: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { PreferencesTypeEnum, WorkflowPreferences } from '@novu/shared';
import { PreferencesTypeEnum } from '@novu/shared';
import { PreferencesEntity } from '@novu/dal';
import { deepMerge } from '../../utils';
import { GetPreferencesResponseDto } from '../get-preferences';
import { MergePreferencesCommand } from './merge-preferences.command';

/**
* Recursively make all properties of type `T` required.
*/
type DeepRequired<T> = T extends object
? {
[P in keyof T]-?: DeepRequired<T[P]>;
}
: T;
import { DeepRequired } from '../../http/utils.types';

/**
* Merge preferences for a subscriber.
Expand Down

0 comments on commit 92c346a

Please sign in to comment.