-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
221 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { ApiService } from '@novu/client'; | ||
|
||
import type { NovuEventEmitter } from '../event-emitter'; | ||
import type { ChannelPreferenceOverride, TODO } from '../types'; | ||
import { PreferenceLevel } from '../types'; | ||
import { Preference } from './preference'; | ||
import type { UpdatePreferencesArgs } from './types'; | ||
|
||
export const mapPreference = (apiPreference: { | ||
template?: TODO; | ||
preference: { | ||
enabled: boolean; | ||
channels: { | ||
email?: boolean; | ||
sms?: boolean; | ||
in_app?: boolean; | ||
chat?: boolean; | ||
push?: boolean; | ||
}; | ||
overrides?: ChannelPreferenceOverride[]; | ||
}; | ||
}): Preference => { | ||
const { template: workflow, preference } = apiPreference; | ||
const hasWorkflow = workflow !== undefined; | ||
const level = hasWorkflow ? PreferenceLevel.TEMPLATE : PreferenceLevel.GLOBAL; | ||
|
||
return new Preference({ | ||
level, | ||
enabled: preference.enabled, | ||
channels: preference.channels, | ||
workflow, | ||
overrides: preference.overrides, | ||
}); | ||
}; | ||
|
||
export const updatePreference = async ({ | ||
emitter, | ||
apiService, | ||
args, | ||
}: { | ||
emitter: NovuEventEmitter; | ||
apiService: ApiService; | ||
args: UpdatePreferencesArgs; | ||
}): Promise<Preference> => { | ||
const { workflowId, enabled, channel } = args; | ||
try { | ||
emitter.emit('preferences.update.pending', { args }); | ||
|
||
let response; | ||
if (workflowId) { | ||
response = await apiService.updateSubscriberPreference(workflowId, channel, enabled); | ||
} else { | ||
response = await apiService.updateSubscriberGlobalPreference([{ channelType: channel, enabled }]); | ||
} | ||
|
||
const preference = new Preference(mapPreference(response)); | ||
emitter.emit('preferences.update.success', { args, result: preference }); | ||
|
||
return preference; | ||
} catch (error) { | ||
emitter.emit('preferences.update.error', { args, error }); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ApiService } from '@novu/client'; | ||
|
||
import { NovuEventEmitter } from '../event-emitter'; | ||
import { ChannelPreference, ChannelPreferenceOverride, ChannelType, PreferenceLevel, WorkflowInfo } from '../types'; | ||
import { ApiServiceSingleton } from '../utils/api-service-signleton'; | ||
import { updatePreference } from './helpers'; | ||
|
||
type PreferenceLike = Pick<Preference, 'level' | 'enabled' | 'channels' | 'workflow' | 'overrides'>; | ||
|
||
export class Preference { | ||
#emitter: NovuEventEmitter; | ||
#apiService: ApiService; | ||
|
||
readonly level: PreferenceLevel; | ||
readonly enabled: boolean; | ||
readonly channels: ChannelPreference; | ||
readonly workflow?: WorkflowInfo; | ||
readonly overrides?: ChannelPreferenceOverride[]; | ||
|
||
constructor(preference: PreferenceLike) { | ||
this.#emitter = NovuEventEmitter.getInstance(); | ||
this.#apiService = ApiServiceSingleton.getInstance(); | ||
|
||
this.level = preference.level; | ||
this.enabled = preference.enabled; | ||
this.channels = preference.channels; | ||
this.workflow = preference.workflow; | ||
this.overrides = preference.overrides; | ||
} | ||
|
||
updatePreference({ enabled, channel }: { enabled: boolean; channel: ChannelType }): Promise<Preference> { | ||
return updatePreference({ | ||
emitter: this.#emitter, | ||
apiService: this.#apiService, | ||
args: { workflowId: this.workflow?._id, enabled, channel }, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,32 @@ | ||
import { BaseModule } from '../base-module'; | ||
import { PreferenceLevel } from '../types'; | ||
import { mapPreference, updatePreference } from './helpers'; | ||
import { Preference } from './preference'; | ||
import type { FetchPreferencesArgs, UpdatePreferencesArgs } from './types'; | ||
|
||
export class Preferences extends BaseModule {} | ||
export class Preferences extends BaseModule { | ||
async fetch({ level = PreferenceLevel.TEMPLATE }: FetchPreferencesArgs = {}): Promise<Preference[]> { | ||
return this.callWithSession(async () => { | ||
const args = { level }; | ||
try { | ||
this._emitter.emit('preferences.fetch.pending', { args }); | ||
|
||
const response = await this._apiService.getPreferences({ level }); | ||
const modifiedResponse: Preference[] = response.map((el) => new Preference(mapPreference(el))); | ||
|
||
this._emitter.emit('preferences.fetch.success', { args, result: modifiedResponse }); | ||
|
||
return modifiedResponse; | ||
} catch (error) { | ||
this._emitter.emit('preferences.fetch.error', { args, error }); | ||
throw error; | ||
} | ||
}); | ||
} | ||
|
||
async update(args: UpdatePreferencesArgs): Promise<Preference> { | ||
return this.callWithSession(async () => | ||
updatePreference({ emitter: this._emitter, apiService: this._apiService, args }) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ChannelType, PreferenceLevel } from '../types'; | ||
|
||
export type FetchPreferencesArgs = { | ||
level?: PreferenceLevel; | ||
}; | ||
|
||
export type UpdatePreferencesArgs = { | ||
workflowId?: string; | ||
enabled: boolean; | ||
channel: ChannelType; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters