Day-Mood is a mobile application backend API that allows users to track their daily moods, activities, and reflections. The backend provides a RESTful API built with NestJS and uses Prisma ORM with PostgreSQL for data persistence.
- Framework: NestJS 9.4.0
- Database: PostgreSQL
- ORM: Prisma
- Authentication: JWT with Passport.js
- Documentation: Swagger/OpenAPI
The application uses the following data models:
Stores user authentication and profile information:
id: Unique identifier (BigInt)email: User's email address (unique)username: User's display namepassword: Hashed passwordphone: Contact number (optional)gender: User's gender (optional)age: User's age (optional)
Predefined mood options users can select:
id: Unique identifiername: Name of the moodcolor: Color code associated with the moodicon: Icon name or pathcreated_time: Creation timestampupdated_time: Last update timestamp
Predefined activities users can associate with their moods:
id: Unique identifiericon: Icon name or pathdescription: Text description of the activitycreated_time: Creation timestampupdated_time: Last update timestamp
User entries combining mood, activity, and notes:
id: Unique identifiernote: User's written reflectioncreated_time: Creation timestampupdated_time: Last update timestampmood_id: Associated moodactivity_id: Associated activityuser_id: Owner of the recordstatus: Record status (ACTIVE, DRAFT, DELETED)
Media files uploaded by users:
id: Unique identifierfname: File nametype: MIME typeurl: Access URLfkey: Storage keysize: File size
Junction table linking records with attached files:
id: Unique identifierrecord_id: Associated recordfile_id: Associated filecreated_at: Creation timestamp
- POST /auth/register: Register a new user
- POST /auth/login: Authenticate and receive JWT tokens
- POST /auth/refresh: Refresh access token using refresh token
- POST /auth/logout: Invalidate current tokens
- GET /users: Get list of users (admin only)
- GET /users/:id: Get user details
- PATCH /users/:id: Update user profile
- DELETE /users/:id: Delete user account
- GET /moods: Get all available moods
- POST /moods: Create a new mood (admin only)
- PATCH /moods/:id: Update mood (admin only)
- DELETE /moods/:id: Delete mood (admin only)
- GET /activities: Get all available activities
- POST /activities: Create a new activity (admin only)
- PATCH /activities/:id: Update activity (admin only)
- DELETE /activities/:id: Delete activity (admin only)
- GET /records: Get user's records (supports filtering by date/mood/activity)
- POST /records: Create a new record
- GET /records/:id: Get record details
- PATCH /records/:id: Update a record
- DELETE /records/:id: Delete a record (soft delete)
- POST /files/upload: Upload a file
- GET /files/:id: Get file details
- DELETE /files/:id: Delete a file
The API uses JWT (JSON Web Tokens) for authentication with two token types:
- Access Token: Short-lived token (15 minutes) for API access
- Refresh Token: Long-lived token (7 days) to obtain new access tokens
All protected endpoints require a valid access token in the Authorization header:
Authorization: Bearer <access_token>
- Node.js (v14+)
- PostgreSQL
- npm or yarn
- Clone the repository:
git clone <repository_url>
- Install dependencies:
npm install
- Set up environment variables: Create a
.envfile in the root directory with the following variables:DATABASE_URL=<your_postgresql_connection_string> JWT_SECRET=<your_jwt_secret> # Add other necessary variables
- Generate Prisma client:
npx prisma generate
- Run database migrations:
npx prisma migrate dev
- Start the application:
npm run start
The API will be available at http://localhost:3000.
Swagger documentation is available at http://localhost:3000/api.
npm run build: Build the applicationnpm run format: Format code with Prettiernpm run start: Start the applicationnpm run dev: Start with hot-reload (development)npm run lint: Run ESLintnpm run test: Run testsnpm run test:e2e: Run end-to-end tests
For production deployment:
- Build the application:
npm run build
- Set
NODE_ENVtoproduction:export NODE_ENV=production - Start the server:
npm run start:prod
The API uses standard HTTP status codes:
- 200 OK: Successful request
- 201 Created: Resource created successfully
- 400 Bad Request: Invalid input
- 401 Unauthorized: Authentication required or failed
- 403 Forbidden: Permission denied
- 404 Not Found: Resource not found
- 500 Server Error: Internal server error
Error responses include an error message and details when applicable.
Input validation is performed using class-validator and class-transformer with DTO (Data Transfer Object) patterns.
- Original development by Hỏi Dân IT
UNLICENSED