From 5fb4c87f1d6b34b22cee2a3c378ae26a2c816afd Mon Sep 17 00:00:00 2001 From: NicolasCantu Date: Mon, 6 Jan 2025 12:36:04 +0100 Subject: [PATCH] Add getDescription --- src/pages/process/process.ts | 56 +++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/src/pages/process/process.ts b/src/pages/process/process.ts index 0e82e70..9de7994 100755 --- a/src/pages/process/process.ts +++ b/src/pages/process/process.ts @@ -1,6 +1,7 @@ import { addSubscription } from '../../utils/subscription.utils'; import Services from '../../services/service'; import { getCorrectDOM } from '~/utils/html.utils'; +import { Process } from 'pkg/sdk_client'; // Initialize function, create initial tokens with itens that are already selected by the user export async function init() { @@ -39,13 +40,19 @@ export async function init() { // Create a new process with hardcoded members for demonstration purpose await createMessagingProcess(); - const processes = await getProcesses(); - for (let process of processes) { - const processName = process['description']; - const opt = new Option(processName); - opt.value = processName; - element.add(opt); - } + setTimeout(async () => { + const processes = await getProcesses(); + for (const {key, value} of processes) { + const processName = await getDescription(key, value); + // const processName = value['description']; + if (processName) { + console.log('adding process name to list:', processName); + const opt = new Option(processName); + opt.value = processName; + element.add(opt); + } + } + }, 1000) // set the wrapper as child (instead of the element) element.parentNode?.replaceChild(wrapper, element); // set element as child of wrapper @@ -457,6 +464,41 @@ async function createMessagingProcess(): Promise { await service.handleApiReturn(apiReturn); } +async function getDescription(processId: string, process: Process): Promise { + const service = await Services.getInstance(); + // Get the `commited_in` value of the last state and remove it from the array + const currentCommitedIn = process.states.pop()?.commited_in; + + if (currentCommitedIn === undefined) { + return null; // No states available + } + + // Find the last state where `commited_in` is different + let lastDifferentState = process.states.findLast( + state => state.commited_in !== currentCommitedIn + ); + + if (!lastDifferentState) { + // It means that we only have one state that is not commited yet, that can happen with process we just created + // let's assume that the right description is in the last concurrent state and not handle the (arguably rare) case where we have multiple concurrent states on a creation + lastDifferentState = process.states.pop(); + } + + // Take the description out of the state, if any + const description = lastDifferentState!.pcd_commitment['description']; + if (description) { + const userDiff = await service.getDiffByValue(description); + if (userDiff) { + console.log("Successfully retrieved userDiff:", userDiff); + return userDiff.new_value; + } else { + console.log("Failed to retrieve a non-null userDiff."); + } + } + + return null; +} + async function getProcesses(): Promise { const service = await Services.getInstance(); const processes = await service.getProcesses();