All files index.js

11.11% Statements 3/27
0% Branches 0/8
0% Functions 0/7
11.11% Lines 3/27

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 63 64 65 66 67 681x 1x   1x                                                                                                                                
const errors = require('./errors');
const { getDefaults, getDynamicHeaders } = require('./helpers');
 
module.exports = function({ registerErrors, utError: { getError } }) {
    return class postbankSms extends require('ut-port-http')(...arguments) {
        get defaults() {
            return {
                namespace: 'postbankSms',
                drainSend: 15000,
                ...getDefaults()
            };
        }
 
        async init(...params) {
            const result = await super.init(...params);
            Object.assign(this.errors, registerErrors(errors));
            return result;
        }
 
        handlers() {
            const { namespace, id } = this.config;
            const { remoteSystemIdentifier, smsType } = this.config.api;
            const { api } = this.bus.config.utPostbank;
            const url = `${api.endpoint}:${api.ports.message}`;
 
            return {
                'drainSend.event.receive'(msg, $meta) {
                    $meta.mtid = 'notification';
                    $meta.method = 'notice.message.process';
                    return {
                        port: id,
                        method: namespace + '.exec',
                        length: msg.length
                    };
                },
                [`${namespace}.exec.request.send`]: (payload, $meta) => {
                    const recipient = payload.to.replace(/ /g, ''); // API does not work if phone number contains spaces, e.g. "+359 88 812 3456"
                    const message = payload.body.trim();
 
                    return {
                        url,
                        uri: '/sendSMS',
                        headers: getDynamicHeaders($meta),
                        payload: {
                            Recipient: recipient,
                            SMSText: message,
                            RemoteSystemIdentifier: remoteSystemIdentifier,
                            SMSType: smsType
                        }
                    };
                },
                receive: (msg, $meta) => {
                    if ($meta.mtid === 'error') {
                        throw this.errors['postbankSms.httpError']({ msg });
                    } else if (!msg.payload) {
                        throw this.errors['postbankSms.missingPayload']({ msg });
                    } else if (msg.payload.errorCode !== '000') {
                        const error = msg.payload.errorMessage;
                        throw error ? this.errors.postbankSms({ msg: error }) : getError('postbank')();
                    }
 
                    return msg.payload;
                }
            };
        }
    };
};