Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
code: 200,
message: "User Successfully Created",
success: true
});
})
);
// set role
router.put(
"/:user_id/role",
requireAdminStatus,
celebrate({
body: Joi.object().keys({
firstName: Joi.string(),
lastName: Joi.string(),
oauthId: Joi.string(),
propicUrl: Joi.string(),
isApproved: Joi.boolean(),
role: Joi.string().required(),
location: Joi.string(),
email: Joi.string()
})
}),
errorWrap(async (req, res) => {
const data = req.body;
const userId = req.params.user_id;
const user = await User.findByIdAndUpdate(
userId,
{ $set: { role: data.role } },
{ new: true }
);
);
// approve user
router.put(
"/:user_id/approve",
requireAdminStatus,
celebrate({
body: Joi.object().keys({
firstName: Joi.string(),
lastName: Joi.string(),
oauthId: Joi.string(),
propicUrl: Joi.string(),
isApproved: Joi.boolean(),
role: Joi.string(),
location: Joi.string(),
email: Joi.string()
})
}),
errorWrap(async (req, res) => {
const userId = req.params.user_id;
const user = await User.findByIdAndUpdate(
userId,
{ $set: { isApproved: true } },
{ new: true }
);
const ret = user
? {
code: 200,
message: "User Approved Successfully",
success: true
}
});
})
);
// create new user
router.post(
"/",
requireAdminStatus,
celebrate({
body: Joi.object().keys({
firstName: Joi.string().required(),
lastName: Joi.string().required(),
oauthId: Joi.string().required(),
propicUrl: Joi.string(),
isApproved: Joi.boolean().default(false),
role: Joi.string().required(),
location: Joi.string().required(),
email: Joi.string().required()
})
}),
errorWrap(async (req, res) => {
const data = req.body;
const newUser = new User({
firstName: data.firstName,
lastName: data.lastName,
oauthId: data.oauthId,
propicUrl: data.propicUrl,
role: data.role,
location: data.location,
email: data.email
});
await newUser.save();
const resource = await Resource.findById(resourceId);
res.json({
code: 200,
result: resource,
success: true
});
})
);
// edit resource
router.put(
"/:resource_id",
requireAdminStatus,
celebrate({
body: Joi.object().keys({
companyName: Joi.string(),
contactName: Joi.string(),
contactPhone: Joi.string(),
contactEmail: Joi.string(),
description: Joi.string(),
address: Joi.string(),
location: Joi.object({
type: Joi.string().default("Point"),
coordinates: Joi.array()
.length(2)
.items(Joi.number())
}),
notes: Joi.string(),
tags: Joi.array().items(Joi.string())
}),
params: {
resource_id: Joi.objectId().required()
success: true
});
})
);
// edit resource
router.put(
"/:resource_id",
requireAdminStatus,
celebrate({
body: Joi.object().keys({
companyName: Joi.string(),
contactName: Joi.string(),
contactPhone: Joi.string(),
contactEmail: Joi.string(),
description: Joi.string(),
address: Joi.string(),
location: Joi.object({
type: Joi.string().default("Point"),
coordinates: Joi.array()
.length(2)
.items(Joi.number())
}),
notes: Joi.string(),
tags: Joi.array().items(Joi.string())
}),
params: {
resource_id: Joi.objectId().required()
}
}),
errorWrap(async (req, res) => {
const data = req.body;
success: true
});
})
);
// get list of resources filtered by location radius
router.get(
"/filter",
requireVolunteerStatus,
celebrate({
query: {
radius: Joi.number(),
address: Joi.string(),
keyword: Joi.string(),
customWeights: Joi.array(),
tag: Joi.string()
}
}),
errorWrap(async (req, res) => {
const { radius, address, keyword, customWeights, tag } = req.query;
let resources = await Resource.find({});
const latlng = await resourceUtils.addressToLatLong(address);
const lat = latlng.lat;
const long = latlng.lng;
if (radius && lat && long) {
const radiusOfEarth = 3963.2; // in miles
resources = await Resource.find({
location: {
$geoWithin: { $centerSphere: [[long, lat], radius / radiusOfEarth] }
}
res.json({
code: 200,
result: resources,
success: true
});
})
);
// get list of resources filtered by location radius
router.get(
"/filter",
requireVolunteerStatus,
celebrate({
query: {
radius: Joi.number(),
address: Joi.string(),
keyword: Joi.string(),
customWeights: Joi.array(),
tag: Joi.string()
}
}),
errorWrap(async (req, res) => {
const { radius, address, keyword, customWeights, tag } = req.query;
let resources = await Resource.find({});
const latlng = await resourceUtils.addressToLatLong(address);
const lat = latlng.lat;
const long = latlng.lng;
if (radius && lat && long) {
const radiusOfEarth = 3963.2; // in miles
resources = await Resource.find({
res.status(ret.code).json(ret);
})
);
// approve user
router.put(
"/:user_id/approve",
requireAdminStatus,
celebrate({
body: Joi.object().keys({
firstName: Joi.string(),
lastName: Joi.string(),
oauthId: Joi.string(),
propicUrl: Joi.string(),
isApproved: Joi.boolean(),
role: Joi.string(),
location: Joi.string(),
email: Joi.string()
})
}),
errorWrap(async (req, res) => {
const userId = req.params.user_id;
const user = await User.findByIdAndUpdate(
userId,
{ $set: { isApproved: true } },
{ new: true }
);
const ret = user
? {
code: 200,
message: "User Approved Successfully",
})
);
// set role
router.put(
"/:user_id/role",
requireAdminStatus,
celebrate({
body: Joi.object().keys({
firstName: Joi.string(),
lastName: Joi.string(),
oauthId: Joi.string(),
propicUrl: Joi.string(),
isApproved: Joi.boolean(),
role: Joi.string().required(),
location: Joi.string(),
email: Joi.string()
})
}),
errorWrap(async (req, res) => {
const data = req.body;
const userId = req.params.user_id;
const user = await User.findByIdAndUpdate(
userId,
{ $set: { role: data.role } },
{ new: true }
);
const ret = user
? {
code: 200,
message: "User Role Updated Successfully",
'use strict'
import { Joi, celebrate } from 'celebrate'
const { body, params, query, headers } = {
body: Joi.object().keys({
id: Joi.string().required(),
value: Joi.string().required()
}),
params: Joi.object({
id: Joi.string().alphanum().optional()
}).unknown(),
query: {
},
headers: Joi.object({
}).unknown()
}
export default {
BODY: celebrate({ body }),
PARAMS: celebrate({ params }),