From 84bbe3afaa8adb328c71c097eb2489409a5a8e02 Mon Sep 17 00:00:00 2001 From: Ricardo Paes Date: Fri, 25 Feb 2022 08:18:57 -0300 Subject: [PATCH] :sparkles: In user registration add standard whatsapp field --- backend/src/controllers/UserController.ts | 5 ++-- backend/src/helpers/SerializeUser.ts | 5 +++- .../UserServices/CreateUserService.ts | 9 ++++-- .../services/UserServices/ListUsersService.ts | 4 ++- .../services/UserServices/ShowUserService.ts | 6 ++-- .../UserServices/UpdateUserService.ts | 15 ++++------ frontend/src/components/UserModal/index.js | 29 +++++++++++++++++-- frontend/src/pages/Users/index.js | 4 +++ frontend/src/translate/languages/en.js | 2 ++ frontend/src/translate/languages/es.js | 2 ++ frontend/src/translate/languages/pt.js | 2 ++ 11 files changed, 62 insertions(+), 21 deletions(-) diff --git a/backend/src/controllers/UserController.ts b/backend/src/controllers/UserController.ts index 06d329d..001d97b 100644 --- a/backend/src/controllers/UserController.ts +++ b/backend/src/controllers/UserController.ts @@ -27,7 +27,7 @@ export const index = async (req: Request, res: Response): Promise => { }; export const store = async (req: Request, res: Response): Promise => { - 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 => { password, name, profile, - queueIds + queueIds, + whatsappId }); const io = getIO(); diff --git a/backend/src/helpers/SerializeUser.ts b/backend/src/helpers/SerializeUser.ts index 3802500..928f75b 100644 --- a/backend/src/helpers/SerializeUser.ts +++ b/backend/src/helpers/SerializeUser.ts @@ -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 }; }; diff --git a/backend/src/services/UserServices/CreateUserService.ts b/backend/src/services/UserServices/CreateUserService.ts index 098846b..83aa987 100644 --- a/backend/src/services/UserServices/CreateUserService.ts +++ b/backend/src/services/UserServices/CreateUserService.ts @@ -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 => { 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); diff --git a/backend/src/services/UserServices/ListUsersService.ts b/backend/src/services/UserServices/ListUsersService.ts index fb8a202..200ce05 100644 --- a/backend/src/services/UserServices/ListUsersService.ts +++ b/backend/src/services/UserServices/ListUsersService.ts @@ -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"] }, ] }); diff --git a/backend/src/services/UserServices/ShowUserService.ts b/backend/src/services/UserServices/ShowUserService.ts index 03ecab4..d98a8c0 100644 --- a/backend/src/services/UserServices/ShowUserService.ts +++ b/backend/src/services/UserServices/ShowUserService.ts @@ -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 => { 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' ] ] }); diff --git a/backend/src/services/UserServices/UpdateUserService.ts b/backend/src/services/UserServices/UpdateUserService.ts index 114d557..b429ca9 100644 --- a/backend/src/services/UserServices/UpdateUserService.ts +++ b/backend/src/services/UserServices/UpdateUserService.ts @@ -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; }; diff --git a/frontend/src/components/UserModal/index.js b/frontend/src/components/UserModal/index.js index 1d27a44..d7da0fb 100644 --- a/frontend/src/components/UserModal/index.js +++ b/frontend/src/components/UserModal/index.js @@ -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 }) => { /> )} /> + ( + + {i18n.t("userModal.form.whatsapp")} + setWhatsappId(e.target.value)} + label={i18n.t("userModal.form.whatsapp")} + > +   + {whatsApps.map((whatsapp) => ( + {whatsapp.name} + ))} + + + )} + />