2018-11-15 19:48:37 +01:00
|
|
|
import Request from './Instance'
|
|
|
|
|
import sendRequest from './Inst-Pao'
|
2019-02-06 18:47:54 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
export * from './ApiTypes'
|
|
|
|
|
import axios from 'axios'
|
2019-02-06 18:47:54 +01:00
|
|
|
|
|
|
|
|
export { addAuthHeaders, removeAuthHeaders, API_URL } from './Instance'
|
2018-11-15 19:48:37 +01:00
|
|
|
// import {AlgoliaSearch} from './AlgoliaController'
|
|
|
|
|
import Paths from '@paths'
|
2019-02-06 18:47:54 +01:00
|
|
|
import { rescodes } from '@src/store/Modules/rescodes'
|
|
|
|
|
|
2019-02-09 18:04:49 +01:00
|
|
|
import { GlobalStore, UserStore } from '@modules'
|
2019-02-08 17:10:25 +01:00
|
|
|
import globalroutines from './../../globalroutines/index'
|
2019-02-09 18:04:49 +01:00
|
|
|
import { serv_constants } from '@src/store/Modules/serv_constants'
|
|
|
|
|
import router from '@router'
|
2019-02-19 02:33:02 +01:00
|
|
|
import * as Types from "@src/store/Api/ApiTypes"
|
2018-11-15 19:48:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// const algoliaApi = new AlgoliaSearch()
|
|
|
|
|
export namespace ApiTool {
|
|
|
|
|
export async function post(path: string, payload?: any) {
|
|
|
|
|
return await Request('post', path, payload)
|
|
|
|
|
}
|
2019-02-06 18:47:54 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
export async function postFormData(path: string, payload?: any) {
|
|
|
|
|
return await Request('postFormData', path, payload)
|
|
|
|
|
}
|
2019-02-06 18:47:54 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
export async function get(path: string, payload?: any) {
|
|
|
|
|
return await Request('get', path, payload)
|
|
|
|
|
}
|
2019-02-06 18:47:54 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
export async function put(path: string, payload?: any) {
|
|
|
|
|
return await Request('put', path, payload)
|
|
|
|
|
}
|
2019-02-06 18:47:54 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
export async function Delete(path: string, payload: any) {
|
|
|
|
|
return await Request('delete', path, payload)
|
|
|
|
|
}
|
2019-02-06 18:47:54 +01:00
|
|
|
|
|
|
|
|
export async function checkSession({ token, refresh_token }) {
|
2018-11-15 19:48:37 +01:00
|
|
|
return await axios.post(process.env.API_URL + Paths.TOKEN_REFRESH, {
|
|
|
|
|
refresh_token
|
|
|
|
|
}, {
|
|
|
|
|
headers: {
|
|
|
|
|
'Authorization': `Bearer ${token}`
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 02:33:02 +01:00
|
|
|
export async function SendReq(url: string, method: string, mydata: any, setAuthToken: boolean = false): Promise<Types.AxiosSuccess | Types.AxiosError> {
|
2019-02-06 18:47:54 +01:00
|
|
|
UserStore.mutations.setServerCode(rescodes.EMPTY)
|
2019-02-09 18:04:49 +01:00
|
|
|
UserStore.mutations.setResStatus(0)
|
2019-02-06 18:47:54 +01:00
|
|
|
return await new Promise(function (resolve, reject) {
|
2019-02-19 02:33:02 +01:00
|
|
|
|
|
|
|
|
return sendRequest(url, method, mydata)
|
|
|
|
|
.then(res => {
|
|
|
|
|
console.log('res', res)
|
2019-02-08 17:10:25 +01:00
|
|
|
|
2019-02-09 18:04:49 +01:00
|
|
|
UserStore.mutations.setResStatus(res.status)
|
2019-02-19 02:33:02 +01:00
|
|
|
if (res.status === serv_constants.RIS_CODE__HTTP_FORBIDDEN_INVALID_TOKEN) {
|
2019-02-09 18:04:49 +01:00
|
|
|
// Forbidden
|
|
|
|
|
// You probably is connectiong with other page...
|
|
|
|
|
UserStore.mutations.setServerCode(rescodes.ERR_AUTHENTICATION)
|
|
|
|
|
UserStore.mutations.setAuth('')
|
|
|
|
|
router.push('/signin')
|
2019-02-12 12:06:01 +01:00
|
|
|
return reject({ code: rescodes.ERR_AUTHENTICATION })
|
2019-02-07 00:53:10 +01:00
|
|
|
}
|
2019-02-06 18:47:54 +01:00
|
|
|
|
2019-02-19 02:33:02 +01:00
|
|
|
return resolve(res)
|
2019-02-06 18:47:54 +01:00
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.catch(error => {
|
2019-02-19 02:33:02 +01:00
|
|
|
console.log('error', error)
|
|
|
|
|
return reject(error)
|
2019-02-06 18:47:54 +01:00
|
|
|
})
|
|
|
|
|
})
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-08 17:10:25 +01:00
|
|
|
export async function syncAlternative(mystrparam) {
|
2019-02-15 01:25:44 +01:00
|
|
|
// console.log('[ALTERNATIVE Background syncing', mystrparam)
|
2019-02-08 17:10:25 +01:00
|
|
|
|
|
|
|
|
let multiparams = mystrparam.split('|')
|
|
|
|
|
if (multiparams) {
|
|
|
|
|
if (multiparams.length > 3) {
|
|
|
|
|
let cmd = multiparams[0]
|
|
|
|
|
let table = multiparams[1]
|
|
|
|
|
let method = multiparams[2]
|
|
|
|
|
let token = multiparams[3]
|
|
|
|
|
// let lang = multiparams[3]
|
|
|
|
|
|
|
|
|
|
if (cmd === 'sync-todos') {
|
2019-02-12 19:09:43 +01:00
|
|
|
// console.log('[Alternative] Syncing', cmd, table, method)
|
2019-02-08 17:10:25 +01:00
|
|
|
|
|
|
|
|
const headers = new Headers()
|
|
|
|
|
headers.append('content-Type', 'application/json')
|
|
|
|
|
headers.append('Accept', 'application/json')
|
|
|
|
|
headers.append('x-auth', token)
|
|
|
|
|
|
2019-02-12 19:09:43 +01:00
|
|
|
let errorfromserver = false
|
|
|
|
|
let lettoqualcosa = false
|
|
|
|
|
|
2019-02-09 18:04:49 +01:00
|
|
|
// console.log('A1) INIZIO.............................................................')
|
2019-02-14 18:38:23 +01:00
|
|
|
return globalroutines(null, 'readall', table, null)
|
2019-02-08 17:10:25 +01:00
|
|
|
.then(function (alldata) {
|
2019-02-12 19:09:43 +01:00
|
|
|
const myrecs = [...alldata]
|
|
|
|
|
// console.log('----------------------- LEGGO QUALCOSA ')
|
|
|
|
|
|
|
|
|
|
const promises = myrecs.map(rec => {
|
|
|
|
|
// console.log('syncing', table, '', rec.descr)
|
|
|
|
|
let link = process.env.MONGODB_HOST + '/todos'
|
|
|
|
|
|
|
|
|
|
if (method !== 'POST')
|
|
|
|
|
link += '/' + rec._id
|
|
|
|
|
|
|
|
|
|
// console.log(' [Alternative] ++++++++++++++++++ SYNCING !!!! ', rec.descr, table, 'FETCH: ', method, link, 'data:')
|
|
|
|
|
|
|
|
|
|
// Insert/Delete/Update table to the server
|
|
|
|
|
return fetch(link, {
|
|
|
|
|
method: method,
|
|
|
|
|
headers: headers,
|
|
|
|
|
cache: 'no-cache',
|
|
|
|
|
mode: 'cors', // 'no-cors',
|
|
|
|
|
body: JSON.stringify(rec)
|
|
|
|
|
})
|
|
|
|
|
.then(() => {
|
|
|
|
|
lettoqualcosa = true
|
2019-02-14 18:38:23 +01:00
|
|
|
return globalroutines(null, 'delete', table, null, rec._id)
|
2019-02-12 19:09:43 +01:00
|
|
|
})
|
|
|
|
|
.then(() => {
|
2019-02-14 18:38:23 +01:00
|
|
|
return globalroutines(null, 'delete', 'swmsg', null, mystrparam)
|
2019-02-12 19:09:43 +01:00
|
|
|
})
|
|
|
|
|
.catch(function (err) {
|
|
|
|
|
if (err.message === 'Failed to fetch') {
|
|
|
|
|
errorfromserver = true
|
|
|
|
|
}
|
|
|
|
|
// console.log(' [Alternative] !!!!!!!!!!!!!!! Error while sending data', err, errorfromserver, 'lettoqualcosa', lettoqualcosa)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// CALL ALL THE PROMISES
|
|
|
|
|
return Promise.all(promises).then(() => {
|
|
|
|
|
return (errorfromserver && !lettoqualcosa)
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
return (errorfromserver && !lettoqualcosa)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}).catch(e => {
|
|
|
|
|
// console.log('ERROR:', e)
|
|
|
|
|
return (errorfromserver && !lettoqualcosa)
|
|
|
|
|
})
|
|
|
|
|
.then((errorfromserver) => {
|
|
|
|
|
// console.log('¨¨¨¨¨¨¨¨¨¨¨¨¨¨ errorfromserver:', errorfromserver)
|
|
|
|
|
const mystate = errorfromserver ? 'offline' : 'online'
|
|
|
|
|
GlobalStore.mutations.setStateConnection(mystate)
|
2019-02-14 18:38:23 +01:00
|
|
|
return globalroutines(null, 'write', 'config', { _id: 2, stateconn: mystate })
|
2019-02-08 17:10:25 +01:00
|
|
|
})
|
2019-02-09 18:04:49 +01:00
|
|
|
|
2019-02-12 19:09:43 +01:00
|
|
|
// console.log(' [Alternative] A2) ?????????????????????????? ESCO DAL LOOP !!!!!!!!!')
|
2019-02-09 18:04:49 +01:00
|
|
|
|
2019-02-08 17:10:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 18:47:54 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
export default ApiTool
|