Skip to content

Commit a912001

Browse files
authored
Feat/read basic request tracker (#9)
* feat: jest config * feat: integrate Redis for logging middleware and data storage - Added Redis as a dependency in package.json. - Implemented Redis client configuration in redis.config.ts. - Created RedisService for handling data storage and retrieval. - Developed logger middleware to log requests and store logs in Redis. - Introduced redis routes for accessing stored data. - Updated app.ts to include logger middleware and Redis routes. - Modified AuthService to include debug logging for user registration. - Updated environment variables to include REDIS_URL. - Disabled SQL logging in data-source.ts for production readiness. * lint: fix lint erros in code * fix: redis env variable missing in ci script * feat: add user role to private the access to the redis routes * feat: update Redis service and tests, refactor auth service logging, and adjust test database imports * fix: fix redis service injection
1 parent 163b27e commit a912001

26 files changed

+813
-513
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ jobs:
7272
REFRESH_EXPIRE: 7d
7373
RESET_SECRET: secret3
7474
RESET_EXPIRE: 15m
75+
REDIS_URL: redis://localhost:6379
7576

7677
steps:
7778
- name: Checkout repository

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ dist
44
*.sqlite
55
*.todo
66
insomnia-collection-complete.json
7-
TODO.md
7+
TODO.md
8+
coverage

docker-compose.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@ services:
1313
POSTGRES_USER: postgres
1414
POSTGRES_PASSWORD: postgres
1515
volumes:
16-
- ./init-db.sql:/docker-entrypoint-initdb.d/init-db.sql
16+
- ./init-db.sql:/docker-entrypoint-initdb.d/init-db.sql
17+
redis:
18+
image: redis:latest
19+
container_name: payment_records_redis
20+
ports:
21+
- "6379:6379"
22+
healthcheck:
23+
test: ["CMD", "redis-cli", "ping"]
24+
interval: 10s
25+
timeout: 5s
26+
retries: 5

jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@ module.exports = {
1414
transform: {
1515
"^.+\\.(t|j)sx?$": ["ts-jest", { useESM: true }],
1616
},
17+
coverageThreshold: {
18+
global: {
19+
branches: 80,
20+
functions: 80,
21+
lines: 80,
22+
statements: 80
23+
}
24+
}
1725
};

package-lock.json

Lines changed: 608 additions & 499 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"nodemailer": "^7.0.6",
3232
"pg": "^8.16.3",
3333
"pg-query-stream": "^4.10.3",
34+
"redis": "^5.10.0",
3435
"reflect-metadata": "^0.2.2",
3536
"swagger-jsdoc": "^6.2.8",
3637
"swagger-ui-express": "^5.0.1",
@@ -58,4 +59,4 @@
5859
"overrides": {
5960
"validator": "13.12.0"
6061
}
61-
}
62+
}

src/app.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import { UserRouter } from "@modules/User/user.routes";
77
import { BankRouter } from "@modules/Bank/bank.routes";
88
import { AccountRouter } from "@modules/Account/account.routes";
99
import { TransactionRouter } from "@modules/Transaction/transaction.routes";
10+
import { redisRouter } from "@modules/redis/redis.routes";
11+
import { loggerMiddleware } from "@middlewares/loggger.middleware";
1012
import cors from "cors";
1113
import { runSeeds } from "@shared/seeds";
1214
import { env } from "@shared/env";
1315
import swaggerUi from "swagger-ui-express"
1416
import docs from "docs/swagger";
1517
import * as dotenv from "dotenv";
18+
import { redisClient } from "@modules/redis/redis.config";
1619

1720
dotenv.config();
1821

@@ -31,12 +34,14 @@ app.use(
3134

3235
app.use(express.json());
3336
app.use(express.urlencoded({ extended: true }));
37+
app.use(loggerMiddleware);
3438
app.use("/auth", AuthRouter);
3539
app.use("/user", UserRouter);
3640
app.use("/bank", BankRouter);
3741
app.use("/account", AccountRouter);
3842
app.use("/transaction", TransactionRouter);
3943
app.use("/api", swaggerUi.serve, swaggerUi.setup(docs));
44+
app.use("/redis", redisRouter);
4045
app.use(ErrorHandler.handle.bind(ErrorHandler));
4146

4247
app.get("/health", (_req, res) => {
@@ -46,9 +51,11 @@ app.get("/health", (_req, res) => {
4651

4752
AppDataSource.initialize()
4853
.then(() => {
49-
app.listen(port, async () => {
50-
await runSeeds();
51-
});
54+
redisClient.connect().then(() => {
55+
app.listen(port, async () => {
56+
await runSeeds();
57+
});
58+
})
5259
})
5360
.catch((err) => {
5461
console.error("Error during Data Source initialization", err);

src/core/interfaces/logger.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface Logger{
2+
store(key: string, value: object): void;
3+
get(key: string): object | null;
4+
}

src/lib/enums.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,8 @@ export const ErrorEnum = {
5353
ACCOUNT_BLOCKED: { message: "Account Blocked", status: 403 },
5454
MISSING_PROPERTIES: { message: "Missing Required Properties", status: 400 },
5555
} as const;
56+
57+
export enum Role{
58+
USER,
59+
ADMIN
60+
}

src/lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import nodemailer from "nodemailer"
2+
import { Role } from "./enums";
23

34
/**
45
* @swagger
@@ -34,6 +35,7 @@ export type MailOptions = nodemailer.SendMailOptions;
3435
export type AccessPayload = {
3536
email: string
3637
id: string
38+
role: Role
3739
}
3840

3941
/**

0 commit comments

Comments
 (0)