diff --git a/src/router.ts b/src/router.ts index 1b1b6f2..b24368b 100755 --- a/src/router.ts +++ b/src/router.ts @@ -597,9 +597,28 @@ export async function registerAllListeners() { if (!process) { throw new Error('Process not found'); } - const lastState = services.getLastCommitedState(process); + let lastState = services.getLastCommitedState(process); if (!lastState) { - throw new Error('Process doesn\'t have a commited state yet'); + const firstState = process.states[0]; + const roles = firstState.roles; + if (services.rolesContainsUs(roles)) { + const approveChangeRes= await services.approveChange(processId, firstState.state_id); + await services.handleApiReturn(approveChangeRes); + const prdUpdateRes = await services.createPrdUpdate(processId, firstState.state_id); + await services.handleApiReturn(prdUpdateRes); + } else { + if (firstState.validation_tokens.length > 0) { + // Try to send it again anyway + const res = await services.createPrdUpdate(processId, firstState.state_id); + await services.handleApiReturn(res); + } + } + // Wait a couple seconds + await new Promise(resolve => setTimeout(resolve, 2000)); + lastState = services.getLastCommitedState(process); + if (!lastState) { + throw new Error('Process doesn\'t have a commited state yet'); + } } const lastStateIndex = services.getLastCommitedStateIndex(process); if (lastStateIndex === null) {