-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsuperhive_hook.js
More file actions
67 lines (58 loc) · 1.8 KB
/
superhive_hook.js
File metadata and controls
67 lines (58 loc) · 1.8 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
const express = require('express')
const router = express.Router()
require('dotenv').config()
router.use(express.json())
router.post('/', async (req, res) => {
/*
Expected data:
{
"status": "complete",
"event_type": "after_sale",
"order_item_id": 12345,
"order_id": 12345,
"order_created_at": "2025-10-21 07:50:20 -0500",
"user_id": 12345,
"product_id": 12345,
"product_name": "PRODUCT NAME",
"product_variant_id": 12345,
"product_variant_name": "PRODUCT VARIANT NAME"
}
*/
// Validate body data
if (!req.body || req.body.event_type !== 'after_sale' || req.body.user_id === undefined) {
return res.status(400).json({
error: '400 Bad Request',
message: 'Invalid request body',
})
}
// Dump the data into Firestore
const admin = require('firebase-admin')
if (!admin.apps.length) {
console.log('Firebase init')
admin.initializeApp({
credential: admin.credential.cert({
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
clientEmail: process.env.FIREBASE_CLIENT_EMAIL,
privateKey: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
}),
})
}
/*
TODO:
Don't just dump the data, we need to responde with the text file that includes the extension URL and access token, and obviously also create the access token.
*/
const db = admin.firestore()
const document = {}
document.hook = req.body
document.access_token_created = false
document.req_headers = req.headers
document.method = req.method
document.received_at = new Date().toISOString()
document.ip = req.headers['x-forwarded-for'] || req.ip || req.connection.remoteAddress
await db.collection('superhive_hooks').add(document)
res.status(200).json({
document,
message: 'OK',
})
})
module.exports = router