updated occupant and employee login completed

This commit is contained in:
2025-06-15 20:25:50 +03:00
parent 9fb517fa15
commit 57c2696bcb
4 changed files with 17 additions and 55 deletions

View File

@@ -8,34 +8,17 @@ import { NextResponse } from "next/server";
export async function GET() {
try {
const selection = await getSelectionFromRedis();
return NextResponse.json({
status: 200,
data: selection || null,
});
return NextResponse.json({ status: 200, data: selection || null });
} catch (error) {
if (error instanceof AuthError) {
// Return 401 Unauthorized for authentication errors
return new NextResponse(
JSON.stringify({
status: 401,
error: error.message,
}),
{
status: 401,
headers: { "Content-Type": "application/json" },
}
JSON.stringify({ status: 401, error: error.message }),
{ status: 401, headers: { "Content-Type": "application/json" } }
);
}
// For other errors, return 500 Internal Server Error
return new NextResponse(
JSON.stringify({
status: 500,
error: "Internal server error",
}),
{
status: 500,
headers: { "Content-Type": "application/json" },
}
JSON.stringify({ status: 500, error: "Internal server error" }),
{ status: 500, headers: { "Content-Type": "application/json" } }
);
}
}
@@ -44,33 +27,17 @@ export async function POST(request: Request) {
try {
const selection = await request.json();
await setActiveSelectionToRedis(selection);
return NextResponse.json({
status: 200,
data: selection || null,
});
return NextResponse.json({ status: 200, data: selection || null });
} catch (error) {
if (error instanceof AuthError) {
return new NextResponse(
JSON.stringify({
status: 401,
error: error.message,
}),
{
status: 401,
headers: { "Content-Type": "application/json" },
}
JSON.stringify({ status: 401, error: error.message }),
{ status: 401, headers: { "Content-Type": "application/json" } }
);
}
return new NextResponse(
JSON.stringify({
status: 500,
error: "Internal server error",
}),
{
status: 500,
headers: { "Content-Type": "application/json" },
}
JSON.stringify({ status: 500, error: "Internal server error" }),
{ status: 500, headers: { "Content-Type": "application/json" } }
);
}
}

View File

@@ -37,8 +37,10 @@ const setActiveSelectionToRedis = async (selectionObject: any) => {
activeSelection: selectionObject
}
})
console.log("oldData", oldData)
if (oldData.online.userType.toUpperCase() === "EMPLOYEE") {
await loginSelectEmployee({ uuid: selectionObject.uuid });
console.log("selectionObject", selectionObject)
await loginSelectEmployee({ uuid: selectionObject.uu_id });
} else if (oldData.online.userType.toUpperCase() === "OCCUPANT") {
await loginSelectOccupant({ uuid: selectionObject.build_living_space_uu_id });
}

View File

@@ -25,10 +25,6 @@ async function logoutActiveSession() {
async function initRedis(loginRespone: any, firstSelection: any, accessToken: string, redisKey: string) {
let alreadyAtRedis = null
console.log("loginRespone", loginRespone)
console.log("firstSelection", firstSelection)
console.log("accessToken", accessToken)
console.log("redisKey", redisKey)
if (!alreadyAtRedis) {
if (loginRespone.user_type.toUpperCase() === "EMPLOYEE") {
const loginObjectToRedis = {
@@ -64,7 +60,7 @@ async function initRedis(loginRespone: any, firstSelection: any, accessToken: st
async function initFirstSelection(firstSelection: any, userType: string) {
if (userType === "EMPLOYEE") {
const uuid = firstSelection.uuid;
const uuid = firstSelection.uu_id;
await loginSelectEmployee({ uuid });
} else if (userType === "OCCUPANT") {
const uuid = firstSelection.build_living_space_uu_id;
@@ -87,13 +83,10 @@ async function loginViaAccessKeys(payload: LoginViaAccessKeys) {
);
await deleteAllCookies()
if (response.status === 200 || response.status === 202) {
const loginRespone: any = response?.data;
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);
firstSelection = loginRespone.selection_list[0];
} else if (loginRespone.user_type.toUpperCase() === "OCCUPANT") {
const firstKeyOfSelectionList = Object.keys(loginRespone.selection_list)[0];
firstSelection = loginRespone.selection_list[firstKeyOfSelectionList].occupants[0];
@@ -165,6 +158,7 @@ async function loginViaAccessKeys(payload: LoginViaAccessKeys) {
}
async function loginSelectEmployee(payload: LoginSelect) {
console.log("payload", payload)
const employeeUUID = payload.uuid;
const selectResponse: any = await fetchDataWithToken(urlLoginSelectEndpoint, { uuid: employeeUUID }, "POST", false);
console.log("selectResponse", selectResponse)