salix/back/methods/edi/syncData.js

78 lines
2.5 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-18 12:05:57 +00:00
Self.remoteMethodCtx('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-18 12:05:57 +00:00
Self.syncData = async ctx => {
2025-02-17 14:03:23 +00:00
const models = Self.app.models;
2025-02-18 12:05:57 +00:00
let tx;
2025-02-17 14:03:23 +00:00
try {
const floricodeConfig = await models.FloricodeConfig.findOne();
if (!floricodeConfig) throw new UserError(`Floricode service is not configured`);
2025-02-18 12:52:27 +00:00
const tables = await models.TableMultiConfig.find();
if (!tables?.length) throw new UserError(`No tables to sync`);
2025-02-17 14:03:23 +00:00
const token = await getToken(floricodeConfig);
for (const table of tables) {
const data = await getData(floricodeConfig.url, table.method, token);
if (!data) continue;
2025-02-18 12:05:57 +00:00
tx = await Self.beginTransaction({});
const options = {transaction: tx, userId: ctx.req.accessToken.userId};
2025-02-17 14:03:23 +00:00
2025-02-18 12:05:57 +00:00
await Self.rawSql(`DELETE FROM edi.??`, [table.toTable]);
const ws = fs.createWriteStream(path.join(__dirname, `/${table.toTable}.csv`));
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-18 12:05:57 +00:00
await Self.rawSql(sqlTemplate, [ws.path], options);
2025-02-18 12:52:27 +00:00
fs.remove(ws.path);
2025-02-18 12:05:57 +00:00
await table.updateAttribute('updated', Date.vnNew(), options);
await tx.commit();
2025-02-17 14:03:23 +00:00
}
} catch (e) {
2025-02-18 12:05:57 +00:00
if (tx) await tx.rollback();
2025-02-17 14:03:23 +00:00
throw e;
}
};
2025-02-18 12:52:27 +00:00
const getToken = async ({ url, user, password }) =>
(await axios.post(`${url}/oauth/token`,
{ grant_type: 'client_credentials', client_id: user, client_secret: password },
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
)).data.access_token;
2025-02-17 14:03:23 +00:00
async function getData(url, method, token) {
let data = [];
let count = 0;
2025-02-18 12:52:27 +00:00
const maxCount = (await get(`${url}/v2/${method}?$count=true`, token))["@odata.count"];
2025-02-18 12:05:57 +00:00
while (count < maxCount) {
2025-02-18 12:52:27 +00:00
const response = await get(`${url}/v2/${method}?$skip=${count}`, token)
data.push(...response.value);
count += response.value.length;
2025-02-17 14:03:23 +00:00
}
return data;
}
2025-02-18 12:52:27 +00:00
const get = async (url, token) =>
(await axios.get(url, { headers: { Authorization: `Bearer ${token}` } })).data;
2025-02-17 14:03:23 +00:00
};