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 { jwtDecode } from "../../../helpers/http/jwt/decode";
|
||||
import { prisma } from "../../../utils/databases/prisma/connection";
|
||||
import { redis } from "../../../utils/databases/redis/connection";
|
||||
import { storeUserSessionToCacheRepo } from "../../userSession/repositories/storeUserSessionToCache.repository";
|
||||
import { checkUserSessionInCacheService } from "../../userSession/services/checkUserSessionInCache.service";
|
||||
import { getUserSessionService } from "../../userSession/services/getUserSession.service";
|
||||
import { storeUserSessionToCacheService } from "../../userSession/services/storeUserSessionToCache.service";
|
||||
import { JWTSessionPayload } from "../auth.types";
|
||||
|
||||
@ -12,14 +11,14 @@ export const authVerificationService = async (cookie: string) => {
|
||||
const jwtSession = jwtDecode(cookie) as JWTSessionPayload;
|
||||
|
||||
// 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 not found in Redis, check the database
|
||||
const sessionCheckOnDB = await prisma.userSession.findUnique({
|
||||
where: {
|
||||
id: jwtSession.id,
|
||||
},
|
||||
});
|
||||
const sessionCheckOnDB = await getUserSessionService(jwtSession.id);
|
||||
|
||||
// If the session found in the database, store it in Redis. if not, throw an error
|
||||
if (
|
||||
@ -35,8 +34,8 @@ export const authVerificationService = async (cookie: string) => {
|
||||
new Date().getTime()) /
|
||||
1000
|
||||
);
|
||||
await storeUserSessionToCacheService(sessionCheckOnDB!, timeExpires);
|
||||
return sessionCheckOnDB;
|
||||
await storeUserSessionToCacheService(sessionCheckOnDB, timeExpires);
|
||||
return "daridb";
|
||||
}
|
||||
} else {
|
||||
return jwtSession;
|
||||
|
||||
@ -3,6 +3,7 @@ import { mainErrorHandler } from "../../helpers/error/handler";
|
||||
import { debugService } from "./debug.service";
|
||||
|
||||
export const debugController = (ctx: Context) => {
|
||||
ctx.set.status = 418;
|
||||
return Math.floor(
|
||||
(new Date("2025-07-13 16:22:12.335").getTime() - new Date().getTime()) /
|
||||
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