96 lines
2.6 KiB
JavaScript
96 lines
2.6 KiB
JavaScript
|
const UserError = require('vn-loopback/util/user-error');
|
||
|
const fs = require("fs");
|
||
|
const fastCsv = require("fast-csv");
|
||
|
const axios = require('axios');
|
||
|
|
||
|
module.exports = Self => {
|
||
|
Self.remoteMethod('syncData', {
|
||
|
description: 'Sync schema data from external provider',
|
||
|
accessType: 'WRITE',
|
||
|
returns: {
|
||
|
type: 'object',
|
||
|
root: true
|
||
|
},
|
||
|
http: {
|
||
|
path: `/syncData`,
|
||
|
verb: 'POST'
|
||
|
}
|
||
|
});
|
||
|
|
||
|
Self.syncData = async () => {
|
||
|
const models = Self.app.models;
|
||
|
const tx = await Self.beginTransaction({});
|
||
|
try {
|
||
|
const tables = await models.TableMultiConfig.find();
|
||
|
if (!tables?.length) throw new UserError(`No tables to sync`);
|
||
|
|
||
|
const floricodeConfig = await models.FloricodeConfig.findOne();
|
||
|
if (!floricodeConfig) throw new UserError(`Floricode service is not configured`);
|
||
|
|
||
|
const token = await getToken(floricodeConfig);
|
||
|
for (const table of tables) {
|
||
|
const data = await getData(floricodeConfig.url, table.method, token);
|
||
|
if (!data) continue;
|
||
|
|
||
|
await Self.rawSql(`TRUNCATE edi.??`, [table.toTable]);
|
||
|
|
||
|
const ws = fs.createWriteStream("data.csv");
|
||
|
|
||
|
// Falta implementar la escritura de los datos en el archivo CSV
|
||
|
await fastCsv
|
||
|
.write(JSON.parse(data[0]), { headers: true, delimiter: ";" })
|
||
|
.pipe(ws);
|
||
|
|
||
|
const templatePath = path.join(__dirname, `./sql/${table.toTable}.sql`);
|
||
|
const sqlTemplate = await fs.readFile(templatePath, 'utf8');
|
||
|
const filePath = path.join(tempDir, file);
|
||
|
await Self.rawSql(sqlTemplate, [filePath], options);
|
||
|
await fs.remove(ws);
|
||
|
await table.updateAttribute('udpated', Date.vnNew());
|
||
|
}
|
||
|
await tx.commit();
|
||
|
} catch (e) {
|
||
|
await tx.rollback();
|
||
|
throw e;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
async function getToken(floricodeConfig) {
|
||
|
const response = await axios.post(`${floricodeConfig.url}/oauth/token`, {
|
||
|
grant_type: 'client_credentials',
|
||
|
client_id: floricodeConfig.user,
|
||
|
client_secret: floricodeConfig.password
|
||
|
}, {
|
||
|
headers: {
|
||
|
'Content-Type': 'application/x-www-form-urlencoded'
|
||
|
}
|
||
|
});
|
||
|
return response.data.access_token;
|
||
|
}
|
||
|
|
||
|
async function getData(url, method, token) {
|
||
|
let data = [];
|
||
|
let count = 0;
|
||
|
const maxCount = await getCount(url, method, token);
|
||
|
while (count < maxCount) {
|
||
|
const request = await axios.get(`${url}/v2/${method}?$skip=${count}`, {
|
||
|
headers: {
|
||
|
'Authorization': `Bearer ${token}`
|
||
|
}
|
||
|
});
|
||
|
data.push(request.data.value);
|
||
|
count += request.data.value.length;
|
||
|
}
|
||
|
return data;
|
||
|
}
|
||
|
|
||
|
async function getCount(url, method, token) {
|
||
|
const request = await axios.get(`${url}/v2/${method}?$count=true`, {
|
||
|
headers: {
|
||
|
'Authorization': `Bearer ${token}`
|
||
|
}
|
||
|
});
|
||
|
return request.data["@odata.count"];
|
||
|
}
|
||
|
};
|