Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- var StringUtils = require('dw/util/StringUtils');
- var Calendar = require('dw/util/Calendar');
- var EmailModel = app.getModel('Email');
- /*
- * Searches in the impex for the given files and sends email with the files attached,
- * after that archives the file
- */
- function MailAndArchive (args) {
- var recieverEmail = args.Email;
- var path = args.Path;
- var filesNames = args.FileNames;
- var archivePath = args.ArchivePath;
- fileNames = fileNames.split('|');
- var files = createFiles(path, fileNames);
- EmailModel.sendMailWithAttachments(files, {
- template: 'mail/mail_with_attachment',
- recipient : recieverEmail,
- subject : 'Back in stock csv',
- mimeType : 'multipart/mixed; boundary=------------000001030701020908040900',
- encoding : 'iso-8859-1'
- });
- archiveFiles(archivePath, files);
- }
- /*
- * Creates files array based on the given filesPath and the array of fileNames
- * @input filesPath : String
- * @input fileNames : Array
- *
- * @output files : Array[File]
- */
- function createFiles (filesPath, fileNames) {
- var files = [];
- var filePath = File.IMPEX + '/' + path + '/';
- for (var i = 0, filesNamesLength = filesNames.length ; i < filesNamesLength ; i++) {
- var file = new File(filePath + fileNames[i]);
- files.push(file);
- }
- return files;
- }
- /*
- * Moves files to archive based on the given archivePath and array of files
- * @input archivePath : String
- * @input files : Array
- *
- * @output void
- */
- function archiveFiles (archivePath, files) {
- var archiveDir = new File(File.IMPEX + '/' + archivePath);
- var timestamp = StringUtils.formatCalendar(new Calendar(), "yyyMMddHHmmss");
- archiveDir.mkdirs();
- files.forEach(function(file){
- archiveFileName = timestamp + file.getName();
- var archiveFile = new File(archiveDir, archiveFileName);
- file.renameTo(archiveFile);
- });
- }
- exports.MailAndArchive = MailAndArchive;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement