2022-10-05 18:03:52 +02:00
|
|
|
// Lodash
|
|
|
|
|
// Http Client
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
// Network Infos
|
2023-08-22 10:32:40 +02:00
|
|
|
import NetInfo from '@react-native-community/netinfo';
|
2022-10-05 18:03:52 +02:00
|
|
|
// Application infos
|
|
|
|
|
import * as Application from 'expo-application';
|
2023-08-22 10:32:40 +02:00
|
|
|
// Database
|
|
|
|
|
import { Contact, Document, SendBrochure, Setting } from '../../Database';
|
2022-10-05 18:03:52 +02:00
|
|
|
|
2023-01-04 15:11:37 +01:00
|
|
|
const api_token = 'G9Q62jt8eL8gN4Ls';
|
2022-10-05 18:03:52 +02:00
|
|
|
|
|
|
|
|
class SyncController {
|
2023-08-22 10:32:40 +02:00
|
|
|
getUrlBo() {
|
|
|
|
|
const test = Setting.get('bo_url');
|
|
|
|
|
if (test === false || test === undefined) {
|
|
|
|
|
return 'noset';
|
|
|
|
|
}
|
|
|
|
|
return test.value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getAppStoreUUID() {
|
|
|
|
|
const test = Setting.get('AppStoreUUID');
|
|
|
|
|
if (test === false || test === undefined) {
|
|
|
|
|
return 'noset';
|
|
|
|
|
}
|
|
|
|
|
return test.value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async checkConnection() {
|
|
|
|
|
return NetInfo.fetch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getVersion() {
|
|
|
|
|
return axios({
|
|
|
|
|
method: 'GET',
|
|
|
|
|
url: this.getUrlBo() + '/api/version',
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
params: {
|
|
|
|
|
token: api_token,
|
|
|
|
|
app_uuid: this.getAppStoreUUID(),
|
|
|
|
|
app_version: Application.nativeApplicationVersion,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getUsers() {
|
|
|
|
|
return axios({
|
|
|
|
|
method: 'GET',
|
|
|
|
|
url: this.getUrlBo() + '/api/users',
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
params: {
|
|
|
|
|
token: api_token,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getContacts(userId) {
|
|
|
|
|
let contacts = Contact.getAll();
|
|
|
|
|
return axios({
|
|
|
|
|
method: 'POST',
|
|
|
|
|
url: this.getUrlBo() + '/api/contacts',
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
params: {
|
|
|
|
|
token: api_token,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
contacts: contacts,
|
|
|
|
|
userId: userId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendBrochure(brochure) {
|
|
|
|
|
return axios({
|
|
|
|
|
method: 'POST',
|
|
|
|
|
url: this.getUrlBo() + '/api/sendBrochure',
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
params: {
|
|
|
|
|
token: api_token,
|
|
|
|
|
emails: brochure,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getDirectories() {
|
|
|
|
|
return axios({
|
|
|
|
|
method: 'GET',
|
|
|
|
|
url: this.getUrlBo() + '/api/directories',
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
params: {
|
|
|
|
|
token: api_token,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getDocuments() {
|
|
|
|
|
return axios({
|
|
|
|
|
method: 'GET',
|
|
|
|
|
url: this.getUrlBo() + '/api/documents',
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
params: {
|
|
|
|
|
token: api_token,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateLastModificationDoc(id, date) {
|
|
|
|
|
Document.update({ id: id, updated_at: date.toString() }, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateVersion(version) {
|
|
|
|
|
const localVersion = Setting.get('version');
|
|
|
|
|
if (localVersion === false || localVersion === undefined) {
|
|
|
|
|
Setting.add({ key: 'version', value: version });
|
|
|
|
|
} else {
|
|
|
|
|
Setting.update({ id: localVersion.id, key: 'version', value: version });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateShowIsActive(value) {
|
|
|
|
|
const local = Setting.get('ShowIsActive');
|
|
|
|
|
if (local === false || local === undefined) {
|
|
|
|
|
Setting.add({ key: 'ShowIsActive', value: value });
|
|
|
|
|
} else {
|
|
|
|
|
Setting.update({ id: local.id, key: 'ShowIsActive', value: value });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isSynced() {
|
|
|
|
|
const brochures = SendBrochure.getAll();
|
|
|
|
|
const Contacts = Contact.getAll();
|
|
|
|
|
|
|
|
|
|
if (brochures !== undefined && Contacts !== undefined) {
|
|
|
|
|
const noSynced = Contacts.filtered('synced = false');
|
|
|
|
|
if (brochures.length > 0 || noSynced.length > 0) {
|
2022-10-06 16:25:15 +02:00
|
|
|
return false;
|
2023-08-22 10:32:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isLogged() {
|
|
|
|
|
const logged = Setting.get('isLogged');
|
|
|
|
|
const loggedTime = Setting.get('loggedTime');
|
|
|
|
|
if (
|
|
|
|
|
logged !== false &&
|
|
|
|
|
loggedTime !== false &&
|
|
|
|
|
logged !== undefined &&
|
|
|
|
|
loggedTime !== undefined
|
|
|
|
|
) {
|
|
|
|
|
let seconds =
|
|
|
|
|
Math.abs(new Date(Number(loggedTime.value)) - Date.now()) / 1000;
|
|
|
|
|
if (seconds < 14400) {
|
|
|
|
|
// 4h
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
needSync(callback) {
|
|
|
|
|
NetInfo.fetch().then(state => {
|
|
|
|
|
if (state.isConnected) {
|
|
|
|
|
axios({
|
|
|
|
|
method: 'get',
|
|
|
|
|
url: this.getUrlBo() + '/version',
|
|
|
|
|
responseType: 'json',
|
|
|
|
|
params: {
|
|
|
|
|
token: api_token,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.then(response => {
|
|
|
|
|
const localVersion = Setting.get('version');
|
|
|
|
|
if (
|
|
|
|
|
localVersion === undefined ||
|
|
|
|
|
localVersion === false ||
|
|
|
|
|
localVersion.value !== response.data.version
|
|
|
|
|
) {
|
|
|
|
|
return callback();
|
2022-12-30 16:44:51 +01:00
|
|
|
}
|
|
|
|
|
return false;
|
2023-08-22 10:32:40 +02:00
|
|
|
})
|
|
|
|
|
.catch(function (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setInstalled(remove = false) {
|
|
|
|
|
if (remove) {
|
|
|
|
|
Setting.delete('installed');
|
|
|
|
|
} else {
|
|
|
|
|
Setting.add({ key: 'installed', value: 'true' });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isInstalled() {
|
|
|
|
|
const test = Setting.get('installed');
|
|
|
|
|
if (test === false || test === undefined) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-10-05 18:03:52 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-22 10:32:40 +02:00
|
|
|
export default new SyncController();
|