-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin.js
158 lines (143 loc) · 5.16 KB
/
admin.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const client = require('./client');
const express = require('express');
const adminRouter = express.Router();
const { ObjectId } = require('mongodb');
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
await client.connect();
const category = client.db('empowerRise').collection('category');
const donation = client.db('empowerRise').collection('donation');
const fund = client.db('empowerRise').collection('fund');
const fundApply = client.db('empowerRise').collection('fundApply');
const notification = client.db('empowerRise').collection('notification');
const user = client.db('empowerRise').collection('users');
adminRouter.route('/getAllCategory')
.get(async(req,res)=>{
const result = await category.find().toArray();
res.send(result);
})
adminRouter.route('/addCategory')
.post(async(req,res)=>{
const data = req.body;
await category.insertOne(data);
res.send({status:true});
})
//ryd - 6-9-23
adminRouter
.route('/adminPostRequest')
.post(async(req,res)=>{
const data = req.body;
if(data.type==='donation'){
const result = await donation.find({status:'pending'}).toArray();
res.send(result);
} else if(data.type==='fund'){
const result = await fund.find({status:'pending'}).toArray();
res.send(result);
}
})
adminRouter
.route('/adminStatusUpdate')
.post(async(req,res)=>{
const data = req.body;
const id = new ObjectId(data.id);
if(data.type==='donation'){
if(data.status==='accept'){
await donation.updateOne({_id:id},{$set:{status:'processing'}});
res.send({status:true});
} else if(data.status==='reject'){
await donation.deleteOne({_id:id});
res.send({status:false});
}
} else if(data.type==='fund'){
if(data.status==='accept'){
await fund.updateOne({_id:id},{$set:{status:'processing'}});
res.send({status:true});
} else if(data.status==='reject'){
await fund.deleteOne({_id:id});
res.send({status:false});
}
} else if(data.type==='fundApply'){
const postId = new ObjectId(req.body.id);
if(data.status==='accept'){
console.log("check log",postId);
await fundApply.updateOne({_id:postId},{$set:{status:'accept'}});
res.send({status:true});
} else if(data.status==='reject'){
await fundApply.deleteOne({postId});
res.send({status:false});
}
}
let userId;
if(data.type==='fund'){
userId = await fund.findOne({ _id: id }, { projection: { userId: true,title:true, _id: false }});
}else if(data.type==='donation'){
userId = await donation.findOne({ _id: id }, { projection: { userId: true,title:true, _id: false }});
} else if(data.type==='fundApply'){
userId = await fundApply.findOne({ _id: id }, { projection: { userId: true,postId:true, _id: false }});
}
//notification
const notifi = {
isRead:false,
role:'user',
postId:data.id,
userId:userId.userId,
description:`Your post '${userId.title}' for ${data.type} is ${data.status}ed by admin.`
}
if(data.type==='fundApply'){
const postId = new ObjectId(userId.postId)
const title = await fund.findOne({_id:postId},{projection:{title:true,_id:false}});
notifi.description = `Your appication for fund ${title.title} is ${data.status}ed by doner.`;
}
await notification.insertOne(notifi);
})
//ryd - 9-9-23
adminRouter.route('/getEditPostList')
.post(async(req,res)=>{
const type = req.body.type;
if(type==='fund'){
const result = await fund.find({status:'processing'}).sort({dayLeft:1}).toArray();
res.send(result);
} else if(type==='donation'){
const result = await donation.find({status:'processing'}).sort({dayLeft:1}).toArray();
res.send(result);
}
})
adminRouter.route('/adminPostStatusUpdate')
.post(async(req,res)=>{
const data = req.body;
const id = new ObjectId(data.id);
const status = req.body.status;
if(data.type==='donation'){
await donation.updateOne({_id:id},{$set:{status}});
res.send({status:true});
} else if(data.type==='fund'){
await fund.updateOne({_id:id},{$set:{status}});
res.send({status:true});
}
//notification
let userId;
if(data.type==='fund'){
userId = await fund.findOne({ _id: id }, { projection: { userId: true,title:true, _id: false }});
}else if(data.type==='donation'){
userId = await donation.findOne({ _id: id }, { projection: { userId: true,title:true, _id: false }});
}
const notifi = {
isRead:false,
role:'user',
postId:data.id,
userId:userId.userId,
description:`Your post '${userId.title}' for ${data.type} is set ${data.status} by admin.`
}
await notification.insertOne(notifi);
})
// Send a ping to confirm a successful connection
await client.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
} finally {
// Ensures that the client will close when you finish/error
//await client.close();
}
}
run().catch(console.dir);
module.exports = adminRouter;