move all db and cache logic from service to repositories
This commit is contained in:
@ -1,8 +1,7 @@
|
|||||||
import { AppError } from "../../../helpers/error/instances/app";
|
import { AppError } from "../../../helpers/error/instances/app";
|
||||||
import { jwtDecode } from "../../../helpers/http/jwt/decode";
|
import { jwtDecode } from "../../../helpers/http/jwt/decode";
|
||||||
import { prisma } from "../../../utils/databases/prisma/connection";
|
import { checkUserSessionInCacheService } from "../../userSession/services/checkUserSessionInCache.service";
|
||||||
import { redis } from "../../../utils/databases/redis/connection";
|
import { getUserSessionService } from "../../userSession/services/getUserSession.service";
|
||||||
import { storeUserSessionToCacheRepo } from "../../userSession/repositories/storeUserSessionToCache.repository";
|
|
||||||
import { storeUserSessionToCacheService } from "../../userSession/services/storeUserSessionToCache.service";
|
import { storeUserSessionToCacheService } from "../../userSession/services/storeUserSessionToCache.service";
|
||||||
import { JWTSessionPayload } from "../auth.types";
|
import { JWTSessionPayload } from "../auth.types";
|
||||||
|
|
||||||
@ -12,14 +11,14 @@ export const authVerificationService = async (cookie: string) => {
|
|||||||
const jwtSession = jwtDecode(cookie) as JWTSessionPayload;
|
const jwtSession = jwtDecode(cookie) as JWTSessionPayload;
|
||||||
|
|
||||||
// Check if the session exists in Redis
|
// Check if the session exists in Redis
|
||||||
const sessionCheckOnRedis = await redis.exists(jwtSession.id);
|
const sessionCheckOnRedis = await checkUserSessionInCacheService(
|
||||||
|
jwtSession.userId,
|
||||||
|
jwtSession.id
|
||||||
|
);
|
||||||
|
|
||||||
if (!sessionCheckOnRedis) {
|
if (!sessionCheckOnRedis) {
|
||||||
// If not found in Redis, check the database
|
// If not found in Redis, check the database
|
||||||
const sessionCheckOnDB = await prisma.userSession.findUnique({
|
const sessionCheckOnDB = await getUserSessionService(jwtSession.id);
|
||||||
where: {
|
|
||||||
id: jwtSession.id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// If the session found in the database, store it in Redis. if not, throw an error
|
// If the session found in the database, store it in Redis. if not, throw an error
|
||||||
if (
|
if (
|
||||||
@ -35,8 +34,8 @@ export const authVerificationService = async (cookie: string) => {
|
|||||||
new Date().getTime()) /
|
new Date().getTime()) /
|
||||||
1000
|
1000
|
||||||
);
|
);
|
||||||
await storeUserSessionToCacheService(sessionCheckOnDB!, timeExpires);
|
await storeUserSessionToCacheService(sessionCheckOnDB, timeExpires);
|
||||||
return sessionCheckOnDB;
|
return "daridb";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return jwtSession;
|
return jwtSession;
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import { mainErrorHandler } from "../../helpers/error/handler";
|
|||||||
import { debugService } from "./debug.service";
|
import { debugService } from "./debug.service";
|
||||||
|
|
||||||
export const debugController = (ctx: Context) => {
|
export const debugController = (ctx: Context) => {
|
||||||
|
ctx.set.status = 418;
|
||||||
return Math.floor(
|
return Math.floor(
|
||||||
(new Date("2025-07-13 16:22:12.335").getTime() - new Date().getTime()) /
|
(new Date("2025-07-13 16:22:12.335").getTime() - new Date().getTime()) /
|
||||||
1000
|
1000
|
||||||
|
|||||||
@ -0,0 +1,13 @@
|
|||||||
|
import { AppError } from "../../../helpers/error/instances/app";
|
||||||
|
import { redis } from "../../../utils/databases/redis/connection";
|
||||||
|
|
||||||
|
export const checkUserSessionInCacheRepo = async (redisKeyName: string) => {
|
||||||
|
try {
|
||||||
|
const userSessionInRedis = await redis.exists(redisKeyName);
|
||||||
|
if (!userSessionInRedis) return false;
|
||||||
|
|
||||||
|
return userSessionInRedis;
|
||||||
|
} catch (error) {
|
||||||
|
throw new AppError(500, "Server cache error");
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -0,0 +1,26 @@
|
|||||||
|
import { prisma } from "../../../utils/databases/prisma/connection";
|
||||||
|
|
||||||
|
export const findUniqueUserSessionInDBRepo = async (identifier: string) => {
|
||||||
|
const userSession = await prisma.userSession.findUnique({
|
||||||
|
where: {
|
||||||
|
id: identifier,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
user: {
|
||||||
|
omit: {
|
||||||
|
password: true,
|
||||||
|
updatedAt: true,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
roles: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
omit: {
|
||||||
|
deletedAt: true,
|
||||||
|
updatedAt: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return userSession;
|
||||||
|
};
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
import { AppError } from "../../../helpers/error/instances/app";
|
||||||
|
import { checkUserSessionInCacheRepo } from "../repositories/checkUserSessionInCache.repository";
|
||||||
|
|
||||||
|
export const checkUserSessionInCacheService = async (
|
||||||
|
userId: string,
|
||||||
|
sessionId: string
|
||||||
|
) => {
|
||||||
|
const redisKeyName = `${process.env.app_name}:users:${userId}:sessions:${sessionId}`;
|
||||||
|
try {
|
||||||
|
const userSessionInRedis = await checkUserSessionInCacheRepo(redisKeyName);
|
||||||
|
return userSessionInRedis;
|
||||||
|
} catch {
|
||||||
|
throw new AppError(502, "Server cache error");
|
||||||
|
}
|
||||||
|
};
|
||||||
11
src/modules/userSession/services/getUserSession.service.ts
Normal file
11
src/modules/userSession/services/getUserSession.service.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { AppError } from "../../../helpers/error/instances/app";
|
||||||
|
import { findUniqueUserSessionInDBRepo } from "../repositories/findUniqueUserSessionInDB.repository";
|
||||||
|
|
||||||
|
export const getUserSessionService = async (identifier: string) => {
|
||||||
|
try {
|
||||||
|
const userSession = await findUniqueUserSessionInDBRepo(identifier);
|
||||||
|
return userSession;
|
||||||
|
} catch (error) {
|
||||||
|
throw new AppError(401, "Unable to get user session", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user