Files
myprojplanet_vite/src/store/MessageStore.ts

163 lines
5.0 KiB
TypeScript
Raw Normal View History

2025-08-09 00:48:44 +02:00
import { defineStore } from 'pinia';
import { Api } from '@api';
2021-09-02 03:22:13 +02:00
2025-08-09 00:48:44 +02:00
import { serv_constants } from './Modules/serv_constants';
2021-09-02 03:22:13 +02:00
2025-08-09 00:48:44 +02:00
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';
2021-09-02 03:22:13 +02:00
2021-09-02 21:29:24 +02:00
export const useMessageStore = defineStore('MessageStore', {
state: (): IMessageState => ({
last_msgs: [],
users_msg: [],
}),
2021-09-02 03:22:13 +02:00
2021-09-02 21:29:24 +02:00
getters: {
getlasts_messages: (mystate: IMessageState) => (): IMessage[] => {
2025-08-09 00:48:44 +02:00
const ctrec = mystate.last_msgs ? mystate.last_msgs.slice(0, 5) : [];
2021-09-02 21:29:24 +02:00
// const ctrec = (mystate.msgs) ? mystate.msgs.slice().reverse().slice(0, 5) : []
2025-08-09 00:48:44 +02:00
return ctrec;
2021-09-02 21:29:24 +02:00
},
getnumMsgUnread: (mystate: IMessageState) => () => {
2025-08-09 00:48:44 +02:00
return mystate.last_msgs.filter((msg) => !msg.read).length;
2021-09-02 21:29:24 +02:00
},
},
actions: {
setMsg(arrmsg: IMessage[], username: string) {
// console.log('arrmsg', arrmsg)
2025-08-09 00:48:44 +02:00
const userStore = useUserStore();
2021-09-02 03:22:13 +02:00
2021-09-02 21:29:24 +02:00
if (arrmsg.length > 0) {
2025-08-09 00:48:44 +02:00
let users_msg: any = this.users_msg.find(
(rec: IMsgUsers) => rec.username === username
);
2021-09-02 21:29:24 +02:00
if (!users_msg) {
2025-08-09 00:48:44 +02:00
this.users_msg.push({ username, msgs: [] });
users_msg = this.users_msg.find((rec) => rec.username === username);
2021-09-02 03:22:13 +02:00
}
2025-08-09 00:48:44 +02:00
users_msg.msgs.push(...arrmsg);
2021-09-02 21:29:24 +02:00
// console.table(users_msg.msgs)
// users_msg.msgs = tools.getUnique(users_msg.msgs, '_id')
// console.table(users_msg.msgs)
if (users_msg.msgs) {
2025-08-09 00:48:44 +02:00
let userother: any = users_msg.msgs.slice(-1)[0].dest;
2021-09-02 21:29:24 +02:00
if (userother === userStore.my.username)
2025-08-09 00:48:44 +02:00
userother = users_msg.msgs.slice(-1)[0].origin;
2021-09-02 21:29:24 +02:00
2025-08-09 00:48:44 +02:00
let index = this.last_msgs.findIndex(
(rec: IMessage) => rec.dest === userother || rec.origin === userother
);
2021-09-02 21:29:24 +02:00
if (index >= 0) {
// Update last message
2025-08-09 00:48:44 +02:00
this.last_msgs[index] = users_msg.msgs.slice(-1)[0];
2021-09-02 21:29:24 +02:00
} else {
2025-08-09 00:48:44 +02:00
this.last_msgs.push(users_msg.msgs.slice(-1)[0]);
index = this.last_msgs.findIndex(
(rec: IMessage) => rec.dest === userother || rec.origin === userother
);
2021-09-02 03:22:13 +02:00
}
2021-09-02 21:29:24 +02:00
if (this.last_msgs[index])
2025-08-09 00:48:44 +02:00
users_msg.lastdataread = this.last_msgs[index].datemsg;
else users_msg.lastdataread = tools.getLastDateReadReset();
2021-09-02 21:29:24 +02:00
} else {
2025-08-09 00:48:44 +02:00
users_msg.lastdataread = tools.getLastDateReadReset();
2021-09-02 03:22:13 +02:00
}
2021-09-02 21:29:24 +02:00
// console.log('RICeVUTO', arrmsg, 'lastdataread', users_msg.lastdataread)
// console.log('this.users_msg', users_msg)
}
},
2025-08-09 00:48:44 +02:00
async updateMsgDataFromServer({
username,
lastdataread,
}: {
username: string;
lastdataread: Date;
}) {
2021-09-02 21:29:24 +02:00
// console.log('updateMsgDataFromServer', username, lastdataread)
2021-09-02 03:22:13 +02:00
2025-08-09 00:48:44 +02:00
return Api.SendReq(
`/sendmsg/${username}/${lastdataread}/${tools.getEnv('VITE_APP_ID')}`,
'GET',
null
)
2021-09-02 21:29:24 +02:00
.then((res) => {
// console.log('res', res)
if (res.status === 200) {
2025-08-09 00:48:44 +02:00
this.setMsg(res.data.arrmsg, username);
return true;
2021-09-02 21:29:24 +02:00
}
2025-08-09 00:48:44 +02:00
return false;
2021-09-02 21:29:24 +02:00
})
.catch((error) => {
2025-08-09 00:48:44 +02:00
console.error(error);
return false;
});
2021-09-02 21:29:24 +02:00
},
2021-09-02 03:22:13 +02:00
2021-09-02 21:29:24 +02:00
async SendMsgEvent(msg: IMessage) {
2025-08-09 00:48:44 +02:00
console.log('SendMsgEvent', msg);
2021-09-02 03:22:13 +02:00
2025-08-09 00:48:44 +02:00
const data: IMessage = { ...MsgDefault, ...msg };
2021-09-02 03:22:13 +02:00
2025-08-09 00:48:44 +02:00
const userStore = useUserStore();
2021-09-02 21:29:24 +02:00
2025-08-09 00:48:44 +02:00
data.source!.page = '';
data.idapp = tools.getEnv('VITE_APP_ID');
data.origin = userStore.my.username;
data.datemsg = tools.getDateNow();
data.status = StatusMessage.WaitingToSend;
2021-09-02 21:29:24 +02:00
// Options
2025-08-09 00:48:44 +02:00
data.typesend = tools.SetBit(
data.typesend,
shared_consts.MessageOptions.Notify_ByEmail
);
data.typesend = tools.SetBit(
data.typesend,
shared_consts.MessageOptions.Notify_ByPushNotification
);
2021-09-02 21:29:24 +02:00
// console.log('DOPO:')
// console.table(data)
2021-09-02 03:22:13 +02:00
2021-09-02 21:29:24 +02:00
return Api.SendReq('/sendmsg', 'POST', data)
.then((res) => {
// console.log('res', res)
if (res.status === 200) {
if (res.data.code === serv_constants.RIS_CODE_OK) {
2025-08-09 00:48:44 +02:00
data._id = res.data.id;
2021-09-02 21:29:24 +02:00
2025-08-09 00:48:44 +02:00
const myarr = [];
myarr.push(data);
2021-09-02 21:29:24 +02:00
2025-08-09 00:48:44 +02:00
this.setMsg(myarr, data.dest);
return true;
2021-09-02 21:29:24 +02:00
}
}
2025-08-09 00:48:44 +02:00
return false;
2021-09-02 21:29:24 +02:00
})
.catch((error) => {
2025-08-09 00:48:44 +02:00
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
2021-09-02 21:29:24 +02:00
},
},
2025-08-09 00:48:44 +02:00
});