1
votes

I have a generic SendMail route which I want to create multiple remote methods to handle multiple request templates. Any ideas on how to return a Email_Type from the remote method back to the base route. I know I could add a default with a code in it, but would like a more elegant solution.

Mail.genericSendMail = function genericEmail(response, callback) {
  console.log(response);
  let templateId=0;

  //PROBLEM: HOW TO KNOW WHICH REMOTE WAS USED

   switch (response.emailType) {
     case "Template-1":
         templateId= 1234;
         break;
      case "Template-2":
         tempalteId = 456;
        break;
      default:
         templateId = 789l
      } //switch

      console.log(templateId);
};



//Want multiple routes like this to support various templates
    Mail.remoteMethod("genericEmail", {
      http: {
        path: "/emailTemplate1",  
        verb: "POST"
      },
       accepts [
         {arg: "request", type:"object",http: {source:"body"},
         default: {firstName:"", lastName:"",emailAddress:""}
      }],
      returns: RESTResponseStatic.loopbackAdapterCommonRestResponseDefinition() 

    });


//Want multiple routes like this to support various templates
Mail.remoteMethod("genericEmail", {
  http: {
    path: "/emailTemplate2",  
    verb: "POST"
  },
   accepts [
     {arg: "request", type:"object",http: {source:"body"},
     default: {emailAddress:"", promoCode:""}
  }],
  returns: RESTResponseStatic.loopbackAdapterCommonRestResponseDefinition() 

});
1

1 Answers

0
votes

There are a couple of different ways to do this. Since it happens to be a POST request, I usually go with attaching data to the body using a before remote hook.

Let's say you have a model method for logging in users. Say we have a multi realm platform, so we need to know what platform we are logging in. If you don't use realms or don't know what they are, don't worry. This just shows you how to populate the data to the model method.

User.login = function(data, cb) {
  if (data.realm == 'platform1) {
    return logUserIntoPlatform1(data, cb);  
  }

  return logUserIntoDefaultPlatform(data, cb);
}

Now let's say you don't want the client/frontend to send the realm and you don't want to do the lookup for realm in the model. We can add a beforeRemote hook like so:

User.beforeRemote('login', function (context, user, next) {
  context.args.data.realm = lookUpRealmSync(context); // 1
  next();
});

This will be called before the login method. Note the next() call: this is how you could do error detection before actually hitting the model method. Something like next({ status: 422, message: 'Parameter missing: password }); would return an error and not execute the User.login method. You may have to look carefully at your context object (i.e. the line marked with 1 may not work exactly as I've shown for you).

If you want to read more about this stuff, I LoopBack's docs are pretty good. It seems they've been updated since I've last used them so I can't link you to the more useful pages. I found the remote method documentation here though.

Edit: I took a closer look at your question. You should be able to retrieve the path from the context object and pass data accordingly. I'm not going to try to code that since I don't know where it would actually be within the object.