Skip to content

Commit

Permalink
Horário de funcionamento do Atendente
Browse files Browse the repository at this point in the history
  • Loading branch information
rtenorioh committed Oct 19, 2022
1 parent c4d22ee commit 404b61a
Show file tree
Hide file tree
Showing 15 changed files with 701 additions and 483 deletions.
22 changes: 16 additions & 6 deletions backend/src/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,22 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
};

export const store = async (req: Request, res: Response): Promise<Response> => {

const { users } = await ListUsersService({});

if (users.length >= Number(process.env.USER_LIMIT)) {
throw new AppError("ERR_USER_CREATION_COUNT", 403);
}

const { email, password, name, profile, queueIds, whatsappId } = req.body;

const {
email,
password,
name,
profile,
queueIds,
whatsappId,
startWork,
endWork
} = req.body;

if (
req.url === "/signup" &&
Expand All @@ -51,7 +59,9 @@ export const store = async (req: Request, res: Response): Promise<Response> => {
name,
profile,
queueIds,
whatsappId
whatsappId,
startWork,
endWork
});

const io = getIO();
Expand Down Expand Up @@ -80,7 +90,7 @@ export const update = async (
const newUserId = userId.toString();
const sessionUserId = req.user.id.toString();

if (req.user.profile !== "admin" && sessionUserId !== newUserId) {
if (req.user.profile !== "admin" && sessionUserId !== newUserId) {
throw new AppError("ERR_NO_PERMISSION", 403);
}

Expand Down Expand Up @@ -116,4 +126,4 @@ export const remove = async (
});

return res.status(200).json({ message: "User deleted" });
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { QueryInterface, DataTypes } from "sequelize";

module.exports = {
up: (queryInterface: QueryInterface) => {
return queryInterface.addColumn("Users", "startWork", {
type: DataTypes.STRING,
allowNull: true,
defaultValue: "00:00"
});
},

down: (queryInterface: QueryInterface) => {
return queryInterface.removeColumn("Users", "startWork");
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { QueryInterface, DataTypes } from "sequelize";

module.exports = {
up: (queryInterface: QueryInterface) => {
return queryInterface.addColumn("Users", "endWork", {
type: DataTypes.STRING,
allowNull: true,
defaultValue: "23:59"
});
},

down: (queryInterface: QueryInterface) => {
return queryInterface.removeColumn("Users", "endWork");
}
};
8 changes: 6 additions & 2 deletions backend/src/helpers/SerializeUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ interface SerializedUser {
profile: string;
queues: Queue[];
whatsapp: Whatsapp;
startWork: string;
endWork: string;
}

export const SerializeUser = (user: User): SerializedUser => {
Expand All @@ -18,6 +20,8 @@ export const SerializeUser = (user: User): SerializedUser => {
email: user.email,
profile: user.profile,
queues: user.queues,
whatsapp: user.whatsapp
whatsapp: user.whatsapp,
startWork: user.startWork,
endWork: user.endWork
};
};
};
10 changes: 9 additions & 1 deletion backend/src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ class User extends Model<User> {
@BelongsTo(() => Whatsapp)
whatsapp: Whatsapp;

@Default("00:00")
@Column
startWork: string;

@Default("23:59")
@Column
endWork: string;

@CreatedAt
createdAt: Date;

Expand All @@ -80,4 +88,4 @@ class User extends Model<User> {
};
}

export default User;
export default User;
20 changes: 20 additions & 0 deletions backend/src/services/UserServices/AuthUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ const AuthUserService = async ({
throw new AppError("ERR_INVALID_CREDENTIALS", 401);
}

const Hr = new Date();

const hh: number = Hr.getHours() * 60 * 60;
const mm: number = Hr.getMinutes() * 60;
const hora = hh + mm;

const inicio: string = user.startWork;
const hhinicio = Number(inicio.split(":")[0]) * 60 * 60;
const mminicio = Number(inicio.split(":")[1]) * 60;
const horainicio = hhinicio + mminicio;

const termino: string = user.endWork;
const hhtermino = Number(termino.split(":")[0]) * 60 * 60;
const mmtermino = Number(termino.split(":")[1]) * 60;
const horatermino = hhtermino + mmtermino;

if (hora < horainicio || hora > horatermino) {
throw new AppError("ERR_OUT_OF_HOURS", 401);
}

if (!(await user.checkPassword(password))) {
throw new AppError("ERR_INVALID_CREDENTIALS", 401);
}
Expand Down
12 changes: 9 additions & 3 deletions backend/src/services/UserServices/CreateUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ interface Request {
queueIds?: number[];
profile?: string;
whatsappId?: number;
startWork?: string;
endWork?: string;
}

interface Response {
Expand All @@ -26,7 +28,9 @@ const CreateUserService = async ({
name,
queueIds = [],
profile = "admin",
whatsappId
whatsappId,
startWork,
endWork
}: Request): Promise<Response> => {
const schema = Yup.object().shape({
name: Yup.string().required().min(2),
Expand Down Expand Up @@ -59,7 +63,9 @@ const CreateUserService = async ({
password,
name,
profile,
whatsappId: whatsappId ? whatsappId : null
whatsappId: whatsappId || null,
startWork,
endWork
},
{ include: ["queues", "whatsapp"] }
);
Expand All @@ -71,4 +77,4 @@ const CreateUserService = async ({
return SerializeUser(user);
};

export default CreateUserService;
export default CreateUserService;
14 changes: 11 additions & 3 deletions backend/src/services/UserServices/ListUsersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ const ListUsersService = async ({

const { count, rows: users } = await User.findAndCountAll({
where: whereCondition,
attributes: ["name", "id", "email", "profile", "createdAt"],
attributes: [
"name",
"id",
"email",
"profile",
"createdAt",
"startWork",
"endWork"
],
limit,
offset,
order: [["createdAt", "DESC"]],
include: [
{ model: Queue, as: "queues", attributes: ["id", "name", "color"] },
{ model: Whatsapp, as: "whatsapp", attributes: ["id", "name"] },
{ model: Whatsapp, as: "whatsapp", attributes: ["id", "name"] }
]
});

Expand All @@ -54,4 +62,4 @@ const ListUsersService = async ({
};
};

export default ListUsersService;
export default ListUsersService;
17 changes: 13 additions & 4 deletions backend/src/services/UserServices/ShowUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ import Whatsapp from "../../models/Whatsapp";

const ShowUserService = async (id: string | number): Promise<User> => {
const user = await User.findByPk(id, {
attributes: ["name", "id", "email", "profile", "tokenVersion", "whatsappId"],
attributes: [
"name",
"id",
"email",
"profile",
"tokenVersion",
"whatsappId",
"startWork",
"endWork"
],
include: [
{ model: Queue, as: "queues", attributes: ["id", "name", "color"] },
{ model: Whatsapp, as: "whatsapp", attributes: ["id", "name"] },
{ model: Whatsapp, as: "whatsapp", attributes: ["id", "name"] }
],
order: [ [ { model: Queue, as: "queues"}, 'name', 'asc' ] ]
order: [[{ model: Queue, as: "queues" }, "name", "asc"]]
});
if (!user) {
throw new AppError("ERR_NO_USER_FOUND", 404);
Expand All @@ -19,4 +28,4 @@ const ShowUserService = async (id: string | number): Promise<User> => {
return user;
};

export default ShowUserService;
export default ShowUserService;
19 changes: 16 additions & 3 deletions backend/src/services/UserServices/UpdateUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ interface UserData {
profile?: string;
queueIds?: number[];
whatsappId?: number;
startWork?: string;
endWork?: string;
}

interface Request {
Expand Down Expand Up @@ -38,7 +40,16 @@ const UpdateUserService = async ({
password: Yup.string()
});

const { email, password, profile, name, queueIds = [], whatsappId } = userData;
const {
email,
password,
profile,
name,
queueIds = [],
whatsappId,
startWork,
endWork
} = userData;

try {
await schema.validate({ email, password, profile, name });
Expand All @@ -51,7 +62,9 @@ const UpdateUserService = async ({
password,
profile,
name,
whatsappId: whatsappId ? whatsappId : null
whatsappId: whatsappId || null,
startWork,
endWork
});

await user.$set("queues", queueIds);
Expand All @@ -61,4 +74,4 @@ const UpdateUserService = async ({
return SerializeUser(user);
};

export default UpdateUserService;
export default UpdateUserService;
Loading

0 comments on commit 404b61a

Please sign in to comment.