Skip to content

Commit

Permalink
fix(js): Align preference.update argument naming with API response
Browse files Browse the repository at this point in the history
It should be channels, not channelPreferences.
  • Loading branch information
SokratisVidros committed Dec 27, 2024
1 parent c31ea4f commit 34f8dba
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
10 changes: 5 additions & 5 deletions packages/js/src/api/inbox-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,17 @@ export class InboxService {
return this.#httpClient.get(`${INBOX_ROUTE}/preferences${query}`);
}

updateGlobalPreferences(channelPreferences: ChannelPreference): Promise<PreferencesResponse> {
return this.#httpClient.patch(`${INBOX_ROUTE}/preferences`, channelPreferences);
updateGlobalPreferences(channels: ChannelPreference): Promise<PreferencesResponse> {
return this.#httpClient.patch(`${INBOX_ROUTE}/preferences`, channels);
}

updateWorkflowPreferences({
workflowId,
channelPreferences,
channels,
}: {
workflowId: string;
channelPreferences: ChannelPreference;
channels: ChannelPreference;
}): Promise<PreferencesResponse> {
return this.#httpClient.patch(`${INBOX_ROUTE}/preferences/${workflowId}`, channelPreferences);
return this.#httpClient.patch(`${INBOX_ROUTE}/preferences/${workflowId}`, channels);
}
}
12 changes: 6 additions & 6 deletions packages/js/src/preferences/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const updatePreference = async ({
useCache,
args,
}: UpdatePreferenceParams): Result<Preference> => {
const { workflowId, channelPreferences } = args;
const { workflowId, channels } = args;
try {
emitter.emit('preference.update.pending', {
args,
Expand All @@ -32,7 +32,7 @@ export const updatePreference = async ({
...args.preference,
channels: {
...args.preference.channels,
...channelPreferences,
...channels,
},
},
{
Expand All @@ -47,10 +47,10 @@ export const updatePreference = async ({

let response;
if (workflowId) {
response = await apiService.updateWorkflowPreferences({ workflowId, channelPreferences });
response = await apiService.updateWorkflowPreferences({ workflowId, channels });
} else {
optimisticUpdateWorkflowPreferences({ emitter, apiService, cache, useCache, args });
response = await apiService.updateGlobalPreferences(channelPreferences);
response = await apiService.updateGlobalPreferences(channels);
}

const preference = new Preference(response, {
Expand Down Expand Up @@ -84,7 +84,7 @@ const optimisticUpdateWorkflowPreferences = ({
...el,
channels: Object.entries(el.channels).reduce((acc, [key, value]) => {
const channelType = key as ChannelType;
acc[channelType] = args.channelPreferences[channelType] ?? value;
acc[channelType] = args.channels[channelType] ?? value;

return acc;
}, {} as ChannelPreference),
Expand All @@ -102,7 +102,7 @@ const optimisticUpdateWorkflowPreferences = ({
emitter.emit('preference.update.pending', {
args: {
workflowId: el.workflow?.id,
channelPreferences: updatedPreference.channels,
channels: updatedPreference.channels,
},
data: updatedPreference,
});
Expand Down
11 changes: 8 additions & 3 deletions packages/js/src/preferences/preference.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { InboxService } from '../api';

import { NovuEventEmitter } from '../event-emitter';
import { ChannelPreference, PreferenceLevel, Result, Workflow } from '../types';
import { ChannelPreference, PreferenceLevel, Result, Workflow, Prettify } from '../types';
import { updatePreference } from './helpers';
import { PreferencesCache } from '../cache/preferences-cache';
import { UpdatePreferencesArgs } from './types';

type PreferenceLike = Pick<Preference, 'level' | 'enabled' | 'channels' | 'workflow'>;

Expand Down Expand Up @@ -42,15 +43,19 @@ export class Preference {
this.workflow = preference.workflow;
}

update({ channelPreferences }: { channelPreferences: ChannelPreference }): Result<Preference> {
update({
channels,
// @deprecated use channels instead
channelPreferences,
}: Prettify<Pick<UpdatePreferencesArgs, 'channels' | 'channelPreferences'>>): Result<Preference> {
return updatePreference({
emitter: this.#emitter,
apiService: this.#apiService,
cache: this.#cache,
useCache: this.#useCache,
args: {
workflowId: this.workflow?.id,
channelPreferences,
channels: channels || channelPreferences,
preference: {
level: this.level,
enabled: this.enabled,
Expand Down
3 changes: 1 addition & 2 deletions packages/js/src/preferences/preferences.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { InboxService } from '../api';
import { NovuEventEmitter } from '../event-emitter';
import { BaseModule } from '../base-module';
import { updatePreference } from './helpers';
import { Preference } from './preference';
import type { ListPreferencesArgs, UpdatePreferencesArgs } from './types';
import type { ListPreferencesArgs } from './types';
import { Result } from '../types';
import { PreferencesCache } from '../cache/preferences-cache';

Expand Down
4 changes: 3 additions & 1 deletion packages/js/src/preferences/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export type ListPreferencesArgs = {

export type UpdatePreferencesArgs = {
workflowId?: string;
channelPreferences: ChannelPreference;
channels: ChannelPreference;
// @deprecated use channels instead
channelPreferences?: ChannelPreference;
preference?: {
level: PreferenceLevel;
enabled: boolean;
Expand Down
2 changes: 2 additions & 0 deletions packages/js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,5 @@ export type NovuOptions = {
*/
__userAgent?: string;
};

export type Prettify<T> = { [K in keyof T]: T[K] } & {};

0 comments on commit 34f8dba

Please sign in to comment.