move all db and cache logic from service to repositories

This commit is contained in:
rafiarrafif
2025-05-25 21:37:55 +07:00
parent 03fd0531af
commit ee8e8d08db
6 changed files with 76 additions and 11 deletions

View File

@ -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");
}
};

View File

@ -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;
};

View File

@ -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");
}
};

View 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);
}
};