SHARE
TWEET

Untitled

a guest Aug 20th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. var StringUtils = require('dw/util/StringUtils');
  4. var Calendar = require('dw/util/Calendar');
  5. var EmailModel = app.getModel('Email');
  6.  
  7. /*
  8.  * Searches in the impex for the given files and sends email with the files attached,
  9.  * after that archives the file
  10.  */
  11. function MailAndArchive (args) {
  12.     var recieverEmail = args.Email;
  13.     var path = args.Path;
  14.     var filesNames = args.FileNames;
  15.     var archivePath = args.ArchivePath;
  16.  
  17.     fileNames = fileNames.split('|');
  18.  
  19.     var files = createFiles(path, fileNames);
  20.  
  21.     EmailModel.sendMailWithAttachments(files, {
  22.         template: 'mail/mail_with_attachment',
  23.         recipient : recieverEmail,
  24.         subject : 'Back in stock csv',
  25.         mimeType : 'multipart/mixed; boundary=------------000001030701020908040900',
  26.         encoding : 'iso-8859-1'
  27.     });
  28.  
  29.     archiveFiles(archivePath, files);
  30. }
  31.  
  32. /*
  33.  * Creates files array based on the given filesPath and the array of fileNames
  34.  * @input filesPath : String
  35.  * @input fileNames : Array
  36.  *
  37.  * @output files : Array[File]
  38.  */
  39. function createFiles (filesPath, fileNames) {
  40.     var files = [];
  41.     var filePath = File.IMPEX + '/' + path + '/';
  42.  
  43.     for (var i = 0, filesNamesLength = filesNames.length ; i < filesNamesLength ; i++) {
  44.         var file = new File(filePath + fileNames[i]);
  45.         files.push(file);
  46.     }
  47.     return files;
  48. }
  49.  
  50. /*
  51.  * Moves files to archive based on the given archivePath and array of files
  52.  * @input archivePath : String
  53.  * @input files : Array
  54.  *
  55.  * @output void
  56.  */
  57. function archiveFiles (archivePath, files) {
  58.     var archiveDir = new File(File.IMPEX + '/' + archivePath);
  59.     var timestamp = StringUtils.formatCalendar(new Calendar(), "yyyMMddHHmmss");
  60.  
  61.     archiveDir.mkdirs();
  62.  
  63.     files.forEach(function(file){
  64.         archiveFileName = timestamp + file.getName();
  65.  
  66.         var archiveFile = new File(archiveDir, archiveFileName);
  67.         file.renameTo(archiveFile);
  68.     });
  69. }
  70.  
  71. exports.MailAndArchive = MailAndArchive;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top