user selection compoleted tested
This commit is contained in:
@@ -3,6 +3,7 @@ import { redis } from "@/lib/redis";
|
||||
import { functionRetrieveUserSelection } from "@/fetchers/fecther";
|
||||
import { ClientSelection, AuthError } from "@/fetchers/types/context";
|
||||
import { getCompleteFromRedis, setCompleteToRedis } from "@/fetchers/custom/context/complete/fetch";
|
||||
import { loginSelectEmployee, loginSelectOccupant } from "@/fetchers/custom/login/login";
|
||||
|
||||
const getSelectionFromRedis = async (): Promise<ClientSelection> => {
|
||||
try {
|
||||
@@ -17,7 +18,7 @@ const getSelectionFromRedis = async (): Promise<ClientSelection> => {
|
||||
} catch (error) { return { selectionList: [], activeSelection: {} } }
|
||||
}
|
||||
|
||||
const setSelectionToRedis = async (selectionObject: ClientSelection) => {
|
||||
const setActiveSelectionToRedis = async (selectionObject: any) => {
|
||||
try {
|
||||
const decrpytUserSelection = await functionRetrieveUserSelection()
|
||||
if (!decrpytUserSelection) throw new AuthError("No user selection found");
|
||||
@@ -30,10 +31,19 @@ const setSelectionToRedis = async (selectionObject: ClientSelection) => {
|
||||
const oldData = await getCompleteFromRedis();
|
||||
if (!oldData) throw new AuthError("No old data found in redis");
|
||||
|
||||
await setCompleteToRedis({ ...oldData, selection: selectionObject })
|
||||
await setCompleteToRedis({
|
||||
...oldData, selection: {
|
||||
selectionList: oldData.selection.selectionList,
|
||||
activeSelection: selectionObject
|
||||
}
|
||||
})
|
||||
if (oldData.online.userType.toUpperCase() === "EMPLOYEE") {
|
||||
await loginSelectEmployee({ uuid: selectionObject.uuid });
|
||||
} else if (oldData.online.userType.toUpperCase() === "OCCUPANT") {
|
||||
await loginSelectOccupant({ uuid: selectionObject.build_living_space_uu_id });
|
||||
}
|
||||
return true;
|
||||
} catch (error) {
|
||||
// Re-throw AuthError instances, wrap other errors as AuthError
|
||||
if (error instanceof AuthError) {
|
||||
throw error;
|
||||
} else {
|
||||
@@ -42,4 +52,4 @@ const setSelectionToRedis = async (selectionObject: ClientSelection) => {
|
||||
}
|
||||
}
|
||||
|
||||
export { getSelectionFromRedis, setSelectionToRedis };
|
||||
export { getSelectionFromRedis, setActiveSelectionToRedis };
|
||||
|
||||
@@ -15,7 +15,7 @@ import {
|
||||
import { retrievePageList } from "@/fetchers/mutual/cookies/token";
|
||||
import { deleteAllCookies } from "@/fetchers/mutual/cookies/cookie-actions";
|
||||
import { setMenuToRedis } from "@/fetchers/custom/context/page/menu/fetch";
|
||||
import { LoginViaAccessKeys, LoginSelect, LoginSelectOccupant } from "@/fetchers/types/login/validations";
|
||||
import { LoginViaAccessKeys, LoginSelect } from "@/fetchers/types/login/validations";
|
||||
|
||||
async function logoutActiveSession() {
|
||||
const response = await fetchDataWithToken(urlLogoutEndpoint, {}, "GET", false);
|
||||
@@ -25,29 +25,50 @@ async function logoutActiveSession() {
|
||||
|
||||
async function initRedis(loginRespone: any, firstSelection: any, accessToken: string, redisKey: string) {
|
||||
let alreadyAtRedis = null
|
||||
try {
|
||||
const redisData = await getCompleteFromRedis();
|
||||
alreadyAtRedis = redisData;
|
||||
} catch (error) { }
|
||||
console.log("loginRespone", loginRespone)
|
||||
console.log("firstSelection", firstSelection)
|
||||
console.log("accessToken", accessToken)
|
||||
console.log("redisKey", redisKey)
|
||||
if (!alreadyAtRedis) {
|
||||
const loginObjectToRedis = {
|
||||
online: {
|
||||
...defaultValuesOnline,
|
||||
lastLogin: new Date(),
|
||||
userType: `${loginRespone.user_type}`.toUpperCase(),
|
||||
lang: loginRespone.user.person.country_code.toLowerCase(),
|
||||
},
|
||||
pageConfig: defaultValuesPageConfig,
|
||||
menu: defaultValuesMenu,
|
||||
header: defaultValuesHeader,
|
||||
selection: { selectionList: loginRespone.selection_list, activeSelection: firstSelection },
|
||||
user: loginRespone.user,
|
||||
settings: { lastOnline: new Date(), token: accessToken },
|
||||
chatRoom: [],
|
||||
notifications: [],
|
||||
messages: [],
|
||||
if (loginRespone.user_type.toUpperCase() === "EMPLOYEE") {
|
||||
const loginObjectToRedis = {
|
||||
online: { ...defaultValuesOnline, lastLogin: new Date(), userType: `${loginRespone.user_type}`.toUpperCase(), lang: loginRespone.user.person.country_code.toLowerCase() },
|
||||
pageConfig: defaultValuesPageConfig,
|
||||
menu: defaultValuesMenu,
|
||||
header: defaultValuesHeader,
|
||||
selection: { selectionList: loginRespone.selection_list, activeSelection: firstSelection },
|
||||
user: loginRespone.user,
|
||||
settings: { lastOnline: new Date(), token: accessToken },
|
||||
chatRoom: [],
|
||||
notifications: [],
|
||||
messages: [],
|
||||
}
|
||||
await setNewCompleteToRedis(loginObjectToRedis, redisKey);
|
||||
} else if (loginRespone.user_type.toUpperCase() === "OCCUPANT") {
|
||||
const loginObjectToRedis = {
|
||||
online: { ...defaultValuesOnline, lastLogin: new Date(), userType: `${loginRespone.user_type}`.toUpperCase(), lang: "tr" },
|
||||
pageConfig: defaultValuesPageConfig,
|
||||
menu: defaultValuesMenu,
|
||||
header: defaultValuesHeader,
|
||||
selection: { selectionList: loginRespone.selection_list, activeSelection: firstSelection },
|
||||
user: loginRespone.user,
|
||||
settings: { lastOnline: new Date(), token: accessToken },
|
||||
chatRoom: [],
|
||||
notifications: [],
|
||||
messages: [],
|
||||
}
|
||||
await setNewCompleteToRedis(loginObjectToRedis, redisKey);
|
||||
}
|
||||
await setNewCompleteToRedis(loginObjectToRedis, redisKey);
|
||||
}
|
||||
}
|
||||
|
||||
async function initFirstSelection(firstSelection: any, userType: string) {
|
||||
if (userType === "EMPLOYEE") {
|
||||
const uuid = firstSelection.uuid;
|
||||
await loginSelectEmployee({ uuid });
|
||||
} else if (userType === "OCCUPANT") {
|
||||
const uuid = firstSelection.build_living_space_uu_id;
|
||||
await loginSelectOccupant({ uuid });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,22 +89,24 @@ async function loginViaAccessKeys(payload: LoginViaAccessKeys) {
|
||||
if (response.status === 200 || response.status === 202) {
|
||||
|
||||
const loginRespone: any = response?.data;
|
||||
const firstSelection = loginRespone.selection_list.find((item: any) => item.uu_id === loginRespone.selection_list[0].uu_id);
|
||||
|
||||
let firstSelection = null
|
||||
|
||||
if (loginRespone.user_type.toUpperCase() === "EMPLOYEE") {
|
||||
firstSelection = loginRespone.selection_list.find((item: any) => item.uu_id === loginRespone.selection_list[0].uu_id);
|
||||
} else if (loginRespone.user_type.toUpperCase() === "OCCUPANT") {
|
||||
const firstKeyOfSelectionList = Object.keys(loginRespone.selection_list)[0];
|
||||
firstSelection = loginRespone.selection_list[firstKeyOfSelectionList].occupants[0];
|
||||
}
|
||||
const nextCrypto = new NextCrypto(tokenSecret);
|
||||
const accessToken = await nextCrypto.encrypt(loginRespone.access_token);
|
||||
|
||||
const redisKey = `CLIENT:${loginRespone.user_type.toUpperCase()}:${firstSelection.uu_id}`;
|
||||
const userType = loginRespone.user_type.toUpperCase()
|
||||
const redisKey = `CLIENT:${userType}:${loginRespone.user.uuid}`;
|
||||
const redisKeyAccess = await nextCrypto.encrypt(redisKey);
|
||||
const usersSelection = await nextCrypto.encrypt(
|
||||
JSON.stringify({
|
||||
selected: firstSelection.uu_id,
|
||||
userType: loginRespone.user_type.toUpperCase(),
|
||||
redisKey,
|
||||
})
|
||||
);
|
||||
const usersSelection = await nextCrypto.encrypt(JSON.stringify({ selected: firstSelection, userType, redisKey }));
|
||||
|
||||
await initRedis(loginRespone, firstSelection, accessToken, redisKey);
|
||||
|
||||
cookieStore.set({
|
||||
name: "eys-zzz",
|
||||
value: accessToken,
|
||||
@@ -99,12 +122,16 @@ async function loginViaAccessKeys(payload: LoginViaAccessKeys) {
|
||||
value: usersSelection,
|
||||
...cookieObject,
|
||||
});
|
||||
|
||||
try {
|
||||
return {
|
||||
completed: true,
|
||||
message: "Login successful",
|
||||
status: 200,
|
||||
data: loginRespone,
|
||||
data: {
|
||||
userType,
|
||||
firstSelection,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("JSON parse error:", error);
|
||||
@@ -112,7 +139,10 @@ async function loginViaAccessKeys(payload: LoginViaAccessKeys) {
|
||||
completed: false,
|
||||
message: "Login NOT successful",
|
||||
status: 401,
|
||||
data: "{}",
|
||||
data: {
|
||||
userType,
|
||||
firstSelection,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -135,65 +165,32 @@ async function loginViaAccessKeys(payload: LoginViaAccessKeys) {
|
||||
}
|
||||
|
||||
async function loginSelectEmployee(payload: LoginSelect) {
|
||||
const cookieStore = await cookies();
|
||||
const nextCrypto = new NextCrypto(tokenSecret);
|
||||
const employeeUUID = payload.uuid;
|
||||
const redisKey = `CLIENT:EMPLOYEE:${employeeUUID}`;
|
||||
const selectResponse: any = await fetchDataWithToken(urlLoginSelectEndpoint, { uuid: employeeUUID }, "POST", false);
|
||||
cookieStore.delete({ name: "eys-sel", ...cookieObject });
|
||||
|
||||
console.log("selectResponse", selectResponse)
|
||||
if (selectResponse.status === 200 || selectResponse.status === 202) {
|
||||
const usersSelection = await nextCrypto.encrypt(
|
||||
JSON.stringify({
|
||||
selected: employeeUUID,
|
||||
userType: "employee",
|
||||
redisKey,
|
||||
})
|
||||
);
|
||||
cookieStore.set({
|
||||
name: "eys-sel",
|
||||
value: usersSelection,
|
||||
...cookieObject,
|
||||
});
|
||||
try {
|
||||
const pageList = await retrievePageList()
|
||||
await setMenuToRedis({
|
||||
selectionList: pageList,
|
||||
activeSelection: "/dashboard"
|
||||
})
|
||||
console.log("selectResponse", selectResponse)
|
||||
} catch (error) { }
|
||||
}
|
||||
return selectResponse;
|
||||
}
|
||||
|
||||
async function loginSelectOccupant(payload: LoginSelectOccupant) {
|
||||
async function loginSelectOccupant(payload: LoginSelect) {
|
||||
const livingSpaceUUID = payload.uuid;
|
||||
const cookieStore = await cookies();
|
||||
const nextCrypto = new NextCrypto(tokenSecret);
|
||||
|
||||
const selectResponse: any = await fetchDataWithToken(urlLoginSelectEndpoint, { uuid: livingSpaceUUID }, "POST", false);
|
||||
const redisKey = `CLIENT:OCCUPANT:${livingSpaceUUID}`;
|
||||
cookieStore.delete("eys-sel");
|
||||
|
||||
console.log("selectResponse", selectResponse)
|
||||
if (selectResponse.status === 200 || selectResponse.status === 202) {
|
||||
const usersSelection = await nextCrypto.encrypt(
|
||||
JSON.stringify({
|
||||
selected: livingSpaceUUID,
|
||||
userType: "OCCUPANT",
|
||||
redisKey,
|
||||
})
|
||||
);
|
||||
cookieStore.set({
|
||||
name: "eys-sel",
|
||||
value: usersSelection,
|
||||
...cookieObject,
|
||||
});
|
||||
try {
|
||||
console.log("selectResponse", selectResponse)
|
||||
} catch (error) { }
|
||||
}
|
||||
return selectResponse;
|
||||
}
|
||||
|
||||
export {
|
||||
loginViaAccessKeys,
|
||||
initFirstSelection,
|
||||
loginSelectEmployee,
|
||||
loginSelectOccupant,
|
||||
logoutActiveSession,
|
||||
|
||||
@@ -8,8 +8,4 @@ interface LoginSelect {
|
||||
uuid: string;
|
||||
}
|
||||
|
||||
interface LoginSelectOccupant {
|
||||
uuid: any;
|
||||
}
|
||||
|
||||
export type { LoginViaAccessKeys, LoginSelect, LoginSelectOccupant };
|
||||
export type { LoginViaAccessKeys, LoginSelect };
|
||||
|
||||
Reference in New Issue
Block a user