@@ -12,7 +12,7 @@ import {
1212} from './deployment' ;
1313import { Engine , SpaceEngine } from './machines' ;
1414import { savedEnginesToEngines } from './saved-engines-helpers' ;
15- import { getCurrentEnvironment } from '@/components/auth' ;
15+ import { getCurrentEnvironment , getCurrentUser } from '@/components/auth' ;
1616import { enableUseDB } from 'FeatureFlags' ;
1717import { getDbEngines , getDbEngineByAddress } from '@/lib/data/db/engines' ;
1818import { asyncFilter , asyncMap , asyncForEach } from '../helpers/javascriptHelpers' ;
@@ -34,6 +34,7 @@ import {
3434 getCorrectVariableState ,
3535 getCorrectMilestoneState ,
3636 inlineScript ,
37+ getGlobalVariables ,
3738} from '@proceed/user-task-helper' ;
3839import { ExtendedTaskListEntry , UserTask } from '../user-task-schema' ;
3940
@@ -49,6 +50,7 @@ import { getProcessIds, getVariablesFromElementById } from '@proceed/bpmn-helper
4950import { Variable } from '@proceed/bpmn-helper/src/getters' ;
5051import Ability from '../ability/abilityHelper' ;
5152import { getUserById } from '../data/db/iam/users' ;
53+ import { getDataObject , isErrorResponse } from '@/app/api/spaces/[spaceId]/data/helper' ;
5254
5355export async function getCorrectTargetEngines (
5456 spaceId : string ,
@@ -299,9 +301,11 @@ export async function getAvailableTaskListEntries(spaceId: string, engines: Engi
299301 username ?: string | null ;
300302 firstName ?: string | null ;
301303 lastName ?: string | null ;
302- } [ ] = await asyncMap ( task . actualOwner , async ( owner ) => {
303- return getUserById ( owner , undefined , tx ) || owner ;
304- } ) ;
304+ } [ ] = (
305+ await asyncMap ( task . actualOwner , async ( owner ) => {
306+ return getUserById ( owner , undefined , tx ) || owner ;
307+ } )
308+ ) . filter ( truthyFilter ) ;
305309
306310 return users . map ( ( user ) =>
307311 typeof user === 'string'
@@ -321,6 +325,38 @@ export async function getAvailableTaskListEntries(spaceId: string, engines: Engi
321325 }
322326}
323327
328+ export async function getGlobalVariablesForHTML (
329+ spaceId : string ,
330+ initiatorId : string ,
331+ html : string ,
332+ ) {
333+ return await getGlobalVariables ( html , async ( varPath ) => {
334+ let segments = varPath . split ( '.' ) ;
335+
336+ let userId : string | undefined ;
337+
338+ if ( segments [ 0 ] === '@process-initiator' ) {
339+ userId = initiatorId ;
340+ } else if ( segments [ 0 ] === '@worker' || ! segments [ 0 ] . startsWith ( '@' ) ) {
341+ ( { userId } = await getCurrentUser ( ) ) ;
342+ } else if ( segments [ 0 ] !== '@organization' ) {
343+ throw new UserFacingError (
344+ `Invalid selector for global data access in user task html. (${ segments [ 0 ] } )` ,
345+ ) ;
346+ }
347+
348+ if ( segments [ 0 ] . startsWith ( '@' ) ) segments = segments . slice ( 1 ) ;
349+
350+ const result = await getDataObject ( spaceId , segments . join ( '.' ) , userId ) ;
351+
352+ if ( isErrorResponse ( result ) ) {
353+ throw new UserFacingError ( await result . data . text ( ) ) ;
354+ }
355+
356+ return result . data ?. value ;
357+ } ) ;
358+ }
359+
324360export async function getTasklistEntryHTML (
325361 spaceId : string ,
326362 userTaskId : string ,
@@ -348,6 +384,7 @@ export async function getTasklistEntryHTML(
348384
349385 if ( engine && ( ! html || ! milestones || ! initialVariables ) ) {
350386 const [ taskId , instanceId , startTimeString ] = userTaskId . split ( '|' ) ;
387+
351388 const [ definitionId ] = instanceId . split ( '-_' ) ;
352389
353390 const startTime = parseInt ( startTimeString ) ;
@@ -371,10 +408,11 @@ export async function getTasklistEntryHTML(
371408
372409 initialVariables = getCorrectVariableState ( userTask , instance ) ;
373410 milestones = await getCorrectMilestoneState ( version . bpmn , userTask , instance ) ;
374- variableChanges = initialVariables ;
375411
376412 html = await getUserTaskFileFromMachine ( engine , definitionId , filename ) ;
377413
414+ variableChanges = initialVariables ;
415+
378416 html = html . replace ( / \/ r e s o u r c e s \/ p r o c e s s [ ^ " ] * / g, ( match ) => {
379417 const path = match . split ( '/' ) ;
380418 return `/api/private/${ spaceId } /engine/resources/process/${ definitionId } /images/${ path . pop ( ) } ` ;
@@ -430,6 +468,23 @@ export async function getTasklistEntryHTML(
430468 if ( ! html ) throw new Error ( 'Failed to get the html for the user task' ) ;
431469 if ( ! milestones ) throw new Error ( 'Failed to get the milestones for the user task' ) ;
432470
471+ let globalVars : Record < string , any > = { } ;
472+
473+ if ( storedUserTask . instanceID ) {
474+ if ( ! engine ) throw new Error ( 'Cannot retrieve the instance initiator information.' ) ;
475+ const [ definitionId ] = storedUserTask . instanceID . split ( '-_' ) ;
476+ const deployment = await fetchDeployment ( engine , definitionId ) ;
477+ const instance = deployment . instances . find (
478+ ( i ) => i . processInstanceId === storedUserTask . instanceID ,
479+ ) ;
480+ if ( ! instance ) throw new Error ( 'Unknown instance' ) ;
481+ if ( ! instance . processInitiator ) throw new Error ( 'Missing initiator information' ) ;
482+
483+ globalVars = await getGlobalVariablesForHTML ( spaceId , instance . processInitiator , html ) ;
484+ }
485+
486+ variableChanges = { ...variableChanges , ...globalVars } ;
487+
433488 return inlineUserTaskData ( html , mapResourceUrls ( variableChanges ) , milestones ) ;
434489 } catch ( e ) {
435490 const message = getErrorMessage ( e ) ;
0 commit comments