updated user selection via select response return
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
"use server";
|
||||
import { redis } from "@/lib/redis";
|
||||
import { functionRetrieveUserSelection } from "@/fetchers/fecther";
|
||||
import { ClientRedisToken, defaultClientRedisToken } from "@/fetchers/types/context";
|
||||
import { REDIS_TIMEOUT } from "@/fetchers/base";
|
||||
import { safeRedisGet, safeRedisSet, safeJsonParse } from "@/utils/redisOperations";
|
||||
|
||||
/**
|
||||
* Gets the complete data from Redis with improved error handling and timeouts
|
||||
@@ -16,14 +16,12 @@ const getCompleteFromRedis = async (): Promise<ClientRedisToken> => {
|
||||
if (!redisKey) { return defaultClientRedisToken }
|
||||
if (redisKey === "default") { return defaultClientRedisToken }
|
||||
|
||||
try {
|
||||
const timeoutPromise = new Promise<string | null>((_, reject) => {
|
||||
setTimeout(() => reject(new Error('Redis operation timed out')), REDIS_TIMEOUT);
|
||||
});
|
||||
const result = await Promise.race([redis.get(`${redisKey}`), timeoutPromise]);
|
||||
if (!result) { return defaultClientRedisToken }
|
||||
try { const parsedResult = JSON.parse(result); return parsedResult } catch (parseError) { return defaultClientRedisToken }
|
||||
} catch (redisError) { return defaultClientRedisToken }
|
||||
// Use safe Redis get operation with proper connection handling
|
||||
const result = await safeRedisGet(`${redisKey}`, REDIS_TIMEOUT);
|
||||
if (!result) { return defaultClientRedisToken }
|
||||
|
||||
// Use safe JSON parsing
|
||||
return safeJsonParse<ClientRedisToken>(result, defaultClientRedisToken);
|
||||
} catch (error) { return defaultClientRedisToken }
|
||||
}
|
||||
|
||||
@@ -40,11 +38,8 @@ const setCompleteToRedis = async (completeObject: ClientRedisToken): Promise<boo
|
||||
if (!decrpytUserSelection) { return false }
|
||||
const redisKey = decrpytUserSelection?.redisKey;
|
||||
if (!redisKey) { return false }
|
||||
try {
|
||||
const timeoutPromise = new Promise((_, reject) => { setTimeout(() => reject(new Error('Redis operation timed out')), REDIS_TIMEOUT) });
|
||||
await Promise.race([redis.set(redisKey, JSON.stringify(completeObject)), timeoutPromise]);
|
||||
return true;
|
||||
} catch (redisError) { return false }
|
||||
// Use safe Redis set operation with proper connection handling
|
||||
return await safeRedisSet(redisKey, JSON.stringify(completeObject), REDIS_TIMEOUT);
|
||||
} catch (error) { return false }
|
||||
}
|
||||
|
||||
@@ -58,11 +53,8 @@ const setNewCompleteToRedis = async (completeObject: ClientRedisToken, redisKey:
|
||||
try {
|
||||
if (!redisKey) { return false }
|
||||
if (!completeObject) { return false }
|
||||
try {
|
||||
const timeoutPromise = new Promise((_, reject) => { setTimeout(() => reject(new Error('Redis operation timed out')), REDIS_TIMEOUT) });
|
||||
await Promise.race([redis.set(redisKey, JSON.stringify(completeObject)), timeoutPromise])
|
||||
return true;
|
||||
} catch (redisError) { return false }
|
||||
// Use safe Redis set operation with proper connection handling
|
||||
return await safeRedisSet(redisKey, JSON.stringify(completeObject), REDIS_TIMEOUT);
|
||||
} catch (error) { return false }
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +1,27 @@
|
||||
"use server";
|
||||
import { redis } from "@/lib/redis";
|
||||
import { functionRetrieveUserSelection } from "@/fetchers/fecther";
|
||||
import { ClientSelection, AuthError } from "@/fetchers/types/context";
|
||||
import { defaultValuesSelection } from "@/fetchers/types/context/selection/validations";
|
||||
import { getCompleteFromRedis, setCompleteToRedis } from "@/fetchers/custom/context/complete/fetch";
|
||||
import { loginSelectEmployee, loginSelectOccupant } from "@/fetchers/custom/login/login";
|
||||
import { safeRedisGet, safeJsonParse } from "@/utils/redisOperations";
|
||||
import { REDIS_TIMEOUT } from "@/fetchers/base";
|
||||
|
||||
const getSelectionFromRedis = async (): Promise<ClientSelection> => {
|
||||
try {
|
||||
const decrpytUserSelection = await functionRetrieveUserSelection();
|
||||
const redisKey = decrpytUserSelection?.redisKey;
|
||||
if (redisKey === "default") { return { selectionList: [], activeSelection: {} } }
|
||||
const result = await redis.get(`${redisKey}`);
|
||||
if (!result) { return { selectionList: [], activeSelection: {} } }
|
||||
const parsedResult = JSON.parse(result);
|
||||
if (!parsedResult.selection) { return { selectionList: [], activeSelection: {} } }
|
||||
if (redisKey === "default") { return defaultValuesSelection }
|
||||
|
||||
// Use safe Redis get operation with proper connection handling
|
||||
const result = await safeRedisGet(`${redisKey}`, REDIS_TIMEOUT);
|
||||
if (!result) { return defaultValuesSelection }
|
||||
|
||||
// Use safe JSON parsing with proper default object type
|
||||
const parsedResult = safeJsonParse(result, { selection: defaultValuesSelection });
|
||||
if (!parsedResult.selection) { return defaultValuesSelection }
|
||||
return parsedResult.selection;
|
||||
} catch (error) { return { selectionList: [], activeSelection: {} } }
|
||||
} catch (error) { return defaultValuesSelection }
|
||||
}
|
||||
|
||||
const setActiveSelectionToRedis = async (selectionObject: any) => {
|
||||
@@ -37,11 +43,11 @@ const setActiveSelectionToRedis = async (selectionObject: any) => {
|
||||
activeSelection: selectionObject
|
||||
}
|
||||
})
|
||||
console.log("oldData", oldData)
|
||||
if (oldData.online.userType.toUpperCase() === "EMPLOYEE") {
|
||||
console.log("selectionObject", selectionObject)
|
||||
await loginSelectEmployee({ uuid: selectionObject.uu_id });
|
||||
} else if (oldData.online.userType.toUpperCase() === "OCCUPANT") {
|
||||
console.log("selectionObject", selectionObject.build_living_space_uu_id)
|
||||
await loginSelectOccupant({ uuid: selectionObject.build_living_space_uu_id });
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
"use server";
|
||||
import { redis } from "@/lib/redis";
|
||||
import { functionRetrieveUserSelection } from "@/fetchers/fecther";
|
||||
import { ClientSettings, AuthError } from "@/fetchers/types/context";
|
||||
import { defaultValuesSettings } from "@/fetchers/types/context/settings/validations";
|
||||
import { getCompleteFromRedis, setCompleteToRedis } from "@/fetchers/custom/context/complete/fetch";
|
||||
import { safeRedisGet, safeJsonParse } from "@/utils/redisOperations";
|
||||
import { REDIS_TIMEOUT } from "@/fetchers/base";
|
||||
|
||||
const getSettingsFromRedis = async (): Promise<ClientSettings> => {
|
||||
try {
|
||||
const decrpytUserSelection = await functionRetrieveUserSelection()
|
||||
const redisKey = decrpytUserSelection?.redisKey;
|
||||
if (!redisKey) throw new AuthError("No redis key found");
|
||||
const result = await redis.get(`${redisKey}`);
|
||||
// Use safe Redis get operation with proper connection handling
|
||||
const result = await safeRedisGet(`${redisKey}`, REDIS_TIMEOUT);
|
||||
if (!result) throw new AuthError("No data found in redis");
|
||||
const parsedResult = JSON.parse(result);
|
||||
|
||||
// Use safe JSON parsing with proper default object type
|
||||
const parsedResult = safeJsonParse(result, { settings: defaultValuesSettings });
|
||||
if (!parsedResult.settings) throw new AuthError("No settings found in redis");
|
||||
return parsedResult.settings;
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
"use server";
|
||||
import { redis } from "@/lib/redis";
|
||||
import { functionRetrieveUserSelection } from "@/fetchers/fecther";
|
||||
import { ClientUser, AuthError } from "@/fetchers/types/context";
|
||||
import { defaultValuesUser } from "@/fetchers/types/context/user/validations";
|
||||
import { getCompleteFromRedis, setCompleteToRedis } from "@/fetchers/custom/context/complete/fetch";
|
||||
import { safeRedisGet, safeJsonParse } from "@/utils/redisOperations";
|
||||
import { REDIS_TIMEOUT } from "@/fetchers/base";
|
||||
|
||||
const getUserFromRedis = async (): Promise<ClientUser> => {
|
||||
try {
|
||||
const decrpytUserSelection = await functionRetrieveUserSelection()
|
||||
const redisKey = decrpytUserSelection?.redisKey;
|
||||
if (!redisKey) throw new AuthError("No redis key found");
|
||||
const result = await redis.get(`${redisKey}`);
|
||||
// Use safe Redis get operation with proper connection handling
|
||||
const result = await safeRedisGet(`${redisKey}`, REDIS_TIMEOUT);
|
||||
if (!result) throw new AuthError("No data found in redis");
|
||||
const parsedResult = JSON.parse(result);
|
||||
|
||||
// Use safe JSON parsing with proper default object type
|
||||
const parsedResult = safeJsonParse(result, { user: defaultValuesUser });
|
||||
if (!parsedResult.user) throw new AuthError("No user found in redis");
|
||||
return parsedResult.user;
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
"use server";
|
||||
import { redis } from "@/lib/redis";
|
||||
import { functionRetrieveUserSelection } from "@/fetchers/fecther";
|
||||
import { ClientSettings, AuthError } from "@/fetchers/types/context";
|
||||
import { defaultValuesSettings } from "@/fetchers/types/context/settings/validations";
|
||||
import { getCompleteFromRedis, setCompleteToRedis } from "@/fetchers/custom/context/complete/fetch";
|
||||
import { safeRedisGet, safeJsonParse } from "@/utils/redisOperations";
|
||||
import { REDIS_TIMEOUT } from "@/fetchers/base";
|
||||
|
||||
const getConfigFromRedis = async (): Promise<ClientSettings> => {
|
||||
try {
|
||||
const decrpytUserSelection = await functionRetrieveUserSelection()
|
||||
const redisKey = decrpytUserSelection?.redisKey;
|
||||
if (!redisKey) throw new AuthError("No redis key found");
|
||||
const result = await redis.get(`${redisKey}`);
|
||||
// Use safe Redis get operation with proper connection handling
|
||||
const result = await safeRedisGet(`${redisKey}`, REDIS_TIMEOUT);
|
||||
if (!result) throw new AuthError("No data found in redis");
|
||||
const parsedResult = JSON.parse(result);
|
||||
|
||||
// Use safe JSON parsing with proper default object type
|
||||
const parsedResult = safeJsonParse(result, { settings: defaultValuesSettings });
|
||||
if (!parsedResult.settings) throw new AuthError("No settings found in redis");
|
||||
return parsedResult.settings;
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
"use server";
|
||||
import { redis } from "@/lib/redis";
|
||||
import { functionRetrieveUserSelection } from "@/fetchers/fecther";
|
||||
import { ClientMenu, AuthError } from "@/fetchers/types/context";
|
||||
import { defaultValuesMenu } from "@/fetchers/types/context/menu/validations";
|
||||
import { getCompleteFromRedis, setCompleteToRedis } from "@/fetchers/custom/context/complete/fetch";
|
||||
import { safeRedisGet, safeJsonParse } from "@/utils/redisOperations";
|
||||
import { REDIS_TIMEOUT } from "@/fetchers/base";
|
||||
|
||||
const getMenuFromRedis = async (): Promise<ClientMenu> => {
|
||||
try {
|
||||
const decrpytUserSelection = await functionRetrieveUserSelection()
|
||||
const redisKey = decrpytUserSelection?.redisKey;
|
||||
if (!redisKey) throw new AuthError("No redis key found");
|
||||
const result = await redis.get(`${redisKey}`);
|
||||
// Use safe Redis get operation with proper connection handling
|
||||
const result = await safeRedisGet(`${redisKey}`, REDIS_TIMEOUT);
|
||||
if (!result) throw new AuthError("No data found in redis");
|
||||
const parsedResult = JSON.parse(result);
|
||||
|
||||
// Use safe JSON parsing with proper default object type
|
||||
const parsedResult = safeJsonParse(result, { menu: defaultValuesMenu });
|
||||
if (!parsedResult.menu) throw new AuthError("No menu found in redis");
|
||||
return parsedResult.menu;
|
||||
} catch (error) { if (error instanceof AuthError) { throw error } else { throw new AuthError(error instanceof Error ? error.message : "Unknown error") } }
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
"use server";
|
||||
import { redis } from "@/lib/redis";
|
||||
import { functionRetrieveUserSelection } from "@/fetchers/fecther";
|
||||
import { ClientOnline, AuthError } from "@/fetchers/types/context";
|
||||
import { defaultValuesOnline } from "@/fetchers/types/context/online/validations";
|
||||
import { getCompleteFromRedis, setCompleteToRedis } from "@/fetchers/custom/context/complete/fetch";
|
||||
import { safeRedisGet, safeJsonParse } from "@/utils/redisOperations";
|
||||
import { REDIS_TIMEOUT } from "@/fetchers/base";
|
||||
|
||||
/**
|
||||
@@ -18,14 +19,15 @@ const getOnlineFromRedis = async (): Promise<ClientOnline> => {
|
||||
if (!redisKey) { throw new AuthError('No redis key found') }
|
||||
if (redisKey === "default") { throw new AuthError('Invalid redis key') }
|
||||
try {
|
||||
const timeoutPromise = new Promise((_, reject) => { setTimeout(() => reject(new Error('Redis operation timed out')), REDIS_TIMEOUT) });
|
||||
result = await Promise.race([redis.get(`${redisKey}`), timeoutPromise]) as string | null;
|
||||
// Use safe Redis get operation with proper connection handling
|
||||
result = await safeRedisGet(`${redisKey}`, REDIS_TIMEOUT);
|
||||
} catch (redisError) { throw new AuthError('Failed to access Redis data') }
|
||||
|
||||
if (!result) { throw new AuthError('No data found in redis') }
|
||||
|
||||
try {
|
||||
const parsedResult = JSON.parse(result);
|
||||
// Use safe JSON parsing with proper default object type
|
||||
const parsedResult = safeJsonParse(result, { online: defaultValuesOnline });
|
||||
if (!parsedResult.online) { throw new AuthError('No online data found in redis') }
|
||||
return parsedResult.online;
|
||||
} catch (parseError) { throw new AuthError('Invalid data format in redis') }
|
||||
@@ -51,8 +53,8 @@ const setOnlineToRedis = async (onlineObject: ClientOnline): Promise<boolean> =>
|
||||
try { oldData = await getCompleteFromRedis() } catch (error) { throw new AuthError('Failed to retrieve existing data from Redis') }
|
||||
if (!oldData) { throw new AuthError('No old data found in redis') }
|
||||
try {
|
||||
const timeoutPromise = new Promise((_, reject) => { setTimeout(() => reject(new Error('Redis operation timed out')), REDIS_TIMEOUT) });
|
||||
await Promise.race([setCompleteToRedis({ ...oldData, online: onlineObject }), timeoutPromise]);
|
||||
// Use the setCompleteToRedis function which already uses safe Redis operations
|
||||
await setCompleteToRedis({ ...oldData, online: onlineObject });
|
||||
return true;
|
||||
} catch (redisError) { throw new AuthError('Failed to update Redis data') }
|
||||
} catch (error) { if (error instanceof AuthError) throw error; throw new AuthError(error instanceof Error ? error.message : 'Unknown error') }
|
||||
|
||||
@@ -62,6 +62,7 @@ async function initRedis(loginRespone: any, firstSelection: any, accessToken: st
|
||||
async function initFirstSelection(firstSelection: any, userType: string) {
|
||||
if (userType === "EMPLOYEE") {
|
||||
const uuid = firstSelection.uu_id;
|
||||
console.log("uuid", uuid)
|
||||
await loginSelectEmployee({ uuid });
|
||||
} else if (userType === "OCCUPANT") {
|
||||
const uuid = firstSelection.build_living_space_uu_id;
|
||||
@@ -99,8 +100,6 @@ async function loginViaAccessKeys(payload: LoginViaAccessKeys) {
|
||||
const redisKeyAccess = await nextCrypto.encrypt(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,
|
||||
@@ -117,6 +116,8 @@ async function loginViaAccessKeys(payload: LoginViaAccessKeys) {
|
||||
...cookieObject,
|
||||
});
|
||||
|
||||
await initRedis(loginRespone, firstSelection, accessToken, redisKey);
|
||||
|
||||
try {
|
||||
return {
|
||||
completed: true,
|
||||
@@ -156,11 +157,7 @@ async function loginSelectEmployee(payload: LoginSelect) {
|
||||
const employeeUUID = payload.uuid;
|
||||
const selectResponse: any = await fetchDataWithToken(urlLoginSelectEndpoint, { uuid: employeeUUID }, "POST", false);
|
||||
if (selectResponse.status === 200 || selectResponse.status === 202) {
|
||||
try {
|
||||
console.log("selectResponse", selectResponse) // Get Menu URL's of Employee
|
||||
const validUrls = await retrieveValidUrlsOfRestriction()
|
||||
setMenuToRedis(validUrls)
|
||||
} catch (error) { }
|
||||
try { setMenuToRedis(selectResponse.data.reachable_app_codes || []) } catch (error) { }
|
||||
}
|
||||
return selectResponse;
|
||||
}
|
||||
@@ -169,11 +166,7 @@ async function loginSelectOccupant(payload: LoginSelect) {
|
||||
const livingSpaceUUID = payload.uuid;
|
||||
const selectResponse: any = await fetchDataWithToken(urlLoginSelectEndpoint, { uuid: livingSpaceUUID }, "POST", false);
|
||||
if (selectResponse.status === 200 || selectResponse.status === 202) {
|
||||
try {
|
||||
console.log("selectResponse", selectResponse) // Get Menu URL's of Occupant
|
||||
const validUrls = await retrieveValidUrlsOfRestriction()
|
||||
setMenuToRedis(validUrls)
|
||||
} catch (error) { }
|
||||
try { setMenuToRedis(selectResponse.data.reachable_app_codes || []) } catch (error) { }
|
||||
}
|
||||
return selectResponse;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user