In user registration add standard whatsapp field

This commit is contained in:
Ricardo Paes
2022-02-25 08:18:57 -03:00
parent 61df7c8bc0
commit 84bbe3afaa
11 changed files with 62 additions and 21 deletions

View File

@@ -27,7 +27,7 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
};
export const store = async (req: Request, res: Response): Promise<Response> => {
const { email, password, name, profile, queueIds } = req.body;
const { email, password, name, profile, queueIds, whatsappId } = req.body;
if (
req.url === "/signup" &&
@@ -43,7 +43,8 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
password,
name,
profile,
queueIds
queueIds,
whatsappId
});
const io = getIO();

View File

@@ -1,5 +1,6 @@
import Queue from "../models/Queue";
import User from "../models/User";
import Whatsapp from "../models/Whatsapp";
interface SerializedUser {
id: number;
@@ -7,6 +8,7 @@ interface SerializedUser {
email: string;
profile: string;
queues: Queue[];
whatsapp: Whatsapp;
}
export const SerializeUser = (user: User): SerializedUser => {
@@ -15,6 +17,7 @@ export const SerializeUser = (user: User): SerializedUser => {
name: user.name,
email: user.email,
profile: user.profile,
queues: user.queues
queues: user.queues,
whatsapp: user.whatsapp
};
};

View File

@@ -10,6 +10,7 @@ interface Request {
name: string;
queueIds?: number[];
profile?: string;
whatsappId?: number;
}
interface Response {
@@ -24,7 +25,8 @@ const CreateUserService = async ({
password,
name,
queueIds = [],
profile = "admin"
profile = "admin",
whatsappId
}: Request): Promise<Response> => {
const schema = Yup.object().shape({
name: Yup.string().required().min(2),
@@ -56,9 +58,10 @@ const CreateUserService = async ({
email,
password,
name,
profile
profile,
whatsappId: whatsappId ? whatsappId : null
},
{ include: ["queues"] }
{ include: ["queues", "whatsapp"] }
);
await user.$set("queues", queueIds);

View File

@@ -1,6 +1,7 @@
import { Sequelize, Op } from "sequelize";
import Queue from "../../models/Queue";
import User from "../../models/User";
import Whatsapp from "../../models/Whatsapp";
interface Request {
searchParam?: string;
@@ -39,7 +40,8 @@ const ListUsersService = async ({
offset,
order: [["createdAt", "DESC"]],
include: [
{ model: Queue, as: "queues", attributes: ["id", "name", "color"] }
{ model: Queue, as: "queues", attributes: ["id", "name", "color"] },
{ model: Whatsapp, as: "whatsapp", attributes: ["id", "name"] },
]
});

View File

@@ -1,12 +1,14 @@
import User from "../../models/User";
import AppError from "../../errors/AppError";
import Queue from "../../models/Queue";
import Whatsapp from "../../models/Whatsapp";
const ShowUserService = async (id: string | number): Promise<User> => {
const user = await User.findByPk(id, {
attributes: ["name", "id", "email", "profile", "tokenVersion"],
attributes: ["name", "id", "email", "profile", "tokenVersion", "whatsappId"],
include: [
{ model: Queue, as: "queues", attributes: ["id", "name", "color"] }
{ model: Queue, as: "queues", attributes: ["id", "name", "color"] },
{ model: Whatsapp, as: "whatsapp", attributes: ["id", "name"] },
],
order: [ [ { model: Queue, as: "queues"}, 'name', 'asc' ] ]
});

View File

@@ -1,6 +1,7 @@
import * as Yup from "yup";
import AppError from "../../errors/AppError";
import { SerializeUser } from "../../helpers/SerializeUser";
import ShowUserService from "./ShowUserService";
interface UserData {
@@ -9,6 +10,7 @@ interface UserData {
name?: string;
profile?: string;
queueIds?: number[];
whatsappId?: number;
}
interface Request {
@@ -36,7 +38,7 @@ const UpdateUserService = async ({
password: Yup.string()
});
const { email, password, profile, name, queueIds = [] } = userData;
const { email, password, profile, name, queueIds = [], whatsappId } = userData;
try {
await schema.validate({ email, password, profile, name });
@@ -48,20 +50,15 @@ const UpdateUserService = async ({
email,
password,
profile,
name
name,
whatsappId: whatsappId ? whatsappId : null
});
await user.$set("queues", queueIds);
await user.reload();
const serializedUser = {
id: user.id,
name: user.name,
email: user.email,
profile: user.profile,
queues: user.queues
};
const serializedUser = SerializeUser(user);
return serializedUser;
};

View File

@@ -32,6 +32,7 @@ import toastError from "../../errors/toastError";
import QueueSelect from "../QueueSelect";
import { AuthContext } from "../../context/Auth/AuthContext";
import { Can } from "../Can";
import useWhatsApps from "../../hooks/useWhatsApps";
const useStyles = makeStyles(theme => ({
root: {
@@ -79,7 +80,7 @@ const UserModal = ({ open, onClose, userId }) => {
name: "",
email: "",
password: "",
profile: "user",
profile: "user"
};
const { user: loggedInUser } = useContext(AuthContext);
@@ -87,7 +88,8 @@ const UserModal = ({ open, onClose, userId }) => {
const [user, setUser] = useState(initialState);
const [selectedQueueIds, setSelectedQueueIds] = useState([]);
const [showPassword, setShowPassword] = useState(false);
const [whatsappId, setWhatsappId] = useState(false);
const {loading, whatsApps} = useWhatsApps();
useEffect(() => {
const fetchUser = async () => {
@@ -99,6 +101,7 @@ const UserModal = ({ open, onClose, userId }) => {
});
const userQueueIds = data.queues?.map(queue => queue.id);
setSelectedQueueIds(userQueueIds);
setWhatsappId(data.whatsappId ? data.whatsappId : '');
} catch (err) {
toastError(err);
}
@@ -113,7 +116,7 @@ const UserModal = ({ open, onClose, userId }) => {
};
const handleSaveUser = async values => {
const userData = { ...values, queueIds: selectedQueueIds };
const userData = { ...values, whatsappId, queueIds: selectedQueueIds };
try {
if (userId) {
await api.put(`/users/${userId}`, userData);
@@ -242,6 +245,26 @@ const UserModal = ({ open, onClose, userId }) => {
/>
)}
/>
<Can
role={loggedInUser.profile}
perform="user-modal:editQueues"
yes={() => (
<FormControl variant="outlined" margin="dense" className={classes.maxWidth} fullWidth>
<InputLabel>{i18n.t("userModal.form.whatsapp")}</InputLabel>
<Field
as={Select}
value={whatsappId}
onChange={(e) => setWhatsappId(e.target.value)}
label={i18n.t("userModal.form.whatsapp")}
>
<MenuItem value={''}>&nbsp;</MenuItem>
{whatsApps.map((whatsapp) => (
<MenuItem key={whatsapp.id} value={whatsapp.id}>{whatsapp.name}</MenuItem>
))}
</Field>
</FormControl>
)}
/>
</DialogContent>
<DialogActions>
<Button

View File

@@ -243,6 +243,9 @@ const Users = () => {
<TableCell align="center">
{i18n.t("users.table.profile")}
</TableCell>
<TableCell align="center">
{i18n.t("users.table.whatsapp")}
</TableCell>
<TableCell align="center">
{i18n.t("users.table.actions")}
</TableCell>
@@ -255,6 +258,7 @@ const Users = () => {
<TableCell align="center">{user.name}</TableCell>
<TableCell align="center">{user.email}</TableCell>
<TableCell align="center">{user.profile}</TableCell>
<TableCell align="center">{user.whatsapp?.name}</TableCell>
<TableCell align="center">
<IconButton
size="small"

View File

@@ -206,6 +206,7 @@ const messages = {
email: "Email",
password: "Password",
profile: "Profile",
whatsapp: "Default Connection",
},
buttons: {
okAdd: "Add",
@@ -340,6 +341,7 @@ const messages = {
name: "Name",
email: "Email",
profile: "Profile",
whatsapp: "Default Connection",
actions: "Actions",
},
buttons: {

View File

@@ -209,6 +209,7 @@ const messages = {
email: "Correo Electrónico",
password: "Contraseña",
profile: "Perfil",
whatsapp: "Conexión estándar",
},
buttons: {
okAdd: "Agregar",
@@ -345,6 +346,7 @@ const messages = {
name: "Nombre",
email: "Correo Electrónico",
profile: "Perfil",
whatsapp: "Conexión estándar",
actions: "Acciones",
},
buttons: {

View File

@@ -208,6 +208,7 @@ const messages = {
email: "Email",
password: "Senha",
profile: "Perfil",
whatsapp: "Conexão Padrão",
},
buttons: {
okAdd: "Adicionar",
@@ -344,6 +345,7 @@ const messages = {
name: "Nome",
email: "Email",
profile: "Perfil",
whatsapp: "Conexão Padrão",
actions: "Ações",
},
buttons: {