Browse Source

Payment chain now works as intended

Fixing-CORS-crash
BinHong Lee 7 years ago
parent
commit
cb5b517a5a
1 changed files with 80 additions and 5 deletions
  1. +80
    -5
      index.js

+ 80
- 5
index.js View File

@@ -8,29 +8,42 @@ app.use(bodyParser.json({
limit: '100mb', limit: '100mb',
type: 'application/json' type: 'application/json'
})) }))

app.get('/help', function (req, res) { app.get('/help', function (req, res) {
res.send('https://github.com/binhonglee/Breakups') res.send('https://github.com/binhonglee/Breakups')
}) })

app.post('/total', function (req, res) { app.post('/total', function (req, res) {
input = req.body input = req.body
var toReturn = { 'total': 0 } var toReturn = { 'total': 0 }
toReturn.total = total(input.users) toReturn.total = total(input.users)
res.json(toReturn) res.json(toReturn)
}) })

app.post('/perPerson', function (req, res) { app.post('/perPerson', function (req, res) {
input = req.body input = req.body
var toReturn = { 'perPerson': 0 } var toReturn = { 'perPerson': 0 }
toReturn.perPerson = (total(input.users)) / input.users.length toReturn.perPerson = (total(input.users)) / input.users.length
res.json(toReturn) res.json(toReturn)
}) })

app.post('/oweChart', function (req, res) { app.post('/oweChart', function (req, res) {
input = req.body input = req.body
var perPerson = total(input.users) / input.users.length
for (var i = 0; i < input.users.length; i++) {
input.users[i].amount -= perPerson
}
res.json(input)
input.users = oweChart(input.users)
res.send(input)
})

app.post('/sortedOweChart', function (req, res) {
input = req.body
input.users = mergeSort(oweChart(input.users))
res.send(input)
}) })

app.post('/paymentChain', function (req, res) {
input = req.body
res.json(paymentChain(mergeSort(oweChart(input.users))))
})

// app.listen(3000) // app.listen(3000)
app.listen(process.env.PORT) app.listen(process.env.PORT)


@@ -42,3 +55,65 @@ function total (input) {


return total return total
} }

function oweChart (users) {
var perPerson = total(users) / users.length
for (var i = 0; i < users.length; i++) {
users[i].amount -= perPerson
}

return users
}

function mergeSort (users) {
if (users.length < 2) {
return users
}

var middle = Math.floor(users.length / 2)
var left = users.slice(0, middle)
var right = users.slice(middle)

return merge(mergeSort(left), mergeSort(right))
}

function merge (left, right) {
var result = []
var il = 0
var ir = 0

while (il < left.length && ir < right.length) {
if (left[il].amount < 0 && right[ir].amount < 0) {
if (left[il].amount > right[ir].amount) {
result.push(left[il++])
} else {
result.push(right[ir++])
}
} else {
if (left[il].amount < right[ir].amount) {
result.push(left[il++])
} else {
result.push(right[ir++])
}
}
}

return result.concat(left.slice(il)).concat(right.slice(ir))
}

function paymentChain (users) {
var chain = []
var stack = 0

for (var i = 0; i < users.length - 1; i++) {
var item = {'from': '', 'to': '', 'amount': 0}
item['from'] = users[i].name
item['to'] = users[i + 1].name
var amount = users[i].amount * -1
stack += amount
item['amount'] = stack
chain.push(item)
}

return chain
}

Loading…
Cancel
Save