Add notify and validate api

This commit is contained in:
Sosthene 2025-06-12 17:33:22 +02:00 committed by NicolasCantu
parent 25dba4e67b
commit 2a7c0d6675
2 changed files with 93 additions and 2 deletions

View File

@ -28,6 +28,9 @@ export enum MessageType {
REQUEST_LINK = 'REQUEST_LINK',
LINK_ACCEPTED = 'LINK_ACCEPTED',
CREATE_PROCESS = 'CREATE_PROCESS',
NOTIFY_UPDATE = 'NOTIFY_UPDATE',
UPDATE_NOTIFIED = 'UPDATE_NOTIFIED',
VALIDATE_STATE = 'VALIDATE_STATE',
CREATE_PROFILE = 'CREATE_PROFILE',
PROFILE_CREATED = 'PROFILE_CREATED',
GET_PROCESSES = 'GET_PROCESSES',
@ -35,6 +38,8 @@ export enum MessageType {
PROCESSES_RETRIEVED = 'PROCESSES_RETRIEVED',
CREATE_FOLDER = 'CREATE_FOLDER',
FOLDER_CREATED = 'FOLDER_CREATED',
PROCESS_CREATED = 'PROCESS_CREATED',
STATE_VALIDATED = 'STATE_VALIDATED',
RETRIEVE_DATA = 'RETRIEVE_DATA',
DATA_RETRIEVED = 'DATA_RETRIEVED',
ERROR = 'ERROR',

View File

@ -533,15 +533,97 @@ export async function registerAllListeners() {
}
});
const res = await services.createProcess(privateData, publicData, roles);
await services.handleApiReturn(res);
const createProcessReturn = await services.createProcess(privateData, publicData, roles);
if (!createProcessReturn.updated_process) {
throw new Error('Empty updated_process in createProcessReturn');
}
const processId = createProcessReturn.updated_process.process_id;
const process = createProcessReturn.updated_process.current_process;
const stateId = process.states[0].state_id;
await services.handleApiReturn(createProcessReturn);
const res = {
processId,
process,
folderCreated: processData
}
window.parent.postMessage(
{
type: MessageType.PROCESS_CREATED,
processCreated: res,
},
event.origin
);
} catch (e) {
const errorMsg = `Failed to create process: ${e}`;
errorResponse(errorMsg, event.origin);
}
}
const handleNotifyUpdate = async (event: MessageEvent) => {
if (event.data.type !== MessageType.NOTIFY_UPDATE) return;
if (!services.isPaired()) {
const errorMsg = 'Device not paired';
errorResponse(errorMsg, event.origin);
return;
}
try {
const { processId, stateId, accessToken } = event.data;
if (!accessToken || await !tokenService.validateToken(accessToken, event.origin)) {
throw new Error('Invalid or expired session token');
}
const res = await services.createPrdUpdate(processId, stateId);
await services.handleApiReturn(res);
window.parent.postMessage(
{
type: MessageType.UPDATE_NOTIFIED,
},
event.origin
);
} catch (e) {
const errorMsg = `Failed to notify update for process: ${e}`;
errorResponse(errorMsg, event.origin);
}
}
const handleValidateState = async (event: MessageEvent) => {
if (event.data.type !== MessageType.VALIDATE_STATE) return;
if (!services.isPaired()) {
const errorMsg = 'Device not paired';
errorResponse(errorMsg, event.origin);
return;
}
try {
const { processId, stateId, accessToken } = event.data;
if (!accessToken || await !tokenService.validateToken(accessToken, event.origin)) {
throw new Error('Invalid or expired session token');
}
const res = await services.approveChange(processId, stateId);
await services.handleApiReturn(res);
window.parent.postMessage(
{
type: MessageType.STATE_VALIDATED,
validatedProcess: res.updated_process,
},
event.origin
);
} catch (e) {
const errorMsg = `Failed to validate process: ${e}`;
errorResponse(errorMsg, event.origin);
}
}
// Remove before adding to be sure there's no duplicate
window.removeEventListener('message', handleRequestLink);
window.removeEventListener('message', handleAddProfile);
@ -553,6 +635,8 @@ export async function registerAllListeners() {
window.removeEventListener('message', handleRenewToken);
window.removeEventListener('message', handleGetPairingId);
window.removeEventListener('message', handleCreateProcess);
window.removeEventListener('message', handleValidateState);
window.removeEventListener('message', handleNotifyUpdate);
window.addEventListener('message', handleRequestLink);
window.addEventListener('message', handleAddProfile);
@ -564,6 +648,8 @@ export async function registerAllListeners() {
window.addEventListener('message', handleRenewToken);
window.addEventListener('message', handleGetPairingId);
window.addEventListener('message', handleCreateProcess);
window.addEventListener('message', handleValidateState);
window.addEventListener('message', handleNotifyUpdate);
console.log('Initialized event handlers');