@@ -9,15 +9,16 @@ api domain: `https:\\breakups.herokuapp.com\` | |||
| Type | Call | Request | Response | | |||
|:-----|:-----|:--------|:---------| | |||
| `GET` | `help` | - | URL to github repository | | |||
| `POST` | `total` | [standard format](#standard-format) | `{ "total": 200 }` | | |||
| `POST` | `perPerson` | [standard format](#standard-format) | `{ "perPerson": 30 }` | | |||
| `POST` | `oweChart` | [standard format](#standard-format) | [standard format](#standard-format) | | |||
| `POST` | `sortedOweChart` | [standard format](#standard-format) | [standard format](#standard-format) | | |||
| `POST` | `paymentChain` | [standard format](#standard-format) | [chain format](#chain-format) | | |||
| `POST` | `total` | [standard](#standard) / [email](#email) | `{ "total": 200 }` | | |||
| `POST` | `perPerson` | [standard](#standard) / [email](#email) | `{ "perPerson": 30 }` | | |||
| `POST` | `oweChart` | [standard](#standard) / [email](#email) | [standard](#standard) / [email](#email) | | |||
| `POST` | `sortedOweChart` | [standard](#standard) / [email](#email) | [standard](#standard) / [email](#email) | | |||
| `POST` | `paymentChain` | [standard](#standard) / [email](#email) | [chain](#chain) | | |||
| `POST` | `emailPaymentChain` | [email](#email) | `["Email sent to user1@domain.com", "Email sent to user2@domain.com"]` | | |||
## Expected Request / Response | |||
### standard format | |||
### standard | |||
```JSON | |||
{ | |||
@@ -38,7 +39,7 @@ api domain: `https:\\breakups.herokuapp.com\` | |||
} | |||
``` | |||
### chain format | |||
### chain | |||
```JSON | |||
[ | |||
@@ -54,3 +55,28 @@ api domain: `https:\\breakups.herokuapp.com\` | |||
} | |||
] | |||
``` | |||
```JSON | |||
{ | |||
"users": [ | |||
{ | |||
"name": "Person1", | |||
"email": "person1@domain.com", | |||
"amount": 100 | |||
}, | |||
{ | |||
"name": "Person2", | |||
"email": "person2@domain.com", | |||
"amount": 50 | |||
}, | |||
{ | |||
"name": "Person3", | |||
"email": "person3@domain.com", | |||
"amount": 30 | |||
} | |||
], | |||
"mixmax-api": "your-mixmax-api-key" | |||
} | |||
``` |
@@ -1,6 +1,9 @@ | |||
var bodyParser = require('body-parser') | |||
var express = require('express') | |||
const got = require('got') | |||
var MIXMAX_API_KEY | |||
var app = express() | |||
var input | |||
@@ -44,6 +47,20 @@ app.post('/paymentChain', function (req, res) { | |||
res.json(paymentChain(mergeSort(oweChart(input.users)))) | |||
}) | |||
app.post('/emailPaymentChain', function (req, res) { | |||
input = req.body | |||
MIXMAX_API_KEY = input['mixmax-api'] | |||
var sorted = mergeSort(oweChart(input.users)) | |||
var chain = paymentChain(sorted) | |||
var emails = [] | |||
for (var i = 0; i < chain.length; i++) { | |||
var message = messageCreation(sorted[i], sorted[i + 1], chain[i].amount) | |||
send(message) | |||
emails.push('Email sent to ' + sorted[i].email) | |||
} | |||
res.json(emails) | |||
}) | |||
// app.listen(3000) | |||
app.listen(process.env.PORT) | |||
@@ -117,3 +134,24 @@ function paymentChain (users) { | |||
return chain | |||
} | |||
function messageCreation (user1, user2, amount) { | |||
var signature = '<br><br><p>Sincerely,<br><a href="https://github.com/binhonglee/Breakups">BreakupBills</a></p>' | |||
var message = {} | |||
message.to = user1.email | |||
message.subject = 'Pay ' + user2.name + ' $' + amount | |||
message.html = '<p>Please pay ' + user2.name + ' $' + amount + '</p>' + signature | |||
return message | |||
} | |||
function send (message) { | |||
got.post('https://api.mixmax.com/v1/send', { | |||
body: JSON.stringify({ | |||
message | |||
}), | |||
headers: { | |||
'Content-Type': 'application/json', | |||
'X-API-Token': MIXMAX_API_KEY | |||
} | |||
}) | |||
} |
@@ -11,6 +11,7 @@ | |||
"license": "MIT", | |||
"dependencies": { | |||
"body-parser": "^1.18.2", | |||
"express": "^4.16.2" | |||
"express": "^4.16.2", | |||
"got": "^7.1.0" | |||
} | |||
} |