Guest User

Untitled

a guest
Feb 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. export default function validateSendForm(res, name, email, text, number, host, emailNotification, smsNotification, callNotification) {
  2. let emailValidated = validator.isEmail(email);
  3. let numberValidated;
  4. let formattedNumber;
  5.  
  6. function showInvalidNumber(res) {
  7. res.statusMessage = "invalid number";
  8. res.status(400).end();
  9. }
  10. function showInvalidEmail(res) {
  11. res.statusMessage = "invalid email";
  12. res.status(400).end();
  13. }
  14.  
  15. function sendResponse() {
  16. if(emailValidated && numberValidated) {
  17. sendNotification(name, email, text, number, host, emailNotification, smsNotification, callNotification);
  18. res.end("Sent")
  19. } else {
  20. if(!emailValidated && !numberValidated) {
  21. res.statusMessage = "invalid email and number";
  22. res.status(400).end();
  23. } else if (!emailValidated) {
  24. showInvalidEmail(res)
  25. } else if (!numberValidated) {
  26. showInvalidNumber(res);
  27. }
  28. }
  29. }
  30.  
  31. function validateSend() {
  32. if (emailNotification && (smsNotification || callNotification)) {
  33. sendResponse()
  34. } else if(emailNotification) {
  35. if(emailValidated) {
  36. sendNotification(name, email, text, number, host, emailNotification, smsNotification, callNotification);
  37. res.end("Sent")
  38. } else {
  39. showInvalidEmail(res)
  40. }
  41. } else if(smsNotification || callNotification ) {
  42. if(numberValidated) {
  43. sendNotification(name, email, text, number, host, emailNotification, smsNotification, callNotification);
  44. res.end("Sent")
  45. } else {
  46. showInvalidNumber(res)
  47. }
  48. }
  49. }
  50.  
  51. client.lookups.v1
  52. .phoneNumbers(number)
  53. .fetch({ countryCode: 'US' })
  54. .then((numberData) => {
  55. formattedNumber = numberData.phoneNumber
  56. numberValidated = true;
  57. }, err => numberValidated = false).then(() => {
  58. validateSend();
  59. });
  60. }
Add Comment
Please, Sign In to add comment