-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
142 lines (110 loc) · 4.16 KB
/
server.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const express = require('express')
const app = express()
const port = 3012
const bodyParser = require('body-parser');
const { createClient } = require('@supabase/supabase-js')
require('dotenv').config({path: './.env'});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.post('/', async function requestHandler(req, res) {
try{
const {formattedAmount,paymentPointer, title, url, post_id, monetization} = req.body
if(!paymentPointer){
res.status(405).send({ message: 'Payment pointer is required!' })
return
}
if(!url){
res.status(405).send({ message: 'Url is required!' })
return
}
if(!post_id){
res.status(405).send({ message: 'Post id is required!' })
return
}
if(!monetization){
res.status(405).send({ message: 'Monetization object is required!' })
return
}
if(!formattedAmount){
res.status(200).send({ message: 'amount is 0' })
return
}
const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SECRET)
try{
if(monetization.requestId){
const { data:record, err } = await supabase
.from("wm_posts")
.select("*")
.eq("post_id", post_id)
.eq("wm_session", monetization.requestId.toString())
;
console.log(formattedAmount)
if(!record || !record.length){
const { data, error } = await supabase
.from('wm_posts')
.insert([
{ slug: url,
session_post:post_id+monetization.requestId,
post_id:post_id,
formatted_amount:formattedAmount,
wm_session:monetization.requestId,
wm_data:monetization,
payment_pointer:monetization.paymentPointer,
currency:monetization.assetCode,
title:title
},
])
if(data){
res.status(200).send({ message: 'done' })
return
}else if(error){
console.log(error)
return res.status(500).end('Some error recording the transaction')
}
}else{
const total = ((Number(formattedAmount))+ (Number(record[0].formatted_amount)))
const newFormattedAmount = (total).toFixed(monetization.assetScale)
const { data, error } = await supabase
.from('wm_posts')
.update({
formatted_amount:newFormattedAmount.toString(),
wm_session:monetization.requestId,
wm_data:monetization,
updated_at:((new Date()).toISOString()).toLocaleString(),
title:title
})
.eq('id', record[0].id)
if(data){
return res.status(200).send({ message: 'done' })
}else if(error){
console.log(error)
return res.status(500).end('Some error recording the transaction')
}
res.status(200).send({ message: 'done' })
return
}
}else{
res.status(200).send({ message: 'done' })
return
}
}
catch(e){
}
}catch(e){
console.log(e)
}
});
app.post('/getTotals', async function requestHandler(req, res) {
const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SECRET)
const { data, error } = await supabase.rpc('update_totals_and_clear');
console.log(data)
console.log(error)
res.status(200).send({ message: 'done' })
return
});
app.listen(port, () => {
console.log(`Web Monetization listener on port ${port}`)
})