diff --git a/mods/mods_available/test-actions/main.js b/mods/mods_available/test-actions/main.js new file mode 100644 index 0000000000..3d234f151a --- /dev/null +++ b/mods/mods_available/test-actions/main.js @@ -0,0 +1,138 @@ +/* + * Test-actions extension: declarative actions page for testing user suspension + * and other admin actions. All changes in this single file. + */ + +const { db } = extension.import('data'); +const { invalidate_cached_user } = use('core.util.helpers'); + +// Declarative actions: id, label, and inputs drive the generated GUI. +const ACTIONS = [ + { + id: 'suspend-user', + label: 'Suspend user', + inputs: [ + { name: 'username', label: 'Username', type: 'text' }, + ], + }, + // Add more actions here; each needs a handler in INVOKE_HANDLERS. +]; + +// Handlers for each action id. Receives (req, res, body). +const INVOKE_HANDLERS = { + 'suspend-user': async (req, res, body) => { + const username = body?.username?.trim(); + if ( ! username ) { + return res.status(400).json({ ok: false, error: 'username is required' }); + } + const svc_get_user = req.services.get('get-user'); + const user = await svc_get_user.get_user({ username }); + if ( ! user ) { + return res.status(404).json({ ok: false, error: 'User not found' }); + } + await db.write('UPDATE `user` SET suspended = 1 WHERE id = ? LIMIT 1', [user.id]); + invalidate_cached_user(user); + // Cache invalidation would require backend helpers (ESM); skipped here. + return res.json({ ok: true, message: `User "${username}" suspended.` }); + }, +}; + +const PAGE_HTML = (actionsJson) => ` + +
+ +