diff --git a/functions/advance_rotation/handler.ts b/functions/advance_rotation/handler.ts index 9dc5ecc..9bff74b 100644 --- a/functions/advance_rotation/handler.ts +++ b/functions/advance_rotation/handler.ts @@ -1,6 +1,4 @@ import { SlackFunction } from "deno-slack-sdk/mod.ts"; -import { DatastoreItem } from "deno-slack-api/types.ts"; - import { AdvanceRotationFunctionDefinition } from "./definition.ts"; import RotationDatastore from "../../datastores/rotations.ts"; @@ -12,10 +10,7 @@ export default SlackFunction( // TODO: the `rotation` input is typed as any - what can we do to improve this? const { order: assigneeOrder } = inputs.rotation; const { - rotation_trigger_id, channel, - message, - start_time, repeats_every, repeats_every_number, last_advance_time, @@ -26,29 +21,21 @@ export default SlackFunction( const first = assigneeOrder.shift(); const assignees = [...assigneeOrder, first]; - // 2: generate a new rotation object - const newRotation: DatastoreItem = { - rotation_trigger_id, - channel, - message, - start_time, - repeats_every, - repeats_every_number, - order: assignees, - last_advance_time: next_advance_time, - next_advance_time: getNextAdvanceTimeInSec( - last_advance_time, - repeats_every_number, - repeats_every, - ), - }; - - // 3: update or create the rotation in the datastore - const putResponse = await client.apps.datastore.put< + // 2: update the rotation in the datastore + const putResponse = await client.apps.datastore.update< typeof RotationDatastore.definition >({ datastore: RotationDatastore.name, - item: newRotation, + item: { + channel, + order: assignees, + last_advance_time: next_advance_time, + next_advance_time: getNextAdvanceTimeInSec( + last_advance_time, + repeats_every_number, + repeats_every, + ), + }, }); if (!putResponse.ok) { diff --git a/functions/advance_rotation/handler_test.ts b/functions/advance_rotation/handler_test.ts index 252cbd3..773a648 100644 --- a/functions/advance_rotation/handler_test.ts +++ b/functions/advance_rotation/handler_test.ts @@ -11,9 +11,15 @@ const { createContext } = SlackFunctionTester( // Replaces globalThis.fetch with the mocked copy mf.install(); -mf.mock("POST@/api/apps.datastore.put", () => { +mf.mock("POST@/api/apps.datastore.update", async (args) => { + const body = await args.formData(); + const item = body.get("item") ?? "{}"; + const { order } = JSON.parse(item.toString()); + return new Response( - `{"ok": true, "item": {"repeats_every": "day"}}`, + `{"ok": true, "item": {"repeats_every": "day", "order": ${ + JSON.stringify(order) + } }}`, { status: 200, }, @@ -23,7 +29,7 @@ mf.mock("POST@/api/apps.datastore.put", () => { Deno.test("Sample function test", async () => { const inputs = { rotation: { - order: ["testuser1"], + order: ["testuser1", "realuser2", "fakeuser3"], channel: "channelid", message: "testmessage", start_time: 1, @@ -35,8 +41,10 @@ Deno.test("Sample function test", async () => { }; const { outputs } = await AdvanceRotationFunction(createContext({ inputs })); - await assertEquals( - outputs?.rotation.repeats_every, - "day", - ); + assertEquals(outputs?.rotation.repeats_every, "day"); + assertEquals(outputs?.rotation.order, [ + "realuser2", + "fakeuser3", + "testuser1", + ]); });