From 9ecad6f9b50c1208abfafb1ce594cd4d53017efa Mon Sep 17 00:00:00 2001 From: adriano Date: Thu, 8 Jun 2023 08:16:18 -0300 Subject: [PATCH] =?UTF-8?q?Altera=C3=A7=C3=A3o=20nos=20tickets=20para=20a?= =?UTF-8?q?=20cria=C3=A7=C3=A3o=20do=20ticket=20apenas=20com=20o=20numero?= =?UTF-8?q?=20e=20=20inser=C3=A7=C3=A3o=20automatica=20do=2055=20ao=20salv?= =?UTF-8?q?ar=20o=20contato?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/controllers/ContactController.ts | 29 ++- frontend/src/components/ContactModal/index.js | 2 +- .../src/components/NewTicketModal/index.js | 174 ++++++++++++++++-- frontend/src/components/Ticket/index.js | 4 +- frontend/src/translate/languages/pt.js | 2 +- 5 files changed, 186 insertions(+), 25 deletions(-) diff --git a/backend/src/controllers/ContactController.ts b/backend/src/controllers/ContactController.ts index c5099b0..ec9cd80 100644 --- a/backend/src/controllers/ContactController.ts +++ b/backend/src/controllers/ContactController.ts @@ -52,7 +52,7 @@ export const index = async (req: Request, res: Response): Promise => { const offset = 20 * (+pageNumber - 1); - searchParam = searchParam.replace(/\s+/g, ' ').trim().toLowerCase(); + searchParam = searchParam.replace(/\s+/g, ' ').trim().toLowerCase(); const data = await searchContactCache(searchParam, offset, 20) @@ -88,7 +88,7 @@ export const store = async (req: Request, res: Response): Promise => { number: Yup.string() .required() .matches(/^\d+$/, "Invalid number format. Only numbers is allowed.") - .matches(/^55\d+$/, "The number must start with 55.") + // .matches(/^55\d+$/, "The number must start with 55.") }); try { @@ -97,17 +97,19 @@ export const store = async (req: Request, res: Response): Promise => { throw new AppError(err.message); } + newContact.number = addStartPhoneNumber(newContact.number); + const validNumber = await CheckIsValidContact(newContact.number); // const validNumber: any = await CheckContactNumber(newContact.number) - if(!validNumber){ + if (!validNumber) { throw new AppError("ERR_WAPP_CHECK_CONTACT"); } const profilePicUrl = await GetProfilePicUrl(validNumber); - console.log('xxxxxxxxxxx profilePicUrl: ',profilePicUrl) + console.log('xxxxxxxxxxx profilePicUrl: ', profilePicUrl) // console.log(`newContact.name: ${newContact.name}\n @@ -125,7 +127,7 @@ export const store = async (req: Request, res: Response): Promise => { number, email, profilePicUrl: profilePicUrl, - extraInfo, + extraInfo, }); const io = getIO(); @@ -154,8 +156,8 @@ export const update = async ( const schema = Yup.object().shape({ name: Yup.string(), number: Yup.string() - .matches(/^\d+$/,"Invalid number format. Only numbers is allowed.") - .matches(/^55\d+$/, "The number must start with 55.") + .matches(/^\d+$/, "Invalid number format. Only numbers is allowed.") + // .matches(/^55\d+$/, "The number must start with 55.") }); try { @@ -164,6 +166,8 @@ export const update = async ( throw new AppError(err.message); } + contactData.number = addStartPhoneNumber(contactData.number); + await CheckIsValidContact(contactData.number); const { contactId } = req.params; @@ -225,3 +229,14 @@ export const contacsBulkInsertOnQueue = async (req: Request, res: Response): Pro return res.status(200).json({ message: 'ok' }) }; +function addStartPhoneNumber(phoneNumber: string) { + + const regex = /^55/; + + if (!regex.test(phoneNumber)) { + phoneNumber = '55' + phoneNumber; + } + + return phoneNumber +} + diff --git a/frontend/src/components/ContactModal/index.js b/frontend/src/components/ContactModal/index.js index fb5b67b..4eec00a 100644 --- a/frontend/src/components/ContactModal/index.js +++ b/frontend/src/components/ContactModal/index.js @@ -224,7 +224,7 @@ const ContactModal = ({ open, onClose, contactId, initialValues, onSave }) => { error={touched.number && Boolean(errors.number)} helperText={touched.number && errors.number} - placeholder="5513912344321" + placeholder="11912344321" variant="outlined" margin="dense" /> diff --git a/frontend/src/components/NewTicketModal/index.js b/frontend/src/components/NewTicketModal/index.js index 490c71e..0ffd78a 100644 --- a/frontend/src/components/NewTicketModal/index.js +++ b/frontend/src/components/NewTicketModal/index.js @@ -36,7 +36,19 @@ const NewTicketModal = ({ modalOpen, onClose }) => { const { user } = useContext(AuthContext); useEffect(() => { - if (!modalOpen || searchParam.length < 3) { + + + const regex = /^[0-9\b]+$/; // Regular expression to match only numbers + if (searchParam && !searchParam.match(regex)) return + + if (searchParam && searchParam.length > 9) { + setSelectedContact({}) + } + else { + setSelectedContact(null) + } + + if (!modalOpen || searchParam.length < 5) { setLoading(false); return; } @@ -47,16 +59,21 @@ const NewTicketModal = ({ modalOpen, onClose }) => { const { data } = await api.get("contacts", { params: { searchParam }, }); + + // console.log('data.contacts: ', data.contacts) + setOptions(data.contacts); setLoading(false); } catch (err) { setLoading(false); toastError(err); } + }; fetchContacts(); }, 500); + return () => clearTimeout(delayDebounceFn); }, [searchParam, modalOpen]); @@ -67,32 +84,128 @@ const NewTicketModal = ({ modalOpen, onClose }) => { }; const handleSaveTicket = async contactId => { - if (!contactId) return; - setLoading(true); + try { + + // console.log('=========> options: ', options) + + if (options && options.length > 0) { + console.log('-----> options[0].id: ', options[0].id) + contactId = options[0].id + } + else { + console.log('NO CONTACT whith this searchParam: ', searchParam) + + const values = { + name: searchParam, + number: searchParam, + }; + + const { data: data1 } = await api.post("/contacts", values); + + console.log('data1: ', data1) + + contactId = data1.id + + } + const { data: ticket } = await api.post("/tickets", { contactId: contactId, userId: user.id, status: "open", - }); - - window.location.reload(); + }); + + window.location.reload(); history.push(`/tickets/${ticket.id}`); - window.location.reload(); - + window.location.reload(); + + setLoading(false); + handleClose(); + } catch (err) { + setLoading(false); toastError(err); } - setLoading(false); - handleClose(); + + + + // if (!contactId) return; + // setLoading(true); + // try { + // const { data: ticket } = await api.post("/tickets", { + // contactId: contactId, + // userId: user.id, + // status: "open", + // }); + + // window.location.reload(); + // history.push(`/tickets/${ticket.id}`); + // window.location.reload(); + + // } catch (err) { + // toastError(err); + // } + + }; + + + + const handleChange = (number) => { + + const regex = /^[0-9\b]+$/; // Regular expression to match only numbers + + // setPhone(event.target.value); + + setTimeout(() => { + + let newValue = '' + + for (const char of number) { + + // console.log('char: ', char) + + if (char.match(regex)) { + newValue += char + } + } + + // console.log('newValue: ', newValue) + + setSearchParam(newValue) + + }, 100); + }; const handleSelectOption = (e, newValue) => { + + if (newValue?.number) { + + console.log('newValue: ', newValue) + setSelectedContact(newValue); + } else if (newValue?.name) { - setNewContact({ name: newValue.name }); - setContactModalOpen(true); + + // console.log('newValue?.name: ', newValue?.name) + + const regex = /^[0-9\b]+$/; // Regular expression to match only numbers + + if (newValue.name.match(regex)) { + + console.log('==========> newValue.name', newValue.name) + setNewContact({ name: newValue.name, number: newValue.name }); + + } + else { + + setNewContact({ name: newValue.name }); + setContactModalOpen(true); + + } + + } }; @@ -144,7 +257,42 @@ const NewTicketModal = ({ modalOpen, onClose }) => { {i18n.t("newTicketModal.title")} + + + setSearchParam(e.target.value)} + onChange={e => { + + handleChange(e.target.value) + + // setSearchParam(e.target.value) + }} + onKeyPress={e => { + if (loading || !selectedContact) return; + else if (e.key === "Enter") { + handleSaveTicket(selectedContact.id); + } + }} + // InputProps={{ + // ...params.InputProps, + // endAdornment: ( + // + // {loading ? ( + // + // ) : null} + // {params.InputProps.endAdornment} + // + // ), + // }} + /> + + + {/* { /> )} /> - + */}