First Committ
This commit is contained in:
221
src/store/Api/index.ts
Executable file
221
src/store/Api/index.ts
Executable file
@@ -0,0 +1,221 @@
|
||||
import { useRouter } from 'vue-router'
|
||||
import { useGlobalStore } from '@store/globalStore'
|
||||
import { useUserStore } from '@store/UserStore'
|
||||
|
||||
export * from './ApiTypes'
|
||||
import axios from 'axios'
|
||||
|
||||
export { addAuthHeaders, removeAuthHeaders, API_URL } from './Instance'
|
||||
// import {AlgoliaSearch} from './AlgoliaController'
|
||||
import Paths from '@paths'
|
||||
import { tools } from '@src/store/Modules/tools'
|
||||
import { toolsext } from '@src/store/Modules/toolsext'
|
||||
|
||||
import { serv_constants } from '@src/store/Modules/serv_constants'
|
||||
import router from '@router'
|
||||
import * as Types from '@src/store/Api/ApiTypes'
|
||||
import { costanti } from '@src/store/Modules/costanti'
|
||||
import * as ApiTables from '@src/store/Modules/ApiTables'
|
||||
import sendRequest from './Inst-Pao'
|
||||
import Request from './Instance'
|
||||
import globalroutines from '../../globalroutines/index'
|
||||
|
||||
function ReceiveResponsefromServer(tablesync: string, nametab: string, method: string, risdata: any) {
|
||||
// console.log('ReceiveResponsefromServer', nametab, method, risdata)
|
||||
if (risdata) {
|
||||
// Updated somw data after Server arrived data.
|
||||
if (method === 'PATCH') {
|
||||
if (nametab === 'projects') {
|
||||
if (risdata.projectris) {
|
||||
const copyrec = tools.jsonCopy(risdata.projectris)
|
||||
// +*Todo conv: Projects.updateProject({ objproj: copyrec })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// const algoliaApi = new AlgoliaSearch()
|
||||
export namespace ApiTool {
|
||||
export async function post(path: string, payload?: any) {
|
||||
const globalStore = useGlobalStore()
|
||||
globalStore.connData.downloading_server = 1
|
||||
globalStore.connData.uploading_server = 1
|
||||
return Request('post', path, payload)
|
||||
}
|
||||
|
||||
export async function postFormData(path: string, payload?: any) {
|
||||
const globalStore = useGlobalStore()
|
||||
globalStore.connData.uploading_server = 1
|
||||
globalStore.connData.downloading_server = 1
|
||||
return Request('postFormData', path, payload)
|
||||
}
|
||||
|
||||
export async function get(path: string, payload?: any) {
|
||||
const globalStore = useGlobalStore()
|
||||
globalStore.connData.downloading_server = 1
|
||||
globalStore.connData.uploading_server = 0
|
||||
return Request('get', path, payload)
|
||||
}
|
||||
|
||||
export async function put(path: string, payload?: any) {
|
||||
const globalStore = useGlobalStore()
|
||||
globalStore.connData.uploading_server = 1
|
||||
return Request('put', path, payload)
|
||||
}
|
||||
|
||||
export async function patch(path: string, payload?: any) {
|
||||
const globalStore = useGlobalStore()
|
||||
globalStore.connData.uploading_server = 1
|
||||
return Request('patch', path, payload)
|
||||
}
|
||||
|
||||
export async function Delete(path: string, payload: any) {
|
||||
const globalStore = useGlobalStore()
|
||||
globalStore.connData.uploading_server = 1
|
||||
return Request('delete', path, payload)
|
||||
}
|
||||
|
||||
export async function checkSession({ token, refresh_token }: any) {
|
||||
return axios.post(process.env.API_URL + Paths.TOKEN_REFRESH, {
|
||||
refresh_token,
|
||||
}, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function SendReq(url: string, method: string, mydata: any, setAuthToken: boolean = false): Promise<Types.AxiosSuccess | Types.AxiosError> {
|
||||
const mydataout = {
|
||||
...mydata,
|
||||
keyappid: process.env.PAO_APP_ID,
|
||||
idapp: process.env.APP_ID,
|
||||
}
|
||||
|
||||
// console.log('mydata', mydata)
|
||||
|
||||
const userStore = useUserStore()
|
||||
const globalStore = useGlobalStore()
|
||||
const $router = useRouter()
|
||||
|
||||
userStore.setServerCode(tools.EMPTY)
|
||||
userStore.setResStatus(0)
|
||||
return new Promise((resolve, reject) => sendRequest(url, method, mydataout)
|
||||
.then((res) => {
|
||||
// console.log('res', res)
|
||||
|
||||
setTimeout(() => {
|
||||
if (method === 'get') {
|
||||
globalStore.connData.downloading_server = 0
|
||||
} else {
|
||||
globalStore.connData.uploading_server = 0
|
||||
globalStore.connData.downloading_server = 0
|
||||
}
|
||||
}, 1000)
|
||||
|
||||
if (res.status) {
|
||||
userStore.setResStatus(res.status)
|
||||
if (res.status === serv_constants.RIS_CODE__HTTP_FORBIDDEN_INVALID_TOKEN) {
|
||||
// Forbidden
|
||||
// You probably is connectiong with other page...
|
||||
userStore.setServerCode(toolsext.ERR_AUTHENTICATION)
|
||||
userStore.setAuth('')
|
||||
$router.push('/signin')
|
||||
return reject({ code: toolsext.ERR_AUTHENTICATION })
|
||||
}
|
||||
}
|
||||
|
||||
return resolve(res)
|
||||
})
|
||||
.catch((error) => {
|
||||
setTimeout(() => {
|
||||
if (method === 'get') {
|
||||
globalStore.connData.downloading_server = -1
|
||||
} else {
|
||||
globalStore.connData.uploading_server = -1
|
||||
globalStore.connData.downloading_server = -1
|
||||
}
|
||||
}, 1000)
|
||||
|
||||
console.log('error', error)
|
||||
return reject(error)
|
||||
}))
|
||||
}
|
||||
|
||||
export async function syncAlternative(mystrparam: string) {
|
||||
// console.log('[ALTERNATIVE Background syncing', mystrparam)
|
||||
|
||||
const multiparams = mystrparam.split('|')
|
||||
if (multiparams) {
|
||||
if (multiparams.length > 3) {
|
||||
const cmd = multiparams[0]
|
||||
const tablesync = multiparams[1]
|
||||
const nametab = multiparams[2]
|
||||
const method = multiparams[3]
|
||||
// const token = multiparams[3]
|
||||
|
||||
if (cmd === ApiTables.DB.CMD_SYNC) {
|
||||
let errorfromserver = false
|
||||
let lettoqualcosa = false
|
||||
|
||||
// console.log('A1) INIZIO.............................................................')
|
||||
return globalroutines(null, 'readall', tablesync, null)
|
||||
.then((alldata) => {
|
||||
if (alldata === undefined) {
|
||||
console.log('alldata NON DEFINITA')
|
||||
return true
|
||||
}
|
||||
const myrecs = [...alldata]
|
||||
|
||||
const promises = myrecs.map((rec) => {
|
||||
let link = `/${ApiTables.getLinkByTableName(nametab)}`
|
||||
|
||||
if (method !== 'POST') {
|
||||
link += `/${rec._id}`
|
||||
}
|
||||
|
||||
console.log('----------------------- LEGGO QUALCOSA ', link)
|
||||
|
||||
// Insert/Delete/Update table to the server
|
||||
return SendReq(link, method, rec)
|
||||
.then((ris) => {
|
||||
ReceiveResponsefromServer(tablesync, nametab, method, ris.data)
|
||||
lettoqualcosa = true
|
||||
return globalroutines(null, 'delete', tablesync, null, rec._id)
|
||||
})
|
||||
.then(() => {
|
||||
return globalroutines(null, 'delete', 'swmsg', null, mystrparam)
|
||||
}).catch((err) => {
|
||||
if (err.msgerr) {
|
||||
if (err.msgerr.message.includes('Failed to fetch') || err.msgerr.message.includes('Network Error')) {
|
||||
errorfromserver = true
|
||||
}
|
||||
}
|
||||
console.log(' [Alternative] !!!!!!!!!!!!!!! Error while sending data', err, errorfromserver, 'lettoqualcosa', lettoqualcosa)
|
||||
if (!errorfromserver) {
|
||||
return globalroutines(null, 'delete', 'swmsg', null, mystrparam)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
// CALL ALL THE PROMISES
|
||||
return Promise.all(promises).then(() => (errorfromserver && !lettoqualcosa)).catch((err) => (errorfromserver && !lettoqualcosa))
|
||||
}).catch((error) => {
|
||||
console.log('¨¨¨¨¨¨¨¨¨¨¨¨¨¨ errorfromserver:', errorfromserver, error)
|
||||
return (errorfromserver && !lettoqualcosa)
|
||||
})
|
||||
.then((error) => {
|
||||
const mystate = (error || errorfromserver) ? 'offline' : 'online'
|
||||
|
||||
const globalStore = useGlobalStore()
|
||||
globalStore.setStateConnection(mystate)
|
||||
globalStore.saveConfig({ _id: costanti.CONFIG_ID_STATE_CONN, value: mystate })
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
export default ApiTool
|
||||
Reference in New Issue
Block a user