diff --git a/kontan-server/src/blocks.ts b/kontan-server/src/blocks.ts index bebd3ca..8d690f9 100644 --- a/kontan-server/src/blocks.ts +++ b/kontan-server/src/blocks.ts @@ -28,6 +28,8 @@ export const ACTIONS = { STATUS_MESSAGE_BUTTON: 'status_message_button', STATUS_MESSAGE_DAY: 'status_message_day', STATUS_MESSAGE_MESSAGE: 'status_message_message', + STATUS_MESSAGE_TODAY: 'status_message_today', + STATUS_MESSAGE_TODAY_MESSAGE: 'status_message_today_message', }; export const BLOCK_IDS = { @@ -36,12 +38,15 @@ export const BLOCK_IDS = { COMPACT_MODE: 'compact_mode', STATUS_MESSAGE_DAY: 'status_message_day', STATUS_MESSAGE_MESSAGE: 'status_message_message', + STATUS_MESSAGE_TODAY: 'status_message_today', + STATUS_MESSAGE_TODAY_MESSAGE: 'status_message_today_message', }; export const MODALS = { REGISTER: 'register', SETTINGS: 'settings', STATUS_MESSAGE: 'status_message', + STATUS_MESSAGE_TODAY: 'status_message_today', }; export const newUserBlock: View = { @@ -296,6 +301,55 @@ export const statusMessageModal = ( }; }; +export const statusMessageTodayModal = (): ModalView => { + return { + type: 'modal', + notify_on_close: true, + callback_id: MODALS.STATUS_MESSAGE_TODAY, + title: { + type: 'plain_text', + text: 'Add a status message?', + emoji: true, + }, + submit: { + type: 'plain_text', + text: 'Add', + emoji: true, + }, + close: { + type: 'plain_text', + text: "Nope I'm good", + emoji: true, + }, + blocks: [ + { + type: 'section', + text: { + type: 'plain_text', + text: 'Would you like to add a status message for today? Maybe you want a lunch buddy or someone to grab a fika with. You can also add one later.', + emoji: true, + }, + }, + { + type: 'divider', + }, + { + type: 'input', + element: { + type: 'plain_text_input', + action_id: BLOCK_IDS.STATUS_MESSAGE_TODAY_MESSAGE + '-action', + }, + block_id: BLOCK_IDS.STATUS_MESSAGE_TODAY_MESSAGE, + label: { + type: 'plain_text', + text: 'Your custom status for the day, emojis are supported', + emoji: true, + }, + } as Block, + ], + }; +}; + export const settingsModal = (user: User): ModalView => { return { type: 'modal', diff --git a/kontan-server/src/routes/slack/slack.service.ts b/kontan-server/src/routes/slack/slack.service.ts index 81a2e2e..3cea1b6 100644 --- a/kontan-server/src/routes/slack/slack.service.ts +++ b/kontan-server/src/routes/slack/slack.service.ts @@ -13,8 +13,10 @@ import { registerModal, settingsModal, statusMessageModal, + statusMessageTodayModal, } from '../../blocks'; import * as crypto from 'crypto'; +import { weekdayKeyBuilder } from '../../utils'; export interface Action { action_id: string; @@ -110,8 +112,18 @@ export class SlackService { } if (value === ACTIONS.CHECKIN_BUTTON) { const user = await this.userService.getUser(payload.user.id); - await this.officeService.checkUser(payload.user.id, user.office); - await this.showHomeScreen(payload.user.id); + const res = await this.officeService.checkUser( + payload.user.id, + user.office, + ); + if (res.status === 'INBOUND') { + await this.showAddTodayStatusModal( + payload.user.id, + payload.trigger_id, + ); + } else { + await this.showHomeScreen(payload.user.id); + } } if (value === ACTIONS.REFRESH_BUTTON) { await this.showHomeScreen(payload.user.id); @@ -132,6 +144,16 @@ export class SlackService { } } + // Refresh home screen if user dismissed adding a status for today + if ( + payload.type === 'view_closed' && + payload.view.callback_id === MODALS.STATUS_MESSAGE_TODAY + ) { + console.log(payload); + await this.showHomeScreen(payload.user.id); + return; + } + if (payload.type === ACTIONS.SUBMIT) { if (payload.view.callback_id === MODALS.REGISTER) { const homeOffice = @@ -174,6 +196,22 @@ export class SlackService { } await this.showHomeScreen(user.slackUserId); } + if (payload.view.callback_id === MODALS.STATUS_MESSAGE_TODAY) { + const user = await this.userService.getUser(payload.user.id); + const statusMessage = + payload.view.state.values[BLOCK_IDS.STATUS_MESSAGE_TODAY_MESSAGE][ + BLOCK_IDS.STATUS_MESSAGE_TODAY_MESSAGE + '-action' + ].value; + + if (statusMessage) { + await this.officeService.setStatusMessage( + user, + weekdayKeyBuilder(Date.now()), + statusMessage, + ); + } + await this.showHomeScreen(user.slackUserId); + } if (payload.view.callback_id === MODALS.SETTINGS) { const tag = payload.view.state.values[BLOCK_IDS.NFC_SERIAL][ @@ -194,6 +232,25 @@ export class SlackService { } } + async showAddTodayStatusModal(userId: string, triggerId) { + const user = await this.userService.getUser(userId); + const hasStatus = await this.officeService.userHasStatusMessageForDay( + userId, + weekdayKeyBuilder(Date.now()), + user.office, + ); + + if (!hasStatus) { + await this.web.views.open({ + user_id: userId, + view: statusMessageTodayModal(), + trigger_id: triggerId, + }); + } else { + return false; + } + } + async showHomeScreen(userId: string) { const user = await this.userService.getUser(userId); const offices = await this.officeService.getOffices(); diff --git a/kontan-server/src/services/OfficeService.ts b/kontan-server/src/services/OfficeService.ts index 34923f1..aa6d5e2 100644 --- a/kontan-server/src/services/OfficeService.ts +++ b/kontan-server/src/services/OfficeService.ts @@ -222,6 +222,18 @@ export class OfficeService { return offices; } + async userHasStatusMessageForDay( + userId: string, + dayKey: string, + office: string, + ) { + const messages = await this.getStatusMessagesForOffice(office); + + return messages.some( + (m) => m.dayKey === dayKey && userId === m.slackUserId, + ); + } + async getStatusMessagesForOffice(office: string): Promise { const snapshot = await this.admin .db()