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: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 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;
|
|
|
|
|
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]);
|
2025-02-17 14:03:23 +00:00
|
|
|
|
2025-02-18 12:05:57 +00:00
|
|
|
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);
|
|
|
|
await fs.remove(ws.path);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2025-02-18 12:05:57 +00:00
|
|
|
while (count < maxCount) {
|
2025-02-17 14:03:23 +00:00
|
|
|
const request = await axios.get(`${url}/v2/${method}?$skip=${count}`, {
|
|
|
|
headers: {
|
|
|
|
'Authorization': `Bearer ${token}`
|
|
|
|
}
|
|
|
|
});
|
2025-02-18 12:05:57 +00:00
|
|
|
data.push(...request.data.value);
|
2025-02-17 14:03:23 +00:00
|
|
|
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"];
|
|
|
|
}
|
|
|
|
};
|