-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
102 lines (88 loc) · 2.69 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
const dotenv = require('dotenv')
dotenv.config({path: './config/config.env'})
const path = require('path')
const PORT = process.env.PORT || 3000
const cors = require('cors')
const mongoose = require('mongoose')
const express = require('express')
const {google} = require('googleapis')
const {GoogleAuth} = require('google-auth-library')
const {cleanData, bundleData} = require('./utils')
const connectDB = require('./config/db')
const COA = require('./models/coa')
connectDB()
const ssid = process.env.SSID
const SCOPES = ["https://www.googleapis.com/auth/spreadsheets"]
const app = express()
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use(express.urlencoded({extended: true}))
app.use(express.json())
app.use(cors())
app.use(express.static(path.join(__dirname, 'public')))
app.get('/', (req, res) => {
res.send("All good in the hood. But you shouldn't be using this route.")
})
app.get('/refresh', async (req, res) => {
const auth = new GoogleAuth({
keyFile: "./credentials.json",
scopes: SCOPES
})
const client = await auth.getClient()
const googleSheets = google.sheets({
version: "v4",
auth: client
})
const getRows = await googleSheets.spreadsheets.values.get({
auth,
spreadsheetId: ssid,
range: "Final"
})
const getThirdPartyRows = await googleSheets.spreadsheets.values.get({
auth,
spreadsheetId: ssid,
range: "Third Party"
})
const refactorData = cleanData(getRows.data.values.slice(5))
const refactorThirdData = cleanData(getThirdPartyRows.data.values.slice(5))
const refactorObjArr = bundleData(refactorData, refactorThirdData)
// Build the DB
refactorObjArr.forEach(async lotData => {
try {
await COA.create(lotData)
} catch (err) {
console.error(err)
console.log(lotData)
}
})
res.send(`${refactorObjArr.length} entries added to MongoDB.`)
})
app.get('/api/:targetLotNum', async (req, res) => {
try {
console.log(req.params)
let lotData = await COA.findOne({
lotNum: req.params.targetLotNum
})
.lean()
console.log(lotData)
res.json(lotData)
} catch (err) {
console.error(err)
}
})
app.get('/coa/:targetLotNum', async (req, res) => {
try {
console.log(req.params)
let lotData = await COA.findOne({
lotNum: req.params.targetLotNum
})
.lean()
console.log(lotData)
res.render("coa", {
lotData: lotData
})
} catch (err) {
console.error(err)
}
})
app.listen(PORT, _ => console.log(`Listening on port ${PORT}!`))