Skip to content

Commit

Permalink
feat(migrate): add double id replacing for messed up states
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesjo committed Mar 27, 2020
1 parent 5c613f8 commit 66e9b8d
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions src/app/core/migration/migration.service.ts
Expand Up @@ -18,16 +18,18 @@ import {initialContextState} from '../../features/work-context/store/work-contex
import {Project} from '../../features/project/project.model';
import {concatMap, map} from 'rxjs/operators';
import {migrateTaskState} from '../../features/tasks/migrate-task-state.util';
import shortid from 'shortid';

interface ReplaceIdMap {
[key: string]: string;
}

const EMTPY_ENTITY = () => ({ids: [], entities: {}});

@Injectable({
providedIn: 'root'
})
export class MigrationService {


constructor(
private _persistenceService: PersistenceService,
private _legacyPersistenceService: LegacyPersistenceService,
Expand Down Expand Up @@ -207,17 +209,47 @@ export class MigrationService {
}

private _mergeEntities(states: EntityState<any>[], initial: EntityState<any>): EntityState<any> {
const replacedIdsMap: ReplaceIdMap = {};

return states.reduce(
(acc, s) => {
if (!s || !s.ids) {
return acc;
}
return {
...acc,
ids: [...acc.ids, ...s.ids] as string[],
// NOTE: that this can lead to overwrite when the ids are the same for some reason
entities: {...acc.entities, ...s.entities}
};
// for bad reasons there might be double ids, we copy those
const alreadyExistingIds = (s.ids as string[]).filter((id: string) =>
(acc.ids as string[]).includes(id));
if (alreadyExistingIds.length) {
const replacingIds = alreadyExistingIds.map((idToReplace) => {
const newId = shortid();
replacedIdsMap[idToReplace] = newId;
return newId;
});
const replacingEntities = alreadyExistingIds.reduce((newEnts, alreadyExistingId) => {
const replacingId = replacedIdsMap[alreadyExistingId];
return {
...newEnts,
[replacingId]: {
...s.entities[alreadyExistingId],
id: replacingId,
}
};
}, {});
return {
...acc,
ids: [...acc.ids, ...replacingIds] as string[],
// NOTE: that this can lead to overwrite when the ids are the same for some reason
entities: {...acc.entities, ...replacingEntities}
};
} else {

return {
...acc,
ids: [...acc.ids, ...s.ids] as string[],
// NOTE: that this can lead to overwrite when the ids are the same for some reason
entities: {...acc.entities, ...s.entities}
};
}
}, initial
);
}
Expand Down

0 comments on commit 66e9b8d

Please sign in to comment.