-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathuserPreferences.ts
More file actions
68 lines (66 loc) · 1.8 KB
/
Copy pathuserPreferences.ts
File metadata and controls
68 lines (66 loc) · 1.8 KB
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
import { RequestHandler } from 'express';
import { User } from '../../models/user';
import {
UpdatePreferencesRequestBody,
UpdateCookieConsentRequestBody,
UpdatePreferencesResponseBody,
PublicUserOrError
} from '../../types';
import { saveUser } from './helpers';
/**
* - Method: `PUT`
* - Endpoint: `/preferences`
* - Authenticated: `true`
* - Id: `UserController.updatePreferences`
*
* Description:
* - Update user preferences, such as AppTheme
*/
export const updatePreferences: RequestHandler<
{},
UpdatePreferencesResponseBody,
UpdatePreferencesRequestBody
> = async (req, res) => {
try {
const user = await User.findById(req.user!.id).exec();
if (!user) {
res.status(404).json({ error: 'User not found' });
return;
}
// Shallow merge the new preferences with the existing.
user.preferences = { ...user.preferences, ...req.body.preferences };
await user.save();
res.json(user.preferences);
} catch (err) {
console.error('Could not save preferences:', err);
res.status(500).json({ error: 'Internal server error' });
}
};
/**
* - Method: `PUT`
* - Endpoint: `/cookie-consent`
* - Authenticated: `true`
* - Id: `UserController.updatePreferences`
*
* Description:
* - Update user cookie consent
*/
export const updateCookieConsent: RequestHandler<
{},
PublicUserOrError,
UpdateCookieConsentRequestBody
> = async (req, res) => {
try {
const user = await User.findById(req.user!.id).exec();
if (!user) {
res.status(404).json({ error: 'User not found' });
return;
}
const { cookieConsent } = req.body;
user.cookieConsent = cookieConsent;
await saveUser(res, user);
} catch (err) {
console.error('Could not save cookie consent:', err);
res.status(500).json({ error: 'Internal server error' });
}
};