Added scheduled end of shift message & cleaned up tv mode layout

This commit is contained in:
2025-09-11 12:27:21 -05:00
parent 52be5c93f4
commit 136047ca25
8 changed files with 124 additions and 33 deletions

View File

@@ -3,9 +3,12 @@ import { getAuthUserId } from '@convex-dev/auth/server';
import {
type MutationCtx,
type QueryCtx,
action,
internalMutation,
mutation,
query,
} from './_generated/server';
import { api } from './_generated/api';
import type { Doc, Id } from './_generated/dataModel';
import { paginationOptsValidator } from 'convex/server';
@@ -135,6 +138,29 @@ export const bulkCreate = mutation({
},
});
/**
* Update all statuses for all users.
*/
export const updateAllStatuses = mutation({
args: { message: v.string() },
handler: async (ctx, args) => {
const userIds = await ctx.runQuery(api.auth.getAllUserIds);
const updatedAt = Date.now();
const statusIds: Id<'statuses'>[] = [];
for (const userId of userIds) {
await ensureUser(ctx, userId);
const statusId = await ctx.db.insert('statuses', {
message: args.message,
userId,
updatedAt,
});
await ctx.db.patch(userId, { currentStatusId: statusId });
statusIds.push(statusId);
}
return { statusIds };
},
});
/**
* Current status for a specific user.
* - Uses users.currentStatusId if present,
@@ -199,7 +225,7 @@ export const getCurrentForAll = query({
// Updated by (if different) + URL
let updatedByUser: StatusRow['user'] | null = null;
if (curStatus && curStatus.updatedBy !== u._id) {
if (curStatus && curStatus.updatedBy && curStatus.updatedBy !== u._id) {
const updater = await ctx.db.get(curStatus.updatedBy);
if (!updater) throw new ConvexError('Updater not found.');
const updaterImageId = getImageId(updater);
@@ -286,7 +312,9 @@ export const listHistory = query({
for (const s of result.page) {
const owner = await getDisplay(s.userId);
const updatedBy =
s.updatedBy !== s.userId ? await getDisplay(s.updatedBy) : null;
(s.updatedBy && s.updatedBy !== s.userId)
? await getDisplay(s.updatedBy)
: null;
statuses.push({
user: owner,
@@ -309,3 +337,25 @@ export const listHistory = query({
};
},
});
export const endOfShiftUpdate = action({
handler: async (ctx) => {
const now = new Date(
new Date().toLocaleString('en-US', {
timeZone: 'America/Chicago',
}),
);
const day = now.getDay();
const hour = now.getHours();
const minute = now.getMinutes();
if (day == 0 || day === 6) return;
if (hour === 12) {
await ctx.runMutation(api.statuses.updateAllStatuses, {
message: 'End of shift',
});
} else if (hour === 11) {
const ms = ((60-minute) % 60) * 60 * 1000;
await ctx.scheduler.runAfter(ms, api.statuses.endOfShiftUpdate);
} else return;
}
});