From 320a6fdb8782e08f58a5ba7dcb759ecf158daae1 Mon Sep 17 00:00:00 2001 From: spham21 Date: Thu, 5 Mar 2026 15:49:41 -0800 Subject: [PATCH 1/2] feature: slack integration --- .../dashboard/DashboardAnnouncementModal.tsx | 1 + .../project/events/AnnouncementForm.tsx | 54 ++++++- src/types/events.ts | 2 +- src/types/project.ts | 1 + supabase/.temp/cli-latest | 1 + .../functions/slack-announcement/index.ts | 136 ++++++++++++++++++ tsconfig.json | 3 +- 7 files changed, 193 insertions(+), 5 deletions(-) create mode 100644 supabase/.temp/cli-latest create mode 100644 supabase/functions/slack-announcement/index.ts diff --git a/src/components/portal/dashboard/DashboardAnnouncementModal.tsx b/src/components/portal/dashboard/DashboardAnnouncementModal.tsx index f98d6b77..1d602898 100644 --- a/src/components/portal/dashboard/DashboardAnnouncementModal.tsx +++ b/src/components/portal/dashboard/DashboardAnnouncementModal.tsx @@ -33,6 +33,7 @@ const BOARD_TEAMS: { value: BoardTeam; label: string }[] = [ { value: 'finance', label: 'Finance' }, { value: 'marketing', label: 'Marketing' }, { value: 'design', label: 'Design' }, + { value: 'project_managers', label: 'Project Managers' }, ]; export function DashboardAnnouncementModal({ diff --git a/src/components/portal/project/events/AnnouncementForm.tsx b/src/components/portal/project/events/AnnouncementForm.tsx index 13657e0b..01071f54 100644 --- a/src/components/portal/project/events/AnnouncementForm.tsx +++ b/src/components/portal/project/events/AnnouncementForm.tsx @@ -1,7 +1,7 @@ "use client"; import React, { useState } from 'react'; -import { CreateAnnouncementInput } from '@/types/events'; +import { CreateAnnouncementInput, AnnouncementVisibility, BoardTeam } from '@/types/events'; interface AnnouncementFormProps { projectId: string; @@ -25,6 +25,21 @@ const PRIORITY_OPTIONS = [ { value: 'urgent', label: 'Urgent' }, ]; +const VISIBILITY_OPTIONS = [ + { value: 'project', label: 'Project Only' }, + { value: 'team', label: 'Specific Team' }, + { value: 'board', label: 'Board Members' }, + { value: 'club_wide', label: 'Entire Club' }, +]; + +const TEAM_OPTIONS = [ + { value: 'tech', label: 'Tech' }, + { value: 'finance', label: 'Finance' }, + { value: 'marketing', label: 'Marketing' }, + { value: 'design', label: 'Design' }, + { value: 'project_managers', label: 'Project Managers' }, +]; + export function AnnouncementForm({ projectId, onSubmit, @@ -40,6 +55,8 @@ export function AnnouncementForm({ const [linkUrl, setLinkUrl] = useState(''); const [linkText, setLinkText] = useState(''); const [expireDate, setExpireDate] = useState(''); + const [visibility, setVisibility] = useState('project'); + const [targetTeam, setTargetTeam] = useState(''); const [errors, setErrors] = useState>({}); const validate = (): boolean => { @@ -68,8 +85,8 @@ export function AnnouncementForm({ publish_date: new Date().toISOString(), expire_date: expireDate ? new Date(expireDate).toISOString() : null, is_active: true, - visibility: 'project', - target_team: null, + visibility, + target_team: visibility === 'team' ? (targetTeam as BoardTeam) : null, }; await onSubmit(input); @@ -131,6 +148,37 @@ export function AnnouncementForm({ +
+
+ + +
+ {visibility === 'team' && ( +
+ + +
+ )} +
+
= { + 'club_wide': 'C042EJJE33K', + 'board': 'C042EJJE33K', +}; + +const TEAM_CHANNELS: Record = { + 'tech': 'C07GEAH55EX', + 'finance': 'C07GT3VRNDP', + 'marketing': 'C07GBGY0AH3', + 'design': 'C07GT3VMEHF', + 'project_managers': 'C07GBGXEXMK' +}; + +serve(async (req) => { + try { + const payload = await req.json(); + const { record, type } = payload; + + // Only process new announcements + if (type !== 'INSERT') { + return new Response(JSON.stringify({ message: 'Ignore non-insert event' }), { status: 200 }); + } + + const { + title, + description, + visibility, + project_id, + target_team, + link_url, + priority + } = record; + + let channelId = ''; + + // Determine the correct channel based on visibility + if (visibility === 'project' && project_id) { + // Fetch project's slack channel from the projects table + const { data: project, error: projectError } = await supabase + .from('projects') + .select('slack_channel_id, projectName') + .eq('id', project_id) + .single(); + + if (projectError) { + console.error('Error fetching project slack channel:', projectError); + } else { + channelId = project?.slack_channel_id; + console.log(`Found project channel: ${channelId} for project: ${project?.projectName}`); + } + } else if (visibility === 'team' && target_team) { + channelId = TEAM_CHANNELS[target_team]; + console.log(`Selected team channel: ${channelId} for team: ${target_team}`); + } else { + channelId = VISIBILITY_MAPPING[visibility]; + console.log(`Selected visibility channel: ${channelId} for visibility: ${visibility}`); + } + + if (!channelId) { + console.warn(`No Slack channel mapping found for: visibility=${visibility}, team=${target_team}, project=${project_id}`); + return new Response(JSON.stringify({ error: 'No channel mapping' }), { status: 200 }); + } + + // Ping @channel for high or urgent priority announcements + const shouldPing = priority === 'high' || priority === 'urgent'; + const slackDescription = shouldPing ? `\n${description}` : description; + + // Prepare Slack Message + const blocks = [ + { + type: "header", + text: { + type: "plain_text", + text: `📢 ${title}`, + emoji: true + } + }, + { + type: "section", + text: { + type: "mrkdwn", + text: description + } + } + ]; + + if (link_url) { + blocks.push({ + type: "section", + text: { + type: "mrkdwn", + text: `<${link_url}|View more details>` + } + }); + } + + // Send to Slack via chat.postMessage + const slackRes = await fetch('https://slack.com/api/chat.postMessage', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${SLACK_BOT_TOKEN}` + }, + body: JSON.stringify({ + channel: channelId, + blocks: blocks, + text: `New Announcement: ${title}` // Fallback text + }) + }); + + const slackResult = await slackRes.json(); + if (!slackResult.ok) { + console.error('Slack API error:', slackResult.error); + return new Response(JSON.stringify({ error: slackResult.error }), { status: 500 }); + } + + console.log(`Successfully posted announcement to Slack channel: ${channelId}`); + return new Response(JSON.stringify({ ok: true }), { status: 200 }); + + } catch (err: any) { + console.error('Function error:', err); + return new Response(JSON.stringify({ error: err.message }), { status: 500 }); + } +}) diff --git a/tsconfig.json b/tsconfig.json index 4b1e52a5..abf6b10c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -37,6 +37,7 @@ ".next/dev/types/**/*.ts" ], "exclude": [ - "node_modules" + "node_modules", + "supabase" ] } From 0c008b734fc5c8c6e43f822eccd568d59fae9b8a Mon Sep 17 00:00:00 2001 From: spham21 Date: Thu, 5 Mar 2026 16:01:14 -0800 Subject: [PATCH 2/2] feature: update announcement priority --- .../portal/dashboard/DashboardAnnouncementModal.tsx | 2 -- src/components/portal/project/events/AnnouncementForm.tsx | 2 -- supabase/functions/slack-announcement/index.ts | 4 ++-- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/components/portal/dashboard/DashboardAnnouncementModal.tsx b/src/components/portal/dashboard/DashboardAnnouncementModal.tsx index 1d602898..4cd0df33 100644 --- a/src/components/portal/dashboard/DashboardAnnouncementModal.tsx +++ b/src/components/portal/dashboard/DashboardAnnouncementModal.tsx @@ -22,10 +22,8 @@ const ANNOUNCEMENT_TYPES = [ ]; const PRIORITY_OPTIONS = [ - { value: 'low', label: 'Low' }, { value: 'normal', label: 'Normal' }, { value: 'high', label: 'High' }, - { value: 'urgent', label: 'Urgent' }, ]; const BOARD_TEAMS: { value: BoardTeam; label: string }[] = [ diff --git a/src/components/portal/project/events/AnnouncementForm.tsx b/src/components/portal/project/events/AnnouncementForm.tsx index 01071f54..e0a7d7fc 100644 --- a/src/components/portal/project/events/AnnouncementForm.tsx +++ b/src/components/portal/project/events/AnnouncementForm.tsx @@ -19,10 +19,8 @@ const ANNOUNCEMENT_TYPES = [ ]; const PRIORITY_OPTIONS = [ - { value: 'low', label: 'Low' }, { value: 'normal', label: 'Normal' }, { value: 'high', label: 'High' }, - { value: 'urgent', label: 'Urgent' }, ]; const VISIBILITY_OPTIONS = [ diff --git a/supabase/functions/slack-announcement/index.ts b/supabase/functions/slack-announcement/index.ts index a956a5b0..9824f255 100644 --- a/supabase/functions/slack-announcement/index.ts +++ b/supabase/functions/slack-announcement/index.ts @@ -73,8 +73,8 @@ serve(async (req) => { return new Response(JSON.stringify({ error: 'No channel mapping' }), { status: 200 }); } - // Ping @channel for high or urgent priority announcements - const shouldPing = priority === 'high' || priority === 'urgent'; + // Ping @channel for high priority announcements + const shouldPing = priority === 'high'; const slackDescription = shouldPing ? `\n${description}` : description; // Prepare Slack Message