From 34f8dba24434532ef6fdb1131aea491e4097afe0 Mon Sep 17 00:00:00 2001 From: Sokratis Vidros Date: Fri, 27 Dec 2024 13:34:46 +0200 Subject: [PATCH] fix(js): Align preference.update argument naming with API response It should be channels, not channelPreferences. --- packages/js/src/api/inbox-service.ts | 10 +++++----- packages/js/src/preferences/helpers.ts | 12 ++++++------ packages/js/src/preferences/preference.ts | 11 ++++++++--- packages/js/src/preferences/preferences.ts | 3 +-- packages/js/src/preferences/types.ts | 4 +++- packages/js/src/types.ts | 2 ++ 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/packages/js/src/api/inbox-service.ts b/packages/js/src/api/inbox-service.ts index 70573f4f439..78023598002 100644 --- a/packages/js/src/api/inbox-service.ts +++ b/packages/js/src/api/inbox-service.ts @@ -154,17 +154,17 @@ export class InboxService { return this.#httpClient.get(`${INBOX_ROUTE}/preferences${query}`); } - updateGlobalPreferences(channelPreferences: ChannelPreference): Promise { - return this.#httpClient.patch(`${INBOX_ROUTE}/preferences`, channelPreferences); + updateGlobalPreferences(channels: ChannelPreference): Promise { + return this.#httpClient.patch(`${INBOX_ROUTE}/preferences`, channels); } updateWorkflowPreferences({ workflowId, - channelPreferences, + channels, }: { workflowId: string; - channelPreferences: ChannelPreference; + channels: ChannelPreference; }): Promise { - return this.#httpClient.patch(`${INBOX_ROUTE}/preferences/${workflowId}`, channelPreferences); + return this.#httpClient.patch(`${INBOX_ROUTE}/preferences/${workflowId}`, channels); } } diff --git a/packages/js/src/preferences/helpers.ts b/packages/js/src/preferences/helpers.ts index 1fd9ce0453b..09f9a4f5b03 100644 --- a/packages/js/src/preferences/helpers.ts +++ b/packages/js/src/preferences/helpers.ts @@ -22,7 +22,7 @@ export const updatePreference = async ({ useCache, args, }: UpdatePreferenceParams): Result => { - const { workflowId, channelPreferences } = args; + const { workflowId, channels } = args; try { emitter.emit('preference.update.pending', { args, @@ -32,7 +32,7 @@ export const updatePreference = async ({ ...args.preference, channels: { ...args.preference.channels, - ...channelPreferences, + ...channels, }, }, { @@ -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, { @@ -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), @@ -102,7 +102,7 @@ const optimisticUpdateWorkflowPreferences = ({ emitter.emit('preference.update.pending', { args: { workflowId: el.workflow?.id, - channelPreferences: updatedPreference.channels, + channels: updatedPreference.channels, }, data: updatedPreference, }); diff --git a/packages/js/src/preferences/preference.ts b/packages/js/src/preferences/preference.ts index be6f0f80508..1a822df2a70 100644 --- a/packages/js/src/preferences/preference.ts +++ b/packages/js/src/preferences/preference.ts @@ -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; @@ -42,7 +43,11 @@ export class Preference { this.workflow = preference.workflow; } - update({ channelPreferences }: { channelPreferences: ChannelPreference }): Result { + update({ + channels, + // @deprecated use channels instead + channelPreferences, + }: Prettify>): Result { return updatePreference({ emitter: this.#emitter, apiService: this.#apiService, @@ -50,7 +55,7 @@ export class Preference { useCache: this.#useCache, args: { workflowId: this.workflow?.id, - channelPreferences, + channels: channels || channelPreferences, preference: { level: this.level, enabled: this.enabled, diff --git a/packages/js/src/preferences/preferences.ts b/packages/js/src/preferences/preferences.ts index 62c3e565507..0896584903c 100644 --- a/packages/js/src/preferences/preferences.ts +++ b/packages/js/src/preferences/preferences.ts @@ -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'; diff --git a/packages/js/src/preferences/types.ts b/packages/js/src/preferences/types.ts index 2838d275a0e..7349054d20c 100644 --- a/packages/js/src/preferences/types.ts +++ b/packages/js/src/preferences/types.ts @@ -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; diff --git a/packages/js/src/types.ts b/packages/js/src/types.ts index fc6df5ff43e..77f0140e607 100644 --- a/packages/js/src/types.ts +++ b/packages/js/src/types.ts @@ -186,3 +186,5 @@ export type NovuOptions = { */ __userAgent?: string; }; + +export type Prettify = { [K in keyof T]: T[K] } & {};