2024-01-29 11:28:34 +07:00
|
|
|
import { defineStore } from "pinia";
|
|
|
|
|
import { ref } from "vue";
|
2024-02-02 17:17:30 +07:00
|
|
|
import { io } from "socket.io-client";
|
|
|
|
|
import { useQuasar } from "quasar";
|
|
|
|
|
import http from "@/plugins/http";
|
|
|
|
|
import config from "@/app.config";
|
|
|
|
|
import type {
|
|
|
|
|
SupportMessageResponse,
|
|
|
|
|
SupportIssueResponse,
|
|
|
|
|
SupportIssueCategoryResponse,
|
|
|
|
|
SupportMessageStatusResponse,
|
|
|
|
|
SupportUserStatus,
|
|
|
|
|
SupportMessageStatus,
|
|
|
|
|
} from "@/modules/00_support/interface/index/Main";
|
|
|
|
|
import keycloak from "@/plugins/keycloak";
|
|
|
|
|
import { useCounterMixin } from "@/stores/mixin";
|
2024-01-29 11:28:34 +07:00
|
|
|
|
2024-02-02 17:17:30 +07:00
|
|
|
export const useSupportStore = defineStore("supportServiceStore", () => {
|
|
|
|
|
const { showLoader, hideLoader, messageError } = useCounterMixin();
|
|
|
|
|
const $q = useQuasar();
|
2024-02-06 11:19:42 +07:00
|
|
|
const isOpen = ref<boolean>(false);
|
2024-02-05 17:57:41 +07:00
|
|
|
const icon = ref<string>("mdi-account-check");
|
2024-02-02 17:17:30 +07:00
|
|
|
const userId = ref<string | undefined>(keycloak.subject);
|
|
|
|
|
const userStatus = ref<SupportUserStatus[]>([]);
|
|
|
|
|
const issue = ref<SupportIssueResponse>();
|
|
|
|
|
const issueCategory = ref<SupportIssueCategoryResponse>();
|
|
|
|
|
const messageStatus = ref<SupportMessageStatusResponse>();
|
|
|
|
|
const message = ref<SupportMessageResponse>();
|
|
|
|
|
const currentIssue = ref<string>("");
|
|
|
|
|
const currentTitle = ref<string>("");
|
|
|
|
|
const items = ref<any>([{}, {}, {}, {}, {}, {}, {}]);
|
2024-02-05 17:57:41 +07:00
|
|
|
const scrollContainer = ref();
|
2024-02-02 17:17:30 +07:00
|
|
|
|
2024-02-06 10:03:45 +07:00
|
|
|
const socket = io(config.API.supportSocket, {
|
2024-02-02 17:17:30 +07:00
|
|
|
auth: { token: keycloak.token },
|
|
|
|
|
autoConnect: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on("users", (data: SupportUserStatus[]) => {
|
|
|
|
|
userStatus.value = data;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on("online", (r) => {
|
|
|
|
|
userStatus.value.push({
|
|
|
|
|
socketId: r.socketId,
|
|
|
|
|
userId: r.userId,
|
|
|
|
|
name: r.name,
|
|
|
|
|
role: r.role,
|
|
|
|
|
});
|
|
|
|
|
// console.log("online: ", userStatus.value);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on("offline", (socketId: string) => {
|
|
|
|
|
if (socketId === socket.id) return;
|
|
|
|
|
userStatus.value = userStatus.value.filter((v) => v.socketId !== socketId);
|
|
|
|
|
// console.log("offline: ", userStatus.value);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on("notify-message", (r) => {
|
|
|
|
|
if (issue.value) {
|
|
|
|
|
issue.value.result = issue.value.result.map((v) => {
|
|
|
|
|
if (v.id === r.issueId) {
|
|
|
|
|
v.unreadCount++;
|
|
|
|
|
v.lastMessage = r.content;
|
|
|
|
|
}
|
|
|
|
|
return v;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on("message", (r) => {
|
2024-02-05 17:57:41 +07:00
|
|
|
message.value?.result.message.push(r);
|
|
|
|
|
|
2024-02-02 17:17:30 +07:00
|
|
|
if (issue.value) {
|
|
|
|
|
issue.value.result = issue.value.result.map((v) => {
|
2024-02-05 17:57:41 +07:00
|
|
|
if (v.id === r.issueId) v.lastMessage = r.content;
|
2024-02-02 17:17:30 +07:00
|
|
|
return v;
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-02-05 17:57:41 +07:00
|
|
|
socket.emit("mark-read", { issueId: currentIssue.value });
|
|
|
|
|
scrollToEnd();
|
2024-02-02 17:17:30 +07:00
|
|
|
// console.log(r.id);
|
|
|
|
|
// console.log(message.value?.result.message);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on("read", (r: SupportMessageStatus) => {
|
|
|
|
|
if (messageStatus.value) {
|
|
|
|
|
messageStatus.value.result = messageStatus.value.result.map((v) => {
|
|
|
|
|
if (v.fromUserId !== r.fromUserId) return r;
|
|
|
|
|
return v;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// console.log("event(read):", messageStatus.value);
|
|
|
|
|
});
|
|
|
|
|
|
2024-02-05 17:57:41 +07:00
|
|
|
function scrollToEnd(position: Number = 1) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
scrollContainer.value?.setScrollPercentage("vertical", position);
|
|
|
|
|
}, 150);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-02 17:17:30 +07:00
|
|
|
function sendMessage(content: string, to: string) {
|
|
|
|
|
// console.log(content);
|
|
|
|
|
// console.log(to);
|
|
|
|
|
socket.emit("message", { to, content });
|
2024-02-05 17:57:41 +07:00
|
|
|
scrollToEnd();
|
2024-02-02 17:17:30 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fetchMessageStatus(issueId: string) {
|
|
|
|
|
showLoader();
|
|
|
|
|
const res = await http
|
|
|
|
|
.get(config.API.supportMessageStatus(issueId))
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
messageError($q, err);
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
hideLoader();
|
|
|
|
|
});
|
|
|
|
|
if (res && res.data) {
|
|
|
|
|
messageStatus.value = res.data;
|
|
|
|
|
// console.log(messageStatus.value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fetchMessage(issueId: string) {
|
|
|
|
|
showLoader();
|
|
|
|
|
const res = await http
|
|
|
|
|
.get(config.API.supportMessage(issueId))
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
messageError($q, err);
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
hideLoader();
|
|
|
|
|
});
|
|
|
|
|
if (res && res.data) {
|
|
|
|
|
message.value = await res.data;
|
|
|
|
|
message.value?.result.message.reverse();
|
|
|
|
|
socket.emit("join-issue", { issueId });
|
|
|
|
|
socket.emit("mark-read", { issueId });
|
2024-02-05 17:57:41 +07:00
|
|
|
scrollToEnd();
|
2024-02-02 17:17:30 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fetchIssue() {
|
|
|
|
|
if (!userId.value) return;
|
|
|
|
|
showLoader();
|
|
|
|
|
const res = await http
|
|
|
|
|
.get(config.API.supportIssueUserId(userId.value))
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
messageError($q, err);
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
hideLoader();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (res && res.data) {
|
|
|
|
|
issue.value = res.data;
|
|
|
|
|
// console.log(JSON.stringify(res.data, null, 2));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fetchIssueCategory() {
|
|
|
|
|
showLoader();
|
|
|
|
|
|
|
|
|
|
const res = await http
|
|
|
|
|
.get(config.API.supportIssueCategory)
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
messageError($q, err);
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
hideLoader();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (res && res.data) {
|
|
|
|
|
issueCategory.value = res.data;
|
|
|
|
|
// console.log(JSON.stringify(res.data, null, 2));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function newIssue(title: string, categoryId: string) {
|
|
|
|
|
showLoader();
|
|
|
|
|
|
|
|
|
|
const requestBody = {
|
|
|
|
|
title: title,
|
|
|
|
|
categoryId: categoryId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const res = await http
|
2024-02-05 17:57:41 +07:00
|
|
|
.post(config.API.supportNewIssue, requestBody)
|
2024-02-02 17:17:30 +07:00
|
|
|
.catch((err) => {
|
|
|
|
|
messageError($q, err);
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
hideLoader();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
|
fetchIssue();
|
|
|
|
|
// console.log(JSON.stringify(res.data, null, 2));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-29 11:28:34 +07:00
|
|
|
|
2024-02-05 17:57:41 +07:00
|
|
|
async function searchIssue(searchData: string) {
|
|
|
|
|
const res = await http
|
|
|
|
|
.get(config.API.supportSearchIssue(searchData))
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
messageError($q, err);
|
|
|
|
|
});
|
|
|
|
|
if (res && res.data) {
|
|
|
|
|
issue.value = res.data;
|
|
|
|
|
// console.log(issue.value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 11:28:34 +07:00
|
|
|
return {
|
2024-02-02 17:17:30 +07:00
|
|
|
userId,
|
|
|
|
|
issue,
|
|
|
|
|
issueCategory,
|
|
|
|
|
message,
|
|
|
|
|
messageStatus,
|
|
|
|
|
userStatus,
|
|
|
|
|
currentIssue,
|
|
|
|
|
currentTitle,
|
|
|
|
|
fetchIssue,
|
|
|
|
|
fetchIssueCategory,
|
|
|
|
|
fetchMessageStatus,
|
|
|
|
|
fetchMessage,
|
|
|
|
|
sendMessage,
|
|
|
|
|
newIssue,
|
2024-02-05 17:57:41 +07:00
|
|
|
searchIssue,
|
2024-02-06 11:19:42 +07:00
|
|
|
isOpen,
|
2024-02-02 17:17:30 +07:00
|
|
|
items,
|
|
|
|
|
socket,
|
2024-02-05 17:57:41 +07:00
|
|
|
scrollContainer,
|
|
|
|
|
icon,
|
2024-01-29 11:28:34 +07:00
|
|
|
};
|
|
|
|
|
});
|