* Write-time privacy check wrapper for the admin-settings PUT routes. * Centralizes the "fail-closed on cannot_verify, emit a distinct * message vs wrong_privacy" shape so each endpoint can stay one line. * * Skips the check entirely when Slack isn't configured (local dev * without ADDIE_BOT_TO
( res: Response, channelId: string, expected: 'private' | 'public', contextNoun: string, )
| 50 | * directly (and sends it) when the check fails. Caller just returns. |
| 51 | */ |
| 52 | async function requireChannelPrivacy( |
| 53 | res: Response, |
| 54 | channelId: string, |
| 55 | expected: 'private' | 'public', |
| 56 | contextNoun: string, |
| 57 | ): Promise<Response | null> { |
| 58 | if (!isSlackConfigured()) return null; |
| 59 | const check: ChannelPrivacyCheckResult = await verifyChannelPrivacyForWrite( |
| 60 | channelId, |
| 61 | expected, |
| 62 | ); |
| 63 | if (check.ok) return null; |
| 64 | if (check.reason === 'cannot_verify') { |
| 65 | return res.status(400).json({ |
| 66 | error: 'Could not verify channel', |
| 67 | message: `Could not verify the channel for ${contextNoun}. Invite @Addie to the channel in Slack and save again. If that doesn't work, an AAO engineer may need to re-grant the bot's channel permissions.`, |
| 68 | }); |
| 69 | } |
| 70 | // wrong_privacy |
| 71 | return res.status(400).json({ |
| 72 | error: 'Invalid channel', |
| 73 | message: |
| 74 | expected === 'private' |
| 75 | ? `Only private channels are allowed for ${contextNoun}` |
| 76 | : `Announcement channel must be public — announcements are meant for broad visibility`, |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | export function createAdminSettingsRouter(): Router { |
| 81 | const router = Router(); |
no test coverage detected