Update sendMessage
This commit is contained in:
parent
b468620701
commit
932b659c2b
@ -263,8 +263,8 @@ class ChatElement extends HTMLElement {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.selectedRole) {
|
if (!this.processId) {
|
||||||
console.error('❌ No role selected');
|
console.error('no process id set');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,60 +280,63 @@ class ChatElement extends HTMLElement {
|
|||||||
if (!myAddresses) throw new Error('No paired member found');
|
if (!myAddresses) throw new Error('No paired member found');
|
||||||
|
|
||||||
const timestamp = Date.now();
|
const timestamp = Date.now();
|
||||||
// Ouvrir IndexedDB
|
const message = {
|
||||||
const dbRequest = indexedDB.open('4nk');
|
state: this.messageState,
|
||||||
|
type: 'text',
|
||||||
dbRequest.onerror = (event) => {
|
content: messageText,
|
||||||
console.error("Database error:", dbRequest.error);
|
metadata: {
|
||||||
};
|
createdAt: timestamp,
|
||||||
|
lastModified: timestamp,
|
||||||
dbRequest.onsuccess = async (event) => {
|
sender: myAddresses,
|
||||||
const db = dbRequest.result;
|
recipient: this.selectedMember,
|
||||||
const transaction = db.transaction(['diffs'], 'readwrite');
|
|
||||||
const store = transaction.objectStore('diffs');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const message = {
|
|
||||||
state: this.messageState,
|
|
||||||
type: 'text',
|
|
||||||
content: messageText,
|
|
||||||
metadata: {
|
|
||||||
createdAt: timestamp,
|
|
||||||
lastModified: timestamp,
|
|
||||||
sender: myAddresses,
|
|
||||||
recipient: this.selectedMember,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
await new Promise<void>((resolve, reject) => {
|
|
||||||
const request = store.add(message);
|
|
||||||
request.onsuccess = () => {
|
|
||||||
console.log('✅ Message saved');
|
|
||||||
messageInput.value = '';
|
|
||||||
resolve();
|
|
||||||
};
|
|
||||||
request.onerror = () => reject(request.error);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Attendre la fin de la transaction
|
|
||||||
await new Promise<void>((resolve, reject) => {
|
|
||||||
transaction.oncomplete = () => {
|
|
||||||
console.log('✅ Transaction completed');
|
|
||||||
resolve();
|
|
||||||
};
|
|
||||||
transaction.onerror = () => reject(transaction.error);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Recharger les messages
|
|
||||||
if (this.selectedMember) {
|
|
||||||
await this.loadMemberChat(this.selectedMember);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error('❌ Transaction error:', error);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const process = await service.getProcess(this.processId);
|
||||||
|
|
||||||
|
if (!process) {
|
||||||
|
console.error('Failed to retrieve process from DB');
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
console.log(process);
|
||||||
|
}
|
||||||
|
|
||||||
|
const processTip = process.states[process.states.length - 1].commited_in;
|
||||||
|
console.log(processTip);
|
||||||
|
|
||||||
|
// We take the last commited state and copy all values
|
||||||
|
const lastCommitedState = process.states.findLast(
|
||||||
|
state => state.commited_in !== processTip
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!lastCommitedState) {
|
||||||
|
console.error('Failed to find last commited state');
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
console.log(lastCommitedState);
|
||||||
|
}
|
||||||
|
|
||||||
|
let newState = {};
|
||||||
|
// fetch all the values from diff
|
||||||
|
for (const [attribute, hash] of Object.entries(lastCommitedState.pcd_commitment)) {
|
||||||
|
const value = await service.getDiffByValue(hash);
|
||||||
|
newState[attribute] = value.new_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
newState.message = message;
|
||||||
|
|
||||||
|
// Now we create a new state for the dm process
|
||||||
|
const apiReturn = service.updateProcess(this.processId, newState);
|
||||||
|
const updatedProcess = apiReturn.updated_process.current_process;
|
||||||
|
const newStateId = updatedProcess.states[updatedProcess.states.length - 2 ].state_id; // We take the last concurrent state, just before the tip
|
||||||
|
await service.handleApiReturn(apiReturn);
|
||||||
|
|
||||||
|
const createPrdReturn = service.createPrdUpdate(this.processId, newStateId);
|
||||||
|
await service.handleApiReturn(createPrdReturn);
|
||||||
|
|
||||||
|
// Now we validate the new state
|
||||||
|
const approveChangeReturn = service.approveChange(this.processId, newStateId);
|
||||||
|
await service.handleApiReturn(approveChangeReturn);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Error in sendMessage:', error);
|
console.error('❌ Error in sendMessage:', error);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user