Skip to content

Commit

Permalink
feat: add periodic department syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
rutmanz committed Oct 28, 2024
1 parent 4b23a8d commit 3226d0f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/slack/handlers/actions/departments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 19 additions & 1 deletion src/tasks/slack_groups.ts → src/tasks/departments.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)))
}
}
}
13 changes: 10 additions & 3 deletions src/tasks/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 3226d0f

Please sign in to comment.