Add Contact + Directory + Brochure + User sync
This commit is contained in:
parent
bdf1c994db
commit
c8aa3a4c84
7 changed files with 353 additions and 33 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
66
ComApp/src/App/Models/Contact.js
Normal file
66
ComApp/src/App/Models/Contact.js
Normal file
|
|
@ -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;
|
||||
37
ComApp/src/App/Models/Directory.js
Normal file
37
ComApp/src/App/Models/Directory.js
Normal file
|
|
@ -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;
|
||||
64
ComApp/src/App/Models/SendBrochure.js
Normal file
64
ComApp/src/App/Models/SendBrochure.js
Normal file
|
|
@ -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;
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue