All files / ext.wikilambda.edit/store/modules functionCall.js

95.29% Statements 81/85
75% Branches 9/12
100% Functions 3/3
95.29% Lines 81/85

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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 867x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 14x 14x 14x 5x 5x 5x 13x 9x 9x 9x 9x 9x 14x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 7x 7x  
/*!
 * WikiLambda Vue editor: Handle the request of function calls to the orchestrator.
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
 
const Constants = require( '../../Constants.js' ),
	Row = require( '../classes/Row.js' ),
	extractZIDs = require( '../../mixins/schemata.js' ).methods.extractZIDs,
	performFunctionCall = require( '../../mixins/api.js' ).methods.performFunctionCall;
 
module.exports = exports = {
	actions: {
		/**
		 * Create a new result record, that is detached from the main zObject.
		 * If the given record exists, it resets it by removing all children.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 *
		 * @return {number} resultId
		 */
		initializeResultId: function ( context, payload ) {
			let rowId;
			const row = context.getters.getRowById( payload );
			if ( row ) {
				// payload is a valid row Id, remove its children
				rowId = row.id;
				context.dispatch( 'removeRowChildren', { rowId } );
			} else {
				// payload is not a row ID, add new rowId
				rowId = context.getters.getNextRowId;
				const rootRow = new Row( rowId, undefined, Constants.ROW_VALUE_OBJECT, undefined );
				context.commit( 'pushRow', rootRow );
			}
			return rowId;
		},
		/**
		 * Calls orchestrator and sets orchestrationResult
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {Object} payload.functionCall
		 * @param {Object} payload.resultRowId
		 * @return {Promise}
		 */
		callZFunction: function ( context, payload ) {
			return performFunctionCall( payload.functionCall ).then( ( data ) => {
				// Asynchronously collect the necessary labels
				const zids = extractZIDs( data.response );
				context.dispatch( 'fetchZids', { zids } );
 
				// Success, we inject the response object in the resultRowId
				context.dispatch( 'injectZObjectFromRowId', {
					rowId: payload.resultRowId,
					value: data.response
				} );
			} ).catch( ( error ) => {
				// We might get either a Z22/Response envelope or a
				// HTTP error code response with an API error object:
				// {
				//   error: {
				//     code: 'error_code',
				//     info: 'error message',
				//   }
				// }
				if ( Constants.Z_OBJECT_TYPE in error ) {
					context.dispatch( 'injectZObjectFromRowId', {
						rowId: payload.resultRowId,
						value: error
					} );
				} else if ( 'error' in error ) {
					const hasErrorMsg = 'info' in error.error;
					context.dispatch( 'setError', {
						rowId: payload.resultRowId,
						errorCode: !hasErrorMsg ? Constants.errorCodes.UNKNOWN_ERROR : undefined,
						errorMessage: hasErrorMsg ? error.error.info : undefined,
						errorType: 'error'
					} );
				}
			} );
		}
	}
};