Skip to content
Snippets Groups Projects
onActionProcess.js 5.53 KiB
import("system.vars");
import("system.neon");
import("system.util");
import("system.mail");
import("system.plugin");
import("system.process");
import("system.project");
import("system.logging");
import("system.question");
import("system.translate");
import("IncomingEmailExecutor_lib");

var isError = false;
var activityIDs = [];
var incomingMailExec, mailImportResult, mailObj;
var dbAlias = "Data_alias"; //hardcoded because of a lack of a recordcontainer
var files = vars.get("$local.value");
var status = {
    activitiesCreated: 0,
    otherImportedFiles: 0
};

for (var i = 0, l = files.length; i < l; i++) 
{
    vars.set("$field.INFO", translate.withArguments("processing %0/%1", [i.toString(), l.toString()]) + " ...");
    
    switch (files[i].mimeType)
    {
        case "application/vnd.ms-outlook":
        case "application/octet-stream"://in some cases .msg-files will not get any mimetype from the browser so let's use the default "anything"
        case "message/rfc822":
        case "text/html": //in some cases .eml-files are recognized as text/html, till this is fixed accept the text/html contents too
                          //todo: remove text/html workaround when #1056482 is fixed
 
            mailObj = _getMailObj(files[i]);
            
            if (mailObj)
            {
                mailObj.filename = files[i].filename;
                mailObj.filename = mailObj.filename.replace(/\.msg$/, ".eml");
                
                incomingMailExec = new IncomingEmailExecutor(mailObj);
                incomingMailExec.setAlias(dbAlias);
                
                mailImportResult = incomingMailExec.autoProcess()
                
                if(!mailImportResult.isError)
                {    
                    status.activitiesCreated++;
                }
                else 
                {    
                    isError = true;
                }
                
                if (mailImportResult.activityId)
                {
                    activityIDs.push(mailImportResult.activityId);
                }
            }
            else
            {    
                isError = true;
            }
            break;
        default:
            isError = true;
            break;
    }
}

vars.set("$global.ACTIVITY_IDS", JSON.stringify(activityIDs)); //TODO: Änderung auf andere Variable #1057014

if (status.activitiesCreated > 0 || status.unlinkedMailsCreated > 0 || status.otherImportedFiles > 0)
{    
    neon.refreshAll(); //this is needed for the dashboard: other elements are refreshed and display for example unlinkedMails
}

var messages = _getMessages(status);

if (messages.length)
{    
    vars.set("$field.INFO", translate.withArguments("processed %0/%1:", [i.toString(), l.toString()]) + "\n" + messages.join(", "));
}

if (isError)
{
    question.showMessage(translate.text("During processing the e-mail an error has occurred.\nPlease contact an administrator."), question.ERROR, 
                                        translate.text("E-mail processing error"));
}

neon.refreshAll();

function _getMailObj(pDroppedFile)
{
    var mailStr;
    
    if (pDroppedFile.mimeType == "message/rfc822" || pDroppedFile.mimeType == "text/html") //todo: remove text/html workaround when #1056482 is fixed
    {
       try 
        {
            mailStr = util.decodeBase64String(pDroppedFile.data);
        }
        catch(pException)
        {
            logging.log(translate.text("An error occured during decoding the mails content."), logging.ERROR);
            logging.log(pException, logging.ERROR);
        }        
    }
    else
    {
        try 
        {
            mailStr = plugin.run(null, "de.adito.aditoweb.plugin.msg2rfc.Msg2RfcPlugin", [pDroppedFile.data])[0];
        }
        catch(pException)
        {
            logging.log(translate.text("An error occured during converting the msg file into an standardized mail."), logging.ERROR);
            logging.log(pException, logging.ERROR);
        }
    }
            
    if (mailStr)
    {    
        let parsedMail;
        
        try
        {
            parsedMail = mail.parseRFC(mailStr);
        }
        catch(pException)
        {
            logging.log(translate.text("An error occured during parsing imported mail."), logging.ERROR);
            logging.log(pException, logging.ERROR);
        }
        
        return parsedMail;
    }

    return null;
}

function _getMessages(pStatus)
{
    var res = [];
    
    if (pStatus.skippedFiles == 1)
    {    
        res.push(translate.text("one file ignored"));
    }
    else if (pStatus.skippedFiles > 1)
    {    
        res.push(translate.withArguments("%0 files ignored", [pStatus.skippedFiles]));
    }

    if (pStatus.otherImportedFiles == 1)
    {    
        res.push(translate.text("one file imported"));
    }
    else if (pStatus.otherImportedFiles > 1)
    {    
        res.push(translate.withArguments("%0 files imported", [pStatus.otherImportedFiles]));
    }

    if (pStatus.activitiesCreated == 1)
    {    
        res.push(translate.text("one actitiy from mail created"));
    }
    else if (pStatus.activitiesCreated > 1)
    {        
        res.push(translate.withArguments("%0 activities from mails created", [pStatus.activitiesCreated]));
    }

    if (pStatus.unlinkedMailsCreated == 1)
    {    
        res.push(translate.text("one mail could not be linked automatically"));
    }
    else if (pStatus.unlinkedMailsCreated > 1)
    {    
        res.push(translate.withArguments("%0 mails could not be linked automatically", [pStatus.unlinkedMailsCreated]));
    }
    
    return res;
}