Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "mind-check-app"
}
}
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dist
dist-ssr
*.local
package-lock.json
!functions/package-lock.json

# Editor directories and files
.vscode/*
Expand All @@ -23,3 +24,9 @@ package-lock.json
*.njsproj
*.sln
*.sw?

# Firebase
firebase-debug.log
firebase-debug.*.log
.firebase/
functions/lib/
37 changes: 37 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,43 @@ VITE_FIREBASE_PROJECT_ID=YOUR_FIREBASE_PROJECT_ID
# etc - refer .env.sample for more information
```

### Firebase Cloud Functions

Mind Check uses Firebase Cloud Functions for server-side operations like scheduled database maintenance tasks.

#### Setup

1. Install Firebase CLI globally:
```bash
pnpm install -g firebase-tools
```

2. Login to Firebase:
```bash
firebase login
```

3. Install function dependencies:
```bash
cd functions
npm install
```

#### Development

- Build functions: `npm run build`
- Lint code: `npm run lint`
- Local testing: `npm run serve`

#### Deployment

Deploy all functions from the project root:
```bash
firebase deploy --only functions
```

For detailed documentation on Cloud Functions, see the [functions/README.md](functions/README.md).

### Reporting Issues

If you encounter any issues or have suggestions for improvement, please open an issue on the issue tracker: [https://github.com/kunalkeshan/Mind-Check.git/issues](https://github.com/kunalkeshan/Mind-Check.git/issues)
Expand Down
10 changes: 10 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"functions": {
"source": "functions",
"runtime": "nodejs20",
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
}
24 changes: 24 additions & 0 deletions functions/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: ["tsconfig.json"],
sourceType: "module",
},
ignorePatterns: [
"/lib/**/*",
".eslintrc.js",
],
plugins: [
"@typescript-eslint",
],
rules: {},
};
13 changes: 13 additions & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Compiled JavaScript files
lib/

# Node modules
node_modules/

# Local environment files
.env
.env.local

# Firebase debug logs
firebase-debug.log
firebase-debug.*.log
184 changes: 184 additions & 0 deletions functions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
# Firebase Cloud Functions for Mind Check

This directory contains Firebase Cloud Functions for the Mind Check application. These functions run server-side operations for database maintenance and scheduled tasks.

## Overview

The Cloud Functions in this project handle automated maintenance tasks that run on a schedule to keep the database clean and optimized.

### Available Functions

#### `deleteOldExportStatusDocuments`

A scheduled function that automatically removes old export status documents from Firestore.

- **Schedule**: Runs every 24 hours
- **Purpose**: Cleans up export status tracking documents older than 2 days
- **Database Path**: `users/{userId}/exports/{date}`
- **Document ID Format**: `day-mon-dd-yyyy` (e.g., `wed-dec-25-2024`, lowercase)

**Configuration Constants:**
- `EXPORT_STATUS_RETENTION_DAYS`: Number of days to retain export status documents (default: 2)
- `MAX_CONCURRENT_DELETES`: Maximum concurrent delete operations for performance optimization (default: 10)

## Prerequisites

- Node.js (>= 18.x)
- Firebase CLI installed globally
- Access to the Mind Check Firebase project

## Setup

### 1. Install Firebase CLI

```bash
pnpm install -g firebase-tools
```

### 2. Login to Firebase

```bash
firebase login
```

### 3. Install Dependencies

Navigate to the functions directory and install dependencies:

```bash
cd functions
npm install
```

> **Note**: The `functions` directory uses `npm` for dependency management as it's a separate Node.js project from the main frontend application which uses `pnpm`.

## Development

### Build

Compile TypeScript to JavaScript:

```bash
npm run build
```

### Watch Mode

Compile TypeScript on file changes:

```bash
npm run build:watch
```

### Lint

Run ESLint to check for code issues:

```bash
npm run lint
```

### Local Testing

Start the Firebase emulator for local testing:

```bash
npm run serve
```

Or use the Firebase shell:

```bash
npm run shell
```

## Deployment

### Deploy All Functions

From the project root directory:

```bash
firebase deploy --only functions
```

### Deploy Specific Function

```bash
firebase deploy --only functions:deleteOldExportStatusDocuments
```

### View Logs

```bash
npm run logs
# or
firebase functions:log
```

## Project Structure

```
functions/
├── src/
│ └── index.ts # Main Cloud Functions entry point
├── lib/ # Compiled JavaScript (generated)
├── .eslintrc.js # ESLint configuration
├── .gitignore # Git ignore rules
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
└── README.md # This file
```

## Adding New Functions

1. Add your function to `src/index.ts`
2. Export the function using the appropriate Firebase trigger:
- `functions.pubsub.schedule()` for scheduled tasks
- `functions.firestore.document()` for Firestore triggers
- `functions.https.onRequest()` for HTTP endpoints
3. Build and test locally before deploying
4. Deploy using `firebase deploy --only functions`

## Monitoring

After deployment, you can monitor your functions in the Firebase Console:
- **Dashboard**: View invocation counts and execution times
- **Logs**: Check function execution logs and errors
- **Health**: Monitor function performance and errors

Visit: https://console.firebase.google.com/project/{your-project-id}/functions

> For the Mind Check project, replace `{your-project-id}` with `mind-check-app`.

## Troubleshooting

### Common Issues

1. **Function not deploying**: Ensure you're logged in with `firebase login` and have the correct project selected
2. **Permission errors**: Verify your Firebase account has the necessary permissions
3. **Build errors**: Run `npm run build` to check for TypeScript compilation issues
4. **Runtime errors**: Check logs with `firebase functions:log`

### Useful Commands

```bash
# Check current Firebase project
firebase projects:list

# Switch Firebase project
firebase use mind-check-app

# View function logs (last 50 entries)
firebase functions:log --limit 50

# View logs for specific function
firebase functions:log --only deleteOldExportStatusDocuments
```

## References

- [Firebase Cloud Functions Documentation](https://firebase.google.com/docs/functions)
- [Schedule Functions](https://firebase.google.com/docs/functions/schedule-functions)
- [Firebase Admin SDK](https://firebase.google.com/docs/admin/setup)
- [Functions Samples Repository](https://github.com/firebase/functions-samples)
Loading