move all db and cache logic from service to repositories
This commit is contained in:
@ -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