Alteração nos tickets para a criação do ticket apenas com o numero e inserção automatica do 55 ao salvar o contato

adriano 2023-06-08 08:16:18 -03:00
parent 48834af7de
commit 9ecad6f9b5
5 changed files with 186 additions and 25 deletions

View File

@ -88,7 +88,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
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<Response> => {
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
@ -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
}

View File

@ -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"
/>

View File

@ -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,9 +84,31 @@ 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,
@ -80,19 +119,93 @@ const NewTicketModal = ({ modalOpen, onClose }) => {
history.push(`/tickets/${ticket.id}`);
window.location.reload();
} catch (err) {
toastError(err);
}
setLoading(false);
handleClose();
} catch (err) {
setLoading(false);
toastError(err);
}
// 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) {
// 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 }) => {
<DialogTitle id="form-dialog-title">
{i18n.t("newTicketModal.title")}
</DialogTitle>
<DialogContent dividers>
<TextField
label={i18n.t("newTicketModal.fieldLabel")}
variant="outlined"
autoFocus
value={searchParam}
// onChange={e => 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: (
// <React.Fragment>
// {loading ? (
// <CircularProgress color="inherit" size={20} />
// ) : null}
// {params.InputProps.endAdornment}
// </React.Fragment>
// ),
// }}
/>
</DialogContent>
{/* <DialogContent dividers>
<Autocomplete
options={options}
loading={loading}
@ -184,7 +332,7 @@ const NewTicketModal = ({ modalOpen, onClose }) => {
/>
)}
/>
</DialogContent>
</DialogContent> */}
<DialogActions>
<Button
onClick={handleClose}

View File

@ -166,8 +166,6 @@ const Ticket = () => {
} catch (err) {
setLoading(false);
console.log('ERROR: ', err)
toastError(err);
}
};

View File

@ -280,7 +280,7 @@ const messages = {
},
newTicketModal: {
title: "Criar Ticket",
fieldLabel: "Digite o nome para pesquisar o contato",
fieldLabel: "Digite o numero para criar um ticket",
add: "Adicionar",
buttons: {
ok: "Salvar",