Add handleUpdateProcess in router

This commit is contained in:
Sosthene 2025-06-15 22:17:58 +02:00 committed by NicolasCantu
parent 91ba7205cc
commit cf57681c31

View File

@ -553,8 +553,89 @@ export async function registerAllListeners() {
errorResponse(errorMsg, event.origin);
}
}
const handleUpdateProcess = async (event: MessageEvent) => {
if (event.data.type !== MessageType.UPDATE_PROCESS) return;
if (!services.isPaired()) {
const errorMsg = 'Device not paired';
errorResponse(errorMsg, event.origin);
}
try {
// privateFields is only used if newData contains new fields
// roles can be empty meaning that roles from the last commited state are kept
const { processId, lastStateId, newData, privateFields, roles, accessToken } = event.data;
if (!accessToken || await !tokenService.validateToken(accessToken, event.origin)) {
throw new Error('Invalid or expired session token');
}
if (!isValid32ByteHex(lastStateId)) {
throw new Error('Invalid last state id');
}
// Check if the new data is already in the process or if it's a new field
const process = await services.getProcess(processId);
if (!process) {
throw new Error('Process not found');
}
const lastState = services.getLastCommitedState(process);
if (!lastState) {
throw new Error('Process doesn\'t have a commited state yet');
}
if (lastState.state_id !== lastStateId) {
throw new Error('Process has been updated');
}
console.log('🚀 ~ handleUpdateProcess ~ lastState:', lastState);
const privateData: Record<string, any> = {};
const publicData: Record<string, any> = {};
for (const field of Object.keys(newData)) {
console.log('🚀 ~ handleUpdateProcess ~ newData[field]:', newData[field]);
if (lastState.pcd_commitment[field]) {
// is it a private field ?
if (lastState.public_data[field]) {
// Add it to public data
publicData[field] = newData[field];
} else {
// Add it to private data
privateData[field] = newData[field];
}
} else {
// is it supposed to be private ?
if (privateFields.includes(field)) {
// Add it to private data
privateData[field] = newData[field];
} else {
// Add it to public data
publicData[field] = newData[field];
}
// For new fields we should check that roles have been modified accordingly, otherwise the state would be impossible to commit
}
}
// We'll let the wasm check if roles are consistent
const res = await services.updateProcess(process, privateData, publicData, roles);
await services.handleApiReturn(res);
window.parent.postMessage(
{
type: MessageType.PROCESS_UPDATED,
updatedProcess: res.updated_process,
},
event.origin
);
} catch (e) {
const errorMsg = `Failed to update process: ${e}`;
errorResponse(errorMsg, event.origin);
}
}
// Remove before adding to be sure there's no duplicate
window.removeEventListener('message', handleUpdateProcess);
window.removeEventListener('message', handleRequestLink);
window.removeEventListener('message', handleGetProcesses);
window.removeEventListener('message', handleGetMyProcesses);
@ -566,6 +647,7 @@ export async function registerAllListeners() {
window.removeEventListener('message', handleValidateState);
window.removeEventListener('message', handleNotifyUpdate);
window.addEventListener('message', handleUpdateProcess);
window.addEventListener('message', handleRequestLink);
window.addEventListener('message', handleGetProcesses);
window.addEventListener('message', handleGetMyProcesses);