{"size": 2175, "ext": "js", "max_stars_repo_path": "src/app/composer/services/messageRequest.js", "max_stars_repo_name": "gokula-krishna-dev/WebClient", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/app/composer/services/messageRequest.js", "max_issues_repo_name": "gokula-krishna-dev/WebClient", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/app/composer/services/messageRequest.js", "max_forks_repo_name": "gokula-krishna-dev/WebClient", "max_forks_repo_forks_event_min_datetime": "2021-07-11T23:11:35.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-11T23:11:35.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 30.6338028169, "max_line_length": 111, "alphanum_fraction": 0.5682758621}
import _ from 'lodash';

import { STATUS } from '../../constants';
import { SUCCESS, DRAFT_NOT_EXIST, MESSAGE_ALREADY_SEND } from '../constants/index';

/* @ngInject */
function messageRequest(dispatchers, messageApi, gettextCatalog) {
    const I18N = {
        ERROR_REQUEST_DRAFT: gettextCatalog.getString('Saving draft failed, please  try again', null, 'Error'),
        ERROR_SENDING: gettextCatalog.getString('Cannot send message', null, 'Error')
    };

    const { dispatcher } = dispatchers(['composer.update']);
    const dispatch = (type, data = {}) => dispatcher['composer.update'](type, data);

    function getSendError(data) {
        // The API can return the error via a 4X or via 2X...
        if (_.has(data, 'data')) {
            return getSendError(data.data);
        }

        return new Error(data.Error || I18N.ERROR_SENDING);
    }

    const getEditPromise = (type = STATUS.CREATE, parameters) => {
        if (type === STATUS.UPDATE) {
            return messageApi.updateDraft(parameters);
        }
        return messageApi.createDraft(parameters);
    };

    /**
     * Handle the draft request
     * @param {Object} parameters
     * @param {Integer} type
     * @return {Promise}
     */
    async function draft(parameters, message, type) {
        try {
            const { data } = await getEditPromise(type, parameters);

            if (data.Code === SUCCESS || data.Code === DRAFT_NOT_EXIST) {
                return data;
            }

            if (data.Code === MESSAGE_ALREADY_SEND) {
                return dispatch('close.message', { message });
            }
        } catch (err) {
            const { data = {} } = err || {};

            throw new Error(data.Error || I18N.ERROR_REQUEST_DRAFT);
        }
    }

    async function send(parameters) {
        try {
            const { data = {} } = await messageApi.send(parameters);
            return data;
        } catch (e) {
            // Check if there is an error coming from the server
            const err = getSendError(e);
            err.code = e.Code;
            throw err;
        }
    }

    return { draft, send };
}
export default messageRequest;
