mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 20:29:17 +00:00
feat: set default whatsapp account in frontend
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
const Yup = require("yup");
|
||||||
const Whatsapp = require("../models/Whatsapp");
|
const Whatsapp = require("../models/Whatsapp");
|
||||||
const { getIO } = require("../libs/socket");
|
const { getIO } = require("../libs/socket");
|
||||||
const { getWbot, initWbot, removeWbot } = require("../libs/wbot");
|
const { getWbot, initWbot, removeWbot } = require("../libs/wbot");
|
||||||
@@ -5,13 +6,39 @@ const wbotMessageListener = require("../services/wbotMessageListener");
|
|||||||
const wbotMonitor = require("../services/wbotMonitor");
|
const wbotMonitor = require("../services/wbotMonitor");
|
||||||
|
|
||||||
exports.index = async (req, res) => {
|
exports.index = async (req, res) => {
|
||||||
const whatsapp = await Whatsapp.findAll();
|
const whatsapps = await Whatsapp.findAll();
|
||||||
|
|
||||||
return res.status(200).json(whatsapp);
|
return res.status(200).json(whatsapps);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.store = async (req, res) => {
|
exports.store = async (req, res) => {
|
||||||
|
const schema = Yup.object().shape({
|
||||||
|
name: Yup.string().required().min(2),
|
||||||
|
default: Yup.boolean()
|
||||||
|
.required()
|
||||||
|
.test(
|
||||||
|
"Check-default",
|
||||||
|
"Only one default whatsapp is permited",
|
||||||
|
async value => {
|
||||||
|
// console.log("cai no if", value);
|
||||||
|
if (value === true) {
|
||||||
|
const defaultFound = await Whatsapp.findOne({
|
||||||
|
where: { default: true },
|
||||||
|
});
|
||||||
|
return !Boolean(defaultFound);
|
||||||
|
} else return true;
|
||||||
|
}
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
await schema.validate(req.body);
|
||||||
|
} catch (err) {
|
||||||
|
return res.status(400).json({ error: err.message });
|
||||||
|
}
|
||||||
|
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
|
|
||||||
const whatsapp = await Whatsapp.create(req.body);
|
const whatsapp = await Whatsapp.create(req.body);
|
||||||
|
|
||||||
if (!whatsapp) {
|
if (!whatsapp) {
|
||||||
@@ -45,6 +72,31 @@ exports.show = async (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.update = async (req, res) => {
|
exports.update = async (req, res) => {
|
||||||
|
const schema = Yup.object().shape({
|
||||||
|
name: Yup.string().required().min(2),
|
||||||
|
default: Yup.boolean()
|
||||||
|
.required()
|
||||||
|
.test(
|
||||||
|
"Check-default",
|
||||||
|
"Only one default whatsapp is permited",
|
||||||
|
async value => {
|
||||||
|
// console.log("cai no if", value);
|
||||||
|
if (value === true) {
|
||||||
|
const defaultFound = await Whatsapp.findOne({
|
||||||
|
where: { default: true },
|
||||||
|
});
|
||||||
|
return !Boolean(defaultFound);
|
||||||
|
} else return true;
|
||||||
|
}
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
await schema.validate(req.body);
|
||||||
|
} catch (err) {
|
||||||
|
return res.status(400).json({ error: err.message });
|
||||||
|
}
|
||||||
|
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
const { whatsappId } = req.params;
|
const { whatsappId } = req.params;
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { toast } from "react-toastify";
|
|||||||
import { format, parseISO } from "date-fns";
|
import { format, parseISO } from "date-fns";
|
||||||
|
|
||||||
import { makeStyles } from "@material-ui/core/styles";
|
import { makeStyles } from "@material-ui/core/styles";
|
||||||
|
import { green } from "@material-ui/core/colors";
|
||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
TableBody,
|
TableBody,
|
||||||
@@ -14,7 +15,7 @@ import {
|
|||||||
TableHead,
|
TableHead,
|
||||||
Paper,
|
Paper,
|
||||||
} from "@material-ui/core";
|
} from "@material-ui/core";
|
||||||
import { Edit, DeleteOutline } from "@material-ui/icons";
|
import { Edit, DeleteOutline, CheckCircle } from "@material-ui/icons";
|
||||||
|
|
||||||
import MainContainer from "../../components/MainContainer";
|
import MainContainer from "../../components/MainContainer";
|
||||||
import MainHeader from "../../components/MainHeader";
|
import MainHeader from "../../components/MainHeader";
|
||||||
@@ -228,6 +229,7 @@ const WhatsApps = () => {
|
|||||||
<TableCell align="center">Name</TableCell>
|
<TableCell align="center">Name</TableCell>
|
||||||
<TableCell align="center">Status</TableCell>
|
<TableCell align="center">Status</TableCell>
|
||||||
<TableCell align="center">Last update</TableCell>
|
<TableCell align="center">Last update</TableCell>
|
||||||
|
<TableCell align="center">Default</TableCell>
|
||||||
<TableCell align="center">Actions</TableCell>
|
<TableCell align="center">Actions</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHead>
|
</TableHead>
|
||||||
@@ -258,6 +260,11 @@ const WhatsApps = () => {
|
|||||||
<TableCell align="center">
|
<TableCell align="center">
|
||||||
{format(parseISO(whatsApp.updatedAt), "dd/MM/yy HH:mm")}
|
{format(parseISO(whatsApp.updatedAt), "dd/MM/yy HH:mm")}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
|
<TableCell align="center">
|
||||||
|
{whatsApp.default && (
|
||||||
|
<CheckCircle style={{ color: green[500] }} />
|
||||||
|
)}
|
||||||
|
</TableCell>
|
||||||
<TableCell align="center">
|
<TableCell align="center">
|
||||||
<IconButton
|
<IconButton
|
||||||
size="small"
|
size="small"
|
||||||
|
|||||||
Reference in New Issue
Block a user