-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mutation.js
68 lines (54 loc) · 1.79 KB
/
Mutation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require('es6-promise').polyfill()
require('isomorphic-fetch')
const { validateEmail } = require('../utils')
async function sendMessage(parent, args, context){
const data = { ...args }
delete data.humanKey
// Check fullname
if(args.fullname.length < 4) {
throw new Error(`Fullname so small? 🤔`)
}
// Validate email
if(!validateEmail(args.email)) {
throw new Error(`Please enter your CORRECT EMAIL ADDRESS! 😡`)
}
// Validate subject
if(args.subject.length < 6) {
throw new Error(`I hate lazy ppl writing less in subject field!`)
}
// Validate message
if(args.message.length < 9) {
throw new Error(`Can you not express yourself longer?`)
}
// Validate Phone number
if(args.phone != null) {
const isPhoneValid = await fetch(`http://apilayer.net/api/validate?access_key=${process.env.APILAYER_KEY}&number=${args.phone}`)
.then(res => (res.json()))
.then(json => (json.valid))
.catch(err => { throw new Error(`Error in apilayer API. ${err.message}`) })
if(!isPhoneValid){
throw new Error(`Please write a valid phone number.`)
}
}
// Validate Human
const isHuman = await fetch(`https://www.google.com/recaptcha/api/siteverify`, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
},
body: `secret=${process.env.RECAPTCHA_SERVER_KEY}&response=${args.humanKey}`
})
.then(res => (res.json()))
.then(json => (json.success))
.catch(err => { throw new Error(`Error in Google Siteverify API. ${err.message}`) })
if(args.humanKey == null || !isHuman){
throw new Error(`YOU ARE NOT A HUMAN.`)
}
// JUST DO IT!!!
const contact = await context.prisma.createContact({...data})
return contact
}
module.exports = {
sendMessage
}