Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Commit

Permalink
fix syncing failure when no bills are present before user creation
Browse files Browse the repository at this point in the history
- fix console error not logging error fully
  • Loading branch information
lyqht committed Oct 3, 2022
1 parent 1d2760a commit 86b6179
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
7 changes: 5 additions & 2 deletions src/screens/LoginScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import UserService from '../services/UserService';
import {LoginMode} from '../types/LoginMode';
import BillyHero from '../../assets/BillyHero.png';

const LOGGER_PREFIX = '[Login]';

type LoginScreenProps = NativeStackScreenProps<RootStackParamList, 'Login'>;

interface FormData {
Expand Down Expand Up @@ -69,12 +71,13 @@ const LoginScreen: React.FC<LoginScreenProps> = ({route}) => {
await UserService.signInUser(email, password);
break;
}
await SyncService.syncAllData();
navigation.navigate('Home');
} catch (err) {
console.error(`${LOGGER_PREFIX} ${JSON.stringify(err)}`);
setErrorText(`${err}, please try again later.`);
}

await SyncService.syncAllData();
navigation.navigate('Home');
setLoading(false);
};

Expand Down
58 changes: 32 additions & 26 deletions src/services/SyncService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,40 @@ class SyncService {
}
}

await this.replaceUnsyncBillsTempIDWithCloudID(newIDMap);
const updatedBills: Bill[] = Cache.getBills()!.map(bill => {
const {tempID, ...billDetails} = bill;
return {
...billDetails,
userId: UserService.getUser()!.id,
};
});
try {
// At the moment due to how PostgREST is implemented, upsert does not work as intended
// https://github.com/supabase/postgrest-js/issues/173
// Hence we could have temporary workaround to split into 2 calls: toUpdate, toCreate

// However, it seems that even though it is recommended not to use insert with the upsert parameter
// this somehow works as intended.
const {data, error} = await supabase
.from<Bill>('Bill')
.insert(updatedBills, {upsert: true});

if (error) {
console.error(`${LOGGER_PREFIX} ${error}`);
if (Object.keys(newIDMap).length > 0) {
await this.replaceUnsyncBillsTempIDWithCloudID(newIDMap);
}

const retrievedBills = Cache.getBills();
if (retrievedBills) {
const updatedBills: Bill[] = Cache.getBills()!.map(bill => {
const {tempID, ...billDetails} = bill;
return {
...billDetails,
userId: UserService.getUser()!.id,
};
});
try {
// At the moment due to how PostgREST is implemented, upsert does not work as intended
// https://github.com/supabase/postgrest-js/issues/173
// Hence we could have temporary workaround to split into 2 calls: toUpdate, toCreate

// However, it seems that even though it is recommended not to use insert with the upsert parameter
// this somehow works as intended.
const {data, error} = await supabase
.from<Bill>('Bill')
.insert(updatedBills, {upsert: true});

if (error) {
console.error(`${LOGGER_PREFIX} ${JSON.stringify(error)}`);
throw new Error('Error syncing to cloud');
}
console.debug({data});
await BillService.getBillsFromDB();
} catch (err) {
console.error(`${LOGGER_PREFIX} ${JSON.stringify(err)}`);
throw new Error('Error syncing to cloud');
}
console.debug({data});
await BillService.getBillsFromDB();
} catch (err) {
console.error(`${LOGGER_PREFIX} ${err}`);
throw new Error('Error syncing to cloud');
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class UserService {
});

if (error) {
console.debug({error});
console.error(JSON.stringify(error));
throw Error(error.message);
}

Expand Down

0 comments on commit 86b6179

Please sign in to comment.