diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 555cb5f2..9149e859 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -1,57 +1,82 @@ import PocketBase from "pocketbase"; -import { serializeNonPOJOs } from '$lib/utils'; +import { serializeNonPOJOs } from "$lib/utils"; -const usRegion = new Set(['cle1', 'iad1', 'pdx1', 'sfo1']); +const usRegion = new Set(["cle1", "iad1", "pdx1", "sfo1"]); export const handle = async ({ event, resolve }) => { - // Use optional chaining and nullish coalescing for safer property access - const regionHeader = event?.request?.headers?.get('x-vercel-id') ?? 'fra1::fra1::8t4xg-1700258428633-157d82fdfcc7'; - - // Use a more compatible way to get the first element of the split array - const userRegion = regionHeader.split('::')[0] || ''; + // Use optional chaining and nullish coalescing for safer property access + const regionHeader = + event?.request?.headers?.get("x-vercel-id") ?? + "fra1::fra1::8t4xg-1700258428633-157d82fdfcc7"; - const isUsRegion = usRegion.has(userRegion); + const ip = + event.request.headers.get("x-forwarded-for") || + event.request.headers.get("remote-address"); - // Use a ternary operator instead of the logical OR for better compatibility - const pbURL = isUsRegion ? import.meta.env.VITE_USEAST_POCKETBASE_URL : import.meta.env.VITE_EU_POCKETBASE_URL; - const apiURL = isUsRegion ? import.meta.env.VITE_USEAST_API_URL : import.meta.env.VITE_EU_API_URL; - const fastifyURL = isUsRegion ? import.meta.env.VITE_USEAST_FASTIFY_URL : import.meta.env.VITE_EU_FASTIFY_URL; - const wsURL = isUsRegion ? import.meta.env.VITE_USEAST_WS_URL : import.meta.env.VITE_EU_WS_URL; + let isUS = false; - event.locals = { - region: decodeURIComponent(regionHeader), - pb: new PocketBase(pbURL), - apiURL, - fastifyURL, - wsURL, - apiKey: import.meta.env.VITE_STOCKNEAR_API_KEY - }; - - const authCookie = event?.request?.headers?.get('cookie') || ''; - event.locals.pb.authStore.loadFromCookie(authCookie); - - if (event?.locals?.pb?.authStore?.isValid) { - try { - await event.locals.pb.collection('users').authRefresh(); - event.locals.user = serializeNonPOJOs(event.locals.pb.authStore.model); - } catch (_) { - event.locals.pb.authStore.clear(); - event.locals.user = undefined; - } + if (ip) { + const geoResponse = await fetch(`https://ipinfo.io/${ip}/geo`); + const geoData = await geoResponse.json(); + if (geoData.country === "US") { + isUS = true; + console.log("yelllo", geoData); } + } - const response = await resolve(event); + // Use a more compatible way to get the first element of the split array + const userRegion = regionHeader.split("::")[0] || ""; - // Use a more compatible way to set the cookie - const cookieString = event.locals.pb.authStore.exportToCookie({ - httpOnly: true, - path: '/', - sameSite: 'lax', - secure: true, - maxAge: 60 * 60 * 24 * 365 - }); + const isUsRegion = usRegion.has(userRegion); - response.headers.append('set-cookie', cookieString); + // Use a ternary operator instead of the logical OR for better compatibility + const pbURL = isUsRegion + ? import.meta.env.VITE_USEAST_POCKETBASE_URL + : import.meta.env.VITE_EU_POCKETBASE_URL; + const apiURL = isUsRegion + ? import.meta.env.VITE_USEAST_API_URL + : import.meta.env.VITE_EU_API_URL; + const fastifyURL = isUsRegion + ? import.meta.env.VITE_USEAST_FASTIFY_URL + : import.meta.env.VITE_EU_FASTIFY_URL; + const wsURL = isUsRegion + ? import.meta.env.VITE_USEAST_WS_URL + : import.meta.env.VITE_EU_WS_URL; - return response; -}; \ No newline at end of file + event.locals = { + region: decodeURIComponent(regionHeader), + pb: new PocketBase(pbURL), + apiURL, + fastifyURL, + wsURL, + apiKey: import.meta.env.VITE_STOCKNEAR_API_KEY, + }; + + const authCookie = event?.request?.headers?.get("cookie") || ""; + event.locals.pb.authStore.loadFromCookie(authCookie); + + if (event?.locals?.pb?.authStore?.isValid) { + try { + await event.locals.pb.collection("users").authRefresh(); + event.locals.user = serializeNonPOJOs(event.locals.pb.authStore.model); + } catch (_) { + event.locals.pb.authStore.clear(); + event.locals.user = undefined; + } + } + + const response = await resolve(event); + + // Use a more compatible way to set the cookie + const cookieString = event.locals.pb.authStore.exportToCookie({ + httpOnly: true, + path: "/", + sameSite: "lax", + secure: true, + maxAge: 60 * 60 * 24 * 365, + }); + + response.headers.append("set-cookie", cookieString); + + return response; +}; diff --git a/src/routes/api/options-historical-flow/+server.ts b/src/routes/api/options-historical-flow/+server.ts index d5b1d212..7bc22003 100644 --- a/src/routes/api/options-historical-flow/+server.ts +++ b/src/routes/api/options-historical-flow/+server.ts @@ -3,23 +3,27 @@ import type { RequestHandler } from "./$types"; export const POST: RequestHandler = async ({ request, locals }) => { const data = await request.json(); const selectedDate = data?.selectedDate; - const { apiURL, apiKey } = locals; + const { apiURL, apiKey, user } = locals; let output; - try { - const postData = { date: selectedDate }; - const response = await fetch(apiURL + "/options-historical-flow", { - method: "POST", - headers: { - "Content-Type": "application/json", - "X-API-KEY": apiKey, - }, - body: JSON.stringify(postData), - }); + if (user?.tier === "Pro") { + try { + const postData = { date: selectedDate }; + const response = await fetch(apiURL + "/options-historical-flow", { + method: "POST", + headers: { + "Content-Type": "application/json", + "X-API-KEY": apiKey, + }, + body: JSON.stringify(postData), + }); - output = await response.json(); - } catch (e) { - console.error(e); + output = await response.json(); + } catch (e) { + console.error(e); + output = []; + } + } else { output = []; } diff --git a/src/routes/options-flow/+page.svelte b/src/routes/options-flow/+page.svelte index d0795d42..620fff10 100644 --- a/src/routes/options-flow/+page.svelte +++ b/src/routes/options-flow/+page.svelte @@ -794,7 +794,7 @@ $: { {#if !$isOpen}
- Live flow of {nyseDate} (NYSE Time) + Live flow of {data?.user?.tier === 'Pro' && selectedDate ? df.format(selectedDate?.toDate()) : nyseDate} (NYSE Time)
{/if} diff --git a/src/routes/options-flow/+page.ts b/src/routes/options-flow/+page.ts index 3e63fab1..821cf56b 100644 --- a/src/routes/options-flow/+page.ts +++ b/src/routes/options-flow/+page.ts @@ -1,35 +1,57 @@ -import { isOpen} from '$lib/store'; - +import { isOpen } from "$lib/store"; const checkMarketHour = async () => { - const holidays = ['2024-01-01', '2024-01-15','2024-02-19','2024-03-29','2024-05-27','2024-06-19','2024-07-04','2024-09-02','2024-11-28','2024-12-25']; - const currentDate = new Date().toISOString().split('T')[0]; + const holidays = [ + "2024-01-01", + "2024-01-15", + "2024-02-19", + "2024-03-29", + "2024-05-27", + "2024-06-19", + "2024-07-04", + "2024-09-02", + "2024-11-28", + "2024-12-25", + ]; + const currentDate = new Date().toISOString().split("T")[0]; // Get the current time in the ET time zone - const etTimeZone = 'America/New_York'; - const currentTime = new Date().toLocaleString('en-US', { timeZone: etTimeZone }); + const etTimeZone = "America/New_York"; + const currentTime = new Date().toLocaleString("en-US", { + timeZone: etTimeZone, + }); // Determine if the NYSE is currently open or closed const currentHour = new Date(currentTime).getHours(); - const isWeekendValue = new Date(currentTime).getDay() === 6 || new Date(currentTime).getDay() === 0; - const isBeforeMarketOpenValue = currentHour < 9 || (currentHour === 9 && new Date(currentTime).getMinutes() < 30); + const isWeekendValue = + new Date(currentTime).getDay() === 6 || + new Date(currentTime).getDay() === 0; + const isBeforeMarketOpenValue = + currentHour < 9 || + (currentHour === 9 && new Date(currentTime).getMinutes() < 30); const isAfterMarketCloseValue = currentHour >= 16; - isOpen.set(!(isWeekendValue || isBeforeMarketOpenValue || isAfterMarketCloseValue || holidays?.includes(currentDate))); -} + isOpen.set( + !( + isWeekendValue || + isBeforeMarketOpenValue || + isAfterMarketCloseValue || + holidays?.includes(currentDate) + ) + ); +}; - - -export const load = async ({parent}) => { +export const load = async ({ parent }) => { await checkMarketHour(); - const { apiURL, apiKey} = await parent(); + const { apiURL, apiKey } = await parent(); const getOptionsFlowFeed = async () => { // make the POST request to the endpoint - const response = await fetch(apiURL + '/options-flow-feed', { - method: 'GET', + const response = await fetch(apiURL + "/options-flow-feed", { + method: "GET", headers: { - "Content-Type": "application/json", "X-API-KEY": apiKey + "Content-Type": "application/json", + "X-API-KEY": apiKey, }, }); const output = await response.json(); @@ -41,4 +63,4 @@ export const load = async ({parent}) => { return { getOptionsFlowFeed: await getOptionsFlowFeed(), }; -}; \ No newline at end of file +};