Cleanup status object

This commit is contained in:
2025-09-05 11:52:11 -05:00
parent 1a9871ac5d
commit 7cfc11d30c
11 changed files with 700 additions and 38 deletions

View File

@@ -16,21 +16,14 @@ type StatusRow = {
user: {
id: Id<'users'>;
name: string | null;
imageId: Id<'_storage'> | null;
imageUrl: string | null;
};
latest: {
},
status: {
id: Id<'statuses'>;
message: string;
updatedAt: number;
updatedBy: Id<'users'>;
} | null;
updatedByUser: {
id: Id<'users'>;
name: string | null;
imageId: Id<'_storage'> | null;
imageUrl: string | null;
} | null;
updatedBy: StatusRow['user'] | null;
} | null,
};
// CHANGED: typed helpers
@@ -154,6 +147,7 @@ export const getCurrentForUser = query({
return await latestStatusForOwner(ctx, userId);
},
});
const getName = (u: Doc<'users'>): string | null =>
'name' in u && typeof u.name === 'string' ? u.name : null;
@@ -176,17 +170,17 @@ export const getCurrentForAll = query({
return await Promise.all(
users.map(async (u) => {
// Resolve user's current or latest status
let status: Doc<'statuses'> | null = null;
let curStatus: Doc<'statuses'> | null = null;
if ('currentStatusId' in u && u.currentStatusId) {
status = await ctx.db.get(u.currentStatusId);
curStatus = await ctx.db.get(u.currentStatusId);
}
if (!status) {
if (!curStatus) {
const [latest] = await ctx.db
.query('statuses')
.withIndex('by_user_updatedAt', (q) => q.eq('userId', u._id))
.order('desc')
.take(1);
status = latest ?? null;
curStatus = latest ?? null;
}
// User display + URL
@@ -196,29 +190,27 @@ export const getCurrentForAll = query({
: null;
// Updated by (if different) + URL
let updatedByUser: StatusRow['updatedByUser'] | null = null;
if (status && status.updatedBy !== u._id) {
const updater = await ctx.db.get(status.updatedBy);
let updatedByUser: StatusRow['user'] | null = null;
if (curStatus && curStatus.updatedBy !== u._id) {
const updater = await ctx.db.get(curStatus.updatedBy);
if (!updater) throw new ConvexError('Updater not found.');
const updaterImageId = getImageId(updater);
const updaterImageUrl = updaterImageId
? await ctx.storage.getUrl(updaterImageId)
: null;
updatedByUser = {
id: updater._id,
name: getName(updater),
imageId: updaterImageId,
imageUrl: updaterImageUrl,
};
}
const latest: StatusRow['latest'] = status
const status: StatusRow['status'] = curStatus
? {
id: status._id,
message: status.message,
updatedAt: status.updatedAt,
updatedBy: status.updatedBy,
id: curStatus._id,
message: curStatus.message,
updatedAt: curStatus.updatedAt,
updatedBy: updatedByUser,
}
: null;
@@ -226,11 +218,9 @@ export const getCurrentForAll = query({
user: {
id: u._id,
name: getName(u),
imageId: userImageId,
imageUrl: userImageUrl,
},
latest,
updatedByUser,
status,
};
}),
);