From bf1605997eabc4e7889d1f6c301af1944721a182 Mon Sep 17 00:00:00 2001 From: Balint Dolla Date: Fri, 3 Nov 2023 17:43:03 +0100 Subject: [PATCH] chore: refactor slack messages --- .../slack/constructAskBirthdayMessage.ts | 31 +++------- .../slack/constructAskPresentIdeasMessage.ts | 20 ++----- .../constructBirthdayConfirmedMessage.ts | 12 +--- .../slack/constructConfirmBirthdayMessage.ts | 49 +++------------ .../slack/constructIceBreakerQuestion.ts | 26 ++++---- packages/core/services/slack/messageItems.ts | 59 +++++++++++++++++++ tests/utils/generateIceBreakerTestUsers.ts | 14 ++--- 7 files changed, 100 insertions(+), 111 deletions(-) create mode 100644 packages/core/services/slack/messageItems.ts diff --git a/packages/core/services/slack/constructAskBirthdayMessage.ts b/packages/core/services/slack/constructAskBirthdayMessage.ts index c16d853..c80dcb6 100644 --- a/packages/core/services/slack/constructAskBirthdayMessage.ts +++ b/packages/core/services/slack/constructAskBirthdayMessage.ts @@ -3,6 +3,8 @@ import type { ChatPostMessageArguments } from "@slack/web-api"; import type { ChatReplaceMessageArguments } from "@/types/ChatReplaceMessageArguments"; import { pickBirthdayActionId } from "@/types/SlackInteractionRequest"; +import { makeTextBlock, makeTextBlockWithDatepicker } from "./messageItems"; + type Arguments = { user: string; name: string; @@ -15,30 +17,11 @@ const constructBaseAskBirthdayMessage = ({ }: Arguments): Omit => ({ text: "Please share your birthday with us! 🥳", blocks: [ - { - type: "section", - text: { - type: "mrkdwn", - text: `Hey ${name}! 👋`, - }, - }, - { - type: "section", - text: { - type: "mrkdwn", - text: "Please share your birthday with us! 🥳", - }, - accessory: { - type: "datepicker", - initial_date: "1995-01-01", - placeholder: { - type: "plain_text", - text: "Select a date", - emoji: true, - }, - action_id: pickBirthdayActionId, - }, - }, + makeTextBlock(`Hey ${name}! 👋`), + makeTextBlockWithDatepicker( + "Please share your birthday with us! 🥳", + pickBirthdayActionId, + ), ], metadata: { event_type: "askBirthday", diff --git a/packages/core/services/slack/constructAskPresentIdeasMessage.ts b/packages/core/services/slack/constructAskPresentIdeasMessage.ts index d6dbd1d..2923bf0 100644 --- a/packages/core/services/slack/constructAskPresentIdeasMessage.ts +++ b/packages/core/services/slack/constructAskPresentIdeasMessage.ts @@ -1,5 +1,7 @@ import type { ChatPostMessageArguments } from "@slack/web-api"; +import { makeTextBlock } from "./messageItems"; + type Arguments = { birthdayPerson: string; user: string; @@ -24,19 +26,9 @@ export const constructAskPresentIdeasMessage = ({ }, text: `Hey, <@${user}>! <@${birthdayPerson}>'s birthday is in 2 months. Do you have any present ideas?`, blocks: [ - { - type: "section", - text: { - type: "mrkdwn", - text: `Hey ${name}! 👋`, - }, - }, - { - type: "section", - text: { - type: "mrkdwn", - text: `It's <@${birthdayPerson}>'s birthday is in 2 months. Do you have any present ideas?`, - }, - }, + makeTextBlock(`Hey ${name}! 👋`), + makeTextBlock( + `It's <@${birthdayPerson}>'s birthday is in 2 months. Do you have any present ideas?`, + ), ], }) satisfies ChatPostMessageArguments; diff --git a/packages/core/services/slack/constructBirthdayConfirmedMessage.ts b/packages/core/services/slack/constructBirthdayConfirmedMessage.ts index 5dcc894..d5aff5c 100644 --- a/packages/core/services/slack/constructBirthdayConfirmedMessage.ts +++ b/packages/core/services/slack/constructBirthdayConfirmedMessage.ts @@ -1,16 +1,10 @@ import type { ChatReplaceMessageArguments } from "@/types/ChatReplaceMessageArguments"; +import { makeTextBlock } from "./messageItems"; + export const constructBirthdayConfirmedMessage = (): ChatReplaceMessageArguments => ({ replace_original: true, text: "Thanks for submitting your birthday! 🎉", - blocks: [ - { - type: "section", - text: { - type: "mrkdwn", - text: `Thanks for submitting your birthday! 🎉`, - }, - }, - ], + blocks: [makeTextBlock(`Thanks for submitting your birthday! 🎉`)], }); diff --git a/packages/core/services/slack/constructConfirmBirthdayMessage.ts b/packages/core/services/slack/constructConfirmBirthdayMessage.ts index 9333f9a..54651d5 100644 --- a/packages/core/services/slack/constructConfirmBirthdayMessage.ts +++ b/packages/core/services/slack/constructConfirmBirthdayMessage.ts @@ -4,6 +4,8 @@ import { birthdayIncorrectActionId, } from "@/types/SlackInteractionRequest"; +import { makeActionsBlock, makeTextBlock } from "./messageItems"; + export const constructConfirmBirthdayMessage = ( birthday: string, ): ChatReplaceMessageArguments => @@ -11,45 +13,12 @@ export const constructConfirmBirthdayMessage = ( replace_original: true, text: "Confirm your birthday", blocks: [ - { - type: "section", - text: { - type: "mrkdwn", - text: `Are you sure your birthday is ${birthday}?`, - }, - }, - { - type: "actions", - elements: [ - { - type: "button", - text: { - type: "plain_text", - emoji: true, - text: "Yes", - }, - style: "primary", - action_id: birthdayConfirmActionId, - value: birthday, - }, - { - type: "button", - text: { - type: "plain_text", - emoji: true, - text: "No", - }, - style: "danger", - action_id: birthdayIncorrectActionId, - value: birthday, - }, - ], - }, - ], - metadata: { - event_type: "confirmBirthday", - event_payload: { + makeTextBlock(`Are you sure your birthday is ${birthday}?`), + makeActionsBlock( + birthdayConfirmActionId, + birthdayIncorrectActionId, birthday, - }, - }, + "", + ), + ], }) satisfies ChatReplaceMessageArguments; diff --git a/packages/core/services/slack/constructIceBreakerQuestion.ts b/packages/core/services/slack/constructIceBreakerQuestion.ts index 6154d69..7e7d4f7 100644 --- a/packages/core/services/slack/constructIceBreakerQuestion.ts +++ b/packages/core/services/slack/constructIceBreakerQuestion.ts @@ -1,5 +1,7 @@ import type { ChatPostMessageArguments, KnownBlock } from "@slack/web-api"; +import { makeTextBlock } from "./messageItems"; + type Arguments = { users: string[]; channel: string; @@ -19,25 +21,17 @@ export const constructIceBreakerQuestion = ({ iceBreakerQuestions[Math.floor(Math.random() * iceBreakerQuestions.length)]; const blocks: KnownBlock[] = [ - { - type: "section", - text: { - type: "mrkdwn", - text: `${randomIceBreakerQuestion} Post your picks in the thread! 👇`, - }, - }, + makeTextBlock( + `${randomIceBreakerQuestion} Post your picks in the thread! 👇`, + ), ]; if (users.length) { - blocks.push({ - type: "section", - text: { - type: "mrkdwn", - text: `Let's see your ones ${users - .map((user) => `<@${user}>`) - .join(", ")}!`, - }, - }); + blocks.push( + makeTextBlock( + `Let's see your ones ${users.map((user) => `<@${user}>`).join(", ")}!`, + ), + ); } return { diff --git a/packages/core/services/slack/messageItems.ts b/packages/core/services/slack/messageItems.ts new file mode 100644 index 0000000..a4c760c --- /dev/null +++ b/packages/core/services/slack/messageItems.ts @@ -0,0 +1,59 @@ +import type { ActionsBlock, SectionBlock } from "@slack/web-api"; + +export const makeTextBlock = (text: string): SectionBlock => ({ + type: "section", + text: { + type: "mrkdwn", + text, + }, +}); + +export const makeTextBlockWithDatepicker = ( + text: string, + actionId: string, +): SectionBlock => ({ + ...makeTextBlock(text), + accessory: { + type: "datepicker", + initial_date: "1995-01-01", + placeholder: { + type: "plain_text", + text: "Select a date", + emoji: true, + }, + action_id: actionId, + }, +}); + +export const makeActionsBlock = ( + yesActionId: string, + noActionId: string, + yesValue: string, + noValue: string, +): ActionsBlock => ({ + type: "actions", + elements: [ + { + type: "button", + text: { + type: "plain_text", + emoji: true, + text: "Yes", + }, + style: "primary", + action_id: yesActionId, + value: yesValue, + }, + { + type: "button", + text: { + type: "plain_text", + emoji: true, + text: "No", + }, + style: "danger", + action_id: noActionId, + value: noValue, + }, + ], +}); diff --git a/tests/utils/generateIceBreakerTestUsers.ts b/tests/utils/generateIceBreakerTestUsers.ts index e213254..3d38988 100644 --- a/tests/utils/generateIceBreakerTestUsers.ts +++ b/tests/utils/generateIceBreakerTestUsers.ts @@ -30,14 +30,12 @@ export const generateIceBreakerTestUsers = async (today?: string) => { start.add(6, "months"), // random day outside the window ]; - await Promise.all( - birthdays.map((birthday, i) => - testDb.insert(users).values({ - id: `U${i + 1}`, - teamId: "T1", - birthday: birthday.toDate(), - }), - ), + await testDb.insert(users).values( + birthdays.map((birthday, i) => ({ + id: `U${i + 1}`, + teamId: "T1", + birthday: birthday.toDate(), + })), ); };