Added persistent statuses.
This commit is contained in:
@@ -24,6 +24,7 @@ type StatusRow = {
|
||||
message: string;
|
||||
updatedAt: number;
|
||||
updatedBy: StatusRow['user'] | null;
|
||||
persistentStatus: boolean;
|
||||
} | null;
|
||||
};
|
||||
|
||||
@@ -56,6 +57,7 @@ export const create = mutation({
|
||||
message: v.string(),
|
||||
userId: v.optional(v.id('users')),
|
||||
updatedBy: v.optional(v.id('users')),
|
||||
persistentStatus: v.optional(v.boolean()),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const authUserId: Id<'users'> | null = await getAuthUserId(ctx);
|
||||
@@ -68,6 +70,7 @@ export const create = mutation({
|
||||
}
|
||||
const userId = args.userId ?? authUserId!;
|
||||
const updatedBy = args.updatedBy ?? authUserId;
|
||||
const persistentStatus = args.persistentStatus ?? false;
|
||||
await ensureUser(ctx, userId);
|
||||
let statusId: Id<'statuses'>;
|
||||
if (updatedBy) {
|
||||
@@ -77,12 +80,14 @@ export const create = mutation({
|
||||
userId,
|
||||
updatedBy,
|
||||
updatedAt: Date.now(),
|
||||
persistentStatus,
|
||||
});
|
||||
} else {
|
||||
statusId = await ctx.db.insert('statuses', {
|
||||
message,
|
||||
userId,
|
||||
updatedAt: Date.now(),
|
||||
persistentStatus,
|
||||
});
|
||||
}
|
||||
await ctx.db.patch(userId, { currentStatusId: statusId });
|
||||
@@ -95,6 +100,7 @@ export const bulkCreate = mutation({
|
||||
message: v.string(),
|
||||
userIds: v.array(v.id('users')),
|
||||
updatedBy: v.optional(v.id('users')),
|
||||
persistentStatus: v.optional(v.boolean()),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const authUserId = await getAuthUserId(ctx);
|
||||
@@ -103,6 +109,7 @@ export const bulkCreate = mutation({
|
||||
if (args.userIds.length === 0) return { statusIds: [] };
|
||||
|
||||
const updatedBy = args.updatedBy ?? authUserId;
|
||||
const persistentStatus = args.persistentStatus ?? false;
|
||||
await ensureUser(ctx, updatedBy);
|
||||
|
||||
const message = args.message.trim();
|
||||
@@ -120,6 +127,7 @@ export const bulkCreate = mutation({
|
||||
userId,
|
||||
updatedBy,
|
||||
updatedAt: now,
|
||||
persistentStatus,
|
||||
});
|
||||
await ctx.db.patch(userId, { currentStatusId: statusId });
|
||||
statusIds.push(statusId);
|
||||
@@ -130,10 +138,14 @@ export const bulkCreate = mutation({
|
||||
});
|
||||
|
||||
export const updateAllStatuses = mutation({
|
||||
args: { message: v.string() },
|
||||
args: {
|
||||
message: v.string(),
|
||||
persistentStatus: v.optional(v.boolean())
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
const users = await ctx.db.query('users').collect();
|
||||
const message = args.message.trim();
|
||||
const persistentStatus = args.persistentStatus ?? false;
|
||||
if (message.length === 0) {
|
||||
throw new ConvexError('Message cannot be empty.');
|
||||
}
|
||||
@@ -142,15 +154,20 @@ export const updateAllStatuses = mutation({
|
||||
const now = Date.now();
|
||||
|
||||
for (const user of users) {
|
||||
const statusId = await ctx.db.insert('statuses', {
|
||||
message,
|
||||
const curStatus = await ctx.runQuery(api.statuses.getCurrentForUser, {
|
||||
userId: user._id,
|
||||
updatedAt: now,
|
||||
});
|
||||
await ctx.db.patch(user._id, { currentStatusId: statusId });
|
||||
statusIds.push(statusId);
|
||||
if (!curStatus?.persistentStatus) {
|
||||
const statusId = await ctx.db.insert('statuses', {
|
||||
message,
|
||||
userId: user._id,
|
||||
updatedAt: now,
|
||||
persistentStatus,
|
||||
});
|
||||
await ctx.db.patch(user._id, { currentStatusId: statusId });
|
||||
statusIds.push(statusId);
|
||||
}
|
||||
}
|
||||
|
||||
return { statusIds };
|
||||
},
|
||||
});
|
||||
@@ -161,6 +178,12 @@ export const createLunchStatus = mutation({
|
||||
const authUserId = await getAuthUserId(ctx);
|
||||
const lunchUserId = args.userId ?? authUserId
|
||||
if (!lunchUserId) throw new ConvexError('Not authenticated.');
|
||||
const curStatus = await ctx.runQuery(api.statuses.getCurrentForUser, {
|
||||
userId: lunchUserId,
|
||||
});
|
||||
if (curStatus?.persistentStatus) {
|
||||
return { success: false, error: 'Current status is persistent.'};
|
||||
}
|
||||
await ctx.runMutation(api.statuses.create, {
|
||||
message: 'At lunch',
|
||||
userId: lunchUserId,
|
||||
@@ -179,6 +202,12 @@ export const backFromLunchStatus = mutation({
|
||||
const authUserId = await getAuthUserId(ctx);
|
||||
const lunchUserId = args.userId ?? authUserId
|
||||
if (!lunchUserId) throw new ConvexError('Not authenticated.');
|
||||
const curStatus = await ctx.runQuery(api.statuses.getCurrentForUser, {
|
||||
userId: lunchUserId,
|
||||
});
|
||||
if (curStatus?.persistentStatus) {
|
||||
return { success: false, error: 'Current status is persistent.'};
|
||||
}
|
||||
const user = await ensureUser(ctx, lunchUserId);
|
||||
if (!user.currentStatusId) throw new ConvexError('User has no current status.');
|
||||
const currentStatus = await ctx.db.get(user.currentStatusId);
|
||||
@@ -253,6 +282,7 @@ export const getCurrentForAll = query({
|
||||
message: curStatus.message,
|
||||
updatedAt: curStatus.updatedAt,
|
||||
updatedBy: updatedByUser,
|
||||
persistentStatus: curStatus.persistentStatus ?? false,
|
||||
}
|
||||
: null;
|
||||
return {
|
||||
@@ -318,6 +348,7 @@ export const listHistory = query({
|
||||
message: s.message,
|
||||
updatedAt: s.updatedAt,
|
||||
updatedBy,
|
||||
persistentStatus: s.persistentStatus ?? false,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user