viralraval5

Image upload with complain

Aug 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. exports.user_complain = async (req, res) => {
  2. try{
  3.    if (req.files.length > 0) {
  4.           req.files.forEach(file => {
  5.             imagePaths.push(file.path);
  6.           });
  7.         }
  8.  
  9.         var API_KEY = YOUR_FRESHDESK_API_KEY;
  10.         var PATH = '/api/v2/tickets';
  11.         var URL = YOUR_FRESHDESK_URL + PATH;
  12.  
  13.         var fields = {
  14.           email: '[email protected]',
  15.           subject: 'This is test complain',
  16.           status: 2,
  17.           priority: 1,
  18.           phone: '1234567890',
  19.           name: 'customer name',
  20.           type: 'Issue',
  21.           group_id: 48000389195
  22.         };
  23.        
  24.         var Request = unirest.post(URL);
  25.         Request.auth({
  26.           user: API_KEY,
  27.           pass: 'X',
  28.           sendImmediately: true
  29.         })
  30.           // .type('json')
  31.           .field(fields)
  32.           .attach('attachments[]', imagePaths)
  33.           .end(function(response) {
  34.             if (response.status == 201) {
  35.               return res.status(200).send({
  36.                 data: response.body,
  37.                 message: 'Issue registered sucessfully!',
  38.                 status: true,
  39.                 error: false
  40.               });
  41.             } else {
  42.               console.log(response.status);
  43.               return res.status(404).send({
  44.                 data: response.body,
  45.                 message: 'Issue While registering Complaint. Try Again!!!',
  46.                 error: true
  47.               });
  48.             }
  49.           });
  50.  
  51. //to remove image from folder
  52.         if (imagePaths.length > 0) {
  53.           imagePaths.forEach(imagePath => {
  54.             removeImage(imagePath);
  55.           });
  56.         }
  57. }
  58. catch (err) {
  59.     console.log(err);
  60.     res.status(400).send({
  61.       message: 'Something wrong!!!',
  62.       status: false,
  63.       error: err
  64.     });
  65.   }
  66. }
  67.  
  68. // remove image function
  69. const removeFile = function(delPath) {
  70.   if (fs.existsSync(delPath)) {
  71.     fs.unlinkSync(delPath);
  72.   }
  73. };
Add Comment
Please, Sign In to add comment