Skip to content

Commit

Permalink
refactor(choosingReply): convert to ts
Browse files Browse the repository at this point in the history
  • Loading branch information
MrOrz committed Oct 9, 2023
1 parent 24ea4f3 commit d0923c8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/types/chatbotState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export type Context = {
/** User selected article in DB */
selectedArticleId?: string;
selectedArticleText?: string;

/** FIXME: Probably not required now */
selectedReplyId?: string;
};

export type ChatbotStateHandlerParams = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@ import {
import { getArticleURL, createTypeWords } from 'src/lib/sharedUtils';
import ga from 'src/lib/ga';
import UserSettings from 'src/database/models/userSettings';
import { FlexBubble } from '@line/bot-sdk';
import { ChatbotStateHandler } from 'src/types/chatbotState';
import {
GetReplyRelatedDataQuery,
GetReplyRelatedDataQueryVariables,
ReplyTypeEnum,
} from 'typegen/graphql';

/**
* @param {string} articleId - Article ID of the article-reply to feedback
* @param {string} replyId - Reply ID of the article-reply to feedback
* @returns {object} Flex message bubble object that asks the user if reply is helpful
*/
function createAskReplyFeedbackBubble(articleId, replyId) {
function createAskReplyFeedbackBubble(
articleId: string,
replyId: string
): FlexBubble {
return {
type: 'bubble',
body: {
Expand Down Expand Up @@ -61,12 +71,16 @@ function createAskReplyFeedbackBubble(articleId, replyId) {
}

/**
* @param {string} articleId - article ID to share
* @param {string} fullArticleText - article text
* @param {ReplyTypeEnum} replyTypeEnumValue - reply's type enum
* @param articleId - article ID to share
* @param fullArticleText - article text
* @param replyTypeEnumValue - reply's type enum
* @returns Flex message bubble object that asks user to share
*/
function createShareBubble(articleId, fullArticleText, replyTypeEnumValue) {
function createShareBubble(
articleId: string,
fullArticleText: string,
replyTypeEnumValue: ReplyTypeEnum
): FlexBubble {
const articleUrl = getArticleURL(articleId);
const articleText = ellipsis(fullArticleText, 15);
const replyType = createTypeWords(replyTypeEnumValue).toLowerCase();
Expand Down Expand Up @@ -116,8 +130,9 @@ function createShareBubble(articleId, fullArticleText, replyTypeEnumValue) {
};
}

export default async function choosingReply(params) {
let { data, state, event, userId, replies } = params;
const choosingReply: ChatbotStateHandler = async (params) => {
const { data, state, event, userId } = params;
let { replies } = params;

if (event.type !== 'postback' && event.type !== 'server_choose') {
throw new ManipulationError(t`Please choose from provided options.`);
Expand All @@ -137,9 +152,18 @@ export default async function choosingReply(params) {
replyCount
}
}
`({ id: selectedReplyId, articleId: data.selectedArticleId });
`<GetReplyRelatedDataQuery, GetReplyRelatedDataQueryVariables>({
id: selectedReplyId,
articleId: data.selectedArticleId ?? '',
});

if (errors) {
/* istanbul ignore if */
if (
errors ||
getReplyData.GetReply === null ||
getReplyData.GetArticle === null
) {
console.error('[GetReplyRelatedData]', errors);
throw new ManipulationError(
t`We have problem retrieving message and reply data, please forward the message again`
);
Expand All @@ -151,26 +175,29 @@ export default async function choosingReply(params) {
);

replies = [
...createReplyMessages(GetReply, GetArticle, data.selectedArticleId),
...createReplyMessages(GetReply, GetArticle, data.selectedArticleId ?? ''),
{
type: 'flex',
altText: t`Is the reply helpful?`,
contents: {
type: 'carousel',
contents: [
createAskReplyFeedbackBubble(data.selectedArticleId, selectedReplyId),
createAskReplyFeedbackBubble(
data.selectedArticleId ?? '',
selectedReplyId
),

// Ask user to turn on notification if the user did not turn it on
process.env.NOTIFY_METHOD &&
!allowNewReplyUpdate &&
createNotificationSettingsBubble(),

createShareBubble(
data.selectedArticleId,
data.selectedArticleText,
data.selectedArticleId ?? '',
data.selectedArticleText ?? '',
GetReply.type
),
].filter((m) => m),
].filter(Boolean),
},
},
];
Expand All @@ -183,4 +210,6 @@ export default async function choosingReply(params) {
visitor.send();

return { data, event, userId, replies };
}
};

export default choosingReply;

0 comments on commit d0923c8

Please sign in to comment.