diff --git a/src/pages/chat/chat.ts b/src/pages/chat/chat.ts index 8140175..2aae947 100755 --- a/src/pages/chat/chat.ts +++ b/src/pages/chat/chat.ts @@ -335,6 +335,34 @@ class ChatElement extends HTMLElement { console.error('Error getting diff by state id:', error); } } + + private async lookForChildren(): Promise { + // Filter processes for the children of current process + const service = await Services.getInstance(); + const children: string[] = await service.getChildrenOfProcess(this.processId); + + const processRoles = this.processRoles; + const selectedMember = this.selectedMember; + for (const child of children) { + const roles = await this.getRoles(JSON.parse(child)); + // Check that we and the other members are in the role + if (!service.isChildRole(processRoles, roles)) { + console.error('Child process roles are not a subset of parent') + continue; + } + if (!service.rolesContainsMember(roles, selectedMember)) { + console.error('Member is not part of the process'); + continue; + } + if (!service.rolesContainsUs(roles)) { + console.error('We\'re not part of child process'); + continue; + } + return child; + } + + return null; + } private async loadMemberChat(member: string[]) { if (member.length === 0) { @@ -362,50 +390,39 @@ class ChatElement extends HTMLElement { chatHeader.textContent = `Chat with ${emojis}`; messagesContainer.innerHTML = ''; - // Filter processes for the children of current process - if (!this.processId) { - console.error('No process id'); - return; - } - const children: string[] = await service.getChildrenOfProcess(this.processId); - - let messagesProcess: Process | null = null; - const processRoles = this.processRoles; - for (const child of children) { - const roles = await this.getRoles(JSON.parse(child)); - // Check that we and the other members are in the role - if (!service.isChildRole(processRoles, roles)) { - console.error('Child process roles are not a subset of parent') - continue; - } - if (!service.rolesContainsMember(roles, this.selectedMember)) { - console.error('Member is not part of the process'); - continue; - } - if (!service.rolesContainsUs(roles)) { - console.error('We\'re not part of child process'); - continue; - } - messagesProcess = JSON.parse(child); - } + let messagesProcess = await this.lookForChildren(); if (messagesProcess === null) { console.log('Create a new child process'); // We need to create a new process - let newProcess; try { - if (this.processId) { - const res = await service.createDmProcess(member, this.processId); - // We catch the new process here - messagesProcess = res.updated_process?.current_process ?? null; - await service.handleApiReturn(res); - } else { + if (!this.processId) { console.error('No process id'); + return; } + const res = await service.createDmProcess(member, this.processId); + // We catch the new process here + const updatedProcess = res.updated_process.current_process; + const processId = updatedProcess.states[0].commited_in; + const stateId = updatedProcess.states[0].state_id; + await service.handleApiReturn(res); + setTimeout(async () => { + // Now create a first commitment + console.log('Created child process', processId); + const createPrdReturn = await service.createPrdUpdate(processId, stateId); + await service.handleApiReturn(createPrdReturn); + const approveChangeReturn = service.approveChange(processId, stateId); + await service.handleApiReturn(approveChangeReturn); + }, 500); } catch (e) { console.error(e); return; } + + while (messagesProcess === null) { + messagesProcess = await this.lookForChildren(); + await new Promise(r => setTimeout(r, 500)); + } } else { console.log('Found child process', messagesProcess); } @@ -425,6 +442,8 @@ class ChatElement extends HTMLElement { // Récupérer les messages depuis les états du processus const allMessages: any[] = []; + console.log(messagesProcess); + const childProcess = JSON.parse(messagesProcess); if (messagesProcess?.states) { for (const state of messagesProcess.states) { const pcd_commitment = state.pcd_commitment;