Add getMyProcesses api

This commit is contained in:
Sosthene 2025-08-25 01:13:35 +02:00
parent 770eedf39d
commit 832fa171d8

View File

@ -199,12 +199,81 @@ class SimpleProcessHandlers {
messageId: event.data.messageId
};
} catch (e) {
const errorMsg = `Failed to update process: ${e}`;
throw new Error(errorMsg);
const errorMessage = e instanceof Error ? e.message : String(e || 'Unknown error');
// Remove redundant "Error:" prefix and simplify the message
const cleanMessage = errorMessage.replace(/^Error:\s*/, '');
throw new Error(cleanMessage);
}
}
async handleMessage(event: ServerMessageEvent): Promise<ServerResponse > {
async handleGetMyProcesses(event: ServerMessageEvent): Promise<ServerResponse> {
if (event.data.type !== MessageType.GET_MY_PROCESSES) {
throw new Error('Invalid message type');
}
const processes = this.service.getProcesses();
const myProcesses = await this.service.getMyProcesses();
if (!myProcesses || myProcesses.length === 0) {
throw new Error('No my processes found');
}
const filteredProcesses: Record<string, Process> = {};
for (const processId of myProcesses) {
const process = processes.get(processId);
console.log(processId, ':', process);
if (process) {
filteredProcesses[processId] = process;
}
}
const data: Record<string, any> = {};
// Now we decrypt all we can in the processes
for (const [processId, process] of Object.entries(filteredProcesses)) {
// We also take the public data
const lastState = this.service.getLastCommitedState(process);
if (!lastState) {
console.error(`❌ Process ${processId} doesn't have a commited state`);
continue;
}
const processData: Record<string, any> = {};
for (const attribute of Object.keys(lastState.public_data)) {
try {
const value = this.service.decodeValue(lastState.public_data[attribute]);
if (value) {
processData[attribute] = value;
}
} catch (e) {
console.error(`❌ Error decoding public data ${attribute} for process ${processId}:`, e);
}
}
for (let i = process.states.length - 2; i >= 0; i--) {
const state = process.states[i];
for (const attribute of Object.keys(state.keys)) {
if (processData[attribute] !== undefined && processData[attribute] !== null) continue;
try {
const value = await this.service.decryptAttribute(processId, state, attribute);
if (value) {
processData[attribute] = value;
}
} catch (e) {
console.error(`❌ Error decrypting attribute ${attribute} for process ${processId}:`, e);
}
}
}
data[processId] = processData;
}
return {
type: MessageType.PROCESSES_RETRIEVED,
processes: filteredProcesses,
data,
messageId: event.data.messageId
};
}
async handleMessage(event: ServerMessageEvent): Promise<ServerResponse> {
try {
switch (event.data.type) {
case MessageType.NOTIFY_UPDATE:
@ -213,6 +282,8 @@ class SimpleProcessHandlers {
return await this.handleValidateState(event);
case MessageType.UPDATE_PROCESS:
return await this.handleUpdateProcess(event);
case MessageType.GET_MY_PROCESSES:
return await this.handleGetMyProcesses(event);
default:
throw new Error(`Unhandled message type: ${event.data.type}`);
}