Skip to content

Commit

Permalink
feat: add status message modal when checking in
Browse files Browse the repository at this point in the history
  • Loading branch information
Englund committed Sep 12, 2023
1 parent 6debe28 commit c9f3363
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 2 deletions.
54 changes: 54 additions & 0 deletions kontan-server/src/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand Down Expand Up @@ -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',
Expand Down
61 changes: 59 additions & 2 deletions kontan-server/src/routes/slack/slack.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 =
Expand Down Expand Up @@ -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][
Expand All @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions kontan-server/src/services/OfficeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<StatusMessage[]> {
const snapshot = await this.admin
.db()
Expand Down

0 comments on commit c9f3363

Please sign in to comment.