-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.js
More file actions
75 lines (65 loc) · 1.81 KB
/
create.js
File metadata and controls
75 lines (65 loc) · 1.81 KB
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
69
70
71
72
73
74
75
const { readFileSync } = require('fs')
const jwt = require('jsonwebtoken')
const { SES } = require('aws-sdk')
require('aws-sdk').config.update({ region: 'us-east-1' })
const fetch = require('node-fetch')
const { argv } = require('yargs').demandOption(['id'])
const config = {
secret: process.env.JWT_SECRET,
url: 'https://platezero.com/api/user/recipe',
}
const login = (userId) => {
return new Promise((resolve, reject) => {
jwt.sign({ userId }, config.secret, { expiresIn: '1h' }, (err, token) => {
if (err) {
return reject(err)
}
return resolve(token)
})
})
}
const post = (recipe, { token }) => {
return fetch(config.url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(recipe),
})
}
const email = ({ to, name, title, url }) => {
const params = {
Destination: {
ToAddresses: [to],
},
Message: {
Body: {
Text: {
Charset: 'UTF-8',
Data: `Hi ${name},
Thanks for using the PlateZero importer! We've finished importing the recipe ${title} for you. You can find it here: ${url}
If you have any questions or thoughts, please let us know by replying to this email!
Thank you`,
},
},
Subject: {
Charset: 'UTF-8',
Data: 'PlateZero Importer Success',
},
},
Source: 'importer@platezero.com',
ReplyToAddresses: ['importer@platezero.com'],
}
return new SES({ apiVersion: '2010-12-01' }).sendEmail(params).promise()
}
const main = async () => {
const { id } = argv
const token = await login(id)
const recipe = JSON.parse(readFileSync('recipe.json'))
const res = await post(recipe, { token })
const json = await res.json()
console.log(json)
}
main()