From c8aa3a4c84b0a044ca413bae74ea2cf7a9a7043f Mon Sep 17 00:00:00 2001 From: Vincent BRIDIER Date: Thu, 6 Oct 2022 10:28:41 +0200 Subject: [PATCH] Add Contact + Directory + Brochure + User sync --- ComApp/src/App/Controllers/SyncController.js | 45 ++++++++- ComApp/src/App/Models/Contact.js | 66 +++++++++++++ ComApp/src/App/Models/Directory.js | 37 ++++++++ ComApp/src/App/Models/SendBrochure.js | 64 +++++++++++++ ComApp/src/App/Models/User.js | 4 +- ComApp/src/App/Views/SyncScreen.js | 97 +++++++++++++++----- ComApp/src/Database.js | 73 ++++++++++++++- 7 files changed, 353 insertions(+), 33 deletions(-) create mode 100644 ComApp/src/App/Models/Contact.js create mode 100644 ComApp/src/App/Models/Directory.js create mode 100644 ComApp/src/App/Models/SendBrochure.js diff --git a/ComApp/src/App/Controllers/SyncController.js b/ComApp/src/App/Controllers/SyncController.js index f78bd3a..83a48d4 100644 --- a/ComApp/src/App/Controllers/SyncController.js +++ b/ComApp/src/App/Controllers/SyncController.js @@ -7,7 +7,7 @@ import NetInfo from "@react-native-community/netinfo"; // Application infos import * as Application from 'expo-application'; // Database -import { Setting } from '../../Database'; +import { Setting, Contact, SendBrochure } from '../../Database'; const api_token = 'toto'; const app_uuid = '1f6eda31-1f6d-49fe-aa81-4f0aece0f7ca'; @@ -46,6 +46,49 @@ class SyncController { }); } + async getContacts(userId) { + let contacts = Contact.getAll(); + return axios({ + method: 'POST', + url: url + '/api/contacts', + responseType: 'json', + params: { + token: api_token + }, + data: { + contacts: contacts, + userId: userId, + } + }); + } + + async sendBrochures() { + let brochures = SendBrochure.getAll(); + for (const brochure of brochures) { + await axios({ + method: 'POST', + url: url + '/api/sendBrochure', + responseType: 'json', + params: { + token: api_token, + emails: brochure, + }, + }); + } + SendBrochure.clear(); + } + + async getDirectories() { + return axios({ + method: 'GET', + url: url + '/api/directories', + responseType: 'json', + params: { + token: api_token + }, + }); + } + updateShowIsActive(value) { const local = Setting.get('ShowIsActive'); if (local === false || local === undefined) { diff --git a/ComApp/src/App/Models/Contact.js b/ComApp/src/App/Models/Contact.js new file mode 100644 index 0000000..ad4ba30 --- /dev/null +++ b/ComApp/src/App/Models/Contact.js @@ -0,0 +1,66 @@ +class ContactModel { + constructor(realm) { + this.schema = 'Contacts'; + this.realm = realm; + } + + add(data) { + data.uniq_id = data.id; + data.id = undefined; + this.realm.write(() => { + if (data.id === undefined) { + let id = this.realm.objects(this.schema).max('id'); + data.id = id === undefined ? 1 : id + 1; + } + try { + this.realm.create(this.schema, data, 'never'); + } catch (e) { + console.log('db setting add', e); + } + }); + } + + upsert(data) { + let tmp = this.get(data.key); + if (tmp === false || tmp === undefined) { + this.add(data); + } else { + if (data.id === undefined) { + data.id = tmp.id; + } + this.realm.write(() => { + this.realm.create(this.schema, data, 'modified'); + }); + } + } + + update(data) { + this.realm.write(() => { + this.realm.create(this.schema, data, 'modified'); + }); + } + + get(id) { + let tmp = this.realm.objects(this.schema); + if (tmp.length === 0) { + return false; + } else { + tmp = tmp.filtered('id = ' + id)[0]; + } + return tmp; + } + + getAll() { + let tmp = this.realm.objects(this.schema); + return tmp; + } + + clear() { + this.realm.write(() => { + this.realm.delete(this.getAll()); + }); + } + +} + +export default ContactModel; diff --git a/ComApp/src/App/Models/Directory.js b/ComApp/src/App/Models/Directory.js new file mode 100644 index 0000000..f77ab6e --- /dev/null +++ b/ComApp/src/App/Models/Directory.js @@ -0,0 +1,37 @@ +class DirectoryModel { + constructor(realm) { + this.schema = 'Directories'; + this.realm = realm; + } + + add(data) { + this.realm.write(() => { + this.realm.create(this.schema, data); + }); + } + + update(data) { + this.realm.write(() => { + this.realm.create(this.schema, data, 'modified'); + }); + } + + get(id) { + let tmp = this.realm.objects(this.schema); + if (tmp.length === 0) { + return false; + } else { + tmp = tmp.filtered('id = ' + id)[0]; + } + return tmp; + } + + clear() { + this.realm.write(() => { + this.realm.delete(this.realm.objects(this.schema)); + }); + } + +} + +export default DirectoryModel; diff --git a/ComApp/src/App/Models/SendBrochure.js b/ComApp/src/App/Models/SendBrochure.js new file mode 100644 index 0000000..62c55f7 --- /dev/null +++ b/ComApp/src/App/Models/SendBrochure.js @@ -0,0 +1,64 @@ +class SendBrochureModel { + constructor(realm) { + this.schema = 'SendBrochures'; + this.realm = realm; + } + + add(data) { + this.realm.write(() => { + if (data.id === undefined) { + let id = this.realm.objects(this.schema).max('id'); + data.id = id === undefined ? 1 : id + 1; + } + try { + this.realm.create(this.schema, data, 'never'); + } catch (e) { + console.log('db setting add', e); + } + }); + } + + upsert(data) { + let tmp = this.get(data.key); + if (tmp === false || tmp === undefined) { + this.add(data); + } else { + if (data.id === undefined) { + data.id = tmp.id; + } + this.realm.write(() => { + this.realm.create(this.schema, data, 'modified'); + }); + } + } + + update(data) { + this.realm.write(() => { + this.realm.create(this.schema, data, 'modified'); + }); + } + + get(id) { + let tmp = this.realm.objects(this.schema); + if (tmp.length === 0) { + return false; + } else { + tmp = tmp.filtered('id = ' + id)[0]; + } + return tmp; + } + + getAll() { + let tmp = this.realm.objects(this.schema); + return tmp; + } + + clear() { + this.realm.write(() => { + this.realm.delete(this.getAll()); + }); + } + +} + +export default SendBrochureModel; diff --git a/ComApp/src/App/Models/User.js b/ComApp/src/App/Models/User.js index 4aa626d..6e6db47 100644 --- a/ComApp/src/App/Models/User.js +++ b/ComApp/src/App/Models/User.js @@ -38,12 +38,12 @@ class UserModel { }); } - get(key) { + get(id) { let tmp = this.realm.objects(this.schema); if (tmp.length === 0) { return false; } else { - tmp = tmp.filtered('key = "' + key + '"')[0]; + tmp = tmp.filtered('id = ' + id)[0]; } return tmp; } diff --git a/ComApp/src/App/Views/SyncScreen.js b/ComApp/src/App/Views/SyncScreen.js index f288f29..9d60984 100644 --- a/ComApp/src/App/Views/SyncScreen.js +++ b/ComApp/src/App/Views/SyncScreen.js @@ -10,12 +10,14 @@ import i18n from "../../I18n"; import SyncController from '../Controllers/SyncController'; // Progress import * as Progress from 'react-native-progress'; +// Database +import { User, Contact, Directory } from '../../Database'; function mapStateToProps(state) { return { - documents: state.documents, - syncVersion: state.syncVersion, - currentUser: state.currentUser, + documents: state.app.documents, + syncVersion: state.app.syncVersion, + currentUser: state.app.currentUser, }; } @@ -57,28 +59,16 @@ class SyncScreen extends React.Component { } async sync() { - if (!SyncController.checkConnection()) { - Alert.alert( - i18n.t('global.warning'), - i18n.t('error.must_be_connected_internet_sync') - ); - return; - } - this.setState({ processing: true }); - // getVersion - const resVersion = await SyncController.getVersion().then(res => { - return res.data; - }).catch(error => { - console.log(error); - Alert.alert( - i18n.t('error.error'), - i18n.t('error.error_has_occurred') - ); - }); - this.setState({ newApp: resVersion.newApp }); - // getUsers - this.setState({ txtStep: i18n.t('sync_s.updating_users') + '...' }); - // const resUsers = await SyncController.getUsers().then(res => { + // if (!SyncController.checkConnection()) { + // Alert.alert( + // i18n.t('global.warning'), + // i18n.t('error.must_be_connected_internet_sync') + // ); + // return; + // } + // this.setState({ processing: true }); + // // getVersion + // const resVersion = await SyncController.getVersion().then(res => { // return res.data; // }).catch(error => { // console.log(error); @@ -87,6 +77,63 @@ class SyncScreen extends React.Component { // i18n.t('error.error_has_occurred') // ); // }); + // this.props.dispatch({ + // type: 'UPDATE_VERSION', + // data: resVersion.version, + // }); + // this.props.dispatch({ + // type: 'UPDATE_SHOW_IS_ACTIVE', + // data: resVersion.ShowIsActive, + // }); + // this.setState({ newApp: resVersion.newApp }); + // // getUsers + // this.setState({ txtStep: i18n.t('sync_s.updating_users') + '...' }); + // const resUsers = await SyncController.getUsers().then(res => { + // User.clear(); + // return res.data; + // }).catch(error => { + // console.log(error); + // Alert.alert( + // i18n.t('error.error'), + // i18n.t('error.error_has_occurred') + // ); + // }); + // resUsers.forEach(user => { + // User.add(user); + // }); + // // getContacts + // this.setState({ progress: 0.1, txtStep: i18n.t('sync_s.updating_contacts') + '...' }); + // const resContacts = await SyncController.getContacts().then(res => { + // Contact.clear(); + // return res.data; + // }).catch(error => { + // console.log(error); + // Alert.alert( + // i18n.t('error.error'), + // i18n.t('error.error_has_occurred') + // ); + // }); + // resContacts.forEach(contact => { + // Contact.add(contact); + // }); + // sendBrochures + // this.setState({ progress: 0.2, txtStep: i18n.t('sync_s.sending_flyers') + '...' }); + // await SyncController.sendBrochures(); + // 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); + Alert.alert( + i18n.t('error.error'), + i18n.t('error.error_has_occurred') + ); + }); + resDirectories.forEach(dir => { + Directory.add(dir); + }); // End this.setState({ txtStep: i18n.t('sync_s.update_completed') + '.', processing: false }); } diff --git a/ComApp/src/Database.js b/ComApp/src/Database.js index 4696864..2fe6279 100644 --- a/ComApp/src/Database.js +++ b/ComApp/src/Database.js @@ -2,8 +2,11 @@ import Realm from "realm"; // Models import SettingModel from "./App/Models/Setting"; import UserModel from './App/Models/User'; +import ContactModel from './App/Models/Contact'; +import SendBrochureModel from "./App/Models/SendBrochure"; +import DirectoryModel from "./App/Models/Directory"; -const SettingsSchema = { +const SettingSchema = { name: 'Settings', primaryKey: 'id', properties: { @@ -26,11 +29,65 @@ const UserSchema = { authorizedFolders: 'string', }, }; - + +const ContactSchema = { + name: 'Contacts', + primaryKey: 'id', + properties: { + id: 'int', + email: 'string', + type: 'string', + other: { type: 'string', optional: true }, + select1: { type: 'string', optional: true }, + select2: { type: 'string', optional: true }, + name: 'string', + firstname: 'string', + company: { type: 'string', optional: true }, + address: { type: 'string', optional: true }, + phone: 'string', + products: { type: 'string', optional: true }, + // TODO : card_url ? + notes: { type: 'string', optional: true }, + visits: { type: 'string', optional: true }, + user_id: 'int', + uniq_id: 'string', + to_delete: { type: 'bool', default: false }, + synced: { type: 'bool', default: true }, + }, +}; + +const SendBrochureSchema = { + name: 'SendBrochures', + primaryKey: 'id', + properties: { + id: 'int', + email: 'string', + subject: 'string', + message: 'string', + documents: 'string', + user_id: 'int' + }, +}; + +const DirectorySchema = { + name: 'Directories', + primaryKey: 'id', + properties: { + id: 'int', + name: 'string', + parent: { type: 'int', optional: true }, + order: 'int', + needAuthorization: 'int', + }, +}; + const Database = { schema: [ - SettingsSchema, - UserSchema + SettingSchema, + UserSchema, + ContactSchema, + SendBrochureSchema, + DirectorySchema ], //encryptionKey: new Int8Array(64) }; @@ -38,8 +95,14 @@ const Database = { let realm = new Realm(Database); let Setting = new SettingModel(realm); let User = new UserModel(realm); +let Contact = new ContactModel(realm); +let SendBrochure = new SendBrochureModel(realm); +let Directory = new DirectoryModel(realm); export { Setting, - User + User, + Contact, + SendBrochure, + Directory } \ No newline at end of file