Created
October 30, 2019 05:43
-
-
Save crazywolf132/9353ebc179ba91f3bf9a331f654b0518 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Router } from 'express'; | |
import passport from 'passport'; | |
import isAuth from '../middleware/isAuth'; | |
import validateToken from '../middleware/validateToken'; | |
import search from '../middleware/search'; | |
const fetch = require('node-fetch'); | |
const Sub = require(`${process.cwd()}/models/Subscriptions`); | |
const Events = require(`${process.cwd()}/models/EventModel`); | |
const { | |
sendMessage, | |
createBody, | |
preparePayload, | |
diff | |
} = require(`${process.cwd()}/notifications/FireBaseHandler`); | |
var router = Router(); | |
router.get('/', isAuth, validateToken, search, (req, res, next) => { | |
// Only going to show the stuff linked to this user... | |
var search = { | |
owner: req.token.user._id | |
}; | |
if (req.query.search) { | |
search.title = { $regex: req.query.search, $options: 'i' }; | |
} | |
console.log(req.search); | |
console.log(req.args); | |
// console.log(req); | |
Events.find(search, (err, e) => { | |
if (e) { | |
let eventList = []; | |
e.forEach(element => { | |
let copy = element.toObject(); | |
copy.id = copy._id; | |
delete copy.__v; | |
eventList.push(copy); | |
}); | |
res.set({ 'X-Total-Count': eventList.length }); | |
res.json(eventList); | |
} | |
}); | |
}); | |
router.get('/:id', (req, res, next) => { | |
Events.findById(req.params.id, (err, item) => { | |
if (item) { | |
let copy = item.toObject(); | |
copy.id = copy._id; | |
res.json(copy); | |
} else { | |
res.status(400); | |
res.send(); | |
} | |
}); | |
}); | |
router.put('/:id', (req, res, next) => { | |
Events.findByIdAndUpdate(req.params.id, req.body, (err, item) => { | |
if (err) { | |
res.status(500); | |
console.log(err); | |
res.json(err); | |
} else if (item) { | |
let copy = item.toObject(); | |
copy.id = copy._id; | |
// sendMessage("something", "something else", "cse2isd"); | |
console.log(req.body); | |
console.log('============== RESULTS =============='); | |
console.log(diff(req.body, item.toObject())); | |
console.log('====================================='); | |
console.log(diff(item.toObject(), req.body)); | |
if (item.updatePeopleOnChange) { | |
sendMessage( | |
'Event has updated!', | |
`${item.title} --> ${createBody( | |
diff(item.toObject(), req.body) | |
)}`, | |
preparePayload(item.id), | |
item.id | |
); | |
} | |
console.log('====================================='); | |
console.log(item); | |
res.json(copy); | |
} else { | |
res.status(400); | |
res.send(); | |
} | |
}); | |
}); | |
router.post('/', isAuth, validateToken, (req, res, next) => { | |
console.log(req.body); | |
var newEvent = new Events({ | |
title: req.body.title, | |
description: req.body.description, | |
date: req.body.date, | |
work: req.body.work, | |
owner: req.token.user._id | |
}); | |
newEvent.save(function(err) { | |
if (err) { | |
res.status(500); | |
console.log(err); | |
res.json(err); | |
} else { | |
let copy = newEvent.toObject(); | |
copy.id = req.body.id ? req.body.id : copy._id; | |
copy.dbId = copy._id; | |
delete copy._id; | |
res.json(copy); | |
} | |
}); | |
}); | |
router.delete('/:id', (req, res, next) => { | |
console.log('running'); | |
Events.findByIdAndRemove(req.params.id, (err, item) => { | |
if (err) { | |
res.status(500); | |
res.json(err); | |
} else { | |
res.status(200); | |
// Need to inform the user that the item has been deleted. | |
if (item.updatePeopleOnChange) { | |
sendMessage( | |
'Event has updated!', | |
`${item.title} --> Has been removed!}`, | |
preparePayload(item.id, true), | |
item.id | |
); | |
} | |
res.send('done'); | |
// Now to perform the update. | |
const id = item.id; | |
Sub.find({ events: { $in: id } }, (err, result) => { | |
if (err) console.log(err); | |
if (result) { | |
result.forEach(subscription => { | |
let newBody = subscription.toObject(); | |
let eventsCopy = newBody.events; | |
eventsCopy = eventsCopy.splice( | |
eventsCopy.indexOf(id), | |
1 | |
); | |
newBody.events = eventsCopy; | |
console.log('============== RESULTS =============='); | |
console.log('BEFORE'); | |
console.log(subscription.toObject()); | |
console.log('AFTER'); | |
console.log(newBody); | |
console.log('====================================='); | |
Sub.findByIdAndUpdate(id, newBody, (err, result) => { | |
if (err) console.log(err); | |
}); | |
}); | |
} | |
}); | |
} | |
}); | |
}); | |
router.get('/verify/:id', (req, res, next) => { | |
fetch(`http://localhost:4420/verify/event/${req.params.id}`) | |
.then(res => res.json()) | |
.then(data => { | |
res.send(data); | |
}) | |
.catch(err => { | |
res.send(false); | |
}); | |
}); | |
export default router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment