mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 04:09:26 +00:00
started migration of user domain to ts
This commit is contained in:
53
backend/src/services/CreateUserService.ts
Normal file
53
backend/src/services/CreateUserService.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
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;
|
||||
}
|
||||
|
||||
const CreateUserService = async ({
|
||||
email,
|
||||
password,
|
||||
name,
|
||||
profile = "admin"
|
||||
}: Request): Promise<User> => {
|
||||
// const schema = Yup.object().shape({
|
||||
// name: Yup.string().required().min(2),
|
||||
// email: Yup.string()
|
||||
// .email()
|
||||
// .required()
|
||||
// .test(
|
||||
// "Check-email",
|
||||
// "An user with this email already exists.",
|
||||
// async value => {
|
||||
// const emailExists = await User.findOne({
|
||||
// where: { email: value }
|
||||
// });
|
||||
// return !Boolean(emailExists);
|
||||
// }
|
||||
// ),
|
||||
// password: Yup.string().required().min(5)
|
||||
// });
|
||||
|
||||
// try {
|
||||
// await schema.validate({ email, password, name });
|
||||
// } catch (err) {
|
||||
// throw new AppError(err.message);
|
||||
// }
|
||||
|
||||
const user = User.create({
|
||||
email,
|
||||
passwordHash: password,
|
||||
name,
|
||||
profile
|
||||
});
|
||||
|
||||
return user;
|
||||
};
|
||||
|
||||
export default CreateUserService;
|
||||
21
backend/src/services/FindUserService.ts
Normal file
21
backend/src/services/FindUserService.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { getRepository, Raw } from "typeorm";
|
||||
|
||||
import User from "../models/User";
|
||||
import AppError from "../errors/AppError";
|
||||
|
||||
const FindUserService = async (id: string): Promise<User | undefined> => {
|
||||
const usersRepository = getRepository(User);
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id },
|
||||
select: ["name", "id", "email", "profile"],
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError("No user found with this ID.", 404);
|
||||
}
|
||||
|
||||
return user;
|
||||
};
|
||||
|
||||
export default FindUserService;
|
||||
54
backend/src/services/ListUsersService.ts
Normal file
54
backend/src/services/ListUsersService.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { getRepository, Raw } from "typeorm";
|
||||
|
||||
import User from "../models/User";
|
||||
|
||||
interface Request {
|
||||
searchParam?: string;
|
||||
pageNumber?: number;
|
||||
}
|
||||
|
||||
interface Response {
|
||||
users: User[];
|
||||
count: number;
|
||||
hasMore: boolean;
|
||||
}
|
||||
|
||||
const ListUsersService = async ({
|
||||
searchParam = "",
|
||||
pageNumber = 1,
|
||||
}: Request): Promise<Response> => {
|
||||
const usersRepository = getRepository(User);
|
||||
|
||||
const whereCondition = [
|
||||
{
|
||||
name: Raw(
|
||||
alias => `LOWER(${alias}) Like '%${searchParam.toLowerCase()}%'`
|
||||
),
|
||||
},
|
||||
{
|
||||
email: Raw(
|
||||
alias => `LOWER(${alias}) Like '%${searchParam.toLowerCase()}%'`
|
||||
),
|
||||
},
|
||||
];
|
||||
const take = 20;
|
||||
const skip = take * (pageNumber - 1);
|
||||
|
||||
const [users, count] = await usersRepository.findAndCount({
|
||||
where: whereCondition,
|
||||
select: ["name", "id", "email", "profile"],
|
||||
skip,
|
||||
take,
|
||||
order: { createdAt: "DESC" },
|
||||
});
|
||||
|
||||
const hasMore = count > skip + users.length;
|
||||
|
||||
return {
|
||||
users,
|
||||
count,
|
||||
hasMore,
|
||||
};
|
||||
};
|
||||
|
||||
export default ListUsersService;
|
||||
61
backend/src/services/UpdateUserService.ts
Normal file
61
backend/src/services/UpdateUserService.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { getRepository } from "typeorm";
|
||||
import * as Yup from "yup";
|
||||
|
||||
import AppError from "../errors/AppError";
|
||||
import User from "../models/User";
|
||||
|
||||
interface UserData {
|
||||
email?: string;
|
||||
password?: string;
|
||||
name?: string;
|
||||
profile?: string;
|
||||
}
|
||||
|
||||
interface Request {
|
||||
userData: UserData;
|
||||
userId: string;
|
||||
}
|
||||
|
||||
const UpdateUserService = async ({
|
||||
userData,
|
||||
userId,
|
||||
}: Request): Promise<User | undefined> => {
|
||||
const usersRepository = getRepository(User);
|
||||
|
||||
const schema = Yup.object().shape({
|
||||
name: Yup.string().min(2),
|
||||
email: Yup.string().email(),
|
||||
password: Yup.string(),
|
||||
});
|
||||
|
||||
const { email, password, name } = userData;
|
||||
|
||||
try {
|
||||
await schema.validate({ email, password, name });
|
||||
} catch (err) {
|
||||
throw new AppError(err.message);
|
||||
}
|
||||
|
||||
const user = await usersRepository.findOne({
|
||||
where: { id: userId },
|
||||
select: ["name", "id", "email", "profile"],
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError("No user found with this ID.", 404);
|
||||
}
|
||||
|
||||
const teste = await usersRepository.update(userId, {
|
||||
email,
|
||||
passwordHash: password,
|
||||
name,
|
||||
});
|
||||
|
||||
console.log(teste);
|
||||
|
||||
delete user.passwordHash;
|
||||
|
||||
return user;
|
||||
};
|
||||
|
||||
export default UpdateUserService;
|
||||
Reference in New Issue
Block a user