Family manager bug fixes
This commit is contained in:
@@ -386,6 +386,21 @@ router.get('/:id', authMiddleware, async (req, res) => {
|
||||
[req.user.id]
|
||||
);
|
||||
}
|
||||
|
||||
// Return partner user info if they are in one of this event's user groups
|
||||
if (partnerId) {
|
||||
const partnerInGroup = await queryOne(req.schema, `
|
||||
SELECT 1 FROM event_user_groups eug
|
||||
JOIN user_group_members ugm ON ugm.user_group_id=eug.user_group_id
|
||||
WHERE eug.event_id=$1 AND ugm.user_id=$2
|
||||
`, [event.id, partnerId]);
|
||||
if (partnerInGroup) {
|
||||
event.my_partner = await queryOne(req.schema,
|
||||
'SELECT id,name,display_name,avatar FROM users WHERE id=$1',
|
||||
[partnerId]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
const mine = await queryOne(req.schema, 'SELECT response, note FROM event_availability WHERE event_id=$1 AND user_id=$2', [req.params.id, req.user.id]);
|
||||
event.my_response = mine?.response || null;
|
||||
@@ -692,10 +707,28 @@ router.put('/:id/availability', authMiddleware, async (req, res) => {
|
||||
const event = await queryOne(req.schema, 'SELECT * FROM events WHERE id=$1', [req.params.id]);
|
||||
if (!event) return res.status(404).json({ error: 'Not found' });
|
||||
if (!event.track_availability) return res.status(400).json({ error: 'Availability tracking not enabled' });
|
||||
const { response, note, aliasId } = req.body;
|
||||
const { response, note, aliasId, forPartnerId } = req.body;
|
||||
if (!['going','maybe','not_going'].includes(response)) return res.status(400).json({ error: 'Invalid response' });
|
||||
const trimmedNote = note ? String(note).trim().slice(0, 20) : null;
|
||||
|
||||
if (forPartnerId) {
|
||||
// Respond on behalf of partner — verify partnership and partner's group membership
|
||||
const isPartner = await queryOne(req.schema,
|
||||
'SELECT 1 FROM guardian_partners WHERE (user_id_1=$1 AND user_id_2=$2) OR (user_id_1=$2 AND user_id_2=$1)',
|
||||
[req.user.id, forPartnerId]);
|
||||
if (!isPartner) return res.status(403).json({ error: 'Not your partner' });
|
||||
const partnerInGroup = await queryOne(req.schema, `
|
||||
SELECT 1 FROM event_user_groups eug JOIN user_group_members ugm ON ugm.user_group_id=eug.user_group_id
|
||||
WHERE eug.event_id=$1 AND ugm.user_id=$2
|
||||
`, [event.id, forPartnerId]);
|
||||
if (!partnerInGroup) return res.status(403).json({ error: 'Partner is not assigned to this event' });
|
||||
await exec(req.schema, `
|
||||
INSERT INTO event_availability (event_id,user_id,response,note,updated_at) VALUES ($1,$2,$3,$4,NOW())
|
||||
ON CONFLICT (event_id,user_id) DO UPDATE SET response=$3, note=$4, updated_at=NOW()
|
||||
`, [event.id, forPartnerId, response, trimmedNote]);
|
||||
return res.json({ success: true, response, note: trimmedNote });
|
||||
}
|
||||
|
||||
if (aliasId) {
|
||||
// Alias response (Guardian Only mode) — verify alias belongs to current user or their partner
|
||||
const alias = await queryOne(req.schema,
|
||||
@@ -741,8 +774,14 @@ router.patch('/:id/availability/note', authMiddleware, async (req, res) => {
|
||||
|
||||
router.delete('/:id/availability', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const { aliasId } = req.query;
|
||||
if (aliasId) {
|
||||
const { aliasId, forPartnerId } = req.query;
|
||||
if (forPartnerId) {
|
||||
const isPartner = await queryOne(req.schema,
|
||||
'SELECT 1 FROM guardian_partners WHERE (user_id_1=$1 AND user_id_2=$2) OR (user_id_1=$2 AND user_id_2=$1)',
|
||||
[req.user.id, forPartnerId]);
|
||||
if (!isPartner) return res.status(403).json({ error: 'Not your partner' });
|
||||
await exec(req.schema, 'DELETE FROM event_availability WHERE event_id=$1 AND user_id=$2', [req.params.id, forPartnerId]);
|
||||
} else if (aliasId) {
|
||||
const alias = await queryOne(req.schema,
|
||||
`SELECT id FROM guardian_aliases WHERE id=$1 AND (
|
||||
guardian_id=$2 OR guardian_id IN (
|
||||
|
||||
Reference in New Issue
Block a user