From f80a99f15e80e55404a3e39badc8483dadafaea6 Mon Sep 17 00:00:00 2001 From: Kushagra Gour Date: Tue, 25 Jun 2024 14:54:45 +0530 Subject: [PATCH] try saving with refreshed ids when importing to DB fails --- src/itemService.js | 58 ++++++++++++++++++++++++++++++++++------------ src/utils.js | 13 +++++++++++ 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/src/itemService.js b/src/itemService.js index d6b9a58..531414b 100644 --- a/src/itemService.js +++ b/src/itemService.js @@ -1,5 +1,5 @@ import { deferred } from './deferred'; -import { log } from './utils'; +import { log, refreshItemIds } from './utils'; import { collection, deleteField, @@ -170,21 +170,49 @@ export const itemService = { } else { const remoteDb = window.db.getDb(); - const batch = writeBatch(remoteDb); - /* eslint-disable guard-for-in */ - for (var id in items) { - items[id].createdBy = window.user.uid; - batch.set(doc(remoteDb, `items/${id}`), items[id]); - batch.update(doc(remoteDb, `users/${window.user.uid}`), { - [`items.${id}`]: true - }); - - // Set these items on our cached user object too - window.user.items = window.user.items || {}; - window.user.items[id] = true; + function save(items) { + const batch = writeBatch(remoteDb); + /* eslint-disable guard-for-in */ + for (var id in items) { + items[id].createdBy = window.user.uid; + batch.set(doc(remoteDb, `items/${id}`), items[id]); + batch.update(doc(remoteDb, `users/${window.user.uid}`), { + [`items.${id}`]: true + }); + } + return batch.commit(); + /* eslint-enable guard-for-in */ } - batch.commit().then(d.resolve); - /* eslint-enable guard-for-in */ + + function onSuccess(items) { + window.user.items = window.user.items || {}; + for (var id in items) { + // Set these items on our cached user object too + window.user.items[id] = true; + } + d.resolve(); + } + + save(items) + .then(() => { + onSuccess(items); + }) + .catch(e => { + // The only reason we know for this failing is the same creations were + // imported in other account. And hence they can't again be saved in the + // DB with same ID and different `createdBy` + // So now we save them with different IDs + log('Saving imported items failed. Trying with refreshed IDs now...'); + const refreshedItems = refreshItemIds(items); + save(refreshedItems) + .then(() => { + onSuccess(refreshedItems); + }) + .catch(e => { + log('Error saving items', e); + alert('Your items could not be saved. Please try again later.'); + }); + }); } return d.promise; }, diff --git a/src/utils.js b/src/utils.js index 86adbed..f2ee655 100644 --- a/src/utils.js +++ b/src/utils.js @@ -672,3 +672,16 @@ export function persistAuthUserLocally(user) { keys.map(key => (obj[key] = user[key])); window.localStorage.setItem('user', JSON.stringify(obj)); } + +/** + * items is an object of {itemId: item}. This fn changes all the keys to new item IDs + * */ +export function refreshItemIds(items) { + const newItems = {}; + for (var id in items) { + const newId = generateRandomId(); + items[id].id = newId; + newItems[newId] = items[id]; + } + return newItems; +}