diff --git a/.hgtags b/.hgtags index 7d999a22e76f4ed32986bb40a18c46debd8fedb6..299dad878dbd108a087f9998d1a8aa67e69bb9be 100644 --- a/.hgtags +++ b/.hgtags @@ -25,3 +25,4 @@ d0d3f818406e3eaf22b24068a296144f97b54ad4 v0.2.4 f1e05c41a2acee9b737eb03cae2dd86dc24e1d75 v0.2.5 725629941fcb8cc79281e9da851d400b7ee694a0 v1.0.0 2b89448cf3bdf5ed7a9ff1ec4523b19e55781b43 1.0.1 +0ee3f429052d274a6dc8a568139664fa234e68b3 1.1.0 diff --git a/package.json b/package.json index ab3afc05b14886d235f0b5638914902900041db5..7c96c55cf757f2f6876064c5ad2af081725a21bc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@logilab/cwclientlibjs", - "version": "1.0.1", + "version": "1.1.0", "description": "client library for CubicWeb's rqlcontroller API", "homepage": "https://forge.extranet.logilab.fr/open-source/cwclientlibjs", "repository": { diff --git a/src/index.ts b/src/index.ts index 4bbee0ad6fc6fa8323f73ac8d315cd6d5ac4598e..ffc2be1b19a173957c0e176a695de690f87239f2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,5 +5,6 @@ import * as client from './client'; import * as providers from './providers'; +import * as rqlQueries from './rqlQueries'; -export {client, providers}; +export {client, providers, rqlQueries}; diff --git a/src/rqlQueries/index.ts b/src/rqlQueries/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..6b79b5f6f0ebed4ba5be5eeb94153b50618fccee --- /dev/null +++ b/src/rqlQueries/index.ts @@ -0,0 +1,7 @@ +import {currentState, fireTransition, possibleTransitions} from './workflow'; + +export const workflow = { + currentState, + fireTransition, + possibleTransitions, +}; diff --git a/src/rqlQueries/types.ts b/src/rqlQueries/types.ts new file mode 100644 index 0000000000000000000000000000000000000000..41f6054f69677517288dbdfb45f3d585638b2904 --- /dev/null +++ b/src/rqlQueries/types.ts @@ -0,0 +1 @@ +export type BoundRqlQuery = [string, T]; diff --git a/src/rqlQueries/workflow.ts b/src/rqlQueries/workflow.ts new file mode 100644 index 0000000000000000000000000000000000000000..8fce0ac0e27aa88967b6ca19cec21633e00eaea0 --- /dev/null +++ b/src/rqlQueries/workflow.ts @@ -0,0 +1,76 @@ +/* cwclientlibjs + copyright 2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. + contact http://www.logilab.fr/ -- mailto:contact@logilab.fr +*/ + +import {BoundRqlQuery} from './types'; + +/** + * Generate a RQL query to retrieve the current state of a workflowable entity + * + * @param eid Identifier of the workflowable entity + */ +export function currentState(eid: number): BoundRqlQuery<{eid: number}> { + const rql = ` + Any + STATE_NAME + WHERE + ENTITY eid %(eid)s, + ENTITY in_state STATE, + STATE name STATE_NAME + `; + return [rql, {eid}]; +} + +/** + * Generates a RQL query to retrieve the list of transitions that can be fired + * on a given entity + * + * The query doesn't take the user rights into account. It means that some + * transitions could not be fireable by the user executing the query but still + * appear in the results. + * Each resulting row contains a single value which represents a transition + * name. + * @param eid Identifier of the workflowable entity + */ +export function possibleTransitions(eid: number): BoundRqlQuery<{eid: number}> { + const rql = ` Any TRANSITION_NAME WHERE ENTITY eid %(eid)s, + ENTITY in_state CURRENT_STATE, + CURRENT_STATE allowed_transition TRANSITION, + TRANSITION name TRANSITION_NAME + `; + return [rql, {eid}]; +} + +/** + * Generates a RQL query to fire a transition on an entity + * + * The result contains a single row containing only the eid of the created TrInfo. + * @param eid Identifier of the workflowable entity on which to fire the transition + * @param transitionName Name of transition to fire + * @param options.comment Additional comment to store in the created TrInfo + */ +export function fireTransition( + eid: number, + transitionName: string, + options: {comment?: string} = {} +): BoundRqlQuery<{eid: number; transitionName: string; comment?: string}> { + const vars: {eid: number; transitionName: string; comment?: string} = { + eid, + transitionName, + }; + let commentAssignment = ''; + if (options.comment) { + vars.comment = options.comment; + commentAssignment = ', TR_INFO comment %(comment)s'; + } + const rql = ` + INSERT TrInfo TR_INFO: + TR_INFO wf_info_for ENTITY, TR_INFO by_transition TRANSITION${commentAssignment} + WHERE + ENTITY eid %(eid)s, + ENTITY in_state CURRENT_STATE, CURRENT_STATE state_of WORKFLOW, + TRANSITION transition_of WORKFLOW, TRANSITION name %(transitionName)s + `; + return [rql, vars]; +}