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

30.76% Statements 16/52
100% Branches 0/0
100% Functions 0/0
30.76% Lines 16/52

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 531x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                          
import type {IChatAccountMap, IChatChannelCreateParams} from '@feasibleone/blong/types';
import {handler} from '@feasibleone/blong/types';
 
/**
 * `slack.channel.create` — the `conversations.create` a plan's channel is opened with.
 *
 * A channel the bot creates is one it is already a member of, which is what makes the first message
 * and the questions that follow arrive without a second invite. The name is the caller's, because
 * the caller is the one that knows what the channel stands for; Slack's own rules (lowercase,
 * digits, `-` and `_`, at most 80) are enforced where the name is built (`@@D-805`).
 *
 * A name Slack already holds answers `ok: false, error: 'name_taken'`. That is not an error here:
 * the caller asked for *the* channel of this plan, and one that exists is the answer it wants — the
 * realm decides whether to reuse it or to report the clash.
 */
export default handler(({lib: {error, slackTokenGet, slackBaseGet}}) => {
    const errors = error({
        'slack.channelAccountMissing': {
            message: 'No Slack account {account} is configured',
            statusCode: 404,
        },
    });

    return function slackChannelCreateRequestSend(
        this: {config?: {accounts?: IChatAccountMap; url?: string}},
        params: IChatChannelCreateParams,
    ) {
        const token = slackTokenGet(this.config?.accounts, params.account);
        if (!token) {
            throw errors['slack.channelAccountMissing']({
                params: {account: params.account ?? ''},
            });
        }
        return {
            url: new URL(
                'conversations.create',
                slackBaseGet(this.config?.accounts, params.account, this.config?.url),
            ),
            path: 'conversations.create',
            method: 'POST',
            responseType: 'json',
            headers: {
                authorization: `Bearer ${token}`,
                'content-type': 'application/json; charset=utf-8',
            },
            json: {
                name: params.channelName,
                is_private: params.isPrivate ?? true,
            },
        };
    };
});