Filter unused preferences (#7015)

* small preferences refactor

* filter unused preferences and fix removal of preferences in the db

* Feedback review
This commit is contained in:
Elias Nahum
2023-01-30 21:00:19 +02:00
committed by GitHub
parent 37bc95cf1e
commit 64a59aad55
49 changed files with 246 additions and 212 deletions

View File

@@ -131,7 +131,7 @@ describe('*** Operator: User Handlers tests ***', () => {
},
{
user_id: '9ciscaqbrpd6d8s68k76xb9bte',
category: 'tutorial_step',
category: 'tutorial_step', // we aren't using this category in the app, should be filtered
name: '9ciscaqbrpd6d8s68k76xb9bte',
value: '2',
},
@@ -140,12 +140,13 @@ describe('*** Operator: User Handlers tests ***', () => {
await operator.handlePreferences({
preferences,
prepareRecordsOnly: false,
sync: false,
});
expect(spyOnHandleRecords).toHaveBeenCalledTimes(1);
expect(spyOnHandleRecords).toHaveBeenCalledWith({
fieldName: 'user_id',
createOrUpdateRawValues: preferences,
createOrUpdateRawValues: preferences.filter((p) => p.category !== 'tutorial_step'),
tableName: 'Preference',
prepareRecordsOnly: true,
buildKeyRecordBy: buildPreferenceKey,

View File

@@ -8,6 +8,7 @@ import {
transformUserRecord,
} from '@database/operator/server_data_operator/transformers/user';
import {getUniqueRawsBy} from '@database/operator/utils/general';
import {filterPreferences} from '@helpers/api/preference';
import {logWarning} from '@utils/log';
import type {
@@ -33,22 +34,25 @@ const UserHandler = (superclass: any) => class extends superclass {
* @returns {Promise<PreferenceModel[]>}
*/
handlePreferences = async ({preferences, prepareRecordsOnly = true, sync = false}: HandlePreferencesArgs): Promise<PreferenceModel[]> => {
if (!preferences?.length) {
const records: PreferenceModel[] = [];
const filtered = filterPreferences(preferences);
if (!filtered?.length) {
logWarning(
'An empty or undefined "preferences" array has been passed to the handlePreferences method',
);
return [];
return records;
}
// WE NEED TO SYNC THE PREFS FROM WHAT WE GOT AND WHAT WE HAVE
const deleteValues: PreferenceModel[] = [];
const stored = await this.database.get(PREFERENCE).query().fetch() as PreferenceModel[];
const preferenesMap = new Map(stored.map((p) => {
const storedPreferencesMap = new Map(stored.map((p) => {
return [`${p.category}-${p.name}`, p];
}));
if (sync) {
const rawPreferencesMap = new Map(filtered.map((p) => [`${p.category}-${p.name}`, p]));
for (const pref of stored) {
const exists = preferenesMap.get(`${pref.category}-${pref.name}`);
const exists = rawPreferencesMap.get(`${pref.category}-${pref.name}`);
if (!exists) {
pref.prepareDestroyPermanently();
deleteValues.push(pref);
@@ -56,9 +60,9 @@ const UserHandler = (superclass: any) => class extends superclass {
}
}
const createOrUpdateRawValues = preferences.reduce((res: PreferenceType[], p) => {
const createOrUpdateRawValues = filtered.reduce((res: PreferenceType[], p) => {
const id = `${p.category}-${p.name}`;
const exist = preferenesMap.get(id);
const exist = storedPreferencesMap.get(id);
if (!exist) {
res.push(p);
return res;
@@ -71,18 +75,21 @@ const UserHandler = (superclass: any) => class extends superclass {
return res;
}, []);
if (!createOrUpdateRawValues.length) {
return [];
if (!createOrUpdateRawValues.length && !deleteValues.length) {
return records;
}
const records: PreferenceModel[] = await this.handleRecords({
fieldName: 'user_id',
buildKeyRecordBy: buildPreferenceKey,
transformer: transformPreferenceRecord,
prepareRecordsOnly: true,
createOrUpdateRawValues,
tableName: PREFERENCE,
});
if (createOrUpdateRawValues.length) {
const createOrUpdate: PreferenceModel[] = await this.handleRecords({
fieldName: 'user_id',
buildKeyRecordBy: buildPreferenceKey,
transformer: transformPreferenceRecord,
prepareRecordsOnly: true,
createOrUpdateRawValues,
tableName: PREFERENCE,
});
records.push(...createOrUpdate);
}
if (deleteValues.length) {
records.push(...deleteValues);