const ListSubscriber = require('../../models').listsubscriber;
const List = require('../../models').list;

module.exports = function(req, res) {
  const email = req.query.email;
  const subscribeKey = req.query.subscribeKey;

  List.findOne({
    where: { subscribeKey }
  }).then(list => {
    if (list) {
      const listId = list.dataValues.id;

      ListSubscriber.findOne({
        where: {
          listId,
          email
        }
      }).then(listSubscriber => {
        if (listSubscriber) {
          res.status(400)
            .send({
              status: 'error', // Redundant
              message:'This email already exists'
            });
        } else {
          ListSubscriber.create({
            email, listId
          }).then(() => {
            res.status(201)
              .send({
                status: 'success', // Redundant
                message: `added succesfully`
              });
          });
        }
      });
    } else {
      res.status(400).send('invalid list subscriber key');
    }
  });
};

@@ -425,33 +425,54 @@
  email%0A        %7D
+,%0A        plain: true
 %0A
-
       %7D).then(li
@@ -515,24 +515,167 @@
 bscriber) %7B%0A
+          ListSubscriber.update(%0A            %7B subscribed: true %7D,%0A            %7B where: %7B id: listSubscriber.id %7D %7D%0A          ).then(() =%3E %7B%0A  
           re
@@ -687,13 +687,15 @@
 tus(
-400)%0A
+201)%0A  
     
@@ -716,32 +716,34 @@
 %7B%0A              
+  
 status: 'error',
@@ -739,13 +739,15 @@
 s: '
-error
+success
 ', /
@@ -772,24 +772,26 @@
         
+  
 message:
 'This em
@@ -786,38 +786,48 @@
 age:
-'This email already exists'%0A  
+ %60added successfully%60%0A              %7D);%0A
     

Allow a user to resubscribe to a mailing list