diff --git a/backend/src/routes/schedule.js b/backend/src/routes/schedule.js index cc0516e..88cc666 100644 --- a/backend/src/routes/schedule.js +++ b/backend/src/routes/schedule.js @@ -308,6 +308,15 @@ router.patch('/:id', authMiddleware, async (req, res) => { const member = await queryOne(req.schema, 'SELECT 1 FROM user_group_members WHERE user_id=$1 AND user_group_id=$2', [req.user.id, ugId]); if (!member) return res.status(403).json({ error: 'You can only assign groups you belong to' }); } + // Preserve any existing groups on this event that the user doesn't belong to + // (e.g. groups added by an admin) — silently merge them back into the submitted list + const existingGroupIds = (await query(req.schema, 'SELECT user_group_id FROM event_user_groups WHERE event_id=$1', [req.params.id])).map(r => r.user_group_id); + const submittedSet = new Set(userGroupIds.map(Number)); + for (const gid of existingGroupIds) { + if (submittedSet.has(gid)) continue; + const member = await queryOne(req.schema, 'SELECT 1 FROM user_group_members WHERE user_id=$1 AND user_group_id=$2', [req.user.id, gid]); + if (!member) userGroupIds.push(gid); + } } } const fields = { title, eventTypeId, startAt, endAt, allDay, location, description, isPublic, trackAvailability, recurrenceRule, origEvent: event }; diff --git a/frontend/src/components/SchedulePage.jsx b/frontend/src/components/SchedulePage.jsx index a264b37..f05b171 100644 --- a/frontend/src/components/SchedulePage.jsx +++ b/frontend/src/components/SchedulePage.jsx @@ -555,7 +555,8 @@ function EventForm({ event, userGroups, eventTypes, selectedDate, onSave, onCanc const [desc,setDesc]=useState(event?.description||''); const [pub,setPub]=useState(event?!!event.is_public:!!isToolManager); const [track,setTrack]=useState(!!event?.track_availability); - const [grps,setGrps]=useState(new Set((event?.user_groups||[]).map(g=>g.id))); + const accessibleGroupIds = new Set(userGroups.map(g=>g.id)); + const [grps,setGrps]=useState(new Set((event?.user_groups||[]).map(g=>g.id).filter(id=>isToolManager||accessibleGroupIds.has(id)))); const [saving,setSaving]=useState(false); const [showTypeForm,setShowTypeForm]=useState(false); const [localTypes,setLocalTypes]=useState(eventTypes);