mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 19:59:20 +00:00
changed contacts routes to typescript
This commit is contained in:
32
backend/src/services/ContactServices/CreateContactService.ts
Normal file
32
backend/src/services/ContactServices/CreateContactService.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import AppError from "../../errors/AppError";
|
||||
import Contact from "../../models/Contact";
|
||||
|
||||
interface Request {
|
||||
name: string;
|
||||
number: string;
|
||||
email?: string;
|
||||
}
|
||||
|
||||
const CreateContactService = async ({
|
||||
name,
|
||||
number,
|
||||
email
|
||||
}: Request): Promise<Contact> => {
|
||||
const numberExists = await Contact.findOne({
|
||||
where: { number }
|
||||
});
|
||||
|
||||
if (numberExists) {
|
||||
throw new AppError("A contact with this number already exists.");
|
||||
}
|
||||
|
||||
const contact = await Contact.create({
|
||||
name,
|
||||
number,
|
||||
email
|
||||
});
|
||||
|
||||
return contact;
|
||||
};
|
||||
|
||||
export default CreateContactService;
|
||||
16
backend/src/services/ContactServices/DeleteContactService.ts
Normal file
16
backend/src/services/ContactServices/DeleteContactService.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import Contact from "../../models/Contact";
|
||||
import AppError from "../../errors/AppError";
|
||||
|
||||
const DeleteContactService = async (id: string): Promise<void> => {
|
||||
const contact = await Contact.findOne({
|
||||
where: { id }
|
||||
});
|
||||
|
||||
if (!contact) {
|
||||
throw new AppError("No contact found with this ID.", 404);
|
||||
}
|
||||
|
||||
await contact.destroy();
|
||||
};
|
||||
|
||||
export default DeleteContactService;
|
||||
50
backend/src/services/ContactServices/ListContactsService.ts
Normal file
50
backend/src/services/ContactServices/ListContactsService.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { Sequelize, Op } from "sequelize";
|
||||
import Contact from "../../models/Contact";
|
||||
|
||||
interface Request {
|
||||
searchParam?: string;
|
||||
pageNumber?: string;
|
||||
}
|
||||
|
||||
interface Response {
|
||||
contacts: Contact[];
|
||||
count: number;
|
||||
hasMore: boolean;
|
||||
}
|
||||
|
||||
const ListContactsService = async ({
|
||||
searchParam = "",
|
||||
pageNumber = "1"
|
||||
}: Request): Promise<Response> => {
|
||||
const whereCondition = {
|
||||
[Op.or]: [
|
||||
{
|
||||
name: Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("name")),
|
||||
"LIKE",
|
||||
`%${searchParam.toLowerCase()}%`
|
||||
)
|
||||
},
|
||||
{ number: { [Op.like]: `%${searchParam}%` } }
|
||||
]
|
||||
};
|
||||
const limit = 20;
|
||||
const offset = limit * (+pageNumber - 1);
|
||||
|
||||
const { count, rows: contacts } = await Contact.findAndCountAll({
|
||||
where: whereCondition,
|
||||
limit,
|
||||
offset,
|
||||
order: [["createdAt", "DESC"]]
|
||||
});
|
||||
|
||||
const hasMore = count > offset + contacts.length;
|
||||
|
||||
return {
|
||||
contacts,
|
||||
count,
|
||||
hasMore
|
||||
};
|
||||
};
|
||||
|
||||
export default ListContactsService;
|
||||
17
backend/src/services/ContactServices/ShowContactService.ts
Normal file
17
backend/src/services/ContactServices/ShowContactService.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import Contact from "../../models/Contact";
|
||||
import AppError from "../../errors/AppError";
|
||||
|
||||
const ShowContactService = async (id: string): Promise<Contact> => {
|
||||
const user = await Contact.findOne({
|
||||
where: { id },
|
||||
attributes: ["id", "name", "number", "email"]
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AppError("No contact found with this ID.", 404);
|
||||
}
|
||||
|
||||
return user;
|
||||
};
|
||||
|
||||
export default ShowContactService;
|
||||
39
backend/src/services/ContactServices/UpdateContactService.ts
Normal file
39
backend/src/services/ContactServices/UpdateContactService.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import AppError from "../../errors/AppError";
|
||||
import Contact from "../../models/Contact";
|
||||
|
||||
interface ContactData {
|
||||
email?: string;
|
||||
number?: string;
|
||||
name?: string;
|
||||
}
|
||||
|
||||
interface Request {
|
||||
contactData: ContactData;
|
||||
contactId: string;
|
||||
}
|
||||
|
||||
const UpdateContactService = async ({
|
||||
contactData,
|
||||
contactId
|
||||
}: Request): Promise<Contact> => {
|
||||
const { email, name, number } = contactData;
|
||||
|
||||
const contact = await Contact.findOne({
|
||||
where: { id: contactId },
|
||||
attributes: ["id", "name", "number", "email"]
|
||||
});
|
||||
|
||||
if (!contact) {
|
||||
throw new AppError("No contact found with this ID.", 404);
|
||||
}
|
||||
|
||||
await contact.update({
|
||||
name,
|
||||
number,
|
||||
email
|
||||
});
|
||||
|
||||
return contact;
|
||||
};
|
||||
|
||||
export default UpdateContactService;
|
||||
@@ -3,7 +3,7 @@ import User from "../../models/User";
|
||||
|
||||
interface Request {
|
||||
searchParam?: string;
|
||||
pageNumber?: number;
|
||||
pageNumber?: string;
|
||||
}
|
||||
|
||||
interface Response {
|
||||
@@ -14,7 +14,7 @@ interface Response {
|
||||
|
||||
const ListUsersService = async ({
|
||||
searchParam = "",
|
||||
pageNumber = 1
|
||||
pageNumber = "1"
|
||||
}: Request): Promise<Response> => {
|
||||
const whereCondition = {
|
||||
[Op.or]: [
|
||||
@@ -28,8 +28,8 @@ const ListUsersService = async ({
|
||||
{ email: { [Op.like]: `%${searchParam.toLowerCase()}%` } }
|
||||
]
|
||||
};
|
||||
const limit = 20;
|
||||
const offset = limit * (pageNumber - 1);
|
||||
const limit = 10;
|
||||
const offset = limit * (+pageNumber - 1);
|
||||
|
||||
const { count, rows: users } = await User.findAndCountAll({
|
||||
where: whereCondition,
|
||||
@@ -39,7 +39,7 @@ const ListUsersService = async ({
|
||||
order: [["createdAt", "DESC"]]
|
||||
});
|
||||
|
||||
const hasMore = count > limit + users.length;
|
||||
const hasMore = count > offset + users.length;
|
||||
|
||||
return {
|
||||
users,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import User from "../../models/User";
|
||||
import AppError from "../../errors/AppError";
|
||||
|
||||
const FindUserService = async (id: string): Promise<User | undefined> => {
|
||||
const ShowUserService = async (id: string): Promise<User | undefined> => {
|
||||
const user = await User.findOne({
|
||||
where: { id },
|
||||
attributes: ["name", "id", "email", "profile"]
|
||||
@@ -14,4 +14,4 @@ const FindUserService = async (id: string): Promise<User | undefined> => {
|
||||
return user;
|
||||
};
|
||||
|
||||
export default FindUserService;
|
||||
export default ShowUserService;
|
||||
Reference in New Issue
Block a user