From b4f387b011712289b2ec1a05168a48f9aaff3ae7 Mon Sep 17 00:00:00 2001 From: Zach R Date: Sun, 1 Sep 2024 14:23:47 -0700 Subject: [PATCH] feat: add manual fallback linking --- src/routes/api/index.ts | 3 ++- src/tasks/index.ts | 9 +++++++-- src/tasks/photos.ts | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 src/tasks/photos.ts diff --git a/src/routes/api/index.ts b/src/routes/api/index.ts index 60b0038..18f9de4 100644 --- a/src/routes/api/index.ts +++ b/src/routes/api/index.ts @@ -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) } diff --git a/src/tasks/index.ts b/src/tasks/index.ts index 127042f..ce8b30e 100644 --- a/src/tasks/index.ts +++ b/src/tasks/index.ts @@ -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 type Func = (() => void) | (() => Promise) const tasks: Record = {} -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) { @@ -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') @@ -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) { diff --git a/src/tasks/photos.ts b/src/tasks/photos.ts new file mode 100644 index 0000000..451d94e --- /dev/null +++ b/src/tasks/photos.ts @@ -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 } }) + } + } +}