diff --git a/app/server/src/modules/system/main.ts b/app/server/src/modules/system/main.ts index 20e0749..6fac667 100644 --- a/app/server/src/modules/system/main.ts +++ b/app/server/src/modules/system/main.ts @@ -10,6 +10,7 @@ import { Migrations } from "modules/migrations/main.ts"; import { hotels } from "./hotels/main.ts"; import { backups } from "modules/system/backups.ts"; import { apps } from "modules/system/apps.ts"; +import { onet } from "modules/system/onet.ts"; export const System = (() => { let $config: ConfigTypes; @@ -25,6 +26,7 @@ export const System = (() => { const $accounts = accounts(); const $hotels = hotels(); const $backups = backups(); + const $onet = onet(); let $db: DbMutable; const load = async (envs: Envs, testMode: boolean = false) => { @@ -72,6 +74,7 @@ export const System = (() => { await $email.load(); if (isProduction) await $backups.load(); + if (!testMode) await $onet.load(); $api.load(testMode); }; @@ -106,5 +109,6 @@ export const System = (() => { accounts: $accounts, hotels: $hotels, backups: $backups, + onet: $onet, }; })(); diff --git a/app/server/src/modules/system/onet.ts b/app/server/src/modules/system/onet.ts new file mode 100644 index 0000000..48fdcee --- /dev/null +++ b/app/server/src/modules/system/onet.ts @@ -0,0 +1,66 @@ +import { System } from "modules/system/main.ts"; +import { RequestMethod } from "@oh/utils"; + +type FetchProps = { + method?: RequestMethod; + pathname: string; + data?: unknown; + body?: BodyInit; + headers?: Record; +}; + +type FetchResponse = { + status: number; + data: Data; +}; + +export const onet = () => { + const load = async () => { + const { enabled, api, token } = System.getConfig().onet; + + if (!enabled || !api || !token) { + console.warn("Onet is disabled!"); + return; + } + + try { + const { status, data } = await $fetch<{ valid: boolean }>({ + pathname: "/auth/check", + }); + + if (status !== 200 || !data?.valid) { + console.error("Onet token is not valid!"); + } + } catch (e) { + console.error("Onet service is down!"); + console.error(e); + } + }; + + const $fetch = async ({ + method = RequestMethod.GET, + pathname, + data, + body, + headers = {}, + }: FetchProps): Promise> => { + const { api, token } = System.getConfig().onet; + + const response = await fetch(`${api}${pathname}`, { + method, + body: body ?? (data !== undefined ? JSON.stringify(data) : null), + headers: new Headers({ + ...headers, + "app-token": token, + }), + }); + + const json = await response.json().catch(() => ({})); + return { status: response.status, data: json?.data as Data }; + }; + + return { + load, + fetch: $fetch, + }; +}; diff --git a/app/server/src/shared/consts/config.consts.ts b/app/server/src/shared/consts/config.consts.ts index 5a83f72..809bbfa 100644 --- a/app/server/src/shared/consts/config.consts.ts +++ b/app/server/src/shared/consts/config.consts.ts @@ -54,4 +54,9 @@ export const CONFIG_DEFAULT: ConfigTypes = { notifications: { discord: false, }, + onet: { + enabled: false, + api: "http://localhost:9400/api/v1", + token: "", + }, }; diff --git a/app/server/src/shared/types/config.types.ts b/app/server/src/shared/types/config.types.ts index c2d5bae..27d4fa3 100644 --- a/app/server/src/shared/types/config.types.ts +++ b/app/server/src/shared/types/config.types.ts @@ -52,4 +52,9 @@ export type ConfigTypes = { notifications: { discord: string | false; }; + onet: { + enabled: boolean; + api: string; + token: string; + }; };