208 lines
4.7 KiB
JavaScript
208 lines
4.7 KiB
JavaScript
/*
|
|
2.7.5 - Метод отправки документов по Сделке на проверку
|
|
POST /lk/document
|
|
*/
|
|
import fs from 'fs';
|
|
import axios from 'axios';
|
|
import { Cookies } from 'react-cookie';
|
|
import cookie from 'cookie';
|
|
import moment from 'moment';
|
|
import jwt from 'jsonwebtoken';
|
|
import { inspect } from 'util';
|
|
import FormData from 'form-data';
|
|
import multer from 'multer';
|
|
import { eachLimit } from 'async';
|
|
|
|
import { cors } from '../../../lib/cors';
|
|
import RedisClient from '../../../lib/RedisClient';
|
|
|
|
const uploads = `${ __dirname }/../../../../../uploads/deals/`;
|
|
|
|
/*
|
|
function uploadFiles()
|
|
{
|
|
return new Promise(() => )
|
|
}
|
|
*/
|
|
|
|
export default async function handler(req, res)
|
|
{
|
|
await cors(req, res);
|
|
|
|
// const { number, entity, id, filename } = req.query;
|
|
const { deal_id } = req.body;
|
|
|
|
return new Promise(async (resolve) =>
|
|
{
|
|
if(req.headers.cookie !== undefined)
|
|
{
|
|
const cookies = cookie.parse(req.headers?.cookie ? req.headers?.cookie : "");
|
|
|
|
if(cookies.jwt !== undefined && cookies.jwt !== null)
|
|
{
|
|
var client_jwt_decoded = jwt.verify(cookies.jwt, process.env.JWT_SECRET_CLIENT);
|
|
var crm_jwt = jwt.sign({ acc_number: client_jwt_decoded.acc_number }, process.env.JWT_SECRET_CRM, { noTimestamp: true });
|
|
|
|
const key = `deals_${ client_jwt_decoded.acc_number }`;
|
|
var deals = await RedisClient.get(key);
|
|
|
|
var files = [];
|
|
if(deals !== null)
|
|
{
|
|
deals = JSON.parse(deals);
|
|
|
|
if(deals[ deal_id ] !== undefined)
|
|
{
|
|
for(let i in deals[ deal_id ].files)
|
|
{
|
|
files.push(deals[ deal_id ].files[i]);
|
|
}
|
|
|
|
console.log({ files });
|
|
console.log(files[0])
|
|
|
|
eachLimit(files, 5, (file_entry, callback) =>
|
|
{
|
|
const file = file_entry[0];
|
|
try
|
|
{
|
|
if(fs.existsSync(`${ uploads }${ file.filename }`))
|
|
{
|
|
const file_to_send_data = fs.readFileSync(`${ uploads }${ file.filename }`);
|
|
const data = new FormData();
|
|
data.append("file", file_to_send_data, file.name);
|
|
|
|
const payload = new URLSearchParams({
|
|
name: deal_id,
|
|
entity: "opportunity",
|
|
documentTypeNumber: file.group,
|
|
documentName: file.name,
|
|
});
|
|
|
|
const file_upload_url = `${ process.env.CRM_API_HOST }/lk/document/upload?${ payload.toString() }`;
|
|
console.log( file_upload_url );
|
|
|
|
axios.post(file_upload_url, data,
|
|
{
|
|
headers: {
|
|
"Content-Type": `multipart/form-data; boundary=${ data._boundary }`,
|
|
"Authorization": `Bearer ${ crm_jwt }`,
|
|
},
|
|
withCredentials: true,
|
|
})
|
|
.then(() =>
|
|
{
|
|
console.log("FILE", file.filename, "SENT");
|
|
callback();
|
|
})
|
|
.catch(() =>
|
|
{
|
|
callback();
|
|
});
|
|
}
|
|
else
|
|
{
|
|
callback();
|
|
}
|
|
/*
|
|
console.log({ file_upload_url });
|
|
|
|
|
|
*/
|
|
}
|
|
catch(e)
|
|
{
|
|
console.error(e)
|
|
callback();
|
|
}
|
|
}, () =>
|
|
{
|
|
console.log("ALL FILES SENT");
|
|
res.status(200).json({});
|
|
resolve();
|
|
});
|
|
}
|
|
else
|
|
{
|
|
res.status(403).json({});
|
|
resolve();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
res.status(403).json({});
|
|
resolve();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
res.status(403).json({});
|
|
resolve();
|
|
}
|
|
|
|
/*
|
|
|
|
const payload = new URLSearchParams({
|
|
name: number,
|
|
entity: entity,
|
|
documentTypeNumber: id,
|
|
documentName: filename,
|
|
});
|
|
const path = `${ process.env.CRM_API_HOST }/lk/document/upload?${ payload.toString() }`;
|
|
console.log({ path });
|
|
|
|
upload.single("file")(req, {}, err =>
|
|
{
|
|
const { file } = req;
|
|
file.originalname = Buffer.from(file.originalname, 'latin1').toString('utf8');
|
|
|
|
const data = new FormData();
|
|
data.append("file", file.buffer, file.originalname);
|
|
|
|
try
|
|
{
|
|
axios.post(path, data,
|
|
{
|
|
headers: {
|
|
"Content-Type": `multipart/form-data; boundary=${ data._boundary }`,
|
|
"Authorization": `Bearer ${ crm_jwt }`,
|
|
},
|
|
withCredentials: true,
|
|
})
|
|
.then((crm_response) =>
|
|
{
|
|
console.log("/lk/document/upload SUCCESS");
|
|
res.status(200).json(crm_response.data);
|
|
resolve();
|
|
})
|
|
.catch((error) =>
|
|
{
|
|
console.error("-".repeat(30), "error.response.data:");
|
|
console.error(error.response);
|
|
|
|
res.status(500).json(error.response.data);
|
|
resolve();
|
|
});
|
|
}
|
|
catch(e)
|
|
{
|
|
console.error(e);
|
|
res.status(500).send(e);
|
|
resolve();
|
|
}
|
|
});
|
|
}
|
|
else
|
|
{
|
|
res.status(403).send();
|
|
resolve();
|
|
}
|
|
*/
|
|
}
|
|
else
|
|
{
|
|
res.status(403).send();
|
|
resolve();
|
|
}
|
|
});
|
|
} |