Skip to content

Commit 9be5276

Browse files
feat: 2.0.1
feat: 2.0.1
2 parents b3442aa + 1fe9f1a commit 9be5276

File tree

85 files changed

+3390
-4635
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+3390
-4635
lines changed

.dockerignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
dist
2-
docker-compose.yml.example
32
node_modules
4-
storage.sql
5-
storage.sql.example
6-
README.md
73
.env
84
.env.example
95
.gitignore
6+
docker-compose.yml.example
7+
README.md
8+
storage.sql
9+
storage.sql.example

.env.example

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
1-
NODE_ENV=""
1+
NODE_ENV=development
22

3-
PORT=""
3+
PORT=3892
44

5-
RATE_LIMIT_WINDOW_MS=""
6-
RATE_LIMIT_MAX_REQUESTS=""
5+
APPLICATION_TYPE=fence-tracker
76

8-
STORAGE_DATABASE_URL=""
7+
JWT_SECRET=jwt_secret
8+
JWT_EXPIRES_IN=1h
99

10-
APPLICATION_TYPE=""
10+
LOG_LEVEL=debug
1111

12-
JWT_SECRET=""
13-
JWT_EXPIRES_IN=""
12+
ERROR_LOG_MAX_SIZE=20m
13+
ERROR_LOG_MAX_FILES=30d
14+
15+
WARN_LOG_MAX_SIZE=15m
16+
WARN_LOG_MAX_FILES=14d
17+
18+
INFO_LOG_MAX_SIZE=10m
19+
INFO_LOG_MAX_FILES=7d
20+
21+
DEBUG_LOG_MAX_SIZE=5m
22+
DEBUG_LOG_MAX_FILES=3d
23+
24+
EXCEPTION_LOG_MAX_SIZE=20m
25+
EXCEPTION_LOG_MAX_FILES=30d
26+
27+
REJECTION_LOG_MAX_SIZE=20m
28+
REJECTION_LOG_MAX_FILES=30d
29+
30+
RATE_LIMIT_WINDOW_MS=60000
31+
RATE_LIMIT_MAX_REQUESTS=100
32+
33+
STORAGE_DATABASE_URI=mysql://root:root@127.0.0.1:3306/storage
34+
35+
QUERY_GATEWAY_USERNAME=query_gateway_username
36+
QUERY_GATEWAY_PASSWORD=query_gateway_password
1437

1538
CHAT_PRO_BEARER_TOKEN=""
1639
CHAT_PRO_INSTANCE_ID=""
1740
CHAT_PRO_NUMBER=""
18-
19-
QUERY_GATEWAY_USERNAME=""
20-
QUERY_GATEWAY_PASSWORD=""

.gitignore

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
cert.pem
21
dist
3-
docker-compose.yml
2+
logs/.debug-audit.json*
3+
logs/.error-audit.json*
4+
logs/.exception-audit.json*
5+
logs/.info-audit.json*
6+
logs/.rejection-audit.json*
7+
logs/.warn-audit.json*
8+
logs/*.log*
49
node_modules
5-
key.pem
6-
logs
7-
storage.sql
10+
ssl/*.ext*
11+
ssl/*.cnf*
12+
ssl/*.crt*
13+
ssl/*.csr*
14+
ssl/*.key*
15+
ssl/*.pem*
16+
ssl/*.srl*
817
.env
9-
.log
10-
.pem
18+
docker-compose.yml
19+
package-lock.json
20+
storage.sql

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ WORKDIR /app
77

88
RUN chown -R nodeuser:nodejs /app
99

10-
COPY --chown=nodeuser:nodejs package*.json ./
1110
COPY --chown=nodeuser:nodejs prisma/ ./prisma/
1211
COPY --chown=nodeuser:nodejs scripts/ ./scripts/
12+
COPY --chown=nodeuser:nodejs package*.json ./
1313

1414
USER nodeuser
1515

README.md

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,110 @@
1-
# fence-alert
1+
# 🚧 Fence Tracker
2+
3+
## 📋 Overview
4+
5+
Fence Tracker is a zone-based monitoring and alerting service that identifies when activity levels within specific geographic or logical zones exceed a configured threshold. It fetches data from a centralized Query Gateway, evaluates it against defined rules, and sends alerts when meaningful increases occur.
6+
7+
To prevent redundant notifications, Fence Tracker persists the last triggered state for each zone in a MySQL database. When a new threshold multiple is crossed, it formats and dispatches a structured alert message via an external messaging system.
8+
9+
### 🎯 Objectives
10+
11+
- Fetch the latest fence activity data using a named query from the Query Gateway
12+
- Detect when the quantity per zone exceeds a defined threshold (e.g., 50)
13+
- Persist trigger states in a MySQL database using Prisma to track previously alerted values
14+
- Prevent duplicate alerts by only triggering when thresholds escalate beyond prior values
15+
- Generate structured alert messages containing relevant zone and account information
16+
- Send alerts to a configurable external messaging endpoint
17+
- Use Basic and Bearer authentication strategies for secure communication with the Query Gateway
18+
- Provide a scalable and maintainable alert pipeline for fence-based activity monitoring
19+
20+
---
21+
22+
## 📦 Quick Start
23+
24+
### ⚠️ Prerequisites
25+
26+
- [**Node.js**](https://nodejs.org/)`20.14.0`_JavaScript runtime environment_
27+
- [**MySQL**](https://www.mysql.com/)`8.0`_Relational database_
28+
- [**Query Gateway**](https://github.com/gabrielmendezsoares/query-gateway)`3.0.2`_Configurable data query service_
29+
30+
### ⚙️ Setup
31+
32+
```bash
33+
# Clone & navigate
34+
git clone <repository-url> && cd fence-tracker
35+
36+
# Configure environment
37+
cp .env.example .env # Edit with your settings
38+
39+
# Install dependencies (auto-runs database setup)
40+
npm install
41+
```
42+
43+
> **💡 Database:** Import `storage.sql.example` before running `npm install`
44+
45+
---
46+
47+
## ⚡ Usage
48+
49+
### 🛠️ Development
50+
51+
```bash
52+
npm run start:development
53+
```
54+
55+
### 🏗️ Production
56+
57+
```bash
58+
npm run build && npm run start:production
59+
```
60+
61+
---
62+
63+
## 📚 Command Reference
64+
65+
### 🧰 Core
66+
67+
| Command | Description |
68+
| ------- | ----------- |
69+
| `npm run start:development` | _Start the application in development_ |
70+
| `npm run start:production` | _Start the application in production_ |
71+
| `npm run build` | _Build the application for production_ |
72+
| `npm run build:watch` | _Build the application with watch mode_ |
73+
| `npm run clean` | _Clean application build artifacts_ |
74+
75+
### 🛢️ Database
76+
77+
| Command | Description |
78+
| ------- | ----------- |
79+
| `npm run db:pull` | _Pull database schema into Prisma across all schemas_ |
80+
| `npm run db:push` | _Push Prisma schema to the database across all schemas_ |
81+
| `npm run db:generate` | _Generate Prisma Client for all schemas_ |
82+
| `npm run db:migrate:dev` | _Run development migrations across all schemas_ |
83+
| `npm run db:migrate:deploy` | _Deploy migrations to production across all schemas_ |
84+
| `npm run db:studio` | _Open Prisma Studio (GUI) across all schemas_ |
85+
| `npm run db:reset` | _Reset database (pull + generate) for all schemas_ |
86+
87+
### 🐳 Docker
88+
89+
| Command | Description |
90+
| ------- | ----------- |
91+
| `npm run docker:build:development` | _Build Docker image for development_ |
92+
| `npm run docker:build:production` | _Build Docker image for production_ |
93+
| `npm run docker:run:development` | _Run development Docker container_ |
94+
| `npm run docker:run:production` | _Run production Docker container_ |
95+
| `npm run docker:compose:up:development` | _Start Docker Compose in development_ |
96+
| `npm run docker:compose:up:production` | _Start Docker Compose in production_ |
97+
| `npm run docker:compose:up:build:development` | _Start & rebuild Docker Compose in development_ |
98+
| `npm run docker:compose:up:build:production` | _Start & rebuild Docker Compose in production_ |
99+
| `npm run docker:compose:down` | _Stop Docker Compose services_ |
100+
| `npm run docker:compose:logs` | _View Docker Compose logs_ |
101+
| `npm run docker:prune` | _Clean up unused Docker resources_ |
102+
103+
### 🧪 Testing
104+
105+
| Command | Description |
106+
| ------- | ----------- |
107+
| `npm test` | _Run all tests once_ |
108+
| `npm run test:watch` | _Run tests in watch mode_ |
109+
| `npm run test:coverage` | _Run tests and generate a coverage report_ |
110+

docker-compose.yml.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ x-common-variables: &common-variables
88
services:
99
main-development:
1010
<<: *common-variables
11-
container_name: main-development-fence-alert
11+
container_name: main-development-fence-tracker
1212
build:
1313
context: .
1414
dockerfile: Dockerfile
@@ -24,7 +24,7 @@ services:
2424

2525
main-production:
2626
<<: *common-variables
27-
container_name: main-production-fence-alert
27+
container_name: main-production-fence-tracker
2828
build:
2929
context: .
3030
dockerfile: Dockerfile
@@ -37,7 +37,7 @@ services:
3737

3838
db:
3939
<<: *common-variables
40-
container_name: db-mysql-fence-alert
40+
container_name: db-mysql-fence-tracker
4141
image: mysql:8.0
4242
ports:
4343
- "4406:3306"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Request, Response } from 'express';
2+
import { getAuthenticationService } from '../services/index.js';
3+
import { loggerUtil } from '../utils/index.js';
4+
5+
/**
6+
* ## getAuthentication
7+
*
8+
* Controller for handling authentication requests.
9+
*
10+
* @description
11+
* Delegates the authentication logic to the service layer, processes the response,
12+
* and returns a JSON response with the appropriate HTTP status code.
13+
*
14+
* In case of an unhandled error, logs the error and returns a 500 Internal Server Error
15+
* with a generic error message and suggestion.
16+
*
17+
* @param req - Express request object.
18+
* @param res - Express response object.
19+
*
20+
* @returns
21+
* Sends a JSON response with status code and authentication result.
22+
*/
23+
export const getAuthentication = async (
24+
req: Request,
25+
res: Response
26+
): Promise<void> => {
27+
try {
28+
const { status, data } = await getAuthenticationService.getAuthentication(req);
29+
30+
res.status(status).json(data);
31+
} catch (error: unknown) {
32+
loggerUtil.error(error instanceof Error ? error.message : String(error));
33+
34+
res
35+
.status(500)
36+
.json(
37+
{
38+
message: 'The authentication process encountered a technical issue.',
39+
suggestion: 'Please try again later or contact support if the issue persists.'
40+
}
41+
);
42+
}
43+
};

expressium/controllers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * as getAuthenticationController from './getAuthentication.controller.js';

0 commit comments

Comments
 (0)