old version placed
This commit is contained in:
18
apicalls/accounts/account.tsx
Normal file
18
apicalls/accounts/account.tsx
Normal file
@@ -0,0 +1,18 @@
|
||||
"use server";
|
||||
import { fetchData, fetchDataWithToken } from "../api-fetcher";
|
||||
import { baseUrl, FilterList, FilterListInterface } from "../basics";
|
||||
|
||||
const accountsListEndpoint = `${baseUrl}/account/records/list`;
|
||||
|
||||
async function retrieveaccountsList(payload: FilterListInterface) {
|
||||
const feedObject = new FilterList(payload).filter();
|
||||
const tokenResponse: any = await fetchDataWithToken(
|
||||
accountsListEndpoint,
|
||||
feedObject,
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
export { retrieveaccountsList };
|
||||
132
apicalls/api-fetcher.tsx
Normal file
132
apicalls/api-fetcher.tsx
Normal file
@@ -0,0 +1,132 @@
|
||||
"use server";
|
||||
|
||||
import { retrieveAccessToken } from "@/apicalls/cookies/token";
|
||||
|
||||
const defaultHeaders = {
|
||||
accept: "application/json",
|
||||
"Content-type": "application/json",
|
||||
};
|
||||
|
||||
const DefaultResponse = {
|
||||
completed: false,
|
||||
error: "Hata tipi belirtilmedi",
|
||||
message: "Hata oluştu, lütfen tekrar deneyin",
|
||||
status: "500",
|
||||
data: {},
|
||||
};
|
||||
|
||||
interface HeadersObject {
|
||||
cache: string;
|
||||
method: string;
|
||||
headers: Object;
|
||||
body?: string;
|
||||
}
|
||||
|
||||
const cacheList = ["no-cache", "no-store", "force-cache", "only-if-cached"];
|
||||
|
||||
const prepareResponse = async (response: any) => {
|
||||
try {
|
||||
const responseJson = await response.json();
|
||||
const statusResponse = response?.status;
|
||||
const errorResponse = responseJson?.error || responseJson?.Error;
|
||||
const messageResponse = (responseJson?.message || "").toString();
|
||||
const completeResponse = responseJson?.completed || false;
|
||||
|
||||
const preparedResponse = {
|
||||
completed: completeResponse,
|
||||
message: messageResponse,
|
||||
status: statusResponse,
|
||||
error: errorResponse || "",
|
||||
...responseJson,
|
||||
};
|
||||
|
||||
return preparedResponse;
|
||||
} catch (error) {}
|
||||
return DefaultResponse;
|
||||
};
|
||||
|
||||
const fetchData = async (
|
||||
endpoint: string,
|
||||
payload: any,
|
||||
method: string = "POST",
|
||||
cache: boolean = false
|
||||
) => {
|
||||
let headersObject: any = {
|
||||
cache: cache ? "force-cache" : "no-cache",
|
||||
method: method,
|
||||
headers: defaultHeaders,
|
||||
};
|
||||
if (method !== "GET") {
|
||||
headersObject = {
|
||||
...headersObject,
|
||||
body: JSON.stringify(payload),
|
||||
};
|
||||
}
|
||||
console.log("headersObject", headersObject);
|
||||
try {
|
||||
const response = await fetch(endpoint, headersObject);
|
||||
return await prepareResponse(response);
|
||||
} catch (error) {}
|
||||
return DefaultResponse;
|
||||
};
|
||||
|
||||
const updateDataWithToken = async (
|
||||
endpoint: string,
|
||||
uuid: string,
|
||||
payload: any,
|
||||
method: string = "POST",
|
||||
cache: boolean = false
|
||||
) => {
|
||||
const accessToken = (await retrieveAccessToken()) || "";
|
||||
let headersObject: any = {
|
||||
cache: cache ? "force-cache" : "no-cache",
|
||||
method: method,
|
||||
headers: {
|
||||
...defaultHeaders,
|
||||
"evyos-session-key": accessToken,
|
||||
},
|
||||
};
|
||||
|
||||
if (method !== "GET") {
|
||||
headersObject = {
|
||||
...headersObject,
|
||||
body: JSON.stringify(payload.payload),
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${endpoint}/${uuid}`, headersObject);
|
||||
return await prepareResponse(response);
|
||||
} catch (error) {}
|
||||
return DefaultResponse;
|
||||
};
|
||||
|
||||
const fetchDataWithToken = async (
|
||||
endpoint: string,
|
||||
payload: any,
|
||||
method: string = "POST",
|
||||
cache: boolean = false
|
||||
) => {
|
||||
const accessToken = (await retrieveAccessToken()) || "";
|
||||
let headersObject: any = {
|
||||
cache: cache ? "force-cache" : "no-cache",
|
||||
method: method,
|
||||
headers: {
|
||||
...defaultHeaders,
|
||||
"evyos-session-key": accessToken,
|
||||
},
|
||||
};
|
||||
if (method !== "GET") {
|
||||
headersObject = {
|
||||
...headersObject,
|
||||
body: JSON.stringify(payload),
|
||||
};
|
||||
}
|
||||
try {
|
||||
const response = await fetch(endpoint, headersObject);
|
||||
return await prepareResponse(response);
|
||||
} catch (error) {}
|
||||
return DefaultResponse;
|
||||
};
|
||||
|
||||
export { fetchData, fetchDataWithToken, updateDataWithToken };
|
||||
59
apicalls/basics.ts
Normal file
59
apicalls/basics.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
export const baseUrl = process.env.BASICURL || "";
|
||||
export const tokenSecret = process.env.TOKENSECRET || "";
|
||||
export const cookieObject: any = {
|
||||
httpOnly: true,
|
||||
path: "/",
|
||||
sameSite: "lax",
|
||||
secure: true,
|
||||
maxAge: 3600,
|
||||
};
|
||||
|
||||
interface FilterListInterface {
|
||||
page?: number | null | undefined;
|
||||
size?: number | null | undefined;
|
||||
orderField?: string | null | undefined;
|
||||
orderType?: string | null | undefined;
|
||||
includeJoins?: any[] | null | undefined;
|
||||
query?: any | null | undefined;
|
||||
}
|
||||
|
||||
class FilterList {
|
||||
page: number;
|
||||
size: number;
|
||||
orderField: string;
|
||||
orderType: string;
|
||||
includeJoins: any[];
|
||||
query: any;
|
||||
constructor({
|
||||
page = 1,
|
||||
size = 5,
|
||||
orderField = "id",
|
||||
orderType = "asc",
|
||||
includeJoins = [],
|
||||
query = {},
|
||||
}: FilterListInterface = {}) {
|
||||
this.page = page ?? 1;
|
||||
this.size = size ?? 5;
|
||||
this.orderField = orderField ?? "id";
|
||||
this.orderType = orderType ?? "asc";
|
||||
this.orderType = this.orderType.startsWith("a") ? "asc" : "desc";
|
||||
this.includeJoins = includeJoins ?? [];
|
||||
this.query = query ?? {};
|
||||
|
||||
}
|
||||
filter() {
|
||||
return {
|
||||
page: this.page,
|
||||
size: this.size,
|
||||
order_field: this.orderField,
|
||||
order_type: this.orderType,
|
||||
include_joins: this.includeJoins,
|
||||
query: this.query,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const defaultFilterList = new FilterList({});
|
||||
|
||||
export { FilterList, defaultFilterList };
|
||||
export type { FilterListInterface };
|
||||
52
apicalls/building/build.tsx
Normal file
52
apicalls/building/build.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
"use server";
|
||||
import { fetchDataWithToken, updateDataWithToken } from "../api-fetcher";
|
||||
import {
|
||||
baseUrl,
|
||||
FilterList,
|
||||
FilterListInterface,
|
||||
defaultFilterList,
|
||||
} from "../basics";
|
||||
|
||||
const buildListEndpoint = `${baseUrl}/building/build/list`;
|
||||
const buildCreateEndpoint = `${baseUrl}/building/build/create`;
|
||||
const buildUpdateEndpoint = `${baseUrl}/building/build/update`;
|
||||
|
||||
async function retrieveBuildList(payload: FilterListInterface) {
|
||||
const feedObject = new FilterList(payload).filter();
|
||||
const tokenResponse: any = await fetchDataWithToken(
|
||||
buildListEndpoint,
|
||||
feedObject,
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
interface BuildUpdateInterface {
|
||||
uuid: string;
|
||||
payload: any;
|
||||
}
|
||||
|
||||
async function updateBuild(payload: any) {
|
||||
const { uu_id: extractedField, ...payloadBody } = payload;
|
||||
const tokenResponse: any = await updateDataWithToken(
|
||||
buildUpdateEndpoint,
|
||||
extractedField,
|
||||
payloadBody,
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
async function createBuild(payload: any) {
|
||||
const tokenResponse: any = await fetchDataWithToken(
|
||||
buildCreateEndpoint,
|
||||
payload,
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
export { retrieveBuildList, updateBuild, createBuild };
|
||||
40
apicalls/building/livingSpace.tsx
Normal file
40
apicalls/building/livingSpace.tsx
Normal file
@@ -0,0 +1,40 @@
|
||||
"use server";
|
||||
import { fetchData, fetchDataWithToken } from "../api-fetcher";
|
||||
import { baseUrl, FilterList, FilterListInterface } from "../basics";
|
||||
|
||||
const livingSpaceListEndpoint = `${baseUrl}/building/living_space/list`;
|
||||
const livingSpaceCreateEndpoint = `${baseUrl}/building/living_space/create`;
|
||||
const livingSpaceUpdateEndpoint = `${baseUrl}/building/living_space/update`;
|
||||
|
||||
async function retrievelivingSpaceList(payload: FilterListInterface) {
|
||||
const feedObject = new FilterList(payload).filter();
|
||||
const tokenResponse: any = await fetchDataWithToken(
|
||||
livingSpaceListEndpoint,
|
||||
feedObject,
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
async function createLivingSpace(payload: any) {
|
||||
const tokenResponse: any = await fetchDataWithToken(
|
||||
livingSpaceCreateEndpoint,
|
||||
payload,
|
||||
"POST",
|
||||
true
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
async function updateLivingSpace(payload: any) {
|
||||
const tokenResponse: any = await fetchDataWithToken(
|
||||
livingSpaceUpdateEndpoint,
|
||||
payload,
|
||||
"POST",
|
||||
true
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
export { retrievelivingSpaceList, createLivingSpace, updateLivingSpace };
|
||||
52
apicalls/building/parts.tsx
Normal file
52
apicalls/building/parts.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
"use server";
|
||||
import { fetchDataWithToken, updateDataWithToken } from "../api-fetcher";
|
||||
import {
|
||||
baseUrl,
|
||||
FilterList,
|
||||
FilterListInterface,
|
||||
defaultFilterList,
|
||||
} from "../basics";
|
||||
|
||||
const partsListEndpoint = `${baseUrl}/building/parts/list`;
|
||||
const partsCreateEndpoint = `${baseUrl}/building/parts/create`;
|
||||
const partsUpdateEndpoint = `${baseUrl}/building/parts/update`;
|
||||
|
||||
interface BuildUpdateInterface {
|
||||
uuid: string;
|
||||
payload: any;
|
||||
}
|
||||
|
||||
async function retrievePartsList(payload: FilterListInterface) {
|
||||
const feedObject = new FilterList(payload).filter();
|
||||
const tokenResponse: any = await fetchDataWithToken(
|
||||
partsListEndpoint,
|
||||
feedObject,
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
async function updateParts(payload: any) {
|
||||
const { uu_id: extractedField, ...payloadBody } = payload;
|
||||
const tokenResponse: any = await updateDataWithToken(
|
||||
partsUpdateEndpoint,
|
||||
extractedField,
|
||||
payloadBody,
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
async function createParts(payload: any) {
|
||||
const tokenResponse: any = await fetchDataWithToken(
|
||||
partsCreateEndpoint,
|
||||
payload,
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
export { retrievePartsList, updateParts, createParts };
|
||||
52
apicalls/company/company.tsx
Normal file
52
apicalls/company/company.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
"use server";
|
||||
import { fetchDataWithToken, updateDataWithToken } from "../api-fetcher";
|
||||
import {
|
||||
baseUrl,
|
||||
FilterList,
|
||||
FilterListInterface,
|
||||
defaultFilterList,
|
||||
} from "../basics";
|
||||
|
||||
const CompanyListEndpoint = `${baseUrl}/company/list`;
|
||||
const CompanyCreateEndpoint = `${baseUrl}/company/create`;
|
||||
const CompanyUpdateEndpoint = `${baseUrl}/company/update`;
|
||||
|
||||
interface CompanyUpdateInterface {
|
||||
uuid: string;
|
||||
payload: any;
|
||||
}
|
||||
|
||||
async function retrieveCompanyList(payload: FilterListInterface) {
|
||||
const feedObject = new FilterList(payload).filter();
|
||||
const tokenResponse: any = await fetchDataWithToken(
|
||||
CompanyListEndpoint,
|
||||
feedObject,
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
async function updateCompany(payload: any) {
|
||||
const { uu_id: extractedField, ...payloadBody } = payload;
|
||||
const tokenResponse: any = await updateDataWithToken(
|
||||
CompanyUpdateEndpoint,
|
||||
extractedField,
|
||||
payloadBody,
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
async function createCompany(payload: any) {
|
||||
const tokenResponse: any = await fetchDataWithToken(
|
||||
CompanyCreateEndpoint,
|
||||
payload,
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
export { retrieveCompanyList, updateCompany, createCompany };
|
||||
121
apicalls/cookies/token.tsx
Normal file
121
apicalls/cookies/token.tsx
Normal file
@@ -0,0 +1,121 @@
|
||||
"use server";
|
||||
import { fetchDataWithToken, fetchData } from "../api-fetcher";
|
||||
import { baseUrl, tokenSecret } from "../basics";
|
||||
import { cookies } from "next/headers";
|
||||
import NextCrypto from "next-crypto";
|
||||
|
||||
const checkToken = `${baseUrl}/authentication/valid`;
|
||||
const nextCrypto = new NextCrypto(tokenSecret);
|
||||
|
||||
async function checkAccessTokenIsValid() {
|
||||
const response = await fetchDataWithToken(checkToken, {}, "GET", false);
|
||||
return response?.status === 200 ? true : false;
|
||||
}
|
||||
|
||||
async function retrieveAccessToken() {
|
||||
const cookieStore = await cookies();
|
||||
const encrpytAccessToken = cookieStore.get("accessToken")?.value || "";
|
||||
return encrpytAccessToken
|
||||
? await nextCrypto.decrypt(encrpytAccessToken)
|
||||
: null;
|
||||
}
|
||||
|
||||
async function retrieveUserType() {
|
||||
const cookieStore = await cookies();
|
||||
const encrpytaccessObject = cookieStore.get("accessObject")?.value || "{}";
|
||||
const decrpytUserType = JSON.parse(
|
||||
(await nextCrypto.decrypt(encrpytaccessObject)) || "{}"
|
||||
);
|
||||
return decrpytUserType ? decrpytUserType?.user_type : null;
|
||||
}
|
||||
|
||||
async function retrieveAccessObjects() {
|
||||
const cookieStore = await cookies();
|
||||
const encrpytAccessObject = cookieStore.get("accessObject")?.value || "";
|
||||
const decrpytAccessObject = await nextCrypto.decrypt(encrpytAccessObject);
|
||||
return decrpytAccessObject ? JSON.parse(decrpytAccessObject) : null;
|
||||
}
|
||||
|
||||
async function retrieveAvailableEvents() {
|
||||
const cookieStore = await cookies();
|
||||
const encrpytAccessObject = cookieStore.get("availableEvents")?.value || "";
|
||||
const decrpytAccessObject = await nextCrypto.decrypt(encrpytAccessObject);
|
||||
return decrpytAccessObject ? JSON.parse(decrpytAccessObject) : null;
|
||||
}
|
||||
|
||||
async function retrieveAvatarInfo() {
|
||||
const response = await fetchDataWithToken(
|
||||
`${baseUrl}/authentication/avatar`,
|
||||
{},
|
||||
"POST"
|
||||
);
|
||||
return response;
|
||||
}
|
||||
|
||||
async function retrieveUserSelection() {
|
||||
const cookieStore = await cookies();
|
||||
const encrpytUserSelection = cookieStore.get("userSelection")?.value || "";
|
||||
let decrpytUserSelection: any = await nextCrypto.decrypt(
|
||||
encrpytUserSelection
|
||||
);
|
||||
decrpytUserSelection = decrpytUserSelection
|
||||
? JSON.parse(decrpytUserSelection)
|
||||
: null;
|
||||
|
||||
const userSelection = decrpytUserSelection?.company_uu_id;
|
||||
|
||||
let objectUserSelection = {};
|
||||
|
||||
if (decrpytUserSelection?.user_type === "employee") {
|
||||
const accessObjects = (await retrieveAccessObjects()) || {};
|
||||
const companyList = accessObjects?.companies_list;
|
||||
const selectedCompany = companyList.find(
|
||||
(company: any) => company.uu_id === userSelection
|
||||
);
|
||||
if (selectedCompany) {
|
||||
objectUserSelection = {
|
||||
occupantName: `${selectedCompany?.public_name}`,
|
||||
};
|
||||
}
|
||||
} else if (decrpytUserSelection?.user_type === "occupant") {
|
||||
const buildPartUUID = userSelection?.build_part_uu_id;
|
||||
const occupantUUID = userSelection?.occupant_uu_id;
|
||||
const build_id = userSelection?.build_id;
|
||||
const accessObjects = (await retrieveAccessObjects()) || {};
|
||||
const availableOccupants = accessObjects?.available_occupants[build_id];
|
||||
const buildName = availableOccupants?.build_name;
|
||||
const buildNo = availableOccupants?.build_no;
|
||||
let selectedOccupant: any = null;
|
||||
const occupants = availableOccupants?.occupants;
|
||||
console.log("occupants", occupants);
|
||||
if (occupants) {
|
||||
selectedOccupant = occupants.find(
|
||||
(occupant: any) =>
|
||||
occupant.part_uu_id === buildPartUUID &&
|
||||
occupant.uu_id === occupantUUID
|
||||
);
|
||||
}
|
||||
if (selectedOccupant) {
|
||||
objectUserSelection = {
|
||||
buildName: `${buildName} - No:${buildNo}`,
|
||||
occupantName: `${selectedOccupant?.description} ${selectedOccupant?.part_name}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
const avatarInfo = await retrieveAvatarInfo();
|
||||
return {
|
||||
...objectUserSelection,
|
||||
lang: String(avatarInfo?.data?.lang).toLowerCase(),
|
||||
avatar: avatarInfo?.data?.avatar,
|
||||
fullName: avatarInfo?.data?.full_name,
|
||||
};
|
||||
}
|
||||
|
||||
export {
|
||||
checkAccessTokenIsValid,
|
||||
retrieveAccessToken,
|
||||
retrieveUserType,
|
||||
retrieveAccessObjects,
|
||||
retrieveAvailableEvents,
|
||||
retrieveUserSelection,
|
||||
};
|
||||
11
apicalls/dashboard/menu.tsx
Normal file
11
apicalls/dashboard/menu.tsx
Normal file
@@ -0,0 +1,11 @@
|
||||
"use server";
|
||||
import { fetchDataWithToken } from "../api-fetcher";
|
||||
import { baseUrl } from "../basics";
|
||||
|
||||
const eventList = `${baseUrl}/event/list`;
|
||||
|
||||
async function retrieveEventList() {
|
||||
return await fetchDataWithToken(eventList, {}, "GET", false);
|
||||
}
|
||||
|
||||
export { retrieveEventList };
|
||||
33
apicalls/events/available.tsx
Normal file
33
apicalls/events/available.tsx
Normal file
@@ -0,0 +1,33 @@
|
||||
"use server";
|
||||
import { fetchDataWithToken } from "../api-fetcher";
|
||||
import { cookies } from "next/headers";
|
||||
import { baseUrl, cookieObject, tokenSecret } from "../basics";
|
||||
import NextCrypto from "next-crypto";
|
||||
|
||||
const availableEventsURL = `${baseUrl}/access/endpoints/available`;
|
||||
|
||||
async function setAvailableEvents() {
|
||||
const cookieStore = await cookies();
|
||||
const nextCrypto = new NextCrypto(tokenSecret);
|
||||
|
||||
const availableResponse: any = await fetchDataWithToken(
|
||||
availableEventsURL,
|
||||
{},
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
|
||||
if (availableResponse.status === 200) {
|
||||
const availableEventData = Array.from(availableResponse?.result) || [];
|
||||
const availableEvents = await nextCrypto.encrypt(
|
||||
JSON.stringify({ availableEvents: availableEventData })
|
||||
);
|
||||
cookieStore.set({
|
||||
name: "availableEvents",
|
||||
value: availableEvents,
|
||||
...cookieObject,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export { setAvailableEvents };
|
||||
151
apicalls/login/login.tsx
Normal file
151
apicalls/login/login.tsx
Normal file
@@ -0,0 +1,151 @@
|
||||
"use server";
|
||||
import { fetchData, fetchDataWithToken } from "../api-fetcher";
|
||||
import { cookies } from "next/headers";
|
||||
import { baseUrl, cookieObject, tokenSecret } from "../basics";
|
||||
import NextCrypto from "next-crypto";
|
||||
import { setAvailableEvents } from "../events/available";
|
||||
|
||||
const loginEndpoint = `${baseUrl}/authentication/login`;
|
||||
const loginSelectEndpoint = `${baseUrl}/authentication/select`;
|
||||
|
||||
interface LoginViaAccessKeys {
|
||||
domain: string;
|
||||
accessKey: string;
|
||||
password: string;
|
||||
rememberMe: boolean;
|
||||
}
|
||||
|
||||
interface LoginSelectEmployee {
|
||||
company_uu_id: string;
|
||||
}
|
||||
|
||||
interface LoginSelectOccupant {
|
||||
selectedBuilding: any;
|
||||
build_part_uu_id: string;
|
||||
occupant_uu_id: string;
|
||||
}
|
||||
|
||||
async function loginViaAccessKeys(payload: LoginViaAccessKeys) {
|
||||
const cookieStore = await cookies();
|
||||
const nextCrypto = new NextCrypto(tokenSecret);
|
||||
|
||||
const tokenResponse: any = await fetchData(
|
||||
loginEndpoint,
|
||||
{
|
||||
domain: payload.domain,
|
||||
access_key: payload.accessKey,
|
||||
password: payload.password,
|
||||
remember_me: payload.rememberMe,
|
||||
},
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
|
||||
if (tokenResponse.status === 200) {
|
||||
const accessToken = await nextCrypto.encrypt(tokenResponse.access_token);
|
||||
const accessObject = await nextCrypto.encrypt(
|
||||
JSON.stringify(tokenResponse.access_object)
|
||||
);
|
||||
const userProfile = await nextCrypto.encrypt(
|
||||
JSON.stringify(tokenResponse.user)
|
||||
);
|
||||
const refreshToken = await nextCrypto.encrypt(tokenResponse.refresh_token);
|
||||
|
||||
// const userType = await nextCrypto.encrypt(responseData.user_type);
|
||||
// cookieStore.set({
|
||||
// name: "refreshToken",
|
||||
// value: refreshToken,
|
||||
// httpOnly: true,
|
||||
// path: "/",
|
||||
// });
|
||||
|
||||
cookieStore.set({
|
||||
name: "accessToken",
|
||||
value: accessToken,
|
||||
...cookieObject,
|
||||
});
|
||||
|
||||
cookieStore.set({
|
||||
name: "accessObject",
|
||||
value: accessObject,
|
||||
...cookieObject,
|
||||
});
|
||||
cookieStore.set({
|
||||
name: "userProfile",
|
||||
value: JSON.stringify(userProfile),
|
||||
...cookieObject,
|
||||
});
|
||||
// cookieStore.set({
|
||||
// name: "userType",
|
||||
// value: userType,
|
||||
// ...cookieObject,
|
||||
// });
|
||||
}
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
async function loginSelectEmployee(payload: LoginSelectEmployee) {
|
||||
const cookieStore = await cookies();
|
||||
const nextCrypto = new NextCrypto(tokenSecret);
|
||||
|
||||
const selectResponse: any = await fetchDataWithToken(
|
||||
loginSelectEndpoint,
|
||||
{
|
||||
company_uu_id: payload.company_uu_id,
|
||||
},
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
if (selectResponse.status === 200) {
|
||||
const usersSelection = await nextCrypto.encrypt(
|
||||
JSON.stringify({
|
||||
company_uu_id: payload.company_uu_id,
|
||||
user_type: "employee",
|
||||
})
|
||||
);
|
||||
cookieStore.set({
|
||||
name: "userSelection",
|
||||
value: usersSelection,
|
||||
...cookieObject,
|
||||
});
|
||||
await setAvailableEvents();
|
||||
}
|
||||
return selectResponse;
|
||||
}
|
||||
|
||||
async function loginSelectOccupant(payload: LoginSelectOccupant) {
|
||||
const selectedBuilding = payload.selectedBuilding;
|
||||
const cookieStore = await cookies();
|
||||
const nextCrypto = new NextCrypto(tokenSecret);
|
||||
const selectResponse: any = await fetchDataWithToken(
|
||||
loginSelectEndpoint,
|
||||
{
|
||||
build_part_uu_id: payload.build_part_uu_id,
|
||||
occupant_uu_id: payload.occupant_uu_id,
|
||||
},
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
|
||||
if (selectResponse.status === 200) {
|
||||
const usersSelection = await nextCrypto.encrypt(
|
||||
JSON.stringify({
|
||||
company_uu_id: {
|
||||
build_part_uu_id: payload.build_part_uu_id,
|
||||
occupant_uu_id: payload.occupant_uu_id,
|
||||
build_id: selectedBuilding,
|
||||
},
|
||||
user_type: "occupant",
|
||||
})
|
||||
);
|
||||
cookieStore.set({
|
||||
name: "userSelection",
|
||||
value: usersSelection,
|
||||
...cookieObject,
|
||||
});
|
||||
await setAvailableEvents();
|
||||
}
|
||||
return selectResponse;
|
||||
}
|
||||
|
||||
export { loginViaAccessKeys, loginSelectEmployee, loginSelectOccupant };
|
||||
54
apicalls/login/logout.tsx
Normal file
54
apicalls/login/logout.tsx
Normal file
@@ -0,0 +1,54 @@
|
||||
"use server";
|
||||
import { fetchDataWithToken } from "../api-fetcher";
|
||||
import { cookies } from "next/headers";
|
||||
import { baseUrl } from "../basics";
|
||||
|
||||
const logOutEndpoint = `${baseUrl}/authentication/logout`;
|
||||
const logOutAllEndpoint = `${baseUrl}/authentication/disconnect`;
|
||||
|
||||
interface LoginOutUser {
|
||||
domain: string;
|
||||
}
|
||||
|
||||
async function logoutActiveSession(payload: LoginOutUser) {
|
||||
const cookieStore = await cookies();
|
||||
cookieStore.delete("accessToken");
|
||||
cookieStore.delete("accessObject");
|
||||
cookieStore.delete("userProfile");
|
||||
cookieStore.delete("userSelection");
|
||||
|
||||
const tokenResponse: any = await fetchDataWithToken(
|
||||
logOutEndpoint,
|
||||
{
|
||||
domain: payload.domain,
|
||||
},
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
if (tokenResponse.status === 200) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async function logoutAllSessions(payload: LoginOutUser) {
|
||||
const cookieStore = await cookies();
|
||||
cookieStore.delete("accessToken");
|
||||
cookieStore.delete("accessObject");
|
||||
cookieStore.delete("userProfile");
|
||||
cookieStore.delete("userSelection");
|
||||
const tokenResponse: any = await fetchDataWithToken(
|
||||
logOutAllEndpoint,
|
||||
{
|
||||
domain: payload.domain,
|
||||
},
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
if (tokenResponse.status === 200) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export { logoutActiveSession, logoutAllSessions };
|
||||
69
apicalls/login/password.tsx
Normal file
69
apicalls/login/password.tsx
Normal file
@@ -0,0 +1,69 @@
|
||||
"use server";
|
||||
import { fetchData, fetchDataWithToken } from "../api-fetcher";
|
||||
import { baseUrl } from "../basics";
|
||||
|
||||
const createPasswordEndpoint = `${baseUrl}/authentication/create_password`;
|
||||
const changePasswordEndpoint = `${baseUrl}/authentication/change_password`;
|
||||
const forgotPasswordEndpoint = `${baseUrl}/authentication/reset_password`;
|
||||
|
||||
interface createPasswordViaToken {
|
||||
token: string;
|
||||
password: string;
|
||||
rePassword: string;
|
||||
}
|
||||
|
||||
interface changePasswordViaToken {
|
||||
oldPassword: string;
|
||||
newPassword: string;
|
||||
}
|
||||
|
||||
interface sendForgotPasswordEmail {
|
||||
domain: string;
|
||||
accessKey: string;
|
||||
}
|
||||
|
||||
async function create_password_via_token(payload: createPasswordViaToken) {
|
||||
const createPasswordResponse: any = await fetchData(
|
||||
createPasswordEndpoint,
|
||||
{
|
||||
password_token: payload.token,
|
||||
password: payload.password,
|
||||
re_password: payload.rePassword,
|
||||
},
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return createPasswordResponse;
|
||||
}
|
||||
|
||||
async function change_password_via_token(payload: changePasswordViaToken) {
|
||||
const changePasswordResponse: any = await fetchDataWithToken(
|
||||
changePasswordEndpoint,
|
||||
{
|
||||
old_password: payload.oldPassword,
|
||||
new_password: payload.newPassword,
|
||||
},
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return changePasswordResponse;
|
||||
}
|
||||
|
||||
async function send_forgot_password_email(payload: sendForgotPasswordEmail) {
|
||||
const response: any = await fetchData(
|
||||
forgotPasswordEndpoint,
|
||||
{
|
||||
domain: payload.domain,
|
||||
access_key: payload.accessKey,
|
||||
},
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return response;
|
||||
}
|
||||
|
||||
export {
|
||||
create_password_via_token,
|
||||
change_password_via_token,
|
||||
send_forgot_password_email,
|
||||
};
|
||||
52
apicalls/people/people.tsx
Normal file
52
apicalls/people/people.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
"use server";
|
||||
import { fetchDataWithToken, updateDataWithToken } from "../api-fetcher";
|
||||
import {
|
||||
baseUrl,
|
||||
FilterList,
|
||||
FilterListInterface,
|
||||
defaultFilterList,
|
||||
} from "../basics";
|
||||
|
||||
const peopleListEndpoint = `${baseUrl}/people/list`;
|
||||
const peopleCreateEndpoint = `${baseUrl}/people/create`;
|
||||
const peopleUpdateEndpoint = `${baseUrl}/people/update`;
|
||||
|
||||
interface PeopleUpdateInterface {
|
||||
uuid: string;
|
||||
payload: any;
|
||||
}
|
||||
|
||||
async function retrievePeopleList(payload: FilterListInterface) {
|
||||
const feedObject = new FilterList(payload).filter();
|
||||
const tokenResponse: any = await fetchDataWithToken(
|
||||
peopleListEndpoint,
|
||||
feedObject,
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
async function updatePeople(payload: any) {
|
||||
const { uu_id: extractedField, ...payloadBody } = payload;
|
||||
const tokenResponse: any = await updateDataWithToken(
|
||||
peopleUpdateEndpoint,
|
||||
extractedField,
|
||||
payloadBody,
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
async function createPeople(payload: any) {
|
||||
const tokenResponse: any = await fetchDataWithToken(
|
||||
peopleCreateEndpoint,
|
||||
payload,
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
return tokenResponse;
|
||||
}
|
||||
|
||||
export { retrievePeopleList, updatePeople, createPeople };
|
||||
43
apicalls/validations/validationProcesser.ts
Normal file
43
apicalls/validations/validationProcesser.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { console } from "inspector";
|
||||
|
||||
interface ValidationInterface {
|
||||
required: string[];
|
||||
title: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
interface HeadersAndValidationsInterface {
|
||||
headers: Object;
|
||||
validation: ValidationInterface;
|
||||
language: string;
|
||||
properties: Object;
|
||||
}
|
||||
|
||||
class HeadersAndValidations {
|
||||
headers: Object;
|
||||
validation: ValidationInterface;
|
||||
language: string;
|
||||
validated: any = {};
|
||||
|
||||
constructor({
|
||||
headers,
|
||||
validation,
|
||||
language,
|
||||
}: HeadersAndValidationsInterface) {
|
||||
this.headers = headers;
|
||||
this.validation = validation;
|
||||
this.language = language;
|
||||
this.parseProcesser();
|
||||
}
|
||||
|
||||
parseProcesser() {
|
||||
Object.entries(this.validation).map(([key, value]) => {
|
||||
this.validated[key] = {
|
||||
required: !value.required,
|
||||
fieldType: value?.type,
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export { HeadersAndValidations };
|
||||
67
apicalls/validations/validations.tsx
Normal file
67
apicalls/validations/validations.tsx
Normal file
@@ -0,0 +1,67 @@
|
||||
"use server";
|
||||
import { fetchData, fetchDataWithToken } from "@/apicalls/api-fetcher";
|
||||
import { baseUrl, cookieObject, tokenSecret } from "@/apicalls/basics";
|
||||
|
||||
import { HeadersAndValidations } from "@/apicalls/validations/validationProcesser";
|
||||
|
||||
const headersAndValidationEndpoint = `${baseUrl}/validations/endpoint`;
|
||||
|
||||
interface EndpointInterface {
|
||||
endpoint: string;
|
||||
}
|
||||
|
||||
async function retrieveHeadersEndpoint({ endpoint }: EndpointInterface) {
|
||||
const selectResponse: any = await fetchDataWithToken(
|
||||
headersAndValidationEndpoint,
|
||||
{
|
||||
endpoint: endpoint,
|
||||
},
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
if (selectResponse.status === 200) {
|
||||
return {
|
||||
status: selectResponse.status,
|
||||
headers: selectResponse?.headers,
|
||||
message: selectResponse.message,
|
||||
};
|
||||
}
|
||||
return {
|
||||
status: selectResponse.status,
|
||||
headers: {},
|
||||
message: selectResponse.message,
|
||||
};
|
||||
}
|
||||
|
||||
async function retrieveHeadersAndValidationByEndpoint({
|
||||
endpoint,
|
||||
}: EndpointInterface) {
|
||||
console.log("endpoint", endpoint);
|
||||
const selectResponse: any = await fetchDataWithToken(
|
||||
headersAndValidationEndpoint,
|
||||
{
|
||||
endpoint: endpoint,
|
||||
},
|
||||
"POST",
|
||||
false
|
||||
);
|
||||
if (selectResponse.status === 200) {
|
||||
const responseParsed = new HeadersAndValidations(selectResponse);
|
||||
return {
|
||||
status: selectResponse.status,
|
||||
headers: responseParsed.headers,
|
||||
validated: responseParsed.validated,
|
||||
language: responseParsed.language,
|
||||
message: selectResponse.message,
|
||||
};
|
||||
}
|
||||
return {
|
||||
status: selectResponse.status,
|
||||
message: selectResponse.message,
|
||||
|
||||
headers: null,
|
||||
validated: null,
|
||||
};
|
||||
}
|
||||
|
||||
export { retrieveHeadersAndValidationByEndpoint, retrieveHeadersEndpoint };
|
||||
Reference in New Issue
Block a user