import { Router } from 'express'; import { patchGraph } from '../utils/index.ts'; import { acquireAppToken } from '../helper/msal.ts'; import { AuthManager } from '../auth/index.ts'; export default (auth?: AuthManager) => { const router = Router(); router.patch('/:id', async (req, res) => { try { // Authentication handled via client credentials (resolveCredential + createMsalClient) let token: string; try { token = await acquireAppToken(req, auth); } catch (err: any) { return res.status(401).json({ error: 'Failed to acquire access token', message: err?.message ?? String(err) }); } const eventId = req.params.id; const { subject, start, end, body, location } = req.body; const eventPayload: any = {}; if (subject) eventPayload.subject = subject; if (start) eventPayload.start = { dateTime: start, timeZone: 'UTC' }; if (end) eventPayload.end = { dateTime: end, timeZone: 'UTC' }; if (body) eventPayload.body = { contentType: 'HTML', content: body }; if (location) eventPayload.location = { displayName: location }; const data = await patchGraph(`/me/events/${eventId}`, token, eventPayload); res.json(data); } catch (err: any) { res.status(500).json({ error: err.message }); } }); return router; };