mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 12:49:32 +00:00
improvement: add validation to contacts create/update
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import * as Yup from "yup";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { getIO } from "../libs/socket";
|
import { getIO } from "../libs/socket";
|
||||||
|
|
||||||
@@ -9,23 +10,13 @@ import DeleteContactService from "../services/ContactServices/DeleteContactServi
|
|||||||
|
|
||||||
import CheckIsValidContact from "../services/WbotServices/CheckIsValidContact";
|
import CheckIsValidContact from "../services/WbotServices/CheckIsValidContact";
|
||||||
import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl";
|
import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl";
|
||||||
|
import AppError from "../errors/AppError";
|
||||||
|
|
||||||
type IndexQuery = {
|
type IndexQuery = {
|
||||||
searchParam: string;
|
searchParam: string;
|
||||||
pageNumber: string;
|
pageNumber: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const index = async (req: Request, res: Response): Promise<Response> => {
|
|
||||||
const { searchParam, pageNumber } = req.query as IndexQuery;
|
|
||||||
|
|
||||||
const { contacts, count, hasMore } = await ListContactsService({
|
|
||||||
searchParam,
|
|
||||||
pageNumber
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.json({ contacts, count, hasMore });
|
|
||||||
};
|
|
||||||
|
|
||||||
interface ExtraInfo {
|
interface ExtraInfo {
|
||||||
name: string;
|
name: string;
|
||||||
value: string;
|
value: string;
|
||||||
@@ -37,9 +28,33 @@ interface ContactData {
|
|||||||
extraInfo?: ExtraInfo[];
|
extraInfo?: ExtraInfo[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||||
|
const { searchParam, pageNumber } = req.query as IndexQuery;
|
||||||
|
|
||||||
|
const { contacts, count, hasMore } = await ListContactsService({
|
||||||
|
searchParam,
|
||||||
|
pageNumber
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.json({ contacts, count, hasMore });
|
||||||
|
};
|
||||||
|
|
||||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||||
const newContact: ContactData = req.body;
|
const newContact: ContactData = req.body;
|
||||||
|
|
||||||
|
const schema = Yup.object().shape({
|
||||||
|
name: Yup.string().required(),
|
||||||
|
number: Yup.string()
|
||||||
|
.required()
|
||||||
|
.matches(/^\d+$/, "Invalid number format. Only numbers is allowed.")
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
await schema.validate(newContact);
|
||||||
|
} catch (err) {
|
||||||
|
throw new AppError(err.message);
|
||||||
|
}
|
||||||
|
|
||||||
await CheckIsValidContact(newContact.number);
|
await CheckIsValidContact(newContact.number);
|
||||||
|
|
||||||
const profilePicUrl = await GetProfilePicUrl(newContact.number);
|
const profilePicUrl = await GetProfilePicUrl(newContact.number);
|
||||||
@@ -72,6 +87,22 @@ export const update = async (
|
|||||||
): Promise<Response> => {
|
): Promise<Response> => {
|
||||||
const contactData: ContactData = req.body;
|
const contactData: ContactData = req.body;
|
||||||
|
|
||||||
|
const schema = Yup.object().shape({
|
||||||
|
name: Yup.string(),
|
||||||
|
number: Yup.string().matches(
|
||||||
|
/^\d+$/,
|
||||||
|
"Invalid number format. Only numbers is allowed."
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
await schema.validate(contactData);
|
||||||
|
} catch (err) {
|
||||||
|
throw new AppError(err.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
await CheckIsValidContact(contactData.number);
|
||||||
|
|
||||||
const { contactId } = req.params;
|
const { contactId } = req.params;
|
||||||
|
|
||||||
const contact = await UpdateContactService({ contactData, contactId });
|
const contact = await UpdateContactService({ contactData, contactId });
|
||||||
|
|||||||
Reference in New Issue
Block a user