Friday 21 April 2017

Passing the params to the services to send an email

I need to collect the (name + description passed from the input of the frontEnd(angularJS) and Send them in Email when Click on Confirm button enter image description here

I don't know how to pass the parameter.

This is my code of the backEnd (node.js) to send 2 Emails to supplier and to Admin

`this.saveNewOfferMessage = function (supplierId, request)
  {
    // send the email and return true or an error
    var namem = __offerService(request.body.name);
    var textm = __offerService(request.body.text);

    var query = {
        attributes: ["id", "supplierId"],
        include: [{
            model: __database.dao("Supplier"),
            as: "supplier",
            attributes: ["companyId"],
            include: {
                model: __database.dao("Company"),
                as: "company",
                attributes: ["id", "name", "email"]
            }
        }],
        where: {
            supplierId: supplierId
        }
      };
      // Send new offer email
      var emailParams = [
        {
            name: "SupplierID",
            content: supplierId
        },
        {
            name: "NameMessage",
            content: namem
        },
        {
            name: "Message",
            content: textm
        }
       ];

       let offers = __database.dao("Offer").findAll(query);

        for (let offer of offers) {
        let companyUsers = 
  __userService.findUserByCompany(offer.supplierId);

        var templateName = 
   yemp.server.utilities.mailUtil.getQualifiedTemplateName(
            "production-quotation-submitted-supplier");
        var recipientSupplier = [];
        if (Array.isArray(companyUsers)) {
            for (let userCompany of companyUsers) {
                if ("SUPPLIER" === userCompany.role) {
                    recipientSupplier.push({
                        email: userCompany.email,
                        role: "SUPPLIER"
                    });
                }
            }
        }
        return __database.dao("Supplier").findAll({where: query});

        let info = __notificationService.notifyByEmail(templateName, 
     null, emailParams, recipientSupplier);

        for (let key of Object.keys(info)) {
            var status = info[key];
            if (status.length) {
                for (let message of status) {
                    if ('success' === key) {
                        yemp.server.log.debug(message)
                    }
                    else {
                        yemp.server.log.error(message);
                    }
                }
              }
            }
          }
    __sendNewserviceSuggestionToAdmins(offers[0] , request);
   };
   var __sendNewserviceSuggestionToAdmins = async function (offer , 
   request)
  {
    let adminUsers = await __userService.find({role: 
   "ADMINISTRATOR"});
    var recipients = [];
    var templateName, emailParams;
    templateName = 
     yemp.server.utilities.mailUtil.getQualifiedTemplateName("new-
      offer-suggestion");

       for (let adminUser of adminUsers)
        {
        emailParams = [
        {
            name: "SupplierName",
            content: offer.supplier.supplierName
        },
        {
            name: "OfferName",
            content: request.body.name
            },
            {
            name: "OfferDescription",
            //content: textm
            content: request.body.text
           }
           ];

        recipients.push({
            email: adminUser.email,
            role: "ADMINISTRATOR"
          });
        }
    await __notificationService.notifyByEmail(templateName, null, 
    emailParams, recipients);
    yemp.server.log.debug(
    "Supplier new service suggestion e-mail has been send to admins 
     successfully");
     };`

We used mandrill as a service to send email, but my code missed how to get the name + description and passing them to the service in back?



via Afef Mokhtari

No comments:

Post a Comment