aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/transactions.coffee
blob: 704479017c4484e01bf976d4be50186e1d559a4d (plain) (blame)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Promise = require('bluebird')
db = require('../redis')
config = require("../config")
moment = require('moment') 

txid = (transaction, user) ->
  return transaction if transaction.match(/[a-z]/i)
  return user + ":transactions:" + transaction

module.exports =
  index: (req, res) ->
    res.render(
      'transactions/index'  
      user: req.params.user
      currency: req.session.user.currency
      layout: 'layout'
      navigation: true
      js: (-> global.js)
      css: (-> global.css)
    )

  json: (req, res) ->
    result = 'transactions': []
    promises = []
    pattern = req.params.user
    if req.session.user.username is 'admin'
      x = '*'

    db.keysAsync("#{pattern}:transactions").then((keys) ->
      Promise.all(keys.map((key) -> 
        user = key.substr(0, key.indexOf(':'))
        db.lrangeAsync(key, 0, -1).then((transactions) ->
          Promise.all(transactions.map((transaction) ->
            db.hgetallAsync(txid(transaction, user)).then((transaction) ->
              if transaction
                transaction.user = user
                result.transactions.push(transaction)
            )
          ))
        )
      ))
    ).then(->
      res.write(JSON.stringify(result))
      res.end()
    )


  create: (req, res) ->
    finish = ->
      db.hgetall("user:"+req.params.user.toLowerCase(), (err, user) ->
        res.render('transactions/notification', 
          layout: 'mail'
          amount: (req.body.received * req.body.exchange).toFixed(2).toString() + ' ' + user.currency
          address: req.body.address
          txid: req.body.txid
          js: (-> global.js)
          css: (-> global.css)
          (err, html) ->
            helper = require('sendgrid').mail
            from_email = new helper.Email('[email protected]')
            to_email = new helper.Email(user.email)
            subject = 'Transaction Sent'
            content = new helper.Content('text/html', html)
            mail = new helper.Mail(from_email, subject, to_email, content)

            sg = require('sendgrid')(config.sendgrid_token)
            request = sg.emptyRequest(
              method: 'POST'
              path: '/v3/mail/send'
              body: mail.toJSON()
            )

            sg.API(request, (error, response) ->
              console.log(response.statusCode)
              console.log(response.body)
              console.log(response.headers)
            )
        )
      )
      res.write(JSON.stringify(req.body))
      res.end()

    db.watch(req.body.txid)
    db.exists(req.body.txid, (err, result) ->
      return finish() if result

      multi = db.multi()
      multi.hmset(req.body.txid, req.body)
      multi.rpush("#{req.params.user}:transactions", req.body.txid)
      multi.exec((err, replies) ->
        finish()
      )
    )

  update: (req, res) ->
    db.hset(req.params.txid, 'notes', req.body.notes, ->
      res.end()
    )

  delete: (req, res) ->
    db.del(req.params.txid, ->
      db.lrem(req.params.user + ":transactions", 0, req.params.txid, ->
        res.end()
      )
    )