🚚 create backup folder

create backup folder for archive the old modules
This commit is contained in:
Rafi Arrafif
2025-07-18 23:20:15 +07:00
parent 8eb68cf0ba
commit 8532d7e104
40 changed files with 671 additions and 671 deletions

View File

@ -0,0 +1,19 @@
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
import { checkUserSessionInCacheRepo } from "../repositories/checkUserSessionInCache.repository";
export const checkUserSessionInCacheService = async (
userId: string,
sessionId: string
) => {
try {
// Construct the Redis key name using the userId and sessionId
const redisKeyName = `${process.env.APP_NAME}:users:${userId}:sessions:${sessionId}`;
// 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");
}
};

View File

@ -0,0 +1,27 @@
import { createUserSessionServiceParams } from "../userSession.types";
import { createUserSessionRepo } from "../repositories/insertUserSessionToDB.repository";
import { storeUserSessionToCacheRepo } from "../repositories/storeUserSessionToCache.repository";
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
export const createUserSessionService = async (
data: createUserSessionServiceParams
) => {
const sessionLifetime = Number(process.env.SESSION_EXPIRE!);
try {
const newUserSession = await createUserSessionRepo({
userId: data.userId,
isAuthenticated: true,
deviceType: data.userHeaderInformation.deviceType,
deviceOs: data.userHeaderInformation.deviceOS,
deviceIp: data.userHeaderInformation.ip,
validUntil: new Date(new Date().getTime() + sessionLifetime * 1000),
});
const timeExpires = Number(process.env.SESSION_EXPIRE!);
await storeUserSessionToCacheRepo(newUserSession, timeExpires);
return newUserSession;
} catch (error) {
ErrorForwarder(error);
}
};

View File

@ -0,0 +1,30 @@
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
import { JWTAuthToken } from "../../../helpers/http/jwt/decode/types";
import { deleteUserSessionFromCacheRepo } from "../repositories/deleteUserSessionFromCache.repository";
import { deleteUserSessionFromDBRepo } from "../repositories/deleteUserSessionFromDB.repository";
export const deleteUserSessionInCacheAndDBService = async (
jwtToken: JWTAuthToken
) => {
try {
// Construct the userId and sessionId from the JWT token
const userId = jwtToken.userId;
const sessionId = jwtToken.id;
// Delete the user session from cache and database
await deleteUserSessionFromCacheRepo(userId, sessionId);
const deleteUserSessionFromDB = await deleteUserSessionFromDBRepo(
sessionId
);
return deleteUserSessionFromDB;
// If the session was not found in the cache or database, throw an error
} catch (error) {
ErrorForwarder(
error,
500,
"Delete user session service had encountered error"
);
}
};

View File

@ -0,0 +1,24 @@
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
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 ||
userSession.deletedAt ||
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
ErrorForwarder(error, 401, "Unable to get user session");
}
};

View File

@ -0,0 +1,17 @@
import { Prisma } from "@prisma/client";
import { storeUserSessionToCacheRepo } from "../repositories/storeUserSessionToCache.repository";
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
export const storeUserSessionToCacheService = async (
userSession: Prisma.UserSessionUncheckedCreateInput,
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
ErrorForwarder(error, 401, "Failed to store user session to cache");
}
};