Welcome to the NestJS backend application that manages employee data with CRUD functionalities and integrates an email service using a queue system.
- Employee Module:
- Create, Read, Update, and Delete (CRUD) operations for managing employee data.
- Email Service:
- Sends email notifications asynchronously using a queue system (Bull).
- Example usage: Welcome emails for new employees.
- GraphQL API:
- Provides a GraphQL API for flexible data fetching and manipulation.
Ensure you have the following installed on your system:
- Node.js (v14.x or later)
- npm or yarn
- Redis server (for Bull queue)
-
Clone the repository:
git clone https://github.com/your/repository.git cd repository
-
Install dependencies:
npm install
-
Environment Variables:
Create a
.env
file in the root directory with the following variables:JWT_SECRET=DO NOT USE THIS VALUE. INSTEAD, CREATE A COMPLEX SECRET AND KEEP IT SAFE OUTSIDE OF THE SOURCE CODE. JWT_EXPIRATION=30d MAIL_SERVER_HOST=smtp.example.com MAIL_SERVER_PORT=587 MAIL_SERVER_AUTH_USER=[email protected] MAIL_SERVER_AUTH_PASS=password BULL_REDIS_HOST=localhost BULL_REDIS_PORT=6379
Adjust DATABASE_URL
and other variables as per your environment setup.
- Redis Configuration:
Ensure your Bull module configuration (bull
in app.module.ts
) matches your Redis server
configuration (host
, port
).
Start the application in development mode:
npm run start:dev
To interact with the GraphQL API, access the GraphQL playground at http://localhost:3015/graphql
.
The AuthResolver
handles authentication-related GraphQL mutations, specifically the login
mutation. This mutation allows users to log in by providing a username and password, returning an AuthPayload
containing access and refresh tokens, username, email, and role.
Example login
mutation:
mutation {
login(username: "user", password: "password") {
access_token
refresh_token
username
email
role
}
}
Since the resolvers are protected by the JwtAuthGuard, the JWT token needs to be included in every request.
query {
employees {
id
name
email
jobTitle
department
created
updated
}
}
query {
employee(id: "employee_id_here") {
id
name
email
jobTitle
department
created
updated
}
}
Replace "employee_id_here" with the actual ID of the employee you want to retrieve.
mutation {
createEmployee(
name: "John Doe"
email: "[email protected]"
jobTitle: "Software Engineer"
department: "Engineering"
) {
id
name
email
jobTitle
department
created
updated
}
}
mutation {
updateEmployee(
id: "employee_id_here"
name: "Updated Name"
email: "[email protected]"
jobTitle: "Updated Job Title"
department: "Updated Department"
) {
id
name
email
jobTitle
department
created
updated
}
}
Replace "employee_id_here" with the ID of the employee you want to update.
mutation {
deleteEmployee(id: "employee_id_here")
}
Replace "employee_id_here" with the ID of the employee you want to delete.