- Iniziato a scrivere la CHATBOT...

This commit is contained in:
Surya Paolo
2025-08-09 00:48:44 +02:00
parent 6013a624f6
commit f1f3f5ad07
14 changed files with 186 additions and 68 deletions

View File

@@ -1,14 +1,14 @@
import { defineStore } from 'pinia'
import { Api } from '@api'
import { defineStore } from 'pinia';
import { Api } from '@api';
import { serv_constants } from './Modules/serv_constants'
import { serv_constants } from './Modules/serv_constants';
import type { IMessage, IMessageState, IMsgUsers} from '../model';
import { StatusMessage } from '../model'
import { tools } from '@src/store/Modules/tools'
import { MsgDefault } from '@src/model'
import { shared_consts } from '@src/common/shared_vuejs'
import { useUserStore } from '@store/UserStore'
import type { IMessage, IMessageState, IMsgUsers } from '../model';
import { StatusMessage } from '../model';
import { tools } from '@src/store/Modules/tools';
import { MsgDefault } from '@src/model';
import { shared_consts } from '@src/common/shared_vuejs';
import { useUserStore } from '@store/UserStore';
export const useMessageStore = defineStore('MessageStore', {
state: (): IMessageState => ({
@@ -17,57 +17,57 @@ export const useMessageStore = defineStore('MessageStore', {
}),
getters: {
getlasts_messages: (mystate: IMessageState) => (): IMessage[] => {
const ctrec = (mystate.last_msgs) ? mystate.last_msgs.slice(0, 5) : []
const ctrec = mystate.last_msgs ? mystate.last_msgs.slice(0, 5) : [];
// const ctrec = (mystate.msgs) ? mystate.msgs.slice().reverse().slice(0, 5) : []
return (ctrec)
return ctrec;
},
getnumMsgUnread: (mystate: IMessageState) => () => {
return mystate.last_msgs.filter((msg) => !msg.read).length
return mystate.last_msgs.filter((msg) => !msg.read).length;
},
},
actions: {
setMsg(arrmsg: IMessage[], username: string) {
// console.log('arrmsg', arrmsg)
const userStore = useUserStore()
const userStore = useUserStore();
if (arrmsg.length > 0) {
let users_msg: any = this.users_msg.find((rec: IMsgUsers) => rec.username === username)
let users_msg: any = this.users_msg.find(
(rec: IMsgUsers) => rec.username === username
);
if (!users_msg) {
this.users_msg.push({ username, msgs: [] })
users_msg = this.users_msg.find((rec) => rec.username === username)
this.users_msg.push({ username, msgs: [] });
users_msg = this.users_msg.find((rec) => rec.username === username);
}
users_msg.msgs.push(...arrmsg)
users_msg.msgs.push(...arrmsg);
// console.table(users_msg.msgs)
// users_msg.msgs = tools.getUnique(users_msg.msgs, '_id')
// console.table(users_msg.msgs)
if (users_msg.msgs) {
let userother: any = users_msg.msgs.slice(-1)[0].dest
let userother: any = users_msg.msgs.slice(-1)[0].dest;
if (userother === userStore.my.username)
userother = users_msg.msgs.slice(-1)[0].origin
userother = users_msg.msgs.slice(-1)[0].origin;
let index = this.last_msgs.findIndex((rec: IMessage) => (rec.dest === userother) || (rec.origin === userother))
let index = this.last_msgs.findIndex(
(rec: IMessage) => rec.dest === userother || rec.origin === userother
);
if (index >= 0) {
// Update last message
this.last_msgs[index] = users_msg.msgs.slice(-1)[0]
this.last_msgs[index] = users_msg.msgs.slice(-1)[0];
} else {
this.last_msgs.push(users_msg.msgs.slice(-1)[0])
index = this.last_msgs.findIndex((rec: IMessage) => (rec.dest === userother) || (rec.origin === userother))
this.last_msgs.push(users_msg.msgs.slice(-1)[0]);
index = this.last_msgs.findIndex(
(rec: IMessage) => rec.dest === userother || rec.origin === userother
);
}
if (this.last_msgs[index])
users_msg.lastdataread = this.last_msgs[index].datemsg
else
users_msg.lastdataread = tools.getLastDateReadReset()
users_msg.lastdataread = this.last_msgs[index].datemsg;
else users_msg.lastdataread = tools.getLastDateReadReset();
} else {
users_msg.lastdataread = tools.getLastDateReadReset()
users_msg.lastdataread = tools.getLastDateReadReset();
}
// console.log('RICeVUTO', arrmsg, 'lastdataread', users_msg.lastdataread)
@@ -75,39 +75,55 @@ export const useMessageStore = defineStore('MessageStore', {
}
},
async updateMsgDataFromServer({ username, lastdataread }: { username: string, lastdataread: Date }) {
async updateMsgDataFromServer({
username,
lastdataread,
}: {
username: string;
lastdataread: Date;
}) {
// console.log('updateMsgDataFromServer', username, lastdataread)
return Api.SendReq(`/sendmsg/${username}/${lastdataread}/${tools.getEnv('VITE_APP_ID')}`, 'GET', null)
return Api.SendReq(
`/sendmsg/${username}/${lastdataread}/${tools.getEnv('VITE_APP_ID')}`,
'GET',
null
)
.then((res) => {
// console.log('res', res)
if (res.status === 200) {
this.setMsg(res.data.arrmsg, username)
return true
this.setMsg(res.data.arrmsg, username);
return true;
}
return false
return false;
})
.catch((error) => {
console.error(error)
return false
})
console.error(error);
return false;
});
},
async SendMsgEvent(msg: IMessage) {
console.log('SendMsgEvent', msg)
console.log('SendMsgEvent', msg);
const data: IMessage = { ...MsgDefault, ...msg }
const data: IMessage = { ...MsgDefault, ...msg };
const userStore = useUserStore()
const userStore = useUserStore();
data.source!.page = ''
data.idapp = tools.getEnv('VITE_APP_ID')
data.origin = userStore.my.username
data.datemsg = tools.getDateNow()
data.status = StatusMessage.WaitingToSend
data.source!.page = '';
data.idapp = tools.getEnv('VITE_APP_ID');
data.origin = userStore.my.username;
data.datemsg = tools.getDateNow();
data.status = StatusMessage.WaitingToSend;
// Options
data.typesend = tools.SetBit(data.typesend, shared_consts.MessageOptions.Notify_ByEmail)
data.typesend = tools.SetBit(data.typesend, shared_consts.MessageOptions.Notify_ByPushNotification)
data.typesend = tools.SetBit(
data.typesend,
shared_consts.MessageOptions.Notify_ByEmail
);
data.typesend = tools.SetBit(
data.typesend,
shared_consts.MessageOptions.Notify_ByPushNotification
);
// console.log('DOPO:')
// console.table(data)
@@ -117,21 +133,30 @@ export const useMessageStore = defineStore('MessageStore', {
// console.log('res', res)
if (res.status === 200) {
if (res.data.code === serv_constants.RIS_CODE_OK) {
data._id = res.data.id
data._id = res.data.id;
const myarr = []
myarr.push(data)
const myarr = [];
myarr.push(data);
this.setMsg(myarr, data.dest)
return true
this.setMsg(myarr, data.dest);
return true;
}
}
return false
return false;
})
.catch((error) => {
console.error(error)
return false
})
console.error(error);
return false;
});
},
async chatBot(payload: any): Promise<any> {
const response = await Api.SendReq('/api/chatbot', 'POST', { payload });
if (response.status !== 200) {
throw new Error('Errore nella risposta del server');
}
return response.data; // Supponiamo che il backend ritorni un array di oggetti con id e title
},
},
})
});