diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index 69945064..def2a43a 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -2,6 +2,31 @@ import { error, fail, redirect } from "@sveltejs/kit"; import { validateData } from "$lib/utils"; import { loginUserSchema, registerUserSchema } from "$lib/schemas"; +export const load = async ({ locals, setHeaders }) => { + const { apiKey, apiURL } = locals; + + const getDashboard = async () => { + const response = await fetch(apiURL + "/dashboard-info", { + method: "GET", + headers: { + "Content-Type": "application/json", + "X-API-KEY": apiKey, + }, + }); + + const output = await response?.json(); + + setHeaders({ "cache-control": "public, max-age=300" }); + + return output; + }; + + // Make sure to return a promise + return { + getDashboard: await getDashboard(), + }; +}; + async function checkDisposableEmail(email) { const url = `https://disposable.debounce.io/?email=${encodeURIComponent(email)}`; const response = await fetch(url, { @@ -18,7 +43,7 @@ export const actions = { login: async ({ request, locals }) => { const { formData, errors } = await validateData( await request.formData(), - loginUserSchema, + loginUserSchema ); if (errors) { @@ -52,7 +77,7 @@ export const actions = { register: async ({ locals, request }) => { const { formData, errors } = await validateData( await request.formData(), - registerUserSchema, + registerUserSchema ); if (errors) { return fail(400, { @@ -122,7 +147,7 @@ export const actions = { const redirectURL = `${url.origin}/oauth`; const targetItem = authMethods.authProviders?.findIndex( - (item) => item?.name === providerSelected, + (item) => item?.name === providerSelected ); //console.log("==================") //console.log(authMethods.authProviders) diff --git a/src/routes/+page.ts b/src/routes/+page.ts deleted file mode 100644 index c553a5ba..00000000 --- a/src/routes/+page.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { getCache, setCache } from "$lib/store"; - -export const load = async ({ parent }) => { - const { apiKey, apiURL } = await parent(); - - const getDashboard = async () => { - // Get cached data for the specific tickerID - const cachedData = getCache("", "getDashboard"); - if (cachedData) { - return cachedData; - } else { - const response = await fetch(apiURL + "/dashboard-info", { - method: "GET", - headers: { - "Content-Type": "application/json", - "X-API-KEY": apiKey, - }, - }); - - const output = await response?.json(); - setCache("", output, "getDashboard"); - return output; - } - }; - - // Make sure to return a promise - return { - getDashboard: await getDashboard(), - }; -}; diff --git a/src/routes/stock-screener/+page.server.ts b/src/routes/stock-screener/+page.server.ts index ef693fd6..aadfd145 100644 --- a/src/routes/stock-screener/+page.server.ts +++ b/src/routes/stock-screener/+page.server.ts @@ -2,11 +2,79 @@ import { error, fail, redirect } from "@sveltejs/kit"; import { validateData } from "$lib/utils"; import { loginUserSchema, registerUserSchema } from "$lib/schemas"; +// Define the EMA parameters to check +const emaParameters = ["ema20", "ema50", "ema100", "ema200"]; +// Function to check and add missing EMA parameters +const ensureAllEmaParameters = (params) => { + const includedEmaParameters = params.filter((param) => + emaParameters.includes(param) + ); + if (includedEmaParameters.length > 0) { + emaParameters.forEach((param) => { + if (!params.includes(param)) { + params.push(param); + } + }); + } +}; + +export const load = async ({ locals }) => { + const { apiURL, apiKey, fastifyURL, user } = locals; + + const getAllStrategies = async () => { + const postData = { userId: user?.id }; + const response = await fetch(fastifyURL + "/all-strategies", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(postData), + }); + + const output = (await response.json())?.items; + + output?.sort((a, b) => new Date(b?.updated) - new Date(a?.updated)); + + return output; + }; + + const getStockScreenerData = async () => { + const strategyList = await getAllStrategies(); + + const strategy = strategyList?.at(0); + let getRuleOfList = strategy?.rules?.map((item) => item?.name) || []; + + // Ensure all required EMA parameters are included + ensureAllEmaParameters(getRuleOfList); + + const postData = { ruleOfList: getRuleOfList }; + // make the POST request to the endpoint + const response = await fetch(apiURL + "/stock-screener-data", { + method: "POST", + headers: { + "Content-Type": "application/json", + "X-API-KEY": apiKey, + }, + body: JSON.stringify(postData), + }); + + const output = await response.json(); + + return output; + }; + + // Make sure to return a promise + return { + getStockScreenerData: await getStockScreenerData(), + getAllStrategies: await getAllStrategies(), + }; +}; + export const actions = { login: async ({ request, locals }) => { const { formData, errors } = await validateData( await request.formData(), - loginUserSchema, + loginUserSchema ); if (errors) { @@ -40,7 +108,7 @@ export const actions = { register: async ({ locals, request }) => { const { formData, errors } = await validateData( await request.formData(), - registerUserSchema, + registerUserSchema ); if (errors) { @@ -94,7 +162,7 @@ await locals.pb?.collection('users').update( const redirectURL = `${url.origin}/oauth`; const targetItem = authMethods.authProviders?.findIndex( - (item) => item?.name === providerSelected, + (item) => item?.name === providerSelected ); //console.log("==================") //console.log(authMethods.authProviders) diff --git a/src/routes/stock-screener/+page.ts b/src/routes/stock-screener/+page.ts deleted file mode 100644 index 03c1d467..00000000 --- a/src/routes/stock-screener/+page.ts +++ /dev/null @@ -1,83 +0,0 @@ -import { getCache, setCache } from "$lib/store"; - -// Define the EMA parameters to check -const emaParameters = ["ema20", "ema50", "ema100", "ema200"]; -// Function to check and add missing EMA parameters -const ensureAllEmaParameters = (params) => { - const includedEmaParameters = params.filter((param) => - emaParameters.includes(param), - ); - if (includedEmaParameters.length > 0) { - emaParameters.forEach((param) => { - if (!params.includes(param)) { - params.push(param); - } - }); - } -}; - -export const load = async ({ parent }) => { - const { apiURL, apiKey, fastifyURL, user } = await parent(); - - const getAllStrategies = async () => { - const postData = { userId: user?.id }; - const response = await fetch(fastifyURL + "/all-strategies", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(postData), - }); - - const output = (await response.json())?.items; - - output?.sort((a, b) => new Date(b?.updated) - new Date(a?.updated)); - - //console.log(output); - - return output; - }; - - const getStockScreenerData = async () => { - let output; - const strategyList = await getAllStrategies(); - - const strategy = strategyList?.at(0); - let getRuleOfList = strategy?.rules?.map((item) => item?.name) || []; - - // Ensure all required EMA parameters are included - ensureAllEmaParameters(getRuleOfList); - - const ruleNames = getRuleOfList.sort().join(","); - - // Get cached data for the specific tickerID - const cachedData = getCache(ruleNames, "getStockScreenerData"); - if (cachedData) { - output = cachedData; - } else { - const postData = { ruleOfList: getRuleOfList }; - // make the POST request to the endpoint - const response = await fetch(apiURL + "/stock-screener-data", { - method: "POST", - headers: { - "Content-Type": "application/json", - "X-API-KEY": apiKey, - }, - body: JSON.stringify(postData), - }); - - output = await response.json(); - - // Cache the data for this specific tickerID with a specific name 'getStockScreenerData' - setCache(ruleNames, output, "getStockScreenerData"); - } - - return output; - }; - - // Make sure to return a promise - return { - getStockScreenerData: await getStockScreenerData(), - getAllStrategies: await getAllStrategies(), - }; -};