a fun bot for the hc slack
1import { db } from "../../../libs/db";
2import { takes as takesTable } from "../../../libs/schema";
3import { eq } from "drizzle-orm";
4import TakesConfig from "../../../libs/config";
5import { getActiveTake } from "../services/database";
6import type { MessageResponse } from "../types";
7import { generateSlackDate, prettyPrintTime } from "../../../libs/time";
8import {
9 addNewPeriod,
10 getPausedTimeRemaining,
11 getRemainingTime,
12} from "../../../libs/time-periods";
13
14export default async function handlePause(
15 userId: string,
16): Promise<MessageResponse | undefined> {
17 const activeTake = await getActiveTake(userId);
18 if (activeTake.length === 0) {
19 return {
20 text: `You don't have an active takes session! Use \`/takes start\` to begin.`,
21 response_type: "ephemeral",
22 };
23 }
24
25 const takeToUpdate = activeTake[0];
26 if (!takeToUpdate) {
27 return;
28 }
29
30 const newPeriods = JSON.stringify(
31 addNewPeriod(takeToUpdate.periods, "paused"),
32 );
33
34 const pausedTime = getPausedTimeRemaining(newPeriods);
35 const endTime = getRemainingTime(
36 takeToUpdate.targetDurationMs,
37 takeToUpdate.periods,
38 );
39
40 if (pausedTime > TakesConfig.MAX_PAUSE_DURATION * 60000) {
41 return {
42 text: `You can't pause for more than ${TakesConfig.MAX_PAUSE_DURATION} minutes!`,
43 response_type: "ephemeral",
44 };
45 }
46
47 // Update the takes entry to paused status
48 await db
49 .update(takesTable)
50 .set({
51 status: "paused",
52 periods: newPeriods,
53 notifiedPauseExpiration: false, // Reset pause expiration notification
54 })
55 .where(eq(takesTable.id, takeToUpdate.id));
56
57 const descriptionText = takeToUpdate.description
58 ? `\n\n*Working on:* ${takeToUpdate.description}`
59 : "";
60
61 return {
62 text: `⏸️ Session paused! You have ${prettyPrintTime(endTime.remaining)} remaining. It will automatically finish at ${generateSlackDate(new Date(Date.now() + TakesConfig.MAX_PAUSE_DURATION * 60000))}`,
63 response_type: "ephemeral",
64 blocks: [
65 {
66 type: "section",
67 text: {
68 type: "mrkdwn",
69 text: `⏸️ Session paused! You have ${prettyPrintTime(endTime.remaining)} remaining.${descriptionText}`,
70 },
71 },
72 {
73 type: "divider",
74 },
75 {
76 type: "context",
77 elements: [
78 {
79 type: "mrkdwn",
80 text: `It will automatically finish in ${prettyPrintTime(pausedTime)} (by ${generateSlackDate(new Date(new Date().getTime() - pausedTime))}) if not resumed.`,
81 },
82 ],
83 },
84 {
85 type: "actions",
86 elements: [
87 {
88 type: "button",
89 text: {
90 type: "plain_text",
91 text: "▶️ Resume",
92 emoji: true,
93 },
94 value: "resume",
95 action_id: "takes_resume",
96 },
97 {
98 type: "button",
99 text: {
100 type: "plain_text",
101 text: "⏹️ Stop",
102 emoji: true,
103 },
104 value: "stop",
105 action_id: "takes_stop",
106 style: "danger",
107 },
108 {
109 type: "button",
110 text: {
111 type: "plain_text",
112 text: "🔄 Refresh",
113 emoji: true,
114 },
115 value: "status",
116 action_id: "takes_status",
117 },
118 ],
119 },
120 ],
121 };
122}