add userSession module
This commit is contained in:
@ -0,0 +1,30 @@
|
||||
import { Context } from "elysia";
|
||||
import { createUserSessionService } from "../services/createUserSession.service";
|
||||
import { getUserHeaderInformation } from "../../../helpers/cookies/userHeader/getUserHeaderInformation";
|
||||
import { handlePrismaError } from "../../../utils/databases/prisma/error/handler";
|
||||
import {
|
||||
returnErrorResponse,
|
||||
returnWriteResponse,
|
||||
} from "../../../helpers/callback/httpResponse";
|
||||
|
||||
export const createUserSessionRole = async (
|
||||
ctx: Context & { body: { userId: string } }
|
||||
) => {
|
||||
const userHeaderData = getUserHeaderInformation(ctx);
|
||||
|
||||
try {
|
||||
const newUserSession = await createUserSessionService({
|
||||
userId: ctx.body.userId,
|
||||
userHeaderInformation: userHeaderData,
|
||||
});
|
||||
return returnWriteResponse(
|
||||
ctx.set,
|
||||
201,
|
||||
"User session created",
|
||||
newUserSession
|
||||
);
|
||||
} catch (error) {
|
||||
const { status, message, details } = handlePrismaError(error);
|
||||
return returnErrorResponse(ctx.set, status, message, details);
|
||||
}
|
||||
};
|
||||
7
src/modules/userSession/index.ts
Normal file
7
src/modules/userSession/index.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import Elysia from "elysia";
|
||||
import { createUserSessionRole } from "./controller/createUserSession.controller";
|
||||
|
||||
export const userSessionModule = new Elysia({ prefix: "/user-sessions" }).post(
|
||||
"/",
|
||||
createUserSessionRole
|
||||
);
|
||||
@ -0,0 +1,20 @@
|
||||
import { createUserSessionServiceParams } from "../userSession.types";
|
||||
import { createUserSessionRepo } from "../userSession.repository";
|
||||
|
||||
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),
|
||||
});
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
3
src/modules/userSession/userSession.model.ts
Normal file
3
src/modules/userSession/userSession.model.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { prisma } from "../../utils/databases/prisma/connection";
|
||||
|
||||
export const userSessionModel = prisma.userSession;
|
||||
25
src/modules/userSession/userSession.repository.ts
Normal file
25
src/modules/userSession/userSession.repository.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { userSessionModel } from "./userSession.model";
|
||||
|
||||
export const createUserSessionRepo = async (
|
||||
data: Prisma.UserSessionUncheckedCreateInput
|
||||
) => {
|
||||
try {
|
||||
const newUserRole = await userSessionModel.create({
|
||||
data: data,
|
||||
include: {
|
||||
user: {
|
||||
omit: {
|
||||
password: true,
|
||||
},
|
||||
include: {
|
||||
roles: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
return newUserRole;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
0
src/modules/userSession/userSession.schema.ts
Normal file
0
src/modules/userSession/userSession.schema.ts
Normal file
6
src/modules/userSession/userSession.types.ts
Normal file
6
src/modules/userSession/userSession.types.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import { UserHeaderInformation } from "../../helpers/cookies/userHeader/getUserHeaderInformation/types";
|
||||
|
||||
export interface createUserSessionServiceParams {
|
||||
userId: string;
|
||||
userHeaderInformation: UserHeaderInformation;
|
||||
}
|
||||
Reference in New Issue
Block a user