Let a conversation's menu show the routines that run in it
The header menu had Tasks but nothing for the schedules whose results land in that conversation. It now has Schedules, right under Tasks, opened in a modal that lists only what runs there: a bot's own chat holds the routines that run nowhere, a workspace only that bot's routines in it (`routineScopeSql`, tested against SQLite). The list loads when the modal opens, through `api/admin/bots/list-routines`, which checks the user chats in that conversation first, so the page props stay small. `grabCronJobs` gained an optional `workspace_id`: left out it still means everywhere. The routines page's row, form and edit modal moved to `src/components/general/`, and its subtitle to `src/utils/`, since both the page and the modal use them.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { Database } from "bun:sqlite";
|
||||
import { beforeAll, describe, expect, test } from "bun:test";
|
||||
import routineScopeSql from "./routine-scope-sql";
|
||||
import type { SqlCondition } from "./types";
|
||||
|
||||
const db = new Database(":memory:");
|
||||
|
||||
beforeAll(() => {
|
||||
db.run(`CREATE TABLE cron_jobs (id INTEGER PRIMARY KEY, bot_id INTEGER, workspace_id INTEGER)`);
|
||||
db.run(`INSERT INTO cron_jobs VALUES
|
||||
(1, 2, NULL),
|
||||
(2, 2, 10),
|
||||
(3, 2, 11),
|
||||
(4, 3, NULL),
|
||||
(5, 3, 10)`);
|
||||
});
|
||||
|
||||
function ids({ condition }: { condition: SqlCondition }) {
|
||||
return db
|
||||
.query(`SELECT c.id FROM cron_jobs c WHERE ${condition.sql} ORDER BY c.id`)
|
||||
.all(...condition.values)
|
||||
.map((row) => (row as { id: number }).id);
|
||||
}
|
||||
|
||||
describe("routineScopeSql", () => {
|
||||
test("a bot's own chat holds only the routines that run nowhere", () => {
|
||||
expect(ids({ condition: routineScopeSql({ bot_id: 2 }) })).toEqual([1]);
|
||||
expect(ids({ condition: routineScopeSql({ bot_id: 3, workspace_id: null }) })).toEqual([4]);
|
||||
});
|
||||
|
||||
test("a workspace holds only that bot's routines there", () => {
|
||||
expect(ids({ condition: routineScopeSql({ bot_id: 2, workspace_id: 10 }) })).toEqual([2]);
|
||||
expect(ids({ condition: routineScopeSql({ bot_id: 3, workspace_id: 10 }) })).toEqual([5]);
|
||||
});
|
||||
|
||||
test("another bot's routines in the same workspace are not this conversation's", () => {
|
||||
expect(ids({ condition: routineScopeSql({ bot_id: 2, workspace_id: 11 }) })).toEqual([3]);
|
||||
expect(ids({ condition: routineScopeSql({ bot_id: 9, workspace_id: 11 }) })).toEqual([]);
|
||||
});
|
||||
|
||||
test("a string id from a query string still matches", () => {
|
||||
expect(ids({ condition: routineScopeSql({ bot_id: "2", workspace_id: "10" }) })).toEqual([2]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import type { RoutineScopeSqlParams, SqlCondition } from "./types";
|
||||
|
||||
/**
|
||||
* The routines of one conversation: that bot's schedules in that workspace,
|
||||
* or, for its own chat, the ones that run nowhere (a routine made there
|
||||
* stays there even if the bot later joins a workspace). `workspace_id` is
|
||||
* NULL in SQLite rather than 0, so the two cases can't share a comparison.
|
||||
*/
|
||||
export default function routineScopeSql({
|
||||
bot_id,
|
||||
workspace_id,
|
||||
alias = "c",
|
||||
}: RoutineScopeSqlParams): SqlCondition {
|
||||
if (workspace_id) {
|
||||
return {
|
||||
sql: `(${alias}.bot_id = ? AND ${alias}.workspace_id = ?)`,
|
||||
values: [Number(bot_id), Number(workspace_id)],
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
sql: `(${alias}.bot_id = ? AND ${alias}.workspace_id IS NULL)`,
|
||||
values: [Number(bot_id)],
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import type { SqlCondition } from "@/src/utils/task-access/types";
|
||||
|
||||
/**
|
||||
* The conversation a routines list belongs to: a bot's own chat (no
|
||||
* workspace), or that bot's conversation in one workspace.
|
||||
*/
|
||||
export type RoutineConversationScope = {
|
||||
bot_id: number | string;
|
||||
/**
|
||||
* Null or undefined for the bot's own chat. A workspace id keeps only
|
||||
* the routines running there.
|
||||
*/
|
||||
workspace_id?: number | string | null;
|
||||
};
|
||||
|
||||
export type RoutineScopeSqlParams = RoutineConversationScope & {
|
||||
/**
|
||||
* The table alias the condition reads.
|
||||
*/
|
||||
alias?: string;
|
||||
};
|
||||
|
||||
export type { SqlCondition };
|
||||
Reference in New Issue
Block a user