Skip to content

Commit

Permalink
Validação de contato com ticket aberto II
Browse files Browse the repository at this point in the history
  • Loading branch information
rtenorioh committed Jan 5, 2025
1 parent 1f7e182 commit 0b958e0
Show file tree
Hide file tree
Showing 20 changed files with 559 additions and 208 deletions.
4 changes: 2 additions & 2 deletions backend/src/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
profile,
isTricked,
queueIds,
whatsappId,
whatsappIds,
startWork,
endWork
} = req.body;
Expand All @@ -61,7 +61,7 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
profile,
isTricked,
queueIds,
whatsappId,
whatsappIds,
startWork,
endWork
});
Expand Down
2 changes: 2 additions & 0 deletions backend/src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Tag from "../models/Tag";
import Ticket from "../models/Ticket";
import User from "../models/User";
import UserQueue from "../models/UserQueue";
import UserWhatsapp from "../models/UserWhatsapp";
import Whatsapp from "../models/Whatsapp";
import WhatsappQueue from "../models/WhatsappQueue";

Expand All @@ -33,6 +34,7 @@ const models = [
Queue,
WhatsappQueue,
UserQueue,
UserWhatsapp,
QuickAnswer,
Tag,
ContactTag,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { DataTypes, QueryInterface } from "sequelize";

module.exports = {
up: async (queryInterface: QueryInterface) => {
await queryInterface.removeColumn("Users", "whatsappId");
},

down: async (queryInterface: QueryInterface) => {
await queryInterface.addColumn("Users", "whatsappId", {
type: DataTypes.INTEGER,
allowNull: true,
references: { model: "Whatsapps", key: "id" },
onUpdate: "CASCADE",
onDelete: "CASCADE",
});
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { DataTypes, QueryInterface } from "sequelize";

module.exports = {
up: async (queryInterface: QueryInterface) => {
await queryInterface.createTable("UserWhatsapps", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false,
},
userId: {
type: DataTypes.INTEGER,
allowNull: false,
references: { model: "Users", key: "id" },
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
whatsappId: {
type: DataTypes.INTEGER,
allowNull: false,
references: { model: "Whatsapps", key: "id" },
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
},
});
},

down: async (queryInterface: QueryInterface) => {
await queryInterface.dropTable("UserWhatsapps");
},
};
14 changes: 8 additions & 6 deletions backend/src/helpers/GetDefaultWhatsAppByUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import { logger } from "../utils/logger";
const GetDefaultWhatsAppByUser = async (
userId: number
): Promise<Whatsapp | null> => {
const user = await User.findByPk(userId, {include: ["whatsapp"]});
if( user === null ) {
const user = await User.findByPk(userId, { include: ["whatsapps"] });
if (!user || !user.whatsapps || user.whatsapps.length === 0) {
return null;
}

if(user.whatsapp !== null) {
logger.info(`Found whatsapp linked to user '${user.name}' is '${user.whatsapp.name}'.`);
}
const defaultWhatsapp = user.whatsapps[0];

logger.info(
`Found WhatsApp linked to user '${user.name}' is '${defaultWhatsapp.name}'.`
);

return user.whatsapp;
return defaultWhatsapp;
};

export default GetDefaultWhatsAppByUser;
6 changes: 3 additions & 3 deletions backend/src/helpers/SerializeUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface SerializedUser {
email: string;
profile: string;
queues: Queue[];
whatsapp: Whatsapp;
whatsapps: Whatsapp[];
startWork: string;
endWork: string;
isTricked: string;
Expand All @@ -21,9 +21,9 @@ export const SerializeUser = (user: User): SerializedUser => {
email: user.email,
profile: user.profile,
queues: user.queues,
whatsapp: user.whatsapp,
whatsapps: user.whatsapps,
startWork: user.startWork,
endWork: user.endWork,
isTricked: user.isTricked
};
};
};
35 changes: 15 additions & 20 deletions backend/src/models/User.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { compare, hash } from "bcryptjs";
import {
Table,
AutoIncrement,
BeforeCreate,
BeforeUpdate,
BelongsToMany,
Column,
CreatedAt,
UpdatedAt,
Model,
DataType,
BeforeCreate,
BeforeUpdate,
PrimaryKey,
AutoIncrement,
Default,
HasMany,
BelongsToMany,
ForeignKey,
BelongsTo
Model,
PrimaryKey,
Table,
UpdatedAt
} from "sequelize-typescript";
import { hash, compare } from "bcryptjs";
import Ticket from "./Ticket";
import Queue from "./Queue";
import Ticket from "./Ticket";
import UserQueue from "./UserQueue";
import UserWhatsapp from "./UserWhatsapp";
import Whatsapp from "./Whatsapp";

@Table
Expand Down Expand Up @@ -52,13 +51,6 @@ class User extends Model<User> {
@Column
isTricked: string;

@ForeignKey(() => Whatsapp)
@Column
whatsappId: number;

@BelongsTo(() => Whatsapp)
whatsapp: Whatsapp;

@Default("00:00")
@Column
startWork: string;
Expand All @@ -79,6 +71,9 @@ class User extends Model<User> {
@BelongsToMany(() => Queue, () => UserQueue)
queues: Queue[];

@BelongsToMany(() => Whatsapp, () => UserWhatsapp)
whatsapps: Whatsapp[];

@BeforeUpdate
@BeforeCreate
static hashPassword = async (instance: User): Promise<void> => {
Expand All @@ -92,4 +87,4 @@ class User extends Model<User> {
};
}

export default User;
export default User;
29 changes: 29 additions & 0 deletions backend/src/models/UserWhatsapp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
Column,
CreatedAt,
ForeignKey,
Model,
Table,
UpdatedAt
} from "sequelize-typescript";
import User from "./User";
import Whatsapp from "./Whatsapp";

@Table
class UserWhatsapp extends Model<UserWhatsapp> {
@ForeignKey(() => User)
@Column
userId: number;

@ForeignKey(() => Whatsapp)
@Column
whatsappId: number;

@CreatedAt
createdAt: Date;

@UpdatedAt
updatedAt: Date;
}

export default UserWhatsapp;
8 changes: 5 additions & 3 deletions backend/src/services/UserServices/AuthUserService.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import User from "../../models/User";
import AppError from "../../errors/AppError";
import {
createAccessToken,
createRefreshToken
} from "../../helpers/CreateTokens";
import { SerializeUser } from "../../helpers/SerializeUser";
import Queue from "../../models/Queue";
import User from "../../models/User";
import Whatsapp from "../../models/Whatsapp";

interface SerializedUser {
id: number;
name: string;
email: string;
profile: string;
queues: Queue[];
whatsapps: Whatsapp[];
}

interface Request {
Expand All @@ -32,7 +34,7 @@ const AuthUserService = async ({
}: Request): Promise<Response> => {
const user = await User.findOne({
where: { email },
include: ["queues"]
include: ["queues", "whatsapps"]
});

if (!user) {
Expand Down Expand Up @@ -75,4 +77,4 @@ const AuthUserService = async ({
};
};

export default AuthUserService;
export default AuthUserService;
10 changes: 5 additions & 5 deletions backend/src/services/UserServices/CreateUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface Request {
queueIds?: number[];
profile?: string;
isTricked?: boolean;
whatsappId?: number;
whatsappIds?: number[];
startWork?: string;
endWork?: string;
}
Expand All @@ -30,7 +30,7 @@ const CreateUserService = async ({
queueIds = [],
profile = "admin",
isTricked,
whatsappId,
whatsappIds = [],
startWork,
endWork
}: Request): Promise<Response> => {
Expand Down Expand Up @@ -66,18 +66,18 @@ const CreateUserService = async ({
name,
profile,
isTricked,
whatsappId: whatsappId || null,
startWork,
endWork
},
{ include: ["queues", "whatsapp"] }
{ include: ["queues", "whatsapps"] }
);

await user.$set("queues", queueIds);
await user.$set("whatsapps", whatsappIds);

await user.reload();

return SerializeUser(user);
};

export default CreateUserService;
export default CreateUserService;
2 changes: 1 addition & 1 deletion backend/src/services/UserServices/ListUsersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const ListUsersService = async ({
{ model: Queue, as: "queues", attributes: ["id", "name", "color"] },
{
model: Whatsapp,
as: "whatsapp",
as: "whatsapps",
attributes: ["id", "name", "type", "color"]
}
]
Expand Down
10 changes: 6 additions & 4 deletions backend/src/services/UserServices/ShowUserService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import User from "../../models/User";
import AppError from "../../errors/AppError";
import Queue from "../../models/Queue";
import User from "../../models/User";
import Whatsapp from "../../models/Whatsapp";

const ShowUserService = async (id: string | number): Promise<User> => {
Expand All @@ -12,15 +12,17 @@ const ShowUserService = async (id: string | number): Promise<User> => {
"profile",
"isTricked",
"tokenVersion",
"whatsappId",
"startWork",
"endWork"
],
include: [
{ model: Queue, as: "queues", attributes: ["id", "name", "color"] },
{ model: Whatsapp, as: "whatsapp", attributes: ["id", "name"] }
{ model: Whatsapp, as: "whatsapps", attributes: ["id", "name", "color"] }
],
order: [[{ model: Queue, as: "queues" }, "name", "asc"]]
order: [
[{ model: Queue, as: "queues" }, "name", "asc"],
[{ model: Whatsapp, as: "whatsapps" }, "name", "asc"]
]
});
if (!user) {
throw new AppError("ERR_NO_USER_FOUND", 404);
Expand Down
8 changes: 4 additions & 4 deletions backend/src/services/UserServices/UpdateUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface UserData {
profile?: string;
isTricked?: boolean;
queueIds?: number[];
whatsappId?: number;
whatsappIds?: number[];
startWork?: string;
endWork?: string;
}
Expand Down Expand Up @@ -48,7 +48,7 @@ const UpdateUserService = async ({
isTricked,
name,
queueIds = [],
whatsappId,
whatsappIds = [],
startWork,
endWork
} = userData;
Expand All @@ -65,16 +65,16 @@ const UpdateUserService = async ({
profile,
isTricked,
name,
whatsappId: whatsappId || null,
startWork,
endWork
});

await user.$set("queues", queueIds);
await user.$set("whatsapps", whatsappIds);

await user.reload();

return SerializeUser(user);
};

export default UpdateUserService;
export default UpdateUserService;
Loading

0 comments on commit 0b958e0

Please sign in to comment.