From 4717659bd7dbdb3caa16342136b3c079228dce58 Mon Sep 17 00:00:00 2001 From: Vincent BRIDIER Date: Wed, 4 Jan 2023 11:38:44 +0100 Subject: [PATCH] Fix Sendbrochure + Add Sentry to catch Sync Errors --- ComApp/src/App/Controllers/SyncController.js | 24 ++++----- ComApp/src/App/Models/SendBrochure.js | 9 ++++ ComApp/src/App/Views/ContactScreen.js | 1 + ComApp/src/App/Views/SyncScreen.js | 54 +++++++++++++++++--- 4 files changed, 66 insertions(+), 22 deletions(-) diff --git a/ComApp/src/App/Controllers/SyncController.js b/ComApp/src/App/Controllers/SyncController.js index c6c57c1..49e6391 100644 --- a/ComApp/src/App/Controllers/SyncController.js +++ b/ComApp/src/App/Controllers/SyncController.js @@ -73,20 +73,16 @@ class SyncController { }); } - async sendBrochures() { - let brochures = SendBrochure.getAll(); - for (const brochure of brochures) { - await axios({ - method: 'POST', - url: this.getUrlBo() + '/api/sendBrochure', - responseType: 'json', - params: { - token: api_token, - emails: brochure, - }, - }); - } - SendBrochure.clear(); + async sendBrochure(brochure) { + return axios({ + method: 'POST', + url: this.getUrlBo() + '/api/sendBrochure', + responseType: 'json', + params: { + token: api_token, + emails: brochure, + }, + }); } async getDirectories() { diff --git a/ComApp/src/App/Models/SendBrochure.js b/ComApp/src/App/Models/SendBrochure.js index 9a6c385..8ef1827 100644 --- a/ComApp/src/App/Models/SendBrochure.js +++ b/ComApp/src/App/Models/SendBrochure.js @@ -72,6 +72,15 @@ class SendBrochureModel { }); } + delete(key) { + const tmp = this.get(key); + if (typeof tmp === 'object') { + this.realm.write(() => { + this.realm.delete(tmp); + }); + } + } + } export default SendBrochureModel; diff --git a/ComApp/src/App/Views/ContactScreen.js b/ComApp/src/App/Views/ContactScreen.js index 1db785d..7a14e45 100644 --- a/ComApp/src/App/Views/ContactScreen.js +++ b/ComApp/src/App/Views/ContactScreen.js @@ -496,6 +496,7 @@ class ContactScreen extends React.Component { {this.state.sendDocumentList.map((item, index) => { return ( { let tmp = this.state.sendDocumentList; tmp.splice(this.props.documents[index], 1); diff --git a/ComApp/src/App/Views/SyncScreen.js b/ComApp/src/App/Views/SyncScreen.js index 02fbb74..9754644 100644 --- a/ComApp/src/App/Views/SyncScreen.js +++ b/ComApp/src/App/Views/SyncScreen.js @@ -4,6 +4,8 @@ import { View, Button, Alert, Text } from 'react-native'; import { connect } from 'react-redux'; import { initDirectories } from '../Store/Actions/directories'; import { initContact } from '../Store/Actions/contacts'; +// Sentry +import * as Sentry from "@sentry/react-native"; // Style import styles from '../../Resources/Styles/SyncStyle'; // Translations @@ -13,7 +15,7 @@ import SyncController from '../Controllers/SyncController'; // Progress import * as Progress from 'react-native-progress'; // Database -import { User, Contact, Directory, Document } from '../../Database'; +import { User, Contact, Directory, Document, SendBrochure } from '../../Database'; // FileSytem import * as FileSystem from 'expo-file-system'; @@ -91,6 +93,7 @@ class SyncScreen extends React.Component { } async sync() { + let errorStatus = false; const isConnected = await SyncController.checkConnection().then(state => { //console.log(state.isConnected) return state.isConnected @@ -110,12 +113,16 @@ class SyncScreen extends React.Component { const resVersion = await SyncController.getVersion().then(res => { return res.data; }).catch(error => { - console.log(error); + Sentry.captureException(error); Alert.alert( i18n.t('error.error'), i18n.t('error.error_has_occurred') ); + errorStatus = true }); + if (errorStatus) { + return false; + } this.props.updateVersion(resVersion.version); this.props.updateShowIsActive(resVersion.ShowIsActive); this.setState({ newApp: resVersion.newApp }); @@ -125,12 +132,16 @@ class SyncScreen extends React.Component { User.clear(); return res.data; }).catch(error => { - console.log(error); + Sentry.captureException(error); Alert.alert( i18n.t('error.error'), i18n.t('error.error_has_occurred') ); + errorStatus = true }); + if (errorStatus) { + return false; + } resUsers.forEach(user => { User.add(user); }); @@ -140,30 +151,53 @@ class SyncScreen extends React.Component { Contact.clear(); return res.data; }).catch(error => { - console.log(error); + Sentry.captureException(error); Alert.alert( i18n.t('error.error'), i18n.t('error.error_has_occurred') ); + errorStatus = true }); + if (errorStatus) { + return false; + } resContacts.forEach(contact => { Contact.add(contact); }); //sendBrochures this.setState({ progress: 0.2, txtStep: i18n.t('sync_s.sending_flyers') + '...' }); - await SyncController.sendBrochures(); + let brochures = SendBrochure.getAll(); + for (const brochure of brochures) { + await SyncController.sendBrochure(brochure).then(res => { + SendBrochure.delete(brochure.id); + }).catch(error => { + Sentry.captureException(error); + Alert.alert( + i18n.t('error.error'), + i18n.t('error.error_has_occurred') + ' : Brochures' + ); + errorStatus = true; + }); + } + if (errorStatus) { + return false; + } //getDirectories this.setState({ progress: 0.3, txtStep: i18n.t('sync_s.updating_folders') + '...' }); const resDirectories = await SyncController.getDirectories().then(res => { Directory.clear(); return res.data; }).catch(error => { - console.log(error); + Sentry.captureException(error); Alert.alert( i18n.t('error.error'), i18n.t('error.error_has_occurred') ); + errorStatus = true }); + if (errorStatus) { + return false; + } resDirectories.forEach(dir => { Directory.add(dir); }); @@ -172,12 +206,16 @@ class SyncScreen extends React.Component { const resDocuments = await SyncController.getDocuments().then(res => { return res.data; }).catch(error => { - console.log(error); + Sentry.captureException(error); Alert.alert( i18n.t('error.error'), i18n.t('error.error_has_occurred') ); + errorStatus = true }); + if (errorStatus) { + return false; + } const toDelete = Document.clear(resDocuments); resDocuments.forEach(document => { Document.update(document); @@ -314,7 +352,7 @@ class SyncScreen extends React.Component { SyncController.updateVersion(version); this.setState({ txtStep: i18n.t('sync_s.update_completed') + '.', processing: false }); if (this.state.newApp === true) { - const appUUID = '1f6eda31-1f6d-49fe-aa81-4f0aece0f7ca'; + const appUUID = SyncController.getAppStoreUUID(); const url = 'https://store.ldc.fr' + '/app/' + appUUID; Alert.alert( 'LDC Store',