Skip to content

Commit

Permalink
Horário de atendimeto por setor
Browse files Browse the repository at this point in the history
  • Loading branch information
rtenorioh authored Jun 18, 2022
1 parent 5d7ab7b commit 0a86350
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 20 deletions.
4 changes: 2 additions & 2 deletions backend/src/controllers/QueueController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
};

export const store = async (req: Request, res: Response): Promise<Response> => {
const { name, color, greetingMessage } = req.body;
const { name, color, greetingMessage, startWork, endWork, absenceMessage } = req.body;

const queue = await CreateQueueService({ name, color, greetingMessage });
const queue = await CreateQueueService({ name, color, greetingMessage, startWork, endWork, absenceMessage });

const io = getIO();
io.emit("queue", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ module.exports = {
greetingMessage: {
type: DataTypes.TEXT
},
startWork: {
type: DataTypes.STRING
},
endWork: {
type: DataTypes.STRING
},
absenceMessage: {
type: DataTypes.TEXT
},
createdAt: {
type: DataTypes.DATE,
allowNull: false
Expand Down
9 changes: 9 additions & 0 deletions backend/src/models/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class Queue extends Model<Queue> {
@Column
greetingMessage: string;

@Column
startWork: string;

@Column
endWork: string;

@Column
absenceMessage: string;

@CreatedAt
createdAt: Date;

Expand Down
3 changes: 3 additions & 0 deletions backend/src/services/QueueService/CreateQueueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ interface QueueData {
name: string;
color: string;
greetingMessage?: string;
startWork?: string;
endWork?: string;
absenceMessage?: string;
}

const CreateQueueService = async (queueData: QueueData): Promise<Queue> => {
Expand Down
3 changes: 3 additions & 0 deletions backend/src/services/QueueService/UpdateQueueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ interface QueueData {
name?: string;
color?: string;
greetingMessage?: string;
startWork?: string;
endWork?: string;
absenceMessage?: string;
}

const UpdateQueueService = async (
Expand Down
56 changes: 45 additions & 11 deletions backend/src/services/WbotServices/wbotMessageListener.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Settings from "../../models/Setting";
import ListSettingsServiceOne from "../SettingServices/ListSettingsServiceOne";
import { join } from "path";
import { promisify } from "util";
import { writeFile } from "fs";
Expand All @@ -15,11 +13,13 @@ import {
import Contact from "../../models/Contact";
import Ticket from "../../models/Ticket";
import Message from "../../models/Message";
import Settings from "../../models/Setting";

import { getIO } from "../../libs/socket";
import CreateMessageService from "../MessageServices/CreateMessageService";
import { logger } from "../../utils/logger";
import CreateOrUpdateContactService from "../ContactServices/CreateOrUpdateContactService";
import ListSettingsServiceOne from "../SettingServices/ListSettingsServiceOne";
import FindOrCreateTicketService from "../TicketServices/FindOrCreateTicketService";
import ShowWhatsAppService from "../WhatsappService/ShowWhatsAppService";
import { debounce } from "../../helpers/Debounce";
Expand All @@ -28,6 +28,7 @@ import CreateContactService from "../ContactServices/CreateContactService";
// import GetContactService from "../ContactServices/GetContactService";
import formatBody from "../../helpers/Mustache";


interface Session extends Client {
id?: number;
}
Expand Down Expand Up @@ -161,25 +162,58 @@ const verifyQueue = async (
ticketId: ticket.id,
});

const chat = await msg.getChat();
await chat.sendStateTyping();
const Hr = new Date();

const body = formatBody(
`\u200e${choosenQueue.greetingMessage}`,
contact
);
const hh: number = Hr.getHours() * 60 * 60;
const mm: number = Hr.getMinutes() * 60;
const hora = hh + mm;

const inicio: string = choosenQueue.startWork;
const hhinicio = Number(inicio.split(':')[0]) * 60 * 60;
const mminicio = Number(inicio.split(':')[1]) * 60;
const horainicio = hhinicio + mminicio;

const sentMessage = await wbot.sendMessage(`${contact.number}@c.us`, body);
const termino: string = choosenQueue.endWork;
const hhtermino = Number(termino.split(':')[0]) * 60 * 60;
const mmtermino = Number(termino.split(':')[1]) * 60;
const horatermino = hhtermino + mmtermino;

await verifyMessage(sentMessage, ticket, contact);
if ((hora < horainicio) || (hora > horatermino)) {

const body = formatBody(`\u200e${choosenQueue.absenceMessage}`, contact);
const debouncedSentMessage = debounce(
async () => {
const sentMessage = await wbot.sendMessage(`${contact.number}@c.us`, body);
verifyMessage(sentMessage, ticket, contact);
},
3000,
ticket.id
);

debouncedSentMessage();

} else {

const chat = await msg.getChat();
await chat.sendStateTyping();

const body = formatBody(
`\u200e${choosenQueue.greetingMessage}`,
contact
);

const sentMessage = await wbot.sendMessage(`${contact.number}@c.us`, body);

await verifyMessage(sentMessage, ticket, contact);
}
} else {
let options = "";

const chat = await msg.getChat();
await chat.sendStateTyping();

queues.forEach((queue, index) => {
options += `*${index + 1}* - ${queue.name}\n`;
options += `*${index + 1}* - ${queue.name} das ${queue.startWork} as ${queue.endWork}\n`;
});

const body = formatBody(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const ListWhatsAppsService = async (): Promise<Whatsapp[]> => {
{
model: Queue,
as: "queues",
attributes: ["id", "name", "color", "greetingMessage"]
attributes: ["id", "name", "color", "greetingMessage", "startWork", "endWork", "absenceMessage"]
}
]
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const ShowWhatsAppService = async (id: string | number): Promise<Whatsapp> => {
{
model: Queue,
as: "queues",
attributes: ["id", "name", "color", "greetingMessage"]
attributes: ["id", "name", "color", "greetingMessage", "startWork", "endWork", "absenceMessage"]
}
],
order: [["queues", "name", "ASC"]]
Expand Down
96 changes: 92 additions & 4 deletions frontend/src/components/QueueModal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ const useStyles = makeStyles(theme => ({
marginRight: theme.spacing(1),
flex: 1,
},

container: {
display: 'flex',
flexWrap: 'wrap',
},
btnWrapper: {
position: "relative",
},

buttonProgress: {
color: green[500],
position: "absolute",
Expand All @@ -44,10 +46,12 @@ const useStyles = makeStyles(theme => ({
marginTop: -12,
marginLeft: -12,
},

formControl: {
margin: theme.spacing(1),
minWidth: 120,
},

colorAdorment: {
width: 20,
height: 20,
Expand All @@ -61,6 +65,9 @@ const QueueSchema = Yup.object().shape({
.required("Required"),
color: Yup.string().min(3, "Too Short!").max(9, "Too Long!").required(),
greetingMessage: Yup.string(),
startWork: Yup.string(),
endWork: Yup.string(),
absenceMessage: Yup.string()
});

const QueueModal = ({ open, onClose, queueId }) => {
Expand All @@ -70,11 +77,17 @@ const QueueModal = ({ open, onClose, queueId }) => {
name: "",
color: "",
greetingMessage: "",
startWork: "",
endWork: "",
absenceMessage: ""
};

const [colorPickerModalOpen, setColorPickerModalOpen] = useState(false);
const [queue, setQueue] = useState(initialState);
const greetingRef = useRef();
const absenceRef = useRef();
const startWorkRef = useRef();
const endWorkRef = useRef();

useEffect(() => {
(async () => {
Expand All @@ -94,6 +107,9 @@ const QueueModal = ({ open, onClose, queueId }) => {
name: "",
color: "",
greetingMessage: "",
startWork: "",
endWork: "",
absenceMessage: ""
});
};
}, [queueId, open]);
Expand All @@ -110,7 +126,7 @@ const QueueModal = ({ open, onClose, queueId }) => {
} else {
await api.post("/queue", values);
}
toast.success("Queue saved successfully");
toast.success(`${i18n.t("queueModal.notification.title")}`);
handleClose();
} catch (err) {
toastError(err);
Expand Down Expand Up @@ -200,7 +216,7 @@ const QueueModal = ({ open, onClose, queueId }) => {
type="greetingMessage"
multiline
inputRef={greetingRef}
rows={5}
rows={4}
fullWidth
name="greetingMessage"
error={
Expand All @@ -213,6 +229,78 @@ const QueueModal = ({ open, onClose, queueId }) => {
margin="dense"
/>
</div>
<form className={classes.container} noValidate>
<Field
as={TextField}
label={i18n.t("queueModal.form.startWork")}
type="time"
ampm={false}
defaultValue="08:00"
inputRef={startWorkRef}
InputLabelProps={{
shrink: true,
}}
inputProps={{
step: 600, // 5 min
}}
fullWidth
name="startWork"
error={
touched.startWork && Boolean(errors.startWork)
}
helperText={
touched.startWork && errors.startWork
}
variant="outlined"
margin="dense"
className={classes.textField}
/>
<Field
as={TextField}
label={i18n.t("queueModal.form.endWork")}
type="time"
ampm={false}
defaultValue="18:00"
inputRef={endWorkRef}
InputLabelProps={{
shrink: true,
}}
inputProps={{
step: 600, // 5 min
}}
fullWidth
name="endWork"
error={
touched.endWork && Boolean(errors.endWork)
}
helperText={
touched.endWork && errors.endWork
}
variant="outlined"
margin="dense"
className={classes.textField}
/>
</form>
<div>
<Field
as={TextField}
label={i18n.t("queueModal.form.absenceMessage")}
type="absenceMessage"
multiline
inputRef={absenceRef}
rows={2}
fullWidth
name="absenceMessage"
error={
touched.absenceMessage && Boolean(errors.absenceMessage)
}
helperText={
touched.absenceMessage && errors.absenceMessage
}
variant="outlined"
margin="dense"
/>
</div>
</DialogContent>
<DialogActions>
<Button
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/pages/Queues/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const Queues = () => {
const handleDeleteQueue = async (queueId) => {
try {
await api.delete(`/queue/${queueId}`);
toast.success(i18n.t("Queue deleted successfully!"));
toast.success(i18n.t("queues.notifications.queueDeleted"));
} catch (err) {
toastError(err);
}
Expand Down Expand Up @@ -203,6 +203,12 @@ const Queues = () => {
<TableCell align="center">
{i18n.t("queues.table.greeting")}
</TableCell>
<TableCell align="center">
{i18n.t("queues.table.startWork")}
</TableCell>
<TableCell align="center">
{i18n.t("queues.table.endWork")}
</TableCell>
<TableCell align="center">
{i18n.t("queues.table.actions")}
</TableCell>
Expand Down Expand Up @@ -236,6 +242,8 @@ const Queues = () => {
</Typography>
</div>
</TableCell>
<TableCell align="center">{queue.startWork}</TableCell>
<TableCell align="center">{queue.endWork}</TableCell>
<TableCell align="center">
<IconButton
size="small"
Expand Down
Loading

0 comments on commit 0a86350

Please sign in to comment.