backedn converted to ts
This commit is contained in:
+148
-98
@@ -1,32 +1,40 @@
|
||||
const express = require('express');
|
||||
const axios = require('axios');
|
||||
const config = require('config');
|
||||
const router = express.Router();
|
||||
const auth = require('../../middleware/auth');
|
||||
const { check, validationResult } = require('express-validator');
|
||||
// bring in normalize to give us a proper url, regardless of what user entered
|
||||
const normalize = require('normalize-url');
|
||||
const checkObjectId = require('../../middleware/checkObjectId');
|
||||
import express from 'express';
|
||||
import axios from 'axios';
|
||||
import config from 'config';
|
||||
|
||||
const Profile = require('../../models/Profile');
|
||||
const User = require('../../models/User');
|
||||
const Post = require('../../models/Post');
|
||||
import auth from '../../middleware/auth';
|
||||
import { check, validationResult } from 'express-validator';
|
||||
|
||||
// bring in normalize to give us a proper url, regardless of what user entered
|
||||
import normalize from 'normalize-url';
|
||||
import checkObjectId from '../../middleware/checkObjectId';
|
||||
|
||||
import Profile from '../../models/Profile';
|
||||
import User from '../../models/User';
|
||||
import Post from '../../models/Post';
|
||||
import { isUserId } from '../../utils';
|
||||
const router = express.Router();
|
||||
// @route GET api/profile/me
|
||||
// @desc Get current users profile
|
||||
// @access Private
|
||||
router.get('/me', auth, async (req, res) => {
|
||||
try {
|
||||
const profile = await Profile.findOne({
|
||||
user: req.user.id
|
||||
}).populate('user', ['name', 'avatar']);
|
||||
if (isUserId(req)) {
|
||||
const profile = await Profile.findOne({
|
||||
user: req.user.id
|
||||
}).populate('user', ['name', 'avatar']);
|
||||
|
||||
if (!profile) {
|
||||
return res.status(400).json({ msg: 'There is no profile for this user' });
|
||||
if (!profile) {
|
||||
return res.status(400).json({ msg: 'There is no profile for this user' });
|
||||
}
|
||||
|
||||
res.json(profile);
|
||||
}
|
||||
|
||||
res.json(profile);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
} catch (err: unknown) {
|
||||
if (typeof err === 'string')
|
||||
console.error(err)
|
||||
else if (err instanceof Error)
|
||||
console.error(err.message);
|
||||
res.status(500).send('Server Error');
|
||||
}
|
||||
});
|
||||
@@ -59,40 +67,45 @@ router.post(
|
||||
} = req.body;
|
||||
|
||||
// build a profile
|
||||
const profileFields = {
|
||||
user: req.user.id,
|
||||
website:
|
||||
website && website !== ''
|
||||
? normalize(website, { forceHttps: true })
|
||||
: '',
|
||||
skills: Array.isArray(skills)
|
||||
? skills
|
||||
: skills.split(',').map((skill) => ' ' + skill.trim()),
|
||||
...rest
|
||||
};
|
||||
if (isUserId(req)) {
|
||||
const profileFields = {
|
||||
user: req.user.id,
|
||||
website:
|
||||
website && website !== ''
|
||||
? normalize(website, { forceHttps: true })
|
||||
: '',
|
||||
skills: Array.isArray(skills)
|
||||
? skills
|
||||
: skills.split(',').map((skill: string) => ' ' + skill.trim()),
|
||||
...rest
|
||||
};
|
||||
|
||||
// Build socialFields object
|
||||
const socialFields = { youtube, twitter, instagram, linkedin, facebook };
|
||||
// Build socialFields object
|
||||
const socialFields: { [key: string]: any } = { youtube, twitter, instagram, linkedin, facebook };
|
||||
|
||||
// normalize social fields to ensure valid url
|
||||
for (const [key, value] of Object.entries(socialFields)) {
|
||||
if (value && value.length > 0)
|
||||
socialFields[key] = normalize(value, { forceHttps: true });
|
||||
}
|
||||
// add to profileFields
|
||||
profileFields.social = socialFields;
|
||||
// normalize social fields to ensure valid url
|
||||
for (const [key, value] of Object.entries(socialFields)) {
|
||||
if (value && value.length > 0)
|
||||
socialFields[key] = normalize(value, { forceHttps: true });
|
||||
}
|
||||
// add to profileFields
|
||||
profileFields.social = socialFields;
|
||||
|
||||
try {
|
||||
// Using upsert option (creates new doc if no match is found):
|
||||
let profile = await Profile.findOneAndUpdate(
|
||||
{ user: req.user.id },
|
||||
{ $set: profileFields },
|
||||
{ new: true, upsert: true, setDefaultsOnInsert: true }
|
||||
);
|
||||
return res.json(profile);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
return res.status(500).send('Server Error');
|
||||
try {
|
||||
// Using upsert option (creates new doc if no match is found):
|
||||
let profile = await Profile.findOneAndUpdate(
|
||||
{ user: req.user.id },
|
||||
{ $set: profileFields },
|
||||
{ new: true, upsert: true, setDefaultsOnInsert: true }
|
||||
);
|
||||
return res.json(profile);
|
||||
} catch (err: unknown) {
|
||||
if (typeof err === 'string')
|
||||
console.error(err)
|
||||
else if (err instanceof Error)
|
||||
console.error(err.message);
|
||||
return res.status(500).send('Server Error');
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -104,8 +117,11 @@ router.get('/', async (req, res) => {
|
||||
try {
|
||||
const profiles = await Profile.find().populate('user', ['name', 'avatar']);
|
||||
res.json(profiles);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
} catch (err: unknown) {
|
||||
if (typeof err === 'string')
|
||||
console.error(err)
|
||||
else if (err instanceof Error)
|
||||
console.error(err.message);
|
||||
res.status(500).send('Server Error');
|
||||
}
|
||||
});
|
||||
@@ -125,8 +141,11 @@ router.get(
|
||||
if (!profile) return res.status(400).json({ msg: 'Profile not found' });
|
||||
|
||||
return res.json(profile);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
} catch (err: unknown) {
|
||||
if (typeof err === 'string')
|
||||
console.error(err)
|
||||
else if (err instanceof Error)
|
||||
console.error(err.message);
|
||||
return res.status(500).json({ msg: 'Server error' });
|
||||
}
|
||||
}
|
||||
@@ -140,15 +159,19 @@ router.delete('/', auth, async (req, res) => {
|
||||
// Remove user posts
|
||||
// Remove profile
|
||||
// Remove user
|
||||
await Promise.all([
|
||||
Post.deleteMany({ user: req.user.id }),
|
||||
Profile.findOneAndRemove({ user: req.user.id }),
|
||||
User.findOneAndRemove({ _id: req.user.id })
|
||||
]);
|
||||
if (isUserId(req))
|
||||
await Promise.all([
|
||||
Post.deleteMany({ user: req.user.id }),
|
||||
Profile.findOneAndRemove({ user: req.user.id }),
|
||||
User.findOneAndRemove({ _id: req.user.id })
|
||||
]);
|
||||
|
||||
res.json({ msg: 'User deleted' });
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
} catch (err: unknown) {
|
||||
if (typeof err === 'string')
|
||||
console.error(err)
|
||||
else if (err instanceof Error)
|
||||
console.error(err.message);
|
||||
res.status(500).send('Server Error');
|
||||
}
|
||||
});
|
||||
@@ -171,15 +194,21 @@ router.put(
|
||||
}
|
||||
|
||||
try {
|
||||
const profile = await Profile.findOne({ user: req.user.id });
|
||||
if (isUserId(req)) {
|
||||
const profile = await Profile.findOne({ user: req.user.id });
|
||||
|
||||
profile.experience.unshift(req.body);
|
||||
if (profile) {
|
||||
profile.experience.unshift(req.body);
|
||||
|
||||
await profile.save();
|
||||
|
||||
res.json(profile);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
await profile.save();
|
||||
}
|
||||
res.json(profile);
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
if (typeof err === 'string')
|
||||
console.error(err)
|
||||
else if (err instanceof Error)
|
||||
console.error(err.message);
|
||||
res.status(500).send('Server Error');
|
||||
}
|
||||
}
|
||||
@@ -191,16 +220,22 @@ router.put(
|
||||
|
||||
router.delete('/experience/:exp_id', auth, async (req, res) => {
|
||||
try {
|
||||
const foundProfile = await Profile.findOne({ user: req.user.id });
|
||||
if (isUserId(req)) {
|
||||
const foundProfile = await Profile.findOne({ user: req.user.id });
|
||||
if (foundProfile) {
|
||||
foundProfile.experience = foundProfile.experience.filter(
|
||||
(exp: any) => exp._id.toString() !== req.params.exp_id
|
||||
);
|
||||
|
||||
foundProfile.experience = foundProfile.experience.filter(
|
||||
(exp) => exp._id.toString() !== req.params.exp_id
|
||||
);
|
||||
|
||||
await foundProfile.save();
|
||||
return res.status(200).json(foundProfile);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
await foundProfile.save();
|
||||
}
|
||||
return res.status(200).json(foundProfile);
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
if (typeof err === 'string')
|
||||
console.error(err)
|
||||
else if (err instanceof Error)
|
||||
console.error(err.message);
|
||||
return res.status(500).json({ msg: 'Server error' });
|
||||
}
|
||||
});
|
||||
@@ -224,15 +259,20 @@ router.put(
|
||||
}
|
||||
|
||||
try {
|
||||
const profile = await Profile.findOne({ user: req.user.id });
|
||||
if (isUserId(req)) {
|
||||
const profile = await Profile.findOne({ user: req.user.id });
|
||||
if (profile) {
|
||||
profile.education.unshift(req.body);
|
||||
|
||||
profile.education.unshift(req.body);
|
||||
|
||||
await profile.save();
|
||||
|
||||
res.json(profile);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
await profile.save();
|
||||
}
|
||||
res.json(profile);
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
if (typeof err === 'string')
|
||||
console.error(err)
|
||||
else if (err instanceof Error)
|
||||
console.error(err.message);
|
||||
res.status(500).send('Server Error');
|
||||
}
|
||||
}
|
||||
@@ -244,14 +284,21 @@ router.put(
|
||||
|
||||
router.delete('/education/:edu_id', auth, async (req, res) => {
|
||||
try {
|
||||
const foundProfile = await Profile.findOne({ user: req.user.id });
|
||||
foundProfile.education = foundProfile.education.filter(
|
||||
(edu) => edu._id.toString() !== req.params.edu_id
|
||||
);
|
||||
await foundProfile.save();
|
||||
return res.status(200).json(foundProfile);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
if (isUserId(req)) {
|
||||
const foundProfile = await Profile.findOne({ user: req.user.id });
|
||||
if (foundProfile) {
|
||||
foundProfile.education = foundProfile.education.filter(
|
||||
(edu: any) => edu._id.toString() !== req.params.edu_id
|
||||
);
|
||||
await foundProfile.save();
|
||||
}
|
||||
return res.status(200).json(foundProfile);
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
if (typeof err === 'string')
|
||||
console.error(err)
|
||||
else if (err instanceof Error)
|
||||
console.error(err.message);
|
||||
return res.status(500).json({ msg: 'Server error' });
|
||||
}
|
||||
});
|
||||
@@ -271,10 +318,13 @@ router.get('/github/:username', async (req, res) => {
|
||||
|
||||
const gitHubResponse = await axios.get(uri, { headers });
|
||||
return res.json(gitHubResponse.data);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
} catch (err: unknown) {
|
||||
if (typeof err === 'string')
|
||||
console.error(err)
|
||||
else if (err instanceof Error)
|
||||
console.error(err.message);
|
||||
return res.status(404).json({ msg: 'No Github profile found' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
module.exports = router
|
||||
|
||||
Reference in New Issue
Block a user