mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-21 13:19:21 +00:00
add validation to whatsapp name
This commit is contained in:
@@ -17,15 +17,13 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
|
|||||||
|
|
||||||
interface WhatsappData {
|
interface WhatsappData {
|
||||||
name: string;
|
name: string;
|
||||||
status: string;
|
status?: string;
|
||||||
isDefault?: boolean;
|
isDefault?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||||
// const io = getIO();
|
// const io = getIO();
|
||||||
|
|
||||||
console.log("aqui");
|
|
||||||
|
|
||||||
const { name, status, isDefault }: WhatsappData = req.body;
|
const { name, status, isDefault }: WhatsappData = req.body;
|
||||||
|
|
||||||
const whatsapp = await CreateWhatsAppService({ name, status, isDefault });
|
const whatsapp = await CreateWhatsAppService({ name, status, isDefault });
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ import {
|
|||||||
AutoIncrement,
|
AutoIncrement,
|
||||||
Default,
|
Default,
|
||||||
AllowNull,
|
AllowNull,
|
||||||
HasMany
|
HasMany,
|
||||||
|
Unique
|
||||||
} from "sequelize-typescript";
|
} from "sequelize-typescript";
|
||||||
import Ticket from "./Ticket";
|
import Ticket from "./Ticket";
|
||||||
|
|
||||||
@@ -20,6 +21,8 @@ class Whatsapp extends Model<Whatsapp> {
|
|||||||
@Column
|
@Column
|
||||||
id: number;
|
id: number;
|
||||||
|
|
||||||
|
@AllowNull
|
||||||
|
@Unique
|
||||||
@Column(DataType.TEXT)
|
@Column(DataType.TEXT)
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
|
|||||||
@@ -15,12 +15,27 @@ const CreateWhatsAppService = async ({
|
|||||||
isDefault = false
|
isDefault = false
|
||||||
}: Request): Promise<Whatsapp> => {
|
}: Request): Promise<Whatsapp> => {
|
||||||
const schema = Yup.object().shape({
|
const schema = Yup.object().shape({
|
||||||
name: Yup.string().required().min(2),
|
name: Yup.string()
|
||||||
|
.required()
|
||||||
|
.min(2)
|
||||||
|
.test(
|
||||||
|
"Check-name",
|
||||||
|
"This whatsapp name is already used.",
|
||||||
|
async value => {
|
||||||
|
if (value) {
|
||||||
|
const whatsappFound = await Whatsapp.findOne({
|
||||||
|
where: { name: value }
|
||||||
|
});
|
||||||
|
return !whatsappFound;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
),
|
||||||
isDefault: Yup.boolean()
|
isDefault: Yup.boolean()
|
||||||
.required()
|
.required()
|
||||||
.test(
|
.test(
|
||||||
"Check-default",
|
"Check-default",
|
||||||
"Only one default whatsapp is permited",
|
"Only one default whatsapp is permitted",
|
||||||
async value => {
|
async value => {
|
||||||
if (value === true) {
|
if (value === true) {
|
||||||
const whatsappFound = await Whatsapp.findOne({
|
const whatsappFound = await Whatsapp.findOne({
|
||||||
|
|||||||
Reference in New Issue
Block a user