Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/server/src/modules/system/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) => {
Expand Down Expand Up @@ -72,6 +74,7 @@ export const System = (() => {

await $email.load();
if (isProduction) await $backups.load();
if (!testMode) await $onet.load();

$api.load(testMode);
};
Expand Down Expand Up @@ -106,5 +109,6 @@ export const System = (() => {
accounts: $accounts,
hotels: $hotels,
backups: $backups,
onet: $onet,
};
})();
66 changes: 66 additions & 0 deletions app/server/src/modules/system/onet.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>;
};

type FetchResponse<Data> = {
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 <Data>({
method = RequestMethod.GET,
pathname,
data,
body,
headers = {},
}: FetchProps): Promise<FetchResponse<Data>> => {
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,
};
};
5 changes: 5 additions & 0 deletions app/server/src/shared/consts/config.consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ export const CONFIG_DEFAULT: ConfigTypes = {
notifications: {
discord: false,
},
onet: {
enabled: false,
api: "http://localhost:9400/api/v1",
token: "",
},
};
5 changes: 5 additions & 0 deletions app/server/src/shared/types/config.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ export type ConfigTypes = {
notifications: {
discord: string | false;
};
onet: {
enabled: boolean;
api: string;
token: string;
};
};
Loading