From 3226d0ff089fc0fb482548c533b046f4409a3aa6 Mon Sep 17 00:00:00 2001 From: Zach R Date: Sun, 27 Oct 2024 18:36:56 -0700 Subject: [PATCH] feat: add periodic department syncing --- src/slack/handlers/actions/departments.ts | 2 +- src/tasks/{slack_groups.ts => departments.ts} | 20 ++++++++++++++++++- src/tasks/index.ts | 13 +++++++++--- 3 files changed, 30 insertions(+), 5 deletions(-) rename src/tasks/{slack_groups.ts => departments.ts} (86%) diff --git a/src/slack/handlers/actions/departments.ts b/src/slack/handlers/actions/departments.ts index 4465d7b..ce25cbd 100644 --- a/src/slack/handlers/actions/departments.ts +++ b/src/slack/handlers/actions/departments.ts @@ -6,7 +6,7 @@ import prisma from '~lib/prisma' import logger from '~lib/logger' import { setProfileAttribute } from '~slack/lib/profile' import { formatList } from '~slack/lib/messages' -import { scheduleUpdateSlackUsergroups } from '~tasks/slack_groups' +import { scheduleUpdateSlackUsergroups } from '~tasks/departments' export const handleDepartmentsCommand: CommandMiddleware = async ({ ack, body, client }) => { await ack() diff --git a/src/tasks/slack_groups.ts b/src/tasks/departments.ts similarity index 86% rename from src/tasks/slack_groups.ts rename to src/tasks/departments.ts index 0c62451..80e9dc3 100644 --- a/src/tasks/slack_groups.ts +++ b/src/tasks/departments.ts @@ -1,8 +1,9 @@ import prisma from '~lib/prisma' import logger from '~lib/logger' -import { profile_client } from '~slack/lib/profile' +import { profile_client, setProfileAttribute } from '~slack/lib/profile' import { slack_client } from '~slack' import { getManagers } from '~lib/cert_operations' +import { formatList } from '~slack/lib/messages' let timeout: NodeJS.Timeout @@ -110,3 +111,20 @@ export async function updateSlackUsergroups() { } } } + +export async function updateProfileDepartments() { + const members = await prisma.member.findMany({ + where: { slack_id: { not: null } }, + select: { + slack_id: true, + Departments: { + select: { Department: { select: { name: true } } } + } + } + }) + for (const member of members) { + if (member.slack_id) { + await setProfileAttribute(member.slack_id, 'department', formatList(member.Departments.map((dept) => dept.Department.name))) + } + } +} diff --git a/src/tasks/index.ts b/src/tasks/index.ts index ab5b21d..62cd52e 100644 --- a/src/tasks/index.ts +++ b/src/tasks/index.ts @@ -1,5 +1,5 @@ import logger from '~lib/logger' -import { updateSlackUsergroups } from '~tasks/slack_groups' +import { updateProfileDepartments, updateSlackUsergroups } from '~tasks/departments' import { syncSlackMembers } from '~tasks/slack' import { announceNewCerts, updateProfileCerts } from '~tasks/certs' import { updateSheet } from '~spreadsheet' @@ -47,23 +47,30 @@ function scheduleCronTask(task: TaskFunc, cron_exp: string) { return task } +async function updateProfileFields() { + await updateProfileCerts() + await updateProfileDepartments() +} export function scheduleTasks() { // Offset is to combat Slack's rate limits const isProd = process.env.NODE_ENV === 'prod' - tasks['Sync Sheet'] = scheduleTask(updateSheet, 60 * 5, isProd, 0) + tasks['Sync Profiles'] = createTaskFunc(updateProfileFields) + tasks['Announce Certs'] = scheduleTask(announceNewCerts, 60 * 60, isProd, 60) // Just in case the cert announcement isn't automatically run on changes if (isProd) { // This task affects workspace-wide groups, should not be run while testing if in the same workspace - tasks['Sync Usergroups'] = scheduleTask(updateSlackUsergroups, 60 * 60, isProd, 2 * 60) + tasks['Sync Departments'] = scheduleTask(updateSlackUsergroups, 60 * 60, isProd, 2 * 60) } tasks['Link Fallback Photos'] = createTaskFunc(syncFallbackPhotos) tasks['Logout All'] = scheduleCronTask(createTaskFunc(logoutAll), '0 0 * * *') // Slack is silly and can only handle 5 items in the overflow menu scheduleCronTask(createTaskFunc(promptCheckinMessage), '0 19 * * FRI') + scheduleTask(updateSheet, 60 * 5, isProd, 0) scheduleTask(syncSlackMembers, 60 * 60, isProd, 0) // can be run from the admin members page scheduleTask(updateProfileCerts, 60 * 60 * 24, isProd, 5 * 60) + scheduleTask(updateProfileDepartments, 60 * 60 * 24, isProd, 10 * 60) } export async function runTask(key: string) {