# Lambda Functions for Grinder.js

## Overview
This project breaks down the original `grinder.js` into three AWS Lambda functions to improve modularity, scalability, and maintainability of the golf round tracking system.

## Lambda Functions

### 1. Grind Function (`grind/index.js`)
**Purpose:** 
- Periodically checks for new fixes in the database
- Manages the overall process of processing fixes and updating rounds
- Scheduled to run every 5 minutes

**Key Responsibilities:**
- Retrieve configuration settings
- Fetch new fixes from the database
- Sort and process fixes in batches
- Update last processed fix ID
- Trigger fix handling process

**Trigger:** 
- Scheduled Event (every 5 minutes)

### 2. Handle Fix Function (`handleFix/index.js`)
**Purpose:**
- Process individual fixes for golf rounds
- Determine section and round status
- Update round information based on device movement

**Key Responsibilities:**
- Validate device and fix data
- Determine course section
- Update or create rounds
- Handle round progression
- Manage round timing and pace
- Process special conditions like shotgun starts

**Trigger:**
- SQS Queue Event (triggered by new fixes)

### 3. Get Raw Fixes Function (`getRawFixes/index.js`)
**Purpose:**
- Retrieve raw fix data from the database
- Support flexible fix retrieval from primary or current database
- Provide an API endpoint for fetching fixes

**Key Responsibilities:**
- Fetch fixes based on last processed fix ID
- Support primary and current database retrieval
- Limit and sort fix results
- Populate device information

**Trigger:**
- API Gateway Event (HTTP POST to `/getRawFixes`)

## Environment Variables
- `TIMEZONE`: Timezone for time-based operations

## Deployment
Deployed using AWS SAM (Serverless Application Model) with the following resources:
- Lambda Functions
- SQS Queue
- API Gateway Endpoint

## Dependencies
- Node.js 18.x
- Mongoose
- Moment-timezone
- Async

## Configuration
Configure deployment parameters in `template.yaml`, including database URL and timezone.
