Skip to content

Commit

Permalink
feat: add manual fallback linking
Browse files Browse the repository at this point in the history
  • Loading branch information
rutmanz committed Sep 1, 2024
1 parent 264c4bc commit b4f387b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/routes/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ router.post('/members/fallback_photos', requireWriteAPI, async (c) => {
const { count } = await prisma.fallbackPhoto.createMany({ data: Object.entries(body).map(([k, v]) => ({ email: k.toLowerCase(), url: v })) })
const members = await prisma.member.findMany({ select: { email: true } })
for (const member of members) {
await prisma.member.update({ where: { email: member.email }, data: { fallback_photo: body[member.email] } })
await prisma.member.update({ where: { email: member.email }, data: { fallback_photo: body[member.email.toLowerCase()] } })
}
return c.text(`Updated ${count} fallback photos`)
})

function clockJson(c: Context, payload: APIClockResponse) {
return c.json(payload)
}
Expand Down
9 changes: 7 additions & 2 deletions src/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { updateSlackUsergroups } from '~tasks/slack_groups'
import { syncSlackMembers } from '~tasks/slack'
import { announceNewCerts, updateProfileCerts } from '~tasks/certs'
import { updateSheet } from '~spreadsheet'
import { syncFallbackPhotos } from './photos'

type TaskFunc = (reason: string) => Promise<void>
type Func = (() => void) | (() => Promise<void>)

const tasks: Record<string, TaskFunc> = {}

function scheduleTask(task: Func, interval_seconds: number, runOnInit: boolean, offset_seconds: number): TaskFunc {
function createTaskFunc(task: Func): TaskFunc {
const label = 'task/' + task.name
const cb = async (reason: string) => {
return async (reason: string) => {
try {
await task()
} catch (e) {
Expand All @@ -21,6 +22,9 @@ function scheduleTask(task: Func, interval_seconds: number, runOnInit: boolean,
logger.info({ name: label }, 'Task ran successfully')
return
}
}
function scheduleTask(task: Func, interval_seconds: number, runOnInit: boolean, offset_seconds: number): TaskFunc {
const cb = createTaskFunc(task)
if (runOnInit) {
setTimeout(() => {
cb('initial run')
Expand All @@ -43,6 +47,7 @@ export function scheduleTasks() {
tasks['Announce Certs'] = scheduleTask(announceNewCerts, 60 * 60, isProd, 60) // Just in case the cert announcement isn't automatically run on changes
tasks['Sync Usergroups'] = scheduleTask(updateSlackUsergroups, 60 * 60, isProd, 2 * 60)
tasks['Update Profile Certs'] = scheduleTask(updateProfileCerts, 60 * 60 * 24, isProd, 5 * 60)
tasks['Link Fallback Photos'] = createTaskFunc(syncFallbackPhotos)
}

export async function runTask(key: string) {
Expand Down
15 changes: 15 additions & 0 deletions src/tasks/photos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import prisma from '~lib/prisma'
import logger from '~lib/logger'

export async function syncFallbackPhotos() {
const members = await prisma.member.findMany({ select: { email: true } })

for (const member of members) {
const fallbackPhoto = await prisma.fallbackPhoto.findUnique({ where: { email: member.email } })
if (!fallbackPhoto) {
logger.warn('Could not find photo for ' + member.email)
} else {
await prisma.member.update({ where: { email: member.email }, data: { fallback_photo: fallbackPhoto?.url } })
}
}
}

0 comments on commit b4f387b

Please sign in to comment.