salix/back/methods/edi/syncData.js

94 lines
2.8 KiB
JavaScript
Raw Normal View History

2025-02-17 14:03:23 +00:00
const UserError = require('vn-loopback/util/user-error');
2025-02-18 12:05:57 +00:00
const fs = require('fs-extra');
2025-02-17 14:03:23 +00:00
const fastCsv = require("fast-csv");
const axios = require('axios');
2025-02-18 12:05:57 +00:00
const path = require('path');
const { pipeline } = require('stream/promises');
2025-02-17 14:03:23 +00:00
module.exports = Self => {
2025-02-19 12:45:06 +00:00
Self.remoteMethod('syncData', {
2025-02-17 14:03:23 +00:00
description: 'Sync schema data from external provider',
accessType: 'WRITE',
returns: {
type: 'object',
root: true
},
http: {
path: `/syncData`,
verb: 'POST'
}
});
2025-02-18 12:52:27 +00:00
2025-02-19 12:45:06 +00:00
Self.syncData = async options => {
2025-02-17 14:03:23 +00:00
const models = Self.app.models;
2025-02-19 08:51:04 +00:00
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
let tx, ws;
2025-02-17 14:03:23 +00:00
try {
2025-02-19 08:51:04 +00:00
const floricodeConfig = await models.FloricodeConfig.findOne({}, myOptions);
2025-02-17 14:03:23 +00:00
if (!floricodeConfig) throw new UserError(`Floricode service is not configured`);
2025-02-19 08:51:04 +00:00
const tables = await models.TableMultiConfig.find({}, myOptions);
2025-02-18 12:52:27 +00:00
if (!tables?.length) throw new UserError(`No tables to sync`);
2025-02-19 08:51:04 +00:00
const token = await Self.getToken(floricodeConfig);
2025-02-17 14:03:23 +00:00
for (const table of tables) {
2025-02-19 08:51:04 +00:00
const data = await Self.getData(floricodeConfig.url, table.method, token);
2025-02-17 14:03:23 +00:00
if (!data) continue;
2025-02-19 12:45:06 +00:00
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
2025-02-17 14:03:23 +00:00
2025-02-19 12:45:06 +00:00
await Self.rawSql(`DELETE FROM edi.??`, [table.toTable], myOptions);
ws = fs.createWriteStream(path.join(__dirname, `/${table.toTable}.csv`));
2025-02-18 12:05:57 +00:00
await pipeline(fastCsv.write(data, { delimiter: ';' }), ws);
const templatePath = path.join(__dirname, `./syncSql/${table.toTable}.sql`);
2025-02-17 14:03:23 +00:00
const sqlTemplate = await fs.readFile(templatePath, 'utf8');
2025-02-19 12:45:06 +00:00
await Self.rawSql(sqlTemplate, [ws.path], myOptions);
2025-02-18 12:52:27 +00:00
await fs.remove(ws.path);
2025-02-19 12:45:06 +00:00
await table.updateAttribute('updated', Date.vnNew(), myOptions);
if (tx) {
await tx.commit();
delete myOptions.transaction;
}
2025-02-17 14:03:23 +00:00
}
} catch (e) {
2025-02-18 12:05:57 +00:00
if (tx) await tx.rollback();
if (await fs.pathExists(ws.path))
await fs.remove(ws.path);
2025-02-17 14:03:23 +00:00
throw e;
}
};
2025-02-19 08:51:04 +00:00
Self.getToken = async function ({ url, user, password}) {
return (await axios.post(`${url}/oauth/token`, {
grant_type: 'client_credentials',
client_id: user,
client_secret: password
}, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}
2025-02-18 12:52:27 +00:00
)).data.access_token;
2025-02-19 08:51:04 +00:00
};
2025-02-17 14:03:23 +00:00
2025-02-19 08:51:04 +00:00
Self.getData = async function (url, method, token) {
2025-02-17 14:03:23 +00:00
let data = [];
let count = 0;
2025-02-19 08:51:04 +00:00
const maxCount = (await Self.get(`${url}/v2/${method}?$count=true`, token))["@odata.count"];
2025-02-18 12:05:57 +00:00
while (count < maxCount) {
2025-02-19 08:51:04 +00:00
const response = await Self.get(`${url}/v2/${method}?$skip=${count}`, token)
2025-02-18 12:52:27 +00:00
data.push(...response.value);
count += response.value.length;
2025-02-17 14:03:23 +00:00
}
return data;
2025-02-19 08:51:04 +00:00
};
2025-02-17 14:03:23 +00:00
2025-02-19 08:51:04 +00:00
Self.get = async function get(url, token) {
return (await axios.get(url, { headers: { Authorization: `Bearer ${token}` } })).data;
};
2025-02-17 14:03:23 +00:00
};