-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveUserDetails.ts
122 lines (112 loc) · 3.74 KB
/
saveUserDetails.ts
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
/**
* Express router module to handle user details saving and updating.
* @module saveUserDetails
*/
import dotenv from 'dotenv';
import { NextFunction, Request, Response, Router } from 'express';
import { body, validationResult } from 'express-validator';
import { db } from './index';
dotenv.config();
/**
* Express router instance.
* @type {Router}
*/
const router = Router();
/**
* Middleware to handle validation errors from express-validator.
*
* @param {Request} req - Express request object.
* @param {Response} res - Express response object.
* @param {NextFunction} next - Express next middleware function.
* @returns {void}
*/
const handleValidationErrors = (req: Request, res: Response, next: NextFunction): void => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(400).json({ errors: errors.array() });
}
next();
};
/**
* POST /save-user-details
* Endpoint to save or update user details.
*
* @name POST /save-user-details
* @function
* @memberof module:saveUserDetails
* @param {string} fullName - User's full name.
* @param {string} email - User's email address.
* @param {string} organization - User's organization name.
* @param {string} orgType - Type of the user's organization.
* @param {string} [orgWebsite] - Website of the user's organization (optional).
* @param {string} jobTitle - User's job title.
* @param {string} [linkedin] - User's LinkedIn profile URL (optional).
* @param {string} [expertise] - User's area of expertise (optional).
* @param {string} [aboutMe] - Brief description about the user (optional).
* @returns {Object} JSON object containing a success message and the user ID.
*/
router.post(
'/save-user-details',
[
body('fullName').notEmpty().withMessage('Full Name is required'),
body('email').isEmail().withMessage('Invalid email format'),
body('organization').notEmpty().withMessage('Organization is required'),
body('orgType').notEmpty().withMessage('Organization Type is required'),
body('orgWebsite').optional().isURL().withMessage('Invalid URL format'),
body('jobTitle').notEmpty().withMessage('Job Title is required'),
body('linkedin').optional().isURL().withMessage('Invalid URL format'),
body('expertise').optional(),
body('aboutMe').optional(),
],
handleValidationErrors,
async (req: Request, res: Response): Promise<void> => {
const {
fullName,
email,
organization,
orgType,
orgWebsite,
jobTitle,
linkedin,
expertise,
aboutMe,
} = req.body;
try {
const existingUser = await db('public.user_details').select('id').where({ email }).first();
if (existingUser) {
await db('public.user_details').where({ id: existingUser.id }).update({
full_name: fullName,
organization,
org_type: orgType,
org_website: orgWebsite,
job_title: jobTitle,
linkedin,
expertise,
about_me: aboutMe,
});
res
.status(200)
.json({ message: 'User details updated successfully', userId: existingUser.id });
} else {
const [newUser] = await db('public.user_details')
.insert({
full_name: fullName,
email,
organization,
org_type: orgType,
org_website: orgWebsite,
job_title: jobTitle,
linkedin,
expertise,
about_me: aboutMe,
})
.returning('id');
res.status(201).json({ message: 'User details saved successfully', userId: newUser.id });
}
} catch (error) {
console.error('Error saving user details:', error);
res.status(500).json({ error: 'Internal server error' });
}
}
);
export default router;