-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreferenceModify.js
More file actions
88 lines (69 loc) · 4.12 KB
/
preferenceModify.js
File metadata and controls
88 lines (69 loc) · 4.12 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Preference Modify Controller
* Handles update and delete of preference profiles
* @module controllers/preferenceModify
*/
import db from '../config/db.js';
import buildHateoasLinks from '../utils/hateoasBuilder.js';
import R from '../utils/responseBuilder.js';
import { requireUser } from '../utils/controllerValidators.js';
import { VALID_CATEGORIES } from './preferenceCreate.js';
const normalizeProfiles = (profiles) => (profiles || []).map(p => ({ ...p, categories: Array.isArray(p.categories) ? p.categories : [] }));
const updatePreferenceProfile = async (req, res, next) => {
try {
const userId = parseInt(req.params.userId);
const profileId = parseInt(req.params.profileId);
const user = await requireUser(res, userId);
if (!user) return;
const incomingCategories = Array.isArray(req.body.categories) ? req.body.categories : Array.isArray(req.body.selectedPreferences) ? req.body.selectedPreferences : undefined;
const fieldName = Array.isArray(req.body.categories) ? 'categories' : 'selectedPreferences';
if (incomingCategories !== undefined && incomingCategories.length === 0) {
return R.badRequest(res, 'INVALID_PROFILE_DATA', 'No preferences selected', { field: fieldName, value: [], reason: 'At least one preference category must be selected' });
}
if (incomingCategories !== undefined) {
const invalidCategories = incomingCategories.filter(pref => !VALID_CATEGORIES.includes(pref));
if (invalidCategories.length > 0) {
return R.badRequest(res, 'INVALID_PROFILE_DATA', 'Invalid preference categories', { field: 'categories', invalidValues: invalidCategories });
}
}
const updatePayload = { ...req.body };
if (incomingCategories !== undefined) updatePayload.categories = incomingCategories;
if ('profileName' in updatePayload) { updatePayload.name = updatePayload.profileName; delete updatePayload.profileName; }
if ('selectedPreferences' in updatePayload) delete updatePayload.selectedPreferences;
const updatedProfile = await db.updatePreferenceProfile(userId, profileId, updatePayload);
if (!updatedProfile) {
return R.notFound(res, 'PROFILE_NOT_FOUND', `Profile with ID ${profileId} not found for user ${userId}`);
}
const allProfiles = await db.getPreferenceProfiles(userId);
return R.success(res, { profiles: normalizeProfiles(allProfiles), links: buildHateoasLinks.preferenceProfilesCollection(userId) }, 'Preference profile updated successfully');
} catch (error) { next(error); }
};
const deletePreferenceProfile = async (req, res, next) => {
try {
const userId = parseInt(req.params.userId);
const profileId = parseInt(req.params.profileId);
const user = await requireUser(res, userId);
if (!user) return;
const deleted = await db.deletePreferenceProfile(userId, profileId);
if (!deleted) {
return R.notFound(res, 'PROFILE_NOT_FOUND', `Profile with ID ${profileId} not found for user ${userId}`);
}
return R.noContent(res);
} catch (error) { next(error); }
};
const activatePreferenceProfile = async (req, res, next) => {
try {
const userId = parseInt(req.params.userId);
const profileId = parseInt(req.params.profileId);
const user = await requireUser(res, userId);
if (!user) return;
const profiles = await db.getPreferenceProfiles(userId);
if (!profiles.find(p => p.profileId === profileId)) {
return R.notFound(res, 'PROFILE_NOT_FOUND', `Profile with ID ${profileId} not found for user ${userId}`);
}
await db.updateUserById(userId, { activeProfile: profileId });
const allProfiles = await db.getPreferenceProfiles(userId);
return R.success(res, { profiles: normalizeProfiles(allProfiles), activeProfile: profileId, links: buildHateoasLinks.preferenceProfilesCollection(userId) }, 'Profile activated successfully');
} catch (error) { next(error); }
};
export default { updatePreferenceProfile, deletePreferenceProfile, activatePreferenceProfile };