add default value in forwarder error
This commit is contained in:
@ -1,26 +1,33 @@
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
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,
|
||||
try {
|
||||
const userSession = await prisma.userSession.findUnique({
|
||||
where: {
|
||||
id: identifier,
|
||||
},
|
||||
include: {
|
||||
user: {
|
||||
omit: {
|
||||
password: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
include: {
|
||||
roles: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
omit: {
|
||||
deletedAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
omit: {
|
||||
deletedAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
return userSession;
|
||||
if (!userSession) return false;
|
||||
|
||||
return userSession;
|
||||
} catch (error) {
|
||||
throw new AppError(500, "Database Error", error);
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,12 +1,19 @@
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
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 {
|
||||
// Construct the Redis key name using the userId and sessionId
|
||||
const redisKeyName = `${process.env.app_name}:users:${userId}:sessions:${sessionId}`;
|
||||
|
||||
const userSessionInRedis = await checkUserSessionInCacheRepo(redisKeyName);
|
||||
return userSessionInRedis;
|
||||
// Check if the user session exists in Redis
|
||||
const userSessionInRedis = await checkUserSessionInCacheRepo(redisKeyName);
|
||||
return userSessionInRedis;
|
||||
} catch (error) {
|
||||
// Forward the error with a 400 status code and a message
|
||||
ErrorForwarder(error, 400, "Bad Request");
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,11 +0,0 @@
|
||||
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);
|
||||
}
|
||||
};
|
||||
@ -0,0 +1,23 @@
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { findUniqueUserSessionInDBRepo } from "../repositories/findUniqueUserSessionInDB.repository";
|
||||
|
||||
export const getUserSessionFromDBService = async (identifier: string) => {
|
||||
try {
|
||||
// Check is session exists in DB
|
||||
const userSession = await findUniqueUserSessionInDBRepo(identifier);
|
||||
|
||||
// If session not found, return false
|
||||
if (
|
||||
!userSession ||
|
||||
!userSession.isAuthenticated ||
|
||||
new Date(userSession.validUntil) < new Date()
|
||||
)
|
||||
return false;
|
||||
|
||||
// If session found, return it
|
||||
return userSession;
|
||||
} catch (error) {
|
||||
// If any DB error occurs, throw an AppError
|
||||
throw new AppError(401, "Unable to get user session", error);
|
||||
}
|
||||
};
|
||||
@ -7,9 +7,11 @@ export const storeUserSessionToCacheService = async (
|
||||
timeExpires: number
|
||||
) => {
|
||||
try {
|
||||
// Store user session in cache with expiration time
|
||||
await storeUserSessionToCacheRepo(userSession, timeExpires);
|
||||
return;
|
||||
} catch (error) {
|
||||
// If any error occurs while storing session in cache, throw an AppError
|
||||
throw new AppError(401, "Failed to store user session to cache");
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user