2022-04-18 14:12:49 +00:00
|
|
|
import React, { useState, useEffect, useReducer, useContext, useRef} from "react";
|
2022-01-24 11:44:42 +00:00
|
|
|
import MainContainer from "../../components/MainContainer";
|
2022-01-27 00:33:16 +00:00
|
|
|
import api from "../../services/api";
|
2022-01-24 11:44:42 +00:00
|
|
|
import SelectField from "../../components/Report/SelectField";
|
2022-02-11 12:54:22 +00:00
|
|
|
//import { data } from '../../components/Report/MTable/data';
|
2022-01-24 11:44:42 +00:00
|
|
|
import DatePicker1 from '../../components/Report/DatePicker'
|
2022-04-18 14:12:49 +00:00
|
|
|
import DatePicker2 from '../../components/Report/DatePicker'
|
2022-01-24 11:44:42 +00:00
|
|
|
import MTable from "../../components/Report/MTable";
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Box from '@mui/material/Box';
|
2022-01-27 00:33:16 +00:00
|
|
|
import { AuthContext } from "../../context/Auth/AuthContext";
|
|
|
|
import { Can } from "../../components/Can";
|
2022-01-24 11:44:42 +00:00
|
|
|
|
2022-04-18 14:12:49 +00:00
|
|
|
import { Button } from "@material-ui/core";
|
|
|
|
|
|
|
|
|
|
|
|
// test del
|
|
|
|
|
|
|
|
// import ExportCSV from '../../components/Report/ExportCSV'
|
|
|
|
|
|
|
|
import { CSVLink } from "react-csv";
|
|
|
|
|
|
|
|
let columns = [
|
|
|
|
{
|
|
|
|
key: 'ticket.whatsapp.name',
|
|
|
|
label: 'Loja',
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
key: 'id',
|
|
|
|
label: 'id Mensagem',
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
key: 'ticket.id',
|
|
|
|
label: 'id Conversa',
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
key: 'ticket.contact.name',
|
|
|
|
label: 'Cliente',
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
key: 'ticket.user.name',
|
|
|
|
label: 'Atendente',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'body',
|
|
|
|
label: 'Mensagem',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'fromMe',
|
|
|
|
label: 'Sentido',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'createdAt',
|
|
|
|
label: 'Criada',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'ticket.contact.number',
|
|
|
|
label: 'Telefone cliente',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'ticket.queue.name',
|
|
|
|
label: 'Fila',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'ticket.status',
|
|
|
|
label: 'Status',
|
|
|
|
},
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2022-01-25 14:24:05 +00:00
|
|
|
|
|
|
|
const reducerQ = (state, action) =>{
|
|
|
|
|
|
|
|
if(action.type === 'LOAD_QUERY'){
|
|
|
|
|
2022-03-30 14:09:20 +00:00
|
|
|
|
2022-01-25 14:24:05 +00:00
|
|
|
const queries = action.payload
|
|
|
|
const newQueries = []
|
|
|
|
|
|
|
|
queries.forEach((query) => {
|
|
|
|
|
|
|
|
const queryIndex = state.findIndex((q) => q.id === query.id)
|
|
|
|
|
|
|
|
if(queryIndex !== -1){
|
|
|
|
state[queryIndex] = query
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
newQueries.push(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
return [...state, ...newQueries]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === "RESET") {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-24 11:44:42 +00:00
|
|
|
const reducer = (state, action) => {
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2022-01-24 11:44:42 +00:00
|
|
|
if (action.type === "LOAD_USERS") {
|
|
|
|
const users = action.payload;
|
|
|
|
const newUsers = [];
|
|
|
|
|
|
|
|
users.forEach((user) => {
|
|
|
|
const userIndex = state.findIndex((u) => u.id === user.id);
|
|
|
|
if (userIndex !== -1) {
|
|
|
|
state[userIndex] = user;
|
|
|
|
} else {
|
|
|
|
newUsers.push(user);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return [...state, ...newUsers];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === "RESET") {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2022-01-24 11:44:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
function Item(props) {
|
|
|
|
const { sx, ...other } = props;
|
|
|
|
return (
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
bgcolor: (theme) => (theme.palette.mode === 'dark' ? '#101010' : '#fff'),
|
|
|
|
color: (theme) => (theme.palette.mode === 'dark' ? 'grey.300' : 'grey.800'),
|
|
|
|
border: '1px solid',
|
|
|
|
borderColor: (theme) =>
|
|
|
|
theme.palette.mode === 'dark' ? 'grey.800' : 'grey.300',
|
|
|
|
p: 1,
|
|
|
|
m: 1,
|
|
|
|
borderRadius: 2,
|
|
|
|
fontSize: '0.875rem',
|
|
|
|
fontWeight: '700',
|
|
|
|
...sx,
|
|
|
|
}}
|
|
|
|
{...other}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Item.propTypes = {
|
|
|
|
sx: PropTypes.oneOfType([
|
|
|
|
PropTypes.arrayOf(
|
|
|
|
PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool]),
|
|
|
|
),
|
|
|
|
PropTypes.func,
|
|
|
|
PropTypes.object,
|
|
|
|
]),
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let columnsData = [
|
2022-04-18 14:12:49 +00:00
|
|
|
{ title: 'Unidade', field: 'whatsapp.name' },
|
2022-01-24 11:44:42 +00:00
|
|
|
{ title: 'Atendente', field: 'user.name' },
|
|
|
|
{ title: 'Contato', field: 'contact.number' },
|
2022-04-17 21:02:15 +00:00
|
|
|
{ title: 'Nome', field: 'contact.name' },
|
2022-01-24 11:44:42 +00:00
|
|
|
{ title: 'Assunto', field: 'queue.name' },
|
|
|
|
{ title: 'Status', field: 'status' },
|
|
|
|
{ title: 'Criado', field: 'createdAt' },
|
|
|
|
|
|
|
|
{ title: 'Atualizado', field: 'updatedAt',
|
|
|
|
|
|
|
|
/*cellStyle: {
|
|
|
|
backgroundColor: '#039be5',
|
|
|
|
color: '#FFF'
|
|
|
|
},
|
|
|
|
headerStyle: {
|
|
|
|
backgroundColor: '#039be5',
|
|
|
|
fontSize: 12
|
|
|
|
}*/
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
];
|
|
|
|
|
2022-01-25 14:24:05 +00:00
|
|
|
/*const currencies = [
|
2022-01-24 11:44:42 +00:00
|
|
|
{
|
|
|
|
value: '1',
|
|
|
|
label: 'Adriano',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '2',
|
|
|
|
label: 'Aguinaldo',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '3',
|
|
|
|
label: 'Maria',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '4',
|
|
|
|
label: 'Suely',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: '0',
|
|
|
|
label: '',
|
|
|
|
},
|
|
|
|
|
2022-01-25 14:24:05 +00:00
|
|
|
];*/
|
2022-01-24 11:44:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const Report = () => {
|
2022-04-18 14:12:49 +00:00
|
|
|
|
|
|
|
const csvLink = useRef()
|
2022-01-27 00:33:16 +00:00
|
|
|
|
|
|
|
const { user: userA } = useContext(AuthContext);
|
2022-01-24 11:44:42 +00:00
|
|
|
|
|
|
|
//--------
|
2022-02-11 12:54:22 +00:00
|
|
|
const [searchParam] = useState("");
|
|
|
|
//const [loading, setLoading] = useState(false);
|
|
|
|
//const [hasMore, setHasMore] = useState(false);
|
2022-01-24 11:44:42 +00:00
|
|
|
const [pageNumber, setPageNumber] = useState(1);
|
2022-01-27 00:33:16 +00:00
|
|
|
const [users, dispatch] = useReducer(reducer, []);
|
2022-02-11 12:54:22 +00:00
|
|
|
//const [columns, setColums] = useState([])
|
2022-01-25 14:24:05 +00:00
|
|
|
const [startDate, setDatePicker1] = useState(new Date())
|
|
|
|
const [endDate, setDatePicker2] = useState(new Date())
|
2022-01-27 00:33:16 +00:00
|
|
|
const [userId, setUser] = useState(null)
|
2022-01-25 14:24:05 +00:00
|
|
|
const [query, dispatchQ] = useReducer(reducerQ, [])
|
|
|
|
|
2022-04-18 14:12:49 +00:00
|
|
|
const [dataCSV, setDataCSV] = useState([])
|
|
|
|
const [isMount, setIsMount] = useState(true);
|
|
|
|
|
2022-01-24 11:44:42 +00:00
|
|
|
useEffect(() => {
|
2022-01-25 21:54:46 +00:00
|
|
|
dispatch({ type: "RESET" });
|
2022-01-25 14:24:05 +00:00
|
|
|
dispatchQ({ type: "RESET" })
|
|
|
|
|
2022-01-24 11:44:42 +00:00
|
|
|
setPageNumber(1);
|
|
|
|
}, [searchParam]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-02-11 12:54:22 +00:00
|
|
|
//setLoading(true);
|
2022-01-25 21:54:46 +00:00
|
|
|
|
2022-01-24 11:44:42 +00:00
|
|
|
const delayDebounceFn = setTimeout(() => {
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2022-01-24 11:44:42 +00:00
|
|
|
const fetchUsers = async () => {
|
|
|
|
try {
|
|
|
|
const { data } = await api.get("/users/", {
|
|
|
|
params: { searchParam, pageNumber },
|
2022-01-25 14:24:05 +00:00
|
|
|
});
|
2022-01-25 21:54:46 +00:00
|
|
|
|
2022-01-24 11:44:42 +00:00
|
|
|
dispatch({ type: "LOAD_USERS", payload: data.users });
|
2022-02-11 12:54:22 +00:00
|
|
|
//setHasMore(data.hasMore);
|
|
|
|
//setLoading(false);
|
2022-01-24 11:44:42 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
};
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2022-01-24 11:44:42 +00:00
|
|
|
fetchUsers();
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2022-01-24 11:44:42 +00:00
|
|
|
}, 500);
|
|
|
|
return () => clearTimeout(delayDebounceFn);
|
|
|
|
}, [searchParam, pageNumber]);
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-25 14:24:05 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
2022-02-11 12:54:22 +00:00
|
|
|
//setLoading(true);
|
2022-01-25 14:24:05 +00:00
|
|
|
|
|
|
|
const delayDebounceFn = setTimeout(() => {
|
|
|
|
|
|
|
|
const fetchQueries = async () => {
|
|
|
|
try {
|
2022-01-25 21:54:46 +00:00
|
|
|
|
2022-01-26 02:05:48 +00:00
|
|
|
const dataQuery = await api.get("/reports/", {params: {userId, startDate, endDate },});
|
2022-01-25 21:54:46 +00:00
|
|
|
dispatchQ({ type: "RESET" })
|
2022-01-26 02:05:48 +00:00
|
|
|
dispatchQ({ type: "LOAD_QUERY", payload: dataQuery.data });
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2022-02-11 12:54:22 +00:00
|
|
|
//setLoading(false);
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2022-04-18 14:12:49 +00:00
|
|
|
// console.log('dataQuery: ', dataQuery.data)
|
|
|
|
|
|
|
|
console.log()
|
2022-04-17 21:02:15 +00:00
|
|
|
|
2022-01-25 14:24:05 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fetchQueries();
|
2022-01-24 11:44:42 +00:00
|
|
|
|
2022-01-25 14:24:05 +00:00
|
|
|
}, 500);
|
|
|
|
return () => clearTimeout(delayDebounceFn);
|
2022-01-24 11:44:42 +00:00
|
|
|
|
2022-01-25 14:24:05 +00:00
|
|
|
}, [userId, startDate, endDate]);
|
|
|
|
|
2022-01-24 11:44:42 +00:00
|
|
|
|
|
|
|
// Get from child 1
|
2022-01-25 14:24:05 +00:00
|
|
|
const datePicker1Value = (data) => {
|
2022-03-30 14:09:20 +00:00
|
|
|
|
2022-01-24 11:44:42 +00:00
|
|
|
setDatePicker1(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get from child 2
|
|
|
|
const datePicker2Value = (data) => {
|
2022-03-30 14:09:20 +00:00
|
|
|
|
2022-01-24 11:44:42 +00:00
|
|
|
setDatePicker2(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get from child 3
|
|
|
|
const textFieldSelectUser = (data) => {
|
2022-03-30 14:09:20 +00:00
|
|
|
|
2022-01-24 11:44:42 +00:00
|
|
|
setUser(data)
|
|
|
|
}
|
2022-04-18 14:12:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-02-11 12:54:22 +00:00
|
|
|
|
2022-04-18 14:12:49 +00:00
|
|
|
|
|
|
|
// test del
|
|
|
|
|
|
|
|
const handleCSVMessages = () =>{
|
2022-01-27 00:33:16 +00:00
|
|
|
|
2022-04-18 14:12:49 +00:00
|
|
|
// setLoading(true);
|
|
|
|
|
|
|
|
const fetchQueries = async () => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const dataQuery = await api.get("/reports/messages", {params: {userId, startDate, endDate },});
|
|
|
|
|
|
|
|
// console.log('dataQuery messages: ', dataQuery.data)
|
|
|
|
|
|
|
|
if(dataQuery.data.length > 0){
|
|
|
|
|
|
|
|
let dataCSVFormat = dataQuery.data ;
|
|
|
|
|
|
|
|
for(var i = 0; i<dataCSVFormat.length; i++){
|
|
|
|
if(dataCSVFormat[i].fromMe){
|
|
|
|
dataCSVFormat[i].fromMe = 'Atendente'
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
dataCSVFormat[i].fromMe = 'Cliente'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.log('dataCSVFormat: ', dataCSVFormat)
|
|
|
|
|
|
|
|
setDataCSV(dataCSVFormat)
|
|
|
|
// setDataCSV(dataQuery.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setLoading(false);
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fetchQueries();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
|
|
if(isMount){
|
|
|
|
setIsMount(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
csvLink.current.link.click()
|
|
|
|
|
|
|
|
}, [dataCSV]);
|
|
|
|
|
|
|
|
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2022-01-27 00:33:16 +00:00
|
|
|
return (
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2022-01-27 00:33:16 +00:00
|
|
|
<Can
|
|
|
|
role={userA.profile}
|
|
|
|
perform="ticket-report:show"
|
|
|
|
yes={() => (
|
2022-01-25 21:54:46 +00:00
|
|
|
|
2022-01-27 00:33:16 +00:00
|
|
|
<MainContainer>
|
|
|
|
<Box sx={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)' }}>
|
2022-01-25 21:54:46 +00:00
|
|
|
|
2022-03-06 19:37:09 +00:00
|
|
|
<Item><SelectField func={textFieldSelectUser} emptyField={true} header={'Usuário'} currencies={users.map((obj)=>{
|
2022-01-25 14:24:05 +00:00
|
|
|
return {'value': obj.id, 'label': obj.name}
|
2022-01-27 00:33:16 +00:00
|
|
|
})}/></Item>
|
2022-01-25 14:24:05 +00:00
|
|
|
|
2022-03-21 05:06:56 +00:00
|
|
|
<Item><DatePicker1 func={datePicker1Value} minDate={false} startEmpty={false} title={'Data inicio'}/></Item>
|
|
|
|
<Item><DatePicker2 func={datePicker2Value} minDate={false} startEmpty={false} title={'Data fim'}/></Item>
|
2022-01-24 11:44:42 +00:00
|
|
|
|
|
|
|
<Item sx={{ gridColumn: '4 / 5' }}>
|
2022-04-18 14:12:49 +00:00
|
|
|
|
|
|
|
<Button
|
|
|
|
variant="contained"
|
|
|
|
color="primary"
|
|
|
|
onClick={(e) => handleCSVMessages()}
|
|
|
|
>
|
|
|
|
{"CSV ALL"}
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<CSVLink
|
|
|
|
data={dataCSV}
|
|
|
|
headers={columns}
|
|
|
|
filename= {'Relatorio_detalhado_atendimento_atendentes.csv'}
|
|
|
|
target={'_blank'}
|
|
|
|
ref={csvLink}/>
|
|
|
|
</div>
|
|
|
|
|
2022-01-24 11:44:42 +00:00
|
|
|
</Item>
|
|
|
|
|
|
|
|
</Box>
|
|
|
|
|
|
|
|
<Box sx={{
|
|
|
|
display: 'grid',
|
|
|
|
}}>
|
2022-01-27 00:33:16 +00:00
|
|
|
|
|
|
|
<Item sx={{ gridColumn: '1', gridRow: 'span 1' }}>
|
2022-01-26 23:56:57 +00:00
|
|
|
<MTable data={query}
|
|
|
|
columns={columnsData}
|
2022-03-21 05:06:56 +00:00
|
|
|
hasChild={true}
|
|
|
|
removeClickRow={false}
|
2022-01-27 00:33:16 +00:00
|
|
|
table_title={'Relatório de atendimento por atendentes'}/>
|
2022-01-25 14:24:05 +00:00
|
|
|
</Item>
|
2022-01-24 11:44:42 +00:00
|
|
|
|
2022-01-27 00:33:16 +00:00
|
|
|
</Box>
|
2022-01-24 11:44:42 +00:00
|
|
|
</MainContainer>
|
2022-01-27 00:33:16 +00:00
|
|
|
|
|
|
|
)}
|
|
|
|
/>
|
2022-01-24 11:44:42 +00:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Report;
|