Add handleCreateProcess

This commit is contained in:
Sosthene 2025-06-12 14:34:36 +02:00 committed by NicolasCantu
parent 18e82de549
commit 65d43686cb
2 changed files with 39 additions and 0 deletions

View File

@ -27,6 +27,7 @@ export enum MessageType {
GET_PAIRING_ID = 'GET_PAIRING_ID',
REQUEST_LINK = 'REQUEST_LINK',
LINK_ACCEPTED = 'LINK_ACCEPTED',
CREATE_PROCESS = 'CREATE_PROCESS',
CREATE_PROFILE = 'CREATE_PROFILE',
PROFILE_CREATED = 'PROFILE_CREATED',
GET_PROCESSES = 'GET_PROCESSES',

View File

@ -506,6 +506,42 @@ export async function registerAllListeners() {
}
}
const handleCreateProcess = async (event: MessageEvent) => {
if (event.data.type !== MessageType.CREATE_PROCESS) return;
if (!services.isPaired()) {
const errorMsg = 'Device not paired';
errorResponse(errorMsg, event.origin);
return;
}
try {
const { processData, privateFields, roles, accessToken } = event.data;
if (!accessToken || !tokenService.validateToken(accessToken, event.origin)) {
throw new Error('Invalid or expired session token');
}
const privateData: Record<string, any> = {};
const publicData: Record<string, any> = {};
Object.entries(processData).forEach(([key, value]) => {
if (privateFields.includes(key)) {
privateData[key] = value;
} else {
publicData[key] = value;
}
});
const res = await services.createProcess(privateData, publicData, roles);
await services.handleApiReturn(res);
} catch (e) {
const errorMsg = `Failed to create process: ${e}`;
errorResponse(errorMsg, event.origin);
}
}
// Remove before adding to be sure there's no duplicate
window.removeEventListener('message', handleRequestLink);
window.removeEventListener('message', handleAddProfile);
@ -516,6 +552,7 @@ export async function registerAllListeners() {
window.removeEventListener('message', handleValidateToken);
window.removeEventListener('message', handleRenewToken);
window.removeEventListener('message', handleGetPairingId);
window.removeEventListener('message', handleCreateProcess);
window.addEventListener('message', handleRequestLink);
window.addEventListener('message', handleAddProfile);
@ -526,6 +563,7 @@ export async function registerAllListeners() {
window.addEventListener('message', handleValidateToken);
window.addEventListener('message', handleRenewToken);
window.addEventListener('message', handleGetPairingId);
window.addEventListener('message', handleCreateProcess);
console.log('Initialized event handlers');