All files / core/blong-gogo/src/adapter/server/slack slackPromptSettleRequestSend.ts

24.19% Statements 15/62
100% Branches 0/0
100% Functions 0/0
24.19% Lines 15/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 631x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                
import type {IChatAccountMap, IChatPromptSettleParams} from '@feasibleone/blong/types';
import {handler} from '@feasibleone/blong/types';
 
/**
 * `slack.prompt.settle` — the question's own message, rewritten once it is answered.
 *
 * `chat.update` addresses a message by `channel` and `ts`, which is why the ask keeps the `ts` its
 * send reply named. The blocks come from `slackPromptAnsweredBlocks`: the options stay, the chosen
 * ones are marked, and every interactive element is gone — the message records the answer instead of
 * offering controls that no longer do anything (`@@D-692`).
 *
 * A settle with no id cannot name its message, and inventing one would edit whatever happened to be
 * at the end of the conversation, so it is refused where the mistake is made.
 */
export default handler(({lib: {error, slackTokenGet, slackBaseGet, slackPromptAnsweredBlocks}}) => {
    const errors = error({
        'slack.accountMissing': {
            message: 'No Slack account {account} is configured',
            statusCode: 404,
        },
        'slack.messageUnknown': {
            message:
                'The Slack message for prompt {promptId} was never reported, so it cannot be edited',
            statusCode: 409,
        },
    });

    return function slackPromptSettleRequestSend(
        this: {config?: {accounts?: IChatAccountMap; url?: string}},
        params: IChatPromptSettleParams,
    ) {
        const token = slackTokenGet(this.config?.accounts, params.account);
        if (!token) throw errors['slack.accountMissing']({params: {account: params.account ?? ''}});
        if (!params.messageId)
            throw errors['slack.messageUnknown']({params: {promptId: params.prompt.promptId}});
        const answer = params.answer;
        const text = [answer.value, ...(answer.values ?? []), answer.text]
            .filter((part): part is string => Boolean(part))
            .join(', ');
        return {
            url: new URL(
                'chat.update',
                slackBaseGet(this.config?.accounts, params.account, this.config?.url),
            ),
            path: 'chat.update',
            method: 'POST',
            responseType: 'json',
            headers: {
                authorization: `Bearer ${token}`,
                'content-type': 'application/json; charset=utf-8',
            },
            json: {
                channel: params.conversationId,
                ts: params.messageId,
                // The fallback a notification and a search result read, where blocks are not rendered.
                text: `${params.prompt.text} — ${text || 'no answer'}`,
                mrkdwn: true,
                blocks: slackPromptAnsweredBlocks(params.prompt, params.answer),
            },
        };
    };
});