diff --git a/src/slack/handlers/actions/checkin.ts b/src/slack/handlers/actions/checkin.ts index 94e482b..46e809e 100644 --- a/src/slack/handlers/actions/checkin.ts +++ b/src/slack/handlers/actions/checkin.ts @@ -1,6 +1,8 @@ +import { Blocks, Elements, Message } from 'slack-block-builder' import config from '~lib/config' import logger from '~lib/logger' import prisma from '~lib/prisma' +import { formatList } from '~slack/lib/messages' import { EventMiddleware } from '~slack/lib/types' export const handleAppMentioned: EventMiddleware<'app_mention'> = async ({ event, client }) => { @@ -11,23 +13,54 @@ export const handleAppMentioned: EventMiddleware<'app_mention'> = async ({ event name: 'stopwatch' }) const user = await client.auth.test() - const managers = await prisma.member.findMany({ where: { MemberCerts: { some: { Cert: { isManager: true } } } } }) - const copres_string = config.slack.users.copres.join(',') - for (const manager of managers) { - if (!manager.slack_id) { - logger.warn('No slack id for manager ' + manager.email) + + const departments = await prisma.department.findMany({ + select: { + name: true, + Certs: { + where: { + isManager: true + }, + select: { + Instances: { + select: { + Member: { + select: { + email: true, + slack_id: true + } + } + } + } + } + } + } + }) + + const dept_managers = departments.map((dept) => ({ + name: dept.name, + managers: dept.Certs.flatMap((cert) => cert.Instances.map((instance) => instance.Member.slack_id).filter((v) => v != null)) + })) + + for (const dept of dept_managers) { + if (dept.managers.length == 0) { + logger.warn('No manager slack ids for dept ' + dept.name) continue } const dm = await client.conversations.open({ - users: copres_string + ',' + manager.slack_id + users: [...config.slack.users.copres, ...dept.managers].join(',') }) if (dm.channel?.id == null) { - logger.warn('No group dm for manager ' + manager.email) + logger.warn('No group dm for dept ' + dept.name) continue } + const text = event.text.replace('<@' + user.user_id! + '>', formatList(dept.managers.map((id) => '<@' + id + '>'))) await client.chat.postMessage({ channel: dm.channel!.id!, - text: event.text.replace(user.user_id!, manager.slack_id) + text, + blocks: Message() + .blocks(Blocks.Section().text(text), Blocks.Context().elements('Copresident Checkin for ' + dept.name)) + .buildToObject().blocks }) } await client.reactions.remove({