All files / core/blong-gogo/src/adapter/server/viber responseReceive.ts

46.87% Statements 15/32
100% Branches 0/0
100% Functions 0/0
46.87% Lines 15/32

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 331x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                    
import {handler} from '@feasibleone/blong/types';
 
/**
 * `response.receive` — the answer every Viber call in this group shares.
 *
 * Viber reports a refusal in the body: `status` is `0` for success and a positive number otherwise,
 * with `status_message` beside it. A status-code check alone would read every refusal as success, so
 * the body's own verdict is what decides.
 */
interface IViberAnswer {
    statusCode?: number;
    body?: {status?: number; status_message?: string; [key: string]: unknown};
}
 
export default handler(({lib: {error}}) => {
    const errors = error({
        'viber.refused': {
            message: 'Viber refused the call: {reason}',
            statusCode: 502,
        },
    });

    return function responseReceive(response: IViberAnswer) {
        const body = response?.body ?? {};
        if (body.status !== 0) {
            throw errors['viber.refused']({
                params: {reason: body.status_message ?? `status ${String(body.status)}`},
            });
        }
        return body;
    };
});