mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 12:49:32 +00:00
changed all models to typescript
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
"error",
|
"error",
|
||||||
{ "argsIgnorePattern": "_" }
|
{ "argsIgnorePattern": "_" }
|
||||||
],
|
],
|
||||||
|
"import/prefer-default-export": "off",
|
||||||
"no-console": "off",
|
"no-console": "off",
|
||||||
"no-param-reassign": "off",
|
"no-param-reassign": "off",
|
||||||
"prettier/prettier": "error",
|
"prettier/prettier": "error",
|
||||||
|
|||||||
@@ -2,15 +2,13 @@ import { Request, Response } from "express";
|
|||||||
|
|
||||||
import AuthUserService from "../services/UserServices/AuthUserSerice";
|
import AuthUserService from "../services/UserServices/AuthUserSerice";
|
||||||
|
|
||||||
const store = async (req: Request, res: Response): Promise<Response> => {
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||||
const { email, password } = req.body;
|
const { email, password } = req.body;
|
||||||
|
|
||||||
const { user, token } = await AuthUserService({ email, password });
|
const { token, user } = await AuthUserService({ email, password });
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
user,
|
user,
|
||||||
token
|
token
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export default store;
|
|
||||||
|
|||||||
174
backend/src/controllers/TicketController.ts
Normal file
174
backend/src/controllers/TicketController.ts
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
import { Request, Response } from "express";
|
||||||
|
import ListTicketsService from "../services/TicketServices/ListTicketsService";
|
||||||
|
// const Sequelize = require("sequelize");
|
||||||
|
// const { startOfDay, endOfDay, parseISO } = require("date-fns");
|
||||||
|
|
||||||
|
// const Ticket = require("../models/Ticket");
|
||||||
|
// const Contact = require("../models/Contact");
|
||||||
|
// const Message = require("../models/Message");
|
||||||
|
// const Whatsapp = require("../models/Whatsapp");
|
||||||
|
|
||||||
|
// const { getIO } = require("../libs/socket");
|
||||||
|
|
||||||
|
import Whatsapp from "../models/Whatsapp";
|
||||||
|
import Ticket from "../models/Ticket";
|
||||||
|
import Whatsapp from "../models/Whatsapp";
|
||||||
|
|
||||||
|
type RequestQuery = {
|
||||||
|
searchParam: string;
|
||||||
|
pageNumber: string;
|
||||||
|
status: string;
|
||||||
|
date: string;
|
||||||
|
showAll: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||||
|
const {
|
||||||
|
pageNumber,
|
||||||
|
status,
|
||||||
|
date,
|
||||||
|
searchParam,
|
||||||
|
showAll
|
||||||
|
} = req.query as RequestQuery;
|
||||||
|
|
||||||
|
const userId = req.user.id;
|
||||||
|
|
||||||
|
// let includeCondition = [
|
||||||
|
// {
|
||||||
|
// model: Contact,
|
||||||
|
// as: "contact",
|
||||||
|
// attributes: ["name", "number", "profilePicUrl"]
|
||||||
|
// }
|
||||||
|
// ];
|
||||||
|
|
||||||
|
// if (searchParam) {
|
||||||
|
// includeCondition = [
|
||||||
|
// ...includeCondition,
|
||||||
|
// {
|
||||||
|
// model: Message,
|
||||||
|
// as: "messages",
|
||||||
|
// attributes: ["id", "body"],
|
||||||
|
// where: {
|
||||||
|
// body: Sequelize.where(
|
||||||
|
// Sequelize.fn("LOWER", Sequelize.col("body")),
|
||||||
|
// "LIKE",
|
||||||
|
// "%" + searchParam.toLowerCase() + "%"
|
||||||
|
// )
|
||||||
|
// },
|
||||||
|
// required: false,
|
||||||
|
// duplicating: false
|
||||||
|
// }
|
||||||
|
// ];
|
||||||
|
|
||||||
|
// whereCondition = {
|
||||||
|
// [Sequelize.Op.or]: [
|
||||||
|
// {
|
||||||
|
// "$contact.name$": Sequelize.where(
|
||||||
|
// Sequelize.fn("LOWER", Sequelize.col("name")),
|
||||||
|
// "LIKE",
|
||||||
|
// "%" + searchParam.toLowerCase() + "%"
|
||||||
|
// )
|
||||||
|
// },
|
||||||
|
// { "$contact.number$": { [Sequelize.Op.like]: `%${searchParam}%` } },
|
||||||
|
// {
|
||||||
|
// "$message.body$": Sequelize.where(
|
||||||
|
// Sequelize.fn("LOWER", Sequelize.col("body")),
|
||||||
|
// "LIKE",
|
||||||
|
// "%" + searchParam.toLowerCase() + "%"
|
||||||
|
// )
|
||||||
|
// }
|
||||||
|
// ]
|
||||||
|
// };
|
||||||
|
// }
|
||||||
|
|
||||||
|
const { tickets, count, hasMore } = await ListTicketsService({
|
||||||
|
searchParam,
|
||||||
|
pageNumber,
|
||||||
|
status,
|
||||||
|
date,
|
||||||
|
showAll,
|
||||||
|
userId
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(200).json({ tickets, count, hasMore });
|
||||||
|
};
|
||||||
|
|
||||||
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||||
|
// const io = getIO();
|
||||||
|
|
||||||
|
const defaultWhatsapp = await Whatsapp.findOne({
|
||||||
|
where: { default: true }
|
||||||
|
});
|
||||||
|
|
||||||
|
const ticketData = req.body;
|
||||||
|
|
||||||
|
if (!defaultWhatsapp) {
|
||||||
|
return res
|
||||||
|
.status(404)
|
||||||
|
.json({ error: "No default WhatsApp found. Check Connection page." });
|
||||||
|
}
|
||||||
|
|
||||||
|
const ticket: Ticket = await defaultWhatsapp.$create("ticket", req.body);
|
||||||
|
const contact = await ticket.$get("contact");
|
||||||
|
|
||||||
|
const wapp = await ticket.$get("whatsapp");
|
||||||
|
|
||||||
|
const tickets = await wapp?.$get("tickets");
|
||||||
|
|
||||||
|
// const serializaedTicket = { ...ticket.dataValues, contact: contact };
|
||||||
|
|
||||||
|
// io.to("notification").emit("ticket", {
|
||||||
|
// action: "create",
|
||||||
|
// ticket: serializaedTicket
|
||||||
|
// });
|
||||||
|
|
||||||
|
return res.status(200).json({ ticket, contact, wapp, tickets });
|
||||||
|
};
|
||||||
|
|
||||||
|
// export const update = (req: Request, res: Response): Promise<Response> => {
|
||||||
|
// const io = getIO();
|
||||||
|
// const { ticketId } = req.params;
|
||||||
|
|
||||||
|
// const ticket = await Ticket.findByPk(ticketId, {
|
||||||
|
// include: [
|
||||||
|
// {
|
||||||
|
// model: Contact,
|
||||||
|
// as: "contact",
|
||||||
|
// attributes: ["name", "number", "profilePicUrl"]
|
||||||
|
// }
|
||||||
|
// ]
|
||||||
|
// });
|
||||||
|
|
||||||
|
// if (!ticket) {
|
||||||
|
// return res.status(404).json({ error: "No ticket found with this ID" });
|
||||||
|
// }
|
||||||
|
|
||||||
|
// await ticket.update(req.body);
|
||||||
|
|
||||||
|
// io.to("notification").emit("ticket", {
|
||||||
|
// action: "updateStatus",
|
||||||
|
// ticket: ticket
|
||||||
|
// });
|
||||||
|
|
||||||
|
// return res.status(200).json(ticket);
|
||||||
|
// };
|
||||||
|
|
||||||
|
// export const remove = (req: Request, res: Response): Promise<Response> => {
|
||||||
|
// const io = getIO();
|
||||||
|
// const { ticketId } = req.params;
|
||||||
|
|
||||||
|
// const ticket = await Ticket.findByPk(ticketId);
|
||||||
|
|
||||||
|
// if (!ticket) {
|
||||||
|
// return res.status(400).json({ error: "No ticket found with this ID" });
|
||||||
|
// }
|
||||||
|
|
||||||
|
// await ticket.destroy();
|
||||||
|
|
||||||
|
// io.to("notification").emit("ticket", {
|
||||||
|
// action: "delete",
|
||||||
|
// ticketId: ticket.id
|
||||||
|
// });
|
||||||
|
|
||||||
|
// return res.status(200).json({ message: "ticket deleted" });
|
||||||
|
// };
|
||||||
@@ -19,7 +19,7 @@ exports.store = async (req, res, next) => {
|
|||||||
{ email: user.email, userId: user.id },
|
{ email: user.email, userId: user.id },
|
||||||
authConfig.secret,
|
authConfig.secret,
|
||||||
{
|
{
|
||||||
expiresIn: authConfig.expiresIn,
|
expiresIn: authConfig.expiresIn
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -27,6 +27,6 @@ exports.store = async (req, res, next) => {
|
|||||||
token: token,
|
token: token,
|
||||||
username: user.name,
|
username: user.name,
|
||||||
profile: user.profile,
|
profile: user.profile,
|
||||||
userId: user.id,
|
userId: user.id
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,24 +3,23 @@ import User from "../models/User";
|
|||||||
import Setting from "../models/Setting";
|
import Setting from "../models/Setting";
|
||||||
import Contact from "../models/Contact";
|
import Contact from "../models/Contact";
|
||||||
import Ticket from "../models/Ticket";
|
import Ticket from "../models/Ticket";
|
||||||
|
import Whatsapp from "../models/Whatsapp";
|
||||||
|
import ContactCustomField from "../models/ContactCustomField";
|
||||||
|
import Message from "../models/Message";
|
||||||
|
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
const dbConfig = require("../config/database");
|
const dbConfig = require("../config/database");
|
||||||
// import dbConfig from "../config/database";
|
// import dbConfig from "../config/database";
|
||||||
|
|
||||||
// const Message = require("../models/Message");
|
|
||||||
// const Whatsapp = require("../models/Whatsapp");
|
|
||||||
// const ContactCustomField = require("../models/ContactCustomField");
|
|
||||||
|
|
||||||
const sequelize = new Sequelize(dbConfig);
|
const sequelize = new Sequelize(dbConfig);
|
||||||
|
|
||||||
const models = [
|
const models = [
|
||||||
User,
|
User,
|
||||||
Contact,
|
Contact,
|
||||||
Ticket,
|
Ticket,
|
||||||
// Message,
|
Message,
|
||||||
// Whatsapp,
|
Whatsapp,
|
||||||
// ContactCustomField,
|
ContactCustomField,
|
||||||
Setting
|
Setting
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -4,13 +4,15 @@ import {
|
|||||||
CreatedAt,
|
CreatedAt,
|
||||||
UpdatedAt,
|
UpdatedAt,
|
||||||
Model,
|
Model,
|
||||||
DataType,
|
|
||||||
PrimaryKey,
|
PrimaryKey,
|
||||||
AutoIncrement,
|
AutoIncrement,
|
||||||
AllowNull,
|
AllowNull,
|
||||||
Unique,
|
Unique,
|
||||||
Default
|
Default,
|
||||||
|
HasMany
|
||||||
} from "sequelize-typescript";
|
} from "sequelize-typescript";
|
||||||
|
import ContactCustomField from "./ContactCustomField";
|
||||||
|
import Ticket from "./Ticket";
|
||||||
|
|
||||||
@Table
|
@Table
|
||||||
class Contact extends Model<Contact> {
|
class Contact extends Model<Contact> {
|
||||||
@@ -19,7 +21,7 @@ class Contact extends Model<Contact> {
|
|||||||
@Column
|
@Column
|
||||||
id: number;
|
id: number;
|
||||||
|
|
||||||
@Column(DataType.STRING)
|
@Column
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
@AllowNull(false)
|
@AllowNull(false)
|
||||||
@@ -40,6 +42,12 @@ class Contact extends Model<Contact> {
|
|||||||
|
|
||||||
@UpdatedAt
|
@UpdatedAt
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
|
|
||||||
|
@HasMany(() => Ticket)
|
||||||
|
tickets: Ticket[];
|
||||||
|
|
||||||
|
@HasMany(() => ContactCustomField)
|
||||||
|
extraInfo: ContactCustomField[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Contact;
|
export default Contact;
|
||||||
|
|||||||
41
backend/src/models/ContactCustomField.ts
Normal file
41
backend/src/models/ContactCustomField.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import {
|
||||||
|
Table,
|
||||||
|
Column,
|
||||||
|
CreatedAt,
|
||||||
|
UpdatedAt,
|
||||||
|
Model,
|
||||||
|
PrimaryKey,
|
||||||
|
AutoIncrement,
|
||||||
|
ForeignKey,
|
||||||
|
BelongsTo
|
||||||
|
} from "sequelize-typescript";
|
||||||
|
import Contact from "./Contact";
|
||||||
|
|
||||||
|
@Table
|
||||||
|
class ContactCustomField extends Model<ContactCustomField> {
|
||||||
|
@PrimaryKey
|
||||||
|
@AutoIncrement
|
||||||
|
@Column
|
||||||
|
id: number;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
value: string;
|
||||||
|
|
||||||
|
@ForeignKey(() => Contact)
|
||||||
|
@Column
|
||||||
|
contactId: number;
|
||||||
|
|
||||||
|
@BelongsTo(() => Contact)
|
||||||
|
contact: Contact;
|
||||||
|
|
||||||
|
@CreatedAt
|
||||||
|
createdAt: Date;
|
||||||
|
|
||||||
|
@UpdatedAt
|
||||||
|
updatedAt: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ContactCustomField;
|
||||||
60
backend/src/models/Message.ts
Normal file
60
backend/src/models/Message.ts
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
import {
|
||||||
|
Table,
|
||||||
|
Column,
|
||||||
|
CreatedAt,
|
||||||
|
UpdatedAt,
|
||||||
|
Model,
|
||||||
|
DataType,
|
||||||
|
PrimaryKey,
|
||||||
|
AutoIncrement,
|
||||||
|
Default,
|
||||||
|
BelongsTo,
|
||||||
|
ForeignKey
|
||||||
|
} from "sequelize-typescript";
|
||||||
|
import Ticket from "./Ticket";
|
||||||
|
|
||||||
|
@Table
|
||||||
|
class Message extends Model<Message> {
|
||||||
|
@PrimaryKey
|
||||||
|
@AutoIncrement
|
||||||
|
@Column
|
||||||
|
id: number;
|
||||||
|
|
||||||
|
@Default(0)
|
||||||
|
@Column
|
||||||
|
ack: number;
|
||||||
|
|
||||||
|
@Default(false)
|
||||||
|
@Column
|
||||||
|
read: boolean;
|
||||||
|
|
||||||
|
@Default(false)
|
||||||
|
@Column
|
||||||
|
fromMe: boolean;
|
||||||
|
|
||||||
|
@Column(DataType.TEXT)
|
||||||
|
body: string;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
mediaUrl: string;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
mediaType: string;
|
||||||
|
|
||||||
|
@CreatedAt
|
||||||
|
@Column(DataType.DATE(6))
|
||||||
|
createdAt: Date;
|
||||||
|
|
||||||
|
@UpdatedAt
|
||||||
|
@Column(DataType.DATE(6))
|
||||||
|
updatedAt: Date;
|
||||||
|
|
||||||
|
@ForeignKey(() => Ticket)
|
||||||
|
@Column
|
||||||
|
ticketId: number;
|
||||||
|
|
||||||
|
@BelongsTo(() => Ticket)
|
||||||
|
ticket: Ticket;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Message;
|
||||||
@@ -4,17 +4,16 @@ import {
|
|||||||
CreatedAt,
|
CreatedAt,
|
||||||
UpdatedAt,
|
UpdatedAt,
|
||||||
Model,
|
Model,
|
||||||
DataType,
|
|
||||||
PrimaryKey
|
PrimaryKey
|
||||||
} from "sequelize-typescript";
|
} from "sequelize-typescript";
|
||||||
|
|
||||||
@Table
|
@Table
|
||||||
class Setting extends Model<Setting> {
|
class Setting extends Model<Setting> {
|
||||||
@PrimaryKey
|
@PrimaryKey
|
||||||
@Column(DataType.STRING)
|
@Column
|
||||||
key: string;
|
key: string;
|
||||||
|
|
||||||
@Column(DataType.STRING)
|
@Column
|
||||||
value: string;
|
value: string;
|
||||||
|
|
||||||
@CreatedAt
|
@CreatedAt
|
||||||
|
|||||||
@@ -5,25 +5,32 @@ import {
|
|||||||
UpdatedAt,
|
UpdatedAt,
|
||||||
Model,
|
Model,
|
||||||
DataType,
|
DataType,
|
||||||
PrimaryKey
|
PrimaryKey,
|
||||||
|
ForeignKey,
|
||||||
|
BelongsTo,
|
||||||
|
HasMany,
|
||||||
|
AutoIncrement
|
||||||
} from "sequelize-typescript";
|
} from "sequelize-typescript";
|
||||||
|
|
||||||
|
import Contact from "./Contact";
|
||||||
|
import Message from "./Message";
|
||||||
|
import User from "./User";
|
||||||
|
import Whatsapp from "./Whatsapp";
|
||||||
|
|
||||||
@Table
|
@Table
|
||||||
class Ticket extends Model<Ticket> {
|
class Ticket extends Model<Ticket> {
|
||||||
@PrimaryKey
|
@PrimaryKey
|
||||||
@Column(DataType.NUMBER)
|
@AutoIncrement
|
||||||
|
@Column
|
||||||
id: number;
|
id: number;
|
||||||
|
|
||||||
@Column({ defaultValue: "pending" })
|
@Column({ defaultValue: "pending" })
|
||||||
status: string;
|
status: string;
|
||||||
|
|
||||||
// @Column({ allowNull: false, unique: true })
|
|
||||||
// userId: string;
|
|
||||||
|
|
||||||
@Column(DataType.VIRTUAL)
|
@Column(DataType.VIRTUAL)
|
||||||
unreadMessages: string;
|
unreadMessages: string;
|
||||||
|
|
||||||
@Column(DataType.STRING)
|
@Column
|
||||||
lastMessage: string;
|
lastMessage: string;
|
||||||
|
|
||||||
@CreatedAt
|
@CreatedAt
|
||||||
@@ -31,6 +38,30 @@ class Ticket extends Model<Ticket> {
|
|||||||
|
|
||||||
@UpdatedAt
|
@UpdatedAt
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
|
|
||||||
|
@ForeignKey(() => User)
|
||||||
|
@Column
|
||||||
|
userId: number;
|
||||||
|
|
||||||
|
@BelongsTo(() => User)
|
||||||
|
user: User;
|
||||||
|
|
||||||
|
@ForeignKey(() => Contact)
|
||||||
|
@Column
|
||||||
|
contactId: number;
|
||||||
|
|
||||||
|
@BelongsTo(() => Contact)
|
||||||
|
contact: Contact;
|
||||||
|
|
||||||
|
@ForeignKey(() => Whatsapp)
|
||||||
|
@Column
|
||||||
|
whatsappId: number;
|
||||||
|
|
||||||
|
@BelongsTo(() => Whatsapp)
|
||||||
|
whatsapp: Whatsapp;
|
||||||
|
|
||||||
|
@HasMany(() => Message)
|
||||||
|
messages: Message[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Ticket;
|
export default Ticket;
|
||||||
|
|||||||
@@ -9,9 +9,11 @@ import {
|
|||||||
BeforeUpdate,
|
BeforeUpdate,
|
||||||
PrimaryKey,
|
PrimaryKey,
|
||||||
AutoIncrement,
|
AutoIncrement,
|
||||||
Default
|
Default,
|
||||||
|
HasMany
|
||||||
} from "sequelize-typescript";
|
} from "sequelize-typescript";
|
||||||
import { hash, compare } from "bcryptjs";
|
import { hash, compare } from "bcryptjs";
|
||||||
|
import Ticket from "./Ticket";
|
||||||
|
|
||||||
@Table
|
@Table
|
||||||
class User extends Model<User> {
|
class User extends Model<User> {
|
||||||
@@ -42,6 +44,9 @@ class User extends Model<User> {
|
|||||||
@UpdatedAt
|
@UpdatedAt
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
|
|
||||||
|
@HasMany(() => Ticket)
|
||||||
|
tickets: Ticket[];
|
||||||
|
|
||||||
@BeforeUpdate
|
@BeforeUpdate
|
||||||
@BeforeCreate
|
@BeforeCreate
|
||||||
static hashPassword = async (instance: User): Promise<void> => {
|
static hashPassword = async (instance: User): Promise<void> => {
|
||||||
|
|||||||
53
backend/src/models/Whatsapp.ts
Normal file
53
backend/src/models/Whatsapp.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import {
|
||||||
|
Table,
|
||||||
|
Column,
|
||||||
|
CreatedAt,
|
||||||
|
UpdatedAt,
|
||||||
|
Model,
|
||||||
|
DataType,
|
||||||
|
PrimaryKey,
|
||||||
|
AutoIncrement,
|
||||||
|
Default,
|
||||||
|
AllowNull,
|
||||||
|
HasMany
|
||||||
|
} from "sequelize-typescript";
|
||||||
|
import Ticket from "./Ticket";
|
||||||
|
|
||||||
|
@Table
|
||||||
|
class Whatsapp extends Model<Whatsapp> {
|
||||||
|
@PrimaryKey
|
||||||
|
@AutoIncrement
|
||||||
|
@Column
|
||||||
|
id: number;
|
||||||
|
|
||||||
|
@Column(DataType.TEXT)
|
||||||
|
session: string;
|
||||||
|
|
||||||
|
@Column(DataType.TEXT)
|
||||||
|
qrcode: string;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
status: string;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
battery: string;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
plugged: boolean;
|
||||||
|
|
||||||
|
@Default(false)
|
||||||
|
@AllowNull
|
||||||
|
@Column
|
||||||
|
default: boolean;
|
||||||
|
|
||||||
|
@CreatedAt
|
||||||
|
createdAt: Date;
|
||||||
|
|
||||||
|
@UpdatedAt
|
||||||
|
updatedAt: Date;
|
||||||
|
|
||||||
|
@HasMany(() => Ticket)
|
||||||
|
tickets: Ticket[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Whatsapp;
|
||||||
@@ -5,10 +5,10 @@ class ContactCustomField extends Sequelize.Model {
|
|||||||
super.init(
|
super.init(
|
||||||
{
|
{
|
||||||
name: { type: Sequelize.STRING },
|
name: { type: Sequelize.STRING },
|
||||||
value: { type: Sequelize.STRING },
|
value: { type: Sequelize.STRING }
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sequelize,
|
sequelize
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ class ContactCustomField extends Sequelize.Model {
|
|||||||
static associate(models) {
|
static associate(models) {
|
||||||
this.belongsTo(models.Contact, {
|
this.belongsTo(models.Contact, {
|
||||||
foreignKey: "contactId",
|
foreignKey: "contactId",
|
||||||
as: "extraInfo",
|
as: "extraInfo"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,15 +12,15 @@ class Message extends Sequelize.Model {
|
|||||||
mediaType: { type: Sequelize.STRING },
|
mediaType: { type: Sequelize.STRING },
|
||||||
createdAt: {
|
createdAt: {
|
||||||
type: Sequelize.DATE(6),
|
type: Sequelize.DATE(6),
|
||||||
allowNull: false,
|
allowNull: false
|
||||||
},
|
},
|
||||||
updatedAt: {
|
updatedAt: {
|
||||||
type: Sequelize.DATE(6),
|
type: Sequelize.DATE(6),
|
||||||
allowNull: false,
|
allowNull: false
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sequelize,
|
sequelize
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -13,11 +13,11 @@ class Whatsapp extends Sequelize.Model {
|
|||||||
default: {
|
default: {
|
||||||
type: Sequelize.BOOLEAN,
|
type: Sequelize.BOOLEAN,
|
||||||
defaultValue: false,
|
defaultValue: false,
|
||||||
allowNull: false,
|
allowNull: false
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sequelize,
|
sequelize
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Router, Request, Response } from "express";
|
import { Router, Request, Response } from "express";
|
||||||
import SessionController from "../controllers/SessionController";
|
import * as SessionController from "../controllers/SessionController";
|
||||||
import isAuth from "../middleware/isAuth";
|
import isAuth from "../middleware/isAuth";
|
||||||
import * as UserController from "../controllers/UserController";
|
import * as UserController from "../controllers/UserController";
|
||||||
|
|
||||||
@@ -7,7 +7,7 @@ const authRoutes = Router();
|
|||||||
|
|
||||||
authRoutes.post("/signup", UserController.store);
|
authRoutes.post("/signup", UserController.store);
|
||||||
|
|
||||||
authRoutes.post("/login", SessionController);
|
authRoutes.post("/login", SessionController.store);
|
||||||
|
|
||||||
authRoutes.get("/check", isAuth, (req: Request, res: Response) => {
|
authRoutes.get("/check", isAuth, (req: Request, res: Response) => {
|
||||||
res.status(200).json({ authenticated: true });
|
res.status(200).json({ authenticated: true });
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import userRoutes from "./userRoutes";
|
|||||||
import authRoutes from "./authRoutes";
|
import authRoutes from "./authRoutes";
|
||||||
import settingRoutes from "./settingRoutes";
|
import settingRoutes from "./settingRoutes";
|
||||||
import contactRoutes from "./contactRoutes";
|
import contactRoutes from "./contactRoutes";
|
||||||
|
import ticketRoutes from "./ticketRoutes";
|
||||||
|
|
||||||
// const TicketsRoutes = require("./routes/tickets");
|
|
||||||
// const MessagesRoutes = require("./routes/messages");
|
// const MessagesRoutes = require("./routes/messages");
|
||||||
// const WhatsRoutes = require("./routes/whatsapp");
|
// const WhatsRoutes = require("./routes/whatsapp");
|
||||||
// const UsersRoutes = require("./routes/users");
|
// const UsersRoutes = require("./routes/users");
|
||||||
@@ -16,7 +16,7 @@ routes.use(userRoutes);
|
|||||||
routes.use("/auth", authRoutes);
|
routes.use("/auth", authRoutes);
|
||||||
routes.use(settingRoutes);
|
routes.use(settingRoutes);
|
||||||
routes.use(contactRoutes);
|
routes.use(contactRoutes);
|
||||||
// routes.use(TicketsRoutes);
|
routes.use(ticketRoutes);
|
||||||
// routes.use(MessagesRoutes);
|
// routes.use(MessagesRoutes);
|
||||||
// routes.use(WhatsRoutes);
|
// routes.use(WhatsRoutes);
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
const express = require("express");
|
|
||||||
const isAuth = require("../../middleware/is-auth");
|
|
||||||
|
|
||||||
const TicketController = require("../../controllers/TicketController");
|
|
||||||
|
|
||||||
const routes = express.Router();
|
|
||||||
|
|
||||||
routes.get("/tickets", isAuth, TicketController.index);
|
|
||||||
|
|
||||||
routes.post("/tickets", isAuth, TicketController.store);
|
|
||||||
|
|
||||||
routes.put("/tickets/:ticketId", isAuth, TicketController.update);
|
|
||||||
|
|
||||||
routes.delete("/tickets/:ticketId", isAuth, TicketController.delete);
|
|
||||||
|
|
||||||
module.exports = routes;
|
|
||||||
16
backend/src/routes/ticketRoutes.ts
Normal file
16
backend/src/routes/ticketRoutes.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import express from "express";
|
||||||
|
import isAuth from "../middleware/isAuth";
|
||||||
|
|
||||||
|
import * as TicketController from "../controllers/TicketController";
|
||||||
|
|
||||||
|
const ticketRoutes = express.Router();
|
||||||
|
|
||||||
|
ticketRoutes.get("/tickets", isAuth, TicketController.index);
|
||||||
|
|
||||||
|
ticketRoutes.post("/tickets", isAuth, TicketController.store);
|
||||||
|
|
||||||
|
// ticketRoutes.put("/tickets/:ticketId", isAuth, TicketController.update);
|
||||||
|
|
||||||
|
// ticketRoutes.delete("/tickets/:ticketId", isAuth, TicketController.remove);
|
||||||
|
|
||||||
|
export default ticketRoutes;
|
||||||
24
backend/src/services/TicketServices/CreateTicketService.ts
Normal file
24
backend/src/services/TicketServices/CreateTicketService.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import * as Yup from "yup";
|
||||||
|
|
||||||
|
import AppError from "../../errors/AppError";
|
||||||
|
import User from "../../models/User";
|
||||||
|
|
||||||
|
interface Request {
|
||||||
|
email: string;
|
||||||
|
password: string;
|
||||||
|
name: string;
|
||||||
|
profile?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Response {
|
||||||
|
email: string;
|
||||||
|
name: string;
|
||||||
|
id: number;
|
||||||
|
profile: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const CreateTicketService = async (): Promise<boolean> => {
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CreateTicketService;
|
||||||
74
backend/src/services/TicketServices/ListTicketsService.ts
Normal file
74
backend/src/services/TicketServices/ListTicketsService.ts
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
import { Op } from "sequelize";
|
||||||
|
import { startOfDay, endOfDay, parseISO } from "date-fns";
|
||||||
|
|
||||||
|
import Ticket from "../../models/Ticket";
|
||||||
|
|
||||||
|
interface Request {
|
||||||
|
searchParam?: string;
|
||||||
|
pageNumber?: string;
|
||||||
|
status?: string;
|
||||||
|
date?: string;
|
||||||
|
showAll?: string;
|
||||||
|
userId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Response {
|
||||||
|
tickets: Ticket[];
|
||||||
|
count: number;
|
||||||
|
hasMore: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ListTicketsService = async ({
|
||||||
|
searchParam = "",
|
||||||
|
pageNumber = "1",
|
||||||
|
status,
|
||||||
|
date,
|
||||||
|
showAll,
|
||||||
|
userId
|
||||||
|
}: Request): Promise<Response> => {
|
||||||
|
let whereCondition = {};
|
||||||
|
|
||||||
|
if (showAll === "true") {
|
||||||
|
whereCondition = {};
|
||||||
|
} else {
|
||||||
|
whereCondition = { userId };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (status) {
|
||||||
|
whereCondition = {
|
||||||
|
...whereCondition,
|
||||||
|
status
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (date) {
|
||||||
|
whereCondition = {
|
||||||
|
...whereCondition,
|
||||||
|
createdAt: {
|
||||||
|
[Op.between]: [startOfDay(parseISO(date)), endOfDay(parseISO(date))]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const limit = 20;
|
||||||
|
const offset = limit * (+pageNumber - 1);
|
||||||
|
|
||||||
|
const { count, rows: tickets } = await Ticket.findAndCountAll({
|
||||||
|
where: whereCondition,
|
||||||
|
// distinct: true,
|
||||||
|
// include: includeCondition,
|
||||||
|
limit,
|
||||||
|
offset,
|
||||||
|
order: [["updatedAt", "DESC"]]
|
||||||
|
});
|
||||||
|
|
||||||
|
const hasMore = count > offset + tickets.length;
|
||||||
|
|
||||||
|
return {
|
||||||
|
tickets,
|
||||||
|
count,
|
||||||
|
hasMore
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ListTicketsService;
|
||||||
@@ -28,7 +28,7 @@ const ListUsersService = async ({
|
|||||||
{ email: { [Op.like]: `%${searchParam.toLowerCase()}%` } }
|
{ email: { [Op.like]: `%${searchParam.toLowerCase()}%` } }
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
const limit = 10;
|
const limit = 20;
|
||||||
const offset = limit * (+pageNumber - 1);
|
const offset = limit * (+pageNumber - 1);
|
||||||
|
|
||||||
const { count, rows: users } = await User.findAndCountAll({
|
const { count, rows: users } = await User.findAndCountAll({
|
||||||
|
|||||||
Reference in New Issue
Block a user