Added scheduled end of shift message & cleaned up tv mode layout
This commit is contained in:
@@ -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;
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user