From 9a7682cd1059aef7a01fcd61ebcfc6e46ddf08bf Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 9 Dec 2025 06:43:00 +0000 Subject: [PATCH] Fix: Improve user avatar, infusion logging, and user deletion Co-authored-by: michaelwschultz --- components/header.tsx | 8 ++++- lib/admin-db/infusions.ts | 63 ++++++++++++++++++++++++++++++-------- lib/db/users.ts | 4 +-- pages/api/log-treatment.ts | 2 +- 4 files changed, 61 insertions(+), 16 deletions(-) diff --git a/components/header.tsx b/components/header.tsx index 724b868..2c41bb0 100644 --- a/components/header.tsx +++ b/components/header.tsx @@ -59,6 +59,12 @@ const Header = (props: Props): JSX.Element => { ) if (user) { + const avatarInitial = + user.name?.charAt(0) || + user.displayName?.charAt(0) || + user.email?.charAt(0) || + '?' + return ( <> @@ -83,7 +89,7 @@ const Header = (props: Props): JSX.Element => { diff --git a/lib/admin-db/infusions.ts b/lib/admin-db/infusions.ts index 3493fb8..3c44afc 100644 --- a/lib/admin-db/infusions.ts +++ b/lib/admin-db/infusions.ts @@ -1,7 +1,11 @@ import { adminFirestore } from 'lib/firebase-admin' import { compareDesc, parseISO } from 'date-fns' -import type { TreatmentType } from '../db/infusions' +import { + TreatmentTypeEnum, + type TreatmentType, +} from '../db/infusions' +import type { AttachedUserType } from 'lib/types/users' async function getAllInfusions() { const snapshot = await adminFirestore.collection('infusions').get() @@ -118,7 +122,7 @@ async function getRecentUserInfusionsByApiKey( async function postInfusionByApiKey( apiKey: string, - lastInfusion: TreatmentType, + lastInfusion: TreatmentType | null, newInfusion: Partial ) { try { @@ -134,19 +138,54 @@ async function postInfusionByApiKey( ) } - // Make room for new values - Object.assign(lastInfusion, { - createdAt: new Date().toISOString(), - cause: '', - sites: '', - }) + const userDoc = userSnapshot.docs[0] + const baseUser: AttachedUserType = { + email: userDoc.data().email || '', + name: userDoc.data().name || userDoc.data().displayName || '', + photoUrl: userDoc.data().photoUrl || '', + uid: userDoc.data().uid || userDoc.id, + } + + const now = new Date().toISOString() + const baseInfusion: TreatmentType = lastInfusion + ? { ...lastInfusion } + : { + cause: '', + createdAt: now, + date: newInfusion.date || now.slice(0, 10), + deletedAt: null, + medication: { + brand: newInfusion.medication?.brand || '', + costPerUnit: newInfusion.medication?.costPerUnit, + lot: newInfusion.medication?.lot, + units: newInfusion.medication?.units || 0, + }, + sites: '', + type: newInfusion.type || TreatmentTypeEnum.PROPHY, + user: (newInfusion.user as AttachedUserType) || baseUser, + } + + const sanitizedBase = { + ...baseInfusion, + createdAt: now, + cause: baseInfusion.cause || '', + sites: baseInfusion.sites || '', + deletedAt: null, + user: baseInfusion.user || baseUser, + } - delete lastInfusion.uid + delete (sanitizedBase as Partial).uid - // Keep data from last infusion and replace old with new data - const infusion = { - ...lastInfusion, + const infusion: TreatmentType = { + ...sanitizedBase, ...newInfusion, + user: (newInfusion.user as AttachedUserType) || sanitizedBase.user, + medication: { + ...sanitizedBase.medication, + ...(newInfusion.medication || {}), + }, + createdAt: now, + deletedAt: null, } await adminFirestore diff --git a/lib/db/users.ts b/lib/db/users.ts index 9e68716..61b1ad9 100644 --- a/lib/db/users.ts +++ b/lib/db/users.ts @@ -12,10 +12,10 @@ async function createUser(uid: string, data: any) { } async function deleteUser(uid: string) { - firestore.collection('users').doc(uid).delete() + await firestore.collection('users').doc(uid).delete() const snapshot = await firestore .collection('infusions') - .where('userId', '==', uid) + .where('user.uid', '==', uid) .get() const batch = firestore.batch() diff --git a/pages/api/log-treatment.ts b/pages/api/log-treatment.ts index e6e091c..a5838e4 100644 --- a/pages/api/log-treatment.ts +++ b/pages/api/log-treatment.ts @@ -25,7 +25,7 @@ const logTreatment = async (req: NextApiRequest, res: NextApiResponse) => { if (error) throw error - const mostRecentInfusion = infusions[0] + const mostRecentInfusion = infusions && infusions.length > 0 ? infusions[0] : null try { const { infusion, error } = await postInfusionByApiKey(