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 { generateSlackDate, prettyPrintTime } from "../../../libs/time";
5import { getPausedTake } from "../services/database";
6import type { MessageResponse } from "../types";
7import { addNewPeriod, getRemainingTime } from "../../../libs/time-periods";
8
9export default async function handleResume(
10 userId: string,
11): Promise<MessageResponse | undefined> {
12 const pausedTake = await getPausedTake(userId);
13 if (pausedTake.length === 0) {
14 return {
15 text: `You don't have a paused takes session!`,
16 response_type: "ephemeral",
17 };
18 }
19
20 const pausedSession = pausedTake[0];
21 if (!pausedSession) {
22 return;
23 }
24
25 const now = new Date();
26 const newPeriods = JSON.stringify(
27 addNewPeriod(pausedSession.periods, "active"),
28 );
29
30 // Update the takes entry to active status
31 await db
32 .update(takesTable)
33 .set({
34 status: "active",
35 lastResumeAt: now,
36 periods: newPeriods,
37 notifiedLowTime: false, // Reset low time notification
38 })
39 .where(eq(takesTable.id, pausedSession.id));
40
41 const endTime = getRemainingTime(
42 pausedSession.targetDurationMs,
43 pausedSession.periods,
44 );
45
46 const descriptionText = pausedSession.description
47 ? `\n\n*Working on:* ${pausedSession.description}`
48 : "";
49
50 return {
51 text: `▶️ Takes session resumed! You have ${prettyPrintTime(endTime.remaining)} remaining in your session.`,
52 response_type: "ephemeral",
53 blocks: [
54 {
55 type: "section",
56 text: {
57 type: "mrkdwn",
58 text: `▶️ Takes session resumed!${descriptionText}`,
59 },
60 },
61 {
62 type: "divider",
63 },
64 {
65 type: "context",
66 elements: [
67 {
68 type: "mrkdwn",
69 text: `You have ${prettyPrintTime(endTime.remaining)} remaining until ${generateSlackDate(endTime.endTime)}.`,
70 },
71 ],
72 },
73 {
74 type: "actions",
75 elements: [
76 {
77 type: "button",
78 text: {
79 type: "plain_text",
80 text: "✍️ edit",
81 emoji: true,
82 },
83 value: "edit",
84 action_id: "takes_edit",
85 },
86 {
87 type: "button",
88 text: {
89 type: "plain_text",
90 text: "⏸️ Pause",
91 emoji: true,
92 },
93 value: "pause",
94 action_id: "takes_pause",
95 },
96 {
97 type: "button",
98 text: {
99 type: "plain_text",
100 text: "⏹️ Stop",
101 emoji: true,
102 },
103 value: "stop",
104 action_id: "takes_stop",
105 style: "danger",
106 },
107 {
108 type: "button",
109 text: {
110 type: "plain_text",
111 text: "🔄 Refresh",
112 emoji: true,
113 },
114 value: "status",
115 action_id: "takes_status",
116 },
117 ],
118 },
119 ],
120 };
121}