How towhidcse4/testsafezone-api works
Project Type and Purpose
• This is a Serverless backend application written in Node.js and TypeScript, using the Serverless Framework to deploy a set of AWS Lambda functions behind API Gateway.
• It serves as a domain-driven REST API for user management, subscriptions, notifications, safe zones, and other family-safety features, persisting data in MongoDB Atlas and integrating with AWS services (Cognito, S3, SQS, VPC) plus Firebase Cloud Messaging.
Key Structural Observations
• Top-level “serverless.yml” plus per-handler YAML files define each Lambda function, triggers (HTTP, scheduled), and AWS resources (API Gateway authorizer, Cognito User Pool, S3 bucket, SQS queue, VPC).
• src/handlers/... is organized by domain: auth, cron_jobs, notification_mobile, orders, safezone, subscribe, subscriptions, users, zvelo.
• Layered code:
· actions/: orchestrate high-level operations
· handlers/: function entry points
· models/: database schema/ORM definitions
· modules/: AWS and core utilities (Cognito, S3, SQS, Mongoose bootstrap, request/response)
· service/firebase/: FCM integration
· utils/: JWT, auth helpers, string utilities
• Infrastructure config under config/ defines CloudFormation snippets (VPC, S3, SQS, Cognito, API Gateway Authorizer).
• Data directory holds seed data for subscription plans, user groups, web categories.
a. Main Components
• Clients: Mobile/web frontends consuming the API
• API Gateway:
• Public HTTP endpoints per domain.
• Authorizer integrated with Cognito User Pool.
• AWS Lambdas: grouped by domain (auth, users, subscriptions, cron jobs, notifications, etc.)
• MongoDB Atlas: primary document database, accessed via Mongoose in the VPC
• Cognito User Pool: user authentication & JWT issuance
• S3 Bucket: file storage (configured via config/s3-bucket.yml)
• SQS Queue: message queue for asynchronous tasks
• Firebase Cloud Messaging: push notifications from Lambda → FCM
• CloudWatch Events / Scheduled Cron: triggers for cron_jobs Lambdas
• VPC: secures Lambdas and network access to MongoDB Atlas
• CloudWatch Logs: all Lambdas stream logs here
b. Relationships & Data Flow
1. Client request → API Gateway → Lambda (auth & domain handlers)
2. Authorized Lambda uses modules/cognito to validate token or call Cognito (login/register).
3. Business Lambdas invoke modules/core/mongoose to read/write MongoDB Atlas.
4. Some flows enqueue messages to SQS (modules/sqs) or read from it.
5. Notification-related Lambdas send messages to FCM via service/firebase.
6. Scheduled CloudWatch Events trigger cron_jobs Lambdas on a schedule.
7. Lambdas that need file storage call modules/s3 to read/write S3.
c. Architectural Patterns & Principles
• Domain-Driven Design: each domain’s handlers/models/actions form a bounded context.
• Micro-function / Serverless: each handler is a single-purpose Lambda.
• Layered Modularization: shared code in modules/core, modules/s3, modules/sqs, service, utils.
• Event-Driven: scheduled jobs and SQS for asynchronous processing.
• Infrastructure as Code: Serverless Framework YAML + config snippets.
d. Key Technologies & Frameworks
• Node.js, TypeScript, Serverless Framework
• AWS Lambda, API Gateway, Cognito, S3, SQS, VPC
• MongoDB Atlas with Mongoose
• Firebase Cloud Messaging SDK