mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 12:19:16 +00:00
feat: added user association to queues
This commit is contained in:
@@ -7,6 +7,7 @@ interface Request {
|
||||
email: string;
|
||||
password: string;
|
||||
name: string;
|
||||
queueIds?: number[];
|
||||
profile?: string;
|
||||
}
|
||||
|
||||
@@ -21,6 +22,7 @@ const CreateUserService = async ({
|
||||
email,
|
||||
password,
|
||||
name,
|
||||
queueIds = [],
|
||||
profile = "admin"
|
||||
}: Request): Promise<Response> => {
|
||||
const schema = Yup.object().shape({
|
||||
@@ -47,18 +49,26 @@ const CreateUserService = async ({
|
||||
throw new AppError(err.message);
|
||||
}
|
||||
|
||||
const user = await User.create({
|
||||
email,
|
||||
password,
|
||||
name,
|
||||
profile
|
||||
});
|
||||
const user = await User.create(
|
||||
{
|
||||
email,
|
||||
password,
|
||||
name,
|
||||
profile
|
||||
},
|
||||
{ include: ["queues"] }
|
||||
);
|
||||
|
||||
await user.$set("queues", queueIds);
|
||||
|
||||
await user.reload();
|
||||
|
||||
const serializedUser = {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
profile: user.profile
|
||||
profile: user.profile,
|
||||
queues: user.queues
|
||||
};
|
||||
|
||||
return serializedUser;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Sequelize, Op } from "sequelize";
|
||||
import Queue from "../../models/Queue";
|
||||
import User from "../../models/User";
|
||||
|
||||
interface Request {
|
||||
@@ -19,8 +20,8 @@ const ListUsersService = async ({
|
||||
const whereCondition = {
|
||||
[Op.or]: [
|
||||
{
|
||||
name: Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("name")),
|
||||
"$User.name$": Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("User.name")),
|
||||
"LIKE",
|
||||
`%${searchParam.toLowerCase()}%`
|
||||
)
|
||||
@@ -33,10 +34,13 @@ const ListUsersService = async ({
|
||||
|
||||
const { count, rows: users } = await User.findAndCountAll({
|
||||
where: whereCondition,
|
||||
attributes: ["name", "id", "email", "profile"],
|
||||
attributes: ["name", "id", "email", "profile", "createdAt"],
|
||||
limit,
|
||||
offset,
|
||||
order: [["createdAt", "DESC"]]
|
||||
order: [["createdAt", "DESC"]],
|
||||
include: [
|
||||
{ model: Queue, as: "queues", attributes: ["id", "name", "color"] }
|
||||
]
|
||||
});
|
||||
|
||||
const hasMore = count > offset + users.length;
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
import User from "../../models/User";
|
||||
import AppError from "../../errors/AppError";
|
||||
import Queue from "../../models/Queue";
|
||||
|
||||
const ShowUserService = async (id: string | number): Promise<User> => {
|
||||
const user = await User.findByPk(id, {
|
||||
attributes: ["name", "id", "email", "profile", "tokenVersion"]
|
||||
attributes: ["name", "id", "email", "profile", "tokenVersion"],
|
||||
include: [
|
||||
{ model: Queue, as: "queues", attributes: ["id", "name", "color"] }
|
||||
]
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import * as Yup from "yup";
|
||||
|
||||
import AppError from "../../errors/AppError";
|
||||
import User from "../../models/User";
|
||||
import ShowUserService from "./ShowUserService";
|
||||
|
||||
interface UserData {
|
||||
email?: string;
|
||||
password?: string;
|
||||
name?: string;
|
||||
profile?: string;
|
||||
queueIds?: number[];
|
||||
}
|
||||
|
||||
interface Request {
|
||||
@@ -26,14 +27,7 @@ const UpdateUserService = async ({
|
||||
userData,
|
||||
userId
|
||||
}: Request): Promise<Response | undefined> => {
|
||||
const user = await User.findOne({
|
||||
where: { id: userId },
|
||||
attributes: ["name", "id", "email", "profile"]
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError("ERR_NO_USER_FOUND", 404);
|
||||
}
|
||||
const user = await ShowUserService(userId);
|
||||
|
||||
const schema = Yup.object().shape({
|
||||
name: Yup.string().min(2),
|
||||
@@ -42,7 +36,7 @@ const UpdateUserService = async ({
|
||||
password: Yup.string()
|
||||
});
|
||||
|
||||
const { email, password, profile, name } = userData;
|
||||
const { email, password, profile, name, queueIds = [] } = userData;
|
||||
|
||||
try {
|
||||
await schema.validate({ email, password, profile, name });
|
||||
@@ -57,11 +51,16 @@ const UpdateUserService = async ({
|
||||
name
|
||||
});
|
||||
|
||||
await user.$set("queues", queueIds);
|
||||
|
||||
await user.reload();
|
||||
|
||||
const serializedUser = {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
profile: user.profile
|
||||
profile: user.profile,
|
||||
queues: user.queues
|
||||
};
|
||||
|
||||
return serializedUser;
|
||||
|
||||
@@ -18,7 +18,7 @@ interface Response {
|
||||
const CreateWhatsAppService = async ({
|
||||
name,
|
||||
status = "OPENING",
|
||||
queueIds,
|
||||
queueIds = [],
|
||||
isDefault = false
|
||||
}: Request): Promise<Response> => {
|
||||
const schema = Yup.object().shape({
|
||||
@@ -64,14 +64,19 @@ const CreateWhatsAppService = async ({
|
||||
}
|
||||
}
|
||||
|
||||
const whatsapp = await Whatsapp.create({
|
||||
name,
|
||||
status,
|
||||
isDefault
|
||||
});
|
||||
const whatsapp = await Whatsapp.create(
|
||||
{
|
||||
name,
|
||||
status,
|
||||
isDefault
|
||||
},
|
||||
{ include: ["queues"] }
|
||||
);
|
||||
|
||||
await whatsapp.$set("queues", queueIds);
|
||||
|
||||
await whatsapp.reload();
|
||||
|
||||
return { whatsapp, oldDefaultWhatsapp };
|
||||
};
|
||||
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import Whatsapp from "../../models/Whatsapp";
|
||||
import AppError from "../../errors/AppError";
|
||||
import Queue from "../../models/Queue";
|
||||
|
||||
const ShowWhatsAppService = async (id: string | number): Promise<Whatsapp> => {
|
||||
const whatsapp = await Whatsapp.findByPk(id);
|
||||
const whatsapp = await Whatsapp.findByPk(id, {
|
||||
include: [
|
||||
{ model: Queue, as: "queues", attributes: ["id", "name", "color"] }
|
||||
]
|
||||
});
|
||||
|
||||
if (!whatsapp) {
|
||||
throw new AppError("ERR_NO_WAPP_FOUND", 404);
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Op } from "sequelize";
|
||||
|
||||
import AppError from "../../errors/AppError";
|
||||
import Whatsapp from "../../models/Whatsapp";
|
||||
import ShowWhatsAppService from "./ShowWhatsAppService";
|
||||
|
||||
interface WhatsappData {
|
||||
name?: string;
|
||||
@@ -50,13 +51,8 @@ const UpdateWhatsAppService = async ({
|
||||
}
|
||||
}
|
||||
|
||||
const whatsapp = await Whatsapp.findOne({
|
||||
where: { id: whatsappId }
|
||||
});
|
||||
const whatsapp = await ShowWhatsAppService(whatsappId);
|
||||
|
||||
if (!whatsapp) {
|
||||
throw new AppError("ERR_NO_WAPP_FOUND", 404);
|
||||
}
|
||||
await whatsapp.update({
|
||||
name,
|
||||
status,
|
||||
@@ -66,6 +62,8 @@ const UpdateWhatsAppService = async ({
|
||||
|
||||
await whatsapp.$set("queues", queueIds);
|
||||
|
||||
await whatsapp.reload();
|
||||
|
||||
return { whatsapp, oldDefaultWhatsapp };
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user