Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update records with datastore.update when advancing rotations #10

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
37 changes: 12 additions & 25 deletions functions/advance_rotation/handler.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand All @@ -26,29 +21,21 @@ export default SlackFunction(
const first = assigneeOrder.shift();
const assignees = [...assigneeOrder, first];

// 2: generate a new rotation object
const newRotation: DatastoreItem<typeof RotationDatastore.definition> = {
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) {
Expand Down
22 changes: 15 additions & 7 deletions functions/advance_rotation/handler_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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,
Expand All @@ -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",
]);
});
Loading