-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory.js
97 lines (90 loc) · 3.35 KB
/
inventory.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
const path = require('path');
const express = require('express');
const rootDir = require('../util/path');
const router = express.Router();
const fs = require('fs');
const itemSchema = require('../schemas/item-schema');
const DiscordOauth2 = require("discord-oauth2");
require("dotenv").config();
const jwt = require('jsonwebtoken')
const oauth = new DiscordOauth2({
clientId: process.env.clientId,
clientSecret: process.env.clientSecret,
redirectUri: `${process.env.websiteURL}/discord`,
});
const categorySchema = require('../schemas/category-schema');
const profileSchema = require('../schemas/profile-schema');
const notificationSchema = require('../schemas/notification-schema')
router.get('/inventory', async (req, res, next) => {
let p
let cookies = req.cookies.get('key')
let user = await oauth.getUser(jwt.verify(cookies, process.env.jwtSecret))
var result = await itemSchema.find({userId: user.id})
let fill = req.cookies.get('fill')
if (result.length == 0) {
return res.render('inventory', {filled: "false", prod: [], bool: false, user: '', cats: await categorySchema.find({})});
}
if (req.cookies.get('inv-cat') == undefined) {
k = result
p = k
}
else {
k = result
p = k
}
const notifRes = await notificationSchema.find({userId: user.id})
let notif = notifRes.reverse()
res.render('inventory', {profile: await profileSchema.findOne({userId: user.id}), filled: fill,prod: p, bool: true, user: user, cats: await categorySchema.find({}), notifs: notif});
});
router.get('/admin/orders', async (req, res, next) => {
let l
let cookies = req.cookies.get('key')
let user = await oauth.getUser(jwt.verify(cookies, process.env.jwtSecret))
let result = await itemSchema.find({order: false})
let revRes = result.reverse()
res.render('orders', {prod: revRes, bool: true, user: user, title: 'Pending Orders'});
})
router.get('/admin/orders/fulfil', async (req, res, next) => {
let l
let cookies = req.cookies.get('key')
let user = await oauth.getUser(jwt.verify(cookies, process.env.jwtSecret))
let result = await itemSchema.find({order: true})
let revRes = result.reverse()
res.render('orders', {prod: revRes, bool: true, user: user, title: 'FulFilled Orders'});
})
router.post('/admin/orders', async (req, res, next) => {
let cookies = req.cookies.get('key')
let user = await oauth.getUser(jwt.verify(cookies, process.env.jwtSecret))
let result = await itemSchema.findOne({orderId: req.body.orderId})
if(req.body.status == "true") {
await new notificationSchema({
userId: user.id,
notification: `Your order for ${result.itemName} under ${result.category} has been fulfilled`,
time: Date.now()
}).save()
itemSchema.findOne({orderId: `${req.body.orderId}`}, async (err, doc) => {
if (err) throw err;
if (doc) {
console.log(doc)
doc.order = true
doc.save()
}
})
}
else if (req.body.status == "false") {
await new notificationSchema({
userId: user.id,
notification: `Your order for ${result.itemName} under ${result.category} has been queued for pending`,
time: Date.now()
}).save()
itemSchema.findOne({orderId: `${req.body.orderId}`}, async (err, doc) => {
if (err) throw err;
if (doc) {
console.log(doc)
doc.order = false
doc.save()
}
})
}
})
module.exports = router;