Skip to content

Commit

Permalink
fix(user): SKFP-941 update user config
Browse files Browse the repository at this point in the history
  • Loading branch information
GaelleA committed Feb 8, 2024
1 parent 81e7792 commit 62d1196
Show file tree
Hide file tree
Showing 3 changed files with 475 additions and 11 deletions.
55 changes: 55 additions & 0 deletions src/store/user/thunks.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { cloneDeep } from 'lodash';
import { cleanupConfig } from './thunks';
import {
emptyUserConfig,
updatedConfigDashboard,
updatedConfigParticipant,
updatedConfigSummary,
updatedConfigVariant,
userConfig,
userConfigWithoutVariant,
} from './thunksTestData';

describe('store/user/thunks', () => {
describe('cleanupConfig', () => {
it('should return and empty object if config and his update are empty', () => {
const result = cleanupConfig(emptyUserConfig, emptyUserConfig);
expect(result).toEqual({});
});
it('should return updated config if config is empty', () => {
const result = cleanupConfig(updatedConfigSummary, {});
expect(result).toEqual(updatedConfigSummary);
});
it('should return merged config if config not contains the updated config part', () => {
const result = cleanupConfig(updatedConfigVariant, userConfigWithoutVariant);
const mergedConfig = { ...cloneDeep(userConfigWithoutVariant), ...updatedConfigVariant };
expect(result).toEqual(mergedConfig);
});

it('should return merged config if config and updated summary config are not empty', () => {
const result = cleanupConfig(updatedConfigSummary, userConfig);
const mergedConfig = cloneDeep(userConfig);
mergedConfig.data_exploration.summary = updatedConfigSummary.data_exploration.summary;
expect(result).toEqual(mergedConfig);
});
it('should return merged config if config and updated tables config are not empty', () => {
const result = cleanupConfig(updatedConfigParticipant, userConfig);
const mergedConfig = cloneDeep(userConfig);
mergedConfig.data_exploration.tables.participants =
updatedConfigParticipant.data_exploration.tables.participants;
expect(result).toEqual(mergedConfig);
});
it('should return merged config if config and updated variants config are not empty', () => {
const result = cleanupConfig(updatedConfigVariant, userConfig);
const mergedConfig = cloneDeep(userConfig);
mergedConfig.variants = updatedConfigVariant.variants;
expect(result).toEqual(mergedConfig);
});
it('should return merged config if config and updated dashboard config are not empty', () => {
const result = cleanupConfig(updatedConfigDashboard, userConfig);
const mergedConfig = cloneDeep(userConfig);
mergedConfig.dashboard = updatedConfigDashboard.dashboard;
expect(result).toEqual(mergedConfig);
});
});
});
31 changes: 20 additions & 11 deletions src/store/user/thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,37 @@ const updateUser = createAsyncThunk<
},
);

const cleanupConfig = (config: TUserConfig): TUserConfig => {
export const cleanupConfig = (updateConfig: TUserConfig, config?: TUserConfig): TUserConfig => {
// keep last item
const removeDuplicates = (cols: TColumnStates) =>
cols.filter((c, i) => !cols.some((other, j) => c.key === other.key && j > i));

// for every tables in config replace columns with no duplicates
keys(config.data_exploration?.tables).forEach((key) => {
const path = 'data_exploration.tables.' + key + '.columns';
const cols = get(config, path, []);
set(config, path, removeDuplicates(cols));
});
const configMerged = merge(cloneDeep(config), cloneDeep(updateConfig));

return config;
if (updateConfig.data_exploration?.tables) {
// for every tables in config replace columns with no duplicates
keys(configMerged.data_exploration?.tables).forEach((key) => {
const path = 'data_exploration.tables.' + key + '.columns';
const cols = get(configMerged, path, []);
set(configMerged, path, removeDuplicates(cols));
});

return configMerged;
} else if (updateConfig.data_exploration?.summary && config?.data_exploration?.summary) {
config.data_exploration.summary = updateConfig.data_exploration.summary;
return config;
}

return configMerged;
};

const updateUserConfig = createAsyncThunk<TUserConfig, TUserConfig, { state: RootState }>(
'user/updateConfig',
async (config, thunkAPI) => {
const state = thunkAPI.getState();
const mergedConfig = cleanupConfig(
merge(cloneDeep(state?.user?.userInfo?.config), cloneDeep(config)),
);

const mergedConfig = cleanupConfig(cloneDeep(config), cloneDeep(state?.user?.userInfo?.config));

await UserApi.update({ config: mergedConfig });

return mergedConfig;
Expand Down
Loading

0 comments on commit 62d1196

Please sign in to comment.