431 lines
13 KiB
JavaScript
431 lines
13 KiB
JavaScript
import React from 'react';
|
|
import axios from "axios";
|
|
import I18n from 'react-native-i18n'
|
|
import {db} from './persistences/db';
|
|
import {
|
|
adhesionUrl, checkAppVersion,
|
|
configActionUrl, defaultKey,
|
|
isDebugMode,
|
|
locationActionUrl,
|
|
memberActionUrl, mobileAppVersion,
|
|
networkActionUrl, saveActifSocietyUrl, saveManyActifsSocietyUrl
|
|
} from "./IlinkConstants";
|
|
import {store} from "../redux/store";
|
|
import {
|
|
fetchSaveActifSocietyError,
|
|
fetchSaveActifSocietyPending,
|
|
fetchSaveActifSocietySuccess
|
|
} from "../redux/actions/WalletActions";
|
|
|
|
|
|
var DBEvents = require('react-native-db-models').DBEvents;
|
|
var serializeJSON = function (data) {
|
|
|
|
return Object.keys(data).map(function (keyName) {
|
|
return encodeURIComponent(keyName) + '=' + encodeURIComponent(data[keyName])
|
|
}).join('&');
|
|
|
|
};
|
|
|
|
|
|
export const getCodeInformation = (code) => {
|
|
var data = {
|
|
"tag": 'member', "type": "agen_info_code", "code": code,
|
|
"lang": I18n.currentLocale()
|
|
};
|
|
|
|
return timeout(15000, queryData(data, memberActionUrl));
|
|
}
|
|
|
|
export const getCodeInformationForAddTaxe = (code, id_agent) => {
|
|
var data = {
|
|
"tag": 'member', "type": "agen_info_code", "code": code,
|
|
"lang": I18n.currentLocale(),
|
|
id_agent
|
|
};
|
|
|
|
return queryData(data, memberActionUrl)
|
|
}
|
|
export const getPubActiveObject = (idcpuntry) => {
|
|
return queryData({"tag": "get_pub", "id_country": idcpuntry}, configActionUrl);
|
|
|
|
}
|
|
|
|
export const getDefaultTown = () => {
|
|
return queryData({"tag": "default_locality"}, configActionUrl);
|
|
}
|
|
|
|
export const getPasObject = () => {
|
|
return queryData({"tag": "get_pas"}, configActionUrl);
|
|
}
|
|
|
|
export const getAppVersion = async () => {
|
|
console.warn("REQUEST URL", checkAppVersion + "?mobile_app_version=" + mobileAppVersion);
|
|
let data = {};
|
|
data["lang"] = I18n.currentLocale();
|
|
data["isTest"] = isDebugMode;
|
|
|
|
let response = await fetch(checkAppVersion + "?mobile_app_version=" + mobileAppVersion, {
|
|
method: 'GET',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'Content-Type': 'application/json',
|
|
}
|
|
});
|
|
let responseJson = null;
|
|
let responseText = await response.text()
|
|
try {
|
|
console.warn("SERVER RESPONSE SUCCESS", responseText);
|
|
responseJson = JSON.parse(responseText)
|
|
} catch (e) {
|
|
console.warn("SERVER RESPONSE ERROR", responseText);
|
|
return {"error": "error", "error_msg": responseText}
|
|
}
|
|
return responseJson;
|
|
}
|
|
|
|
function queryAuth(data) {
|
|
return timeout(50000, queryData(data, adhesionUrl));
|
|
}
|
|
|
|
async function queryData(data, url) {
|
|
console.warn("REQUEST URL", url);
|
|
console.warn("REQUEST BODY", JSON.stringify(data));
|
|
data["lang"] = I18n.currentLocale();
|
|
data["isTest"] = isDebugMode;
|
|
|
|
let response = await fetch(url, {
|
|
method: 'POST',
|
|
headers: {
|
|
Accept: 'application/json',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(data),
|
|
});
|
|
let responseJson = null;
|
|
console.log("response1===>>",data,response)
|
|
let responseText = await response.text()
|
|
console.log("response2===>>",data,responseText)
|
|
try {
|
|
console.warn("SERVER RESPONSE SUCCESS", responseText);
|
|
responseJson = JSON.parse(responseText)
|
|
} catch (e) {
|
|
console.warn("SERVER RESPONSE ERROR", responseText);
|
|
return {"error": "error", "error_msg": responseText}
|
|
}
|
|
return responseJson;
|
|
|
|
|
|
};
|
|
|
|
function timeout(ms, promise) {
|
|
return new Promise(function (resolve, reject) {
|
|
setTimeout(function () {
|
|
reject(new Error(I18n.t("TEXT_NETWORK_UNABLE")))
|
|
}, ms)
|
|
promise.then(resolve, reject)
|
|
})
|
|
}
|
|
|
|
export const validateUser = (user) => {
|
|
var data = {
|
|
"tag": 'member', "type": "valider_simple", "phone": user.phone,
|
|
"lang": I18n.currentLocale()
|
|
};
|
|
return queryData(data, memberActionUrl)
|
|
};
|
|
export const updatePosition = async (longitude, latitude) => {
|
|
const user = await readUser()
|
|
const data = {
|
|
'tag': 'member',
|
|
'type': 'update_position',
|
|
"agentId": user['agentId'],
|
|
"longitude": longitude,
|
|
latitude: latitude
|
|
}
|
|
return queryData(data, memberActionUrl);
|
|
}
|
|
export const valifateAgent = (user, info) => {
|
|
var data = info;
|
|
if (data === null) data = {}
|
|
data["tag"] = "member";
|
|
data['type'] = "validate_agent";
|
|
data["phone"] = user.phone;
|
|
data['code_validation'] = user.validation_code;
|
|
if (user.category === "super" && (info === null || (info !== null && (info.nbre_code === undefined || info.nbr_code === null)))) {
|
|
data["nbre_code"] = 10;
|
|
data["nbre_code_superviseur"] = 0
|
|
} else if (info === null || (info !== null && (info.nbre_code === undefined || info.nbre_code === null))) {
|
|
data["nbre_code"] = 0;
|
|
data["nbre_code_superviseur"] = 0
|
|
}
|
|
console.log(data);
|
|
|
|
return timeout(15000, queryData(data, memberActionUrl));
|
|
}
|
|
|
|
export const disconnect = () => {
|
|
return new Promise((resolve, reject) => {
|
|
readBrutUser().then((user) => {
|
|
db.remove({"_id": user._id}, {multi: true}, (error, numRemoved) => {
|
|
if (error !== null) {
|
|
reject(error)
|
|
} else {
|
|
console.log(numRemoved)
|
|
if (numRemoved > 0) {
|
|
resolve()
|
|
}
|
|
}
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
export const login = (phone: String, password: String, type: Number) => {
|
|
console.log("CURRENT_LANG", I18n.currentLocale());
|
|
var data = {
|
|
tag: type === 0 ? "login_user" : "login_agent",
|
|
phone: phone,
|
|
password: password,
|
|
lang: I18n.currentLocale()
|
|
};
|
|
|
|
return queryAuth(data);
|
|
};
|
|
|
|
export const reinitpassword = ($phone, $type) => {
|
|
var data = {"tag": "forgot_pass", "number": $phone, "typeaccount": $type, "lang": I18n.currentLocale()};
|
|
return queryAuth(data)
|
|
}
|
|
export const updateUserData = async (data) => {
|
|
const user = await readUser()
|
|
user.longitude = data.longitude
|
|
user.latitude = data.latitude
|
|
db.update({"user": {$exists: true}}, {user: user}, {}, function (err, numReplaced) {
|
|
console.warn(err, numReplaced)
|
|
})
|
|
/* new Promise((resolve,reject)=>{
|
|
readBrutUser().then((user)=>{
|
|
db.remove({"_id": user._id},{multi:true},(error,numRemoved)=>{
|
|
if(error!==null){
|
|
reject(error)
|
|
}else {
|
|
let muser=user
|
|
|
|
db.insert(muser, function(err){
|
|
if(err!==null) {
|
|
console.log("error when save user")
|
|
console.log(err)
|
|
}
|
|
|
|
})
|
|
console.log(numRemoved)
|
|
if (numRemoved > 0) {
|
|
resolve()
|
|
}
|
|
}
|
|
})
|
|
})
|
|
})*/
|
|
|
|
}
|
|
|
|
export const removeUser = () => {
|
|
return new Promise((resolve, reject) => {
|
|
db.remove({}, { multi: true }, function (reject, resolve) {
|
|
});
|
|
});
|
|
}
|
|
|
|
export const saveNewuser = (muser) => {
|
|
|
|
|
|
readUser().then(async (userParam) => {
|
|
console.log("USER", user);
|
|
let user = {
|
|
user: muser
|
|
}
|
|
if (userParam) {
|
|
db.update(user, user, function (err) {
|
|
if (err !== null) {
|
|
console.log("error when save user")
|
|
console.log(err)
|
|
}
|
|
|
|
});
|
|
} else {
|
|
db.insert(user, function (err) {
|
|
if (err !== null) {
|
|
console.log("error when save user")
|
|
console.log(err)
|
|
}
|
|
|
|
});
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
|
|
const readBrutUser = () => {
|
|
return new Promise((resolve, reject) => {
|
|
db.findOne({"user": {$exists: true}}, (err, result) => {
|
|
if (err !== null || result === null || result.user === undefined) {
|
|
resolve(null)
|
|
} else {
|
|
if (result.user === null) {
|
|
resolve(null)
|
|
} else
|
|
resolve(result)
|
|
}
|
|
;
|
|
})
|
|
});
|
|
}
|
|
|
|
export const readUser = () => {
|
|
return new Promise((resolve, reject) => {
|
|
db.findOne({"user": {$exists: true}}, async (err, result) => {
|
|
if (err !== null || result === null || result.user === undefined) {
|
|
resolve(null)
|
|
} else {
|
|
if (result.user === null) {
|
|
resolve(null)
|
|
} else {
|
|
|
|
resolve(result.user)
|
|
}
|
|
}
|
|
})
|
|
});
|
|
}
|
|
|
|
|
|
export const createGeolocatedAccount = (user) => {
|
|
|
|
return queryData(user, memberActionUrl);
|
|
}
|
|
|
|
export const createGeolocatedAccounts = (agents) => {
|
|
|
|
return queryData({
|
|
"tag": "member",
|
|
"type": "create_geolocated_users",
|
|
"lang": I18n.currentLocale(),
|
|
agents
|
|
}, memberActionUrl);
|
|
}
|
|
|
|
export const saveActifsSociety = (actifs) => {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
const auth = store.getState().authKeyReducer;
|
|
const authKey = defaultKey;
|
|
axios({
|
|
url: `${saveManyActifsSocietyUrl}`,
|
|
method: 'POST',
|
|
data: {assets: actifs},
|
|
headers: {
|
|
'Authorization': auth,
|
|
'X-Localization': I18n.currentLocale()
|
|
}
|
|
})
|
|
.then(response => {
|
|
console.log(response);
|
|
resolve(response);
|
|
})
|
|
.catch(error => {
|
|
if (error.response)
|
|
reject(error.response);
|
|
else if (error.request)
|
|
reject(error.request);
|
|
else
|
|
reject(error.message);
|
|
});
|
|
}
|
|
);
|
|
}
|
|
|
|
export const createUserAccount = (data) => {
|
|
return queryData(data, memberActionUrl)
|
|
}
|
|
|
|
export const acceptDemandAdhesion = (data) => {
|
|
return queryData({
|
|
"tag": "member",
|
|
"type": "activeSupervisor",
|
|
"code": data.code_membre,
|
|
"phone": data.phone
|
|
}, memberActionUrl)
|
|
}
|
|
export const deleteUser = async (data) => {
|
|
return queryData({
|
|
"tag": "member",
|
|
"type": "deleteAgent",
|
|
"code": data.code_membre,
|
|
"phone": data.phone
|
|
}, memberActionUrl)
|
|
}
|
|
export const listDemandAdhesion = async () => {
|
|
const user = await readUser();
|
|
const items = await queryData({
|
|
"type": "list_demande_adhesion",
|
|
"tag": "member",
|
|
"codeparrain": user.code_membre
|
|
}, memberActionUrl)
|
|
return items;
|
|
}
|
|
export const categoryChild = async (code) => {
|
|
const codes = await queryData({"type": "get_category_child", "tag": "member", "codeparrain": code}, memberActionUrl)
|
|
|
|
return codes;
|
|
}
|
|
export const getListCountriesActive = async () => {
|
|
const countries = await queryData({"tag": "avail_countries"}, networkActionUrl);
|
|
return countries;
|
|
|
|
}
|
|
export const getCountryNetwork = (country) => {
|
|
console.warn(country);
|
|
console.warn("GET COUNTRY NETWORK");
|
|
return queryData({"tag": 'listnetworkscountryindicatif', 'indicatif': country}, networkActionUrl)
|
|
}
|
|
export const getTownInformationName = (town) => {
|
|
return queryData({"tag": "town_information_name", "name": town}, locationActionUrl);
|
|
}
|
|
|
|
export const getSupervisorInfoCode = () => {
|
|
return queryData({"type": "all_supervisors_info_code", "tag": "member"}, memberActionUrl);
|
|
}
|
|
export const getSupervisorInfoCodeOffline = () => {
|
|
return queryData({"type": "supervisors_infos_off_line", "tag": "member"}, memberActionUrl);
|
|
}
|
|
export const generateAgentGeo = async (newPhone) => {
|
|
const user = await readUser();
|
|
const items = await queryData({
|
|
"type": "generateNetworkAgentForGeo",
|
|
"tag": "member",
|
|
"code_parrain": user.code_membre,
|
|
'phone': newPhone,
|
|
"lang": I18n.currentLocale()
|
|
}, memberActionUrl)
|
|
return items;
|
|
}
|
|
export const AssignAgentGeo = async (codeValidation, phone, taxes, id_agent, password, id_tax_notice, neighborhood) => {
|
|
const user = await readUser();
|
|
const items = await queryData({
|
|
"type": "assignNetworkAgentToGeo",
|
|
"tag": "member",
|
|
"id_super": user.agentId,
|
|
'code_parrain': codeValidation,
|
|
"phone": phone,
|
|
"taxes": taxes,
|
|
"lang": I18n.currentLocale(),
|
|
agentId: id_agent,
|
|
password,
|
|
id_tax_notice,
|
|
neighborhood
|
|
}, memberActionUrl)
|
|
return items;
|
|
}
|