Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement teleport between hotels - fix #35 #39

Merged
merged 2 commits into from
Dec 12, 2024
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ node_modules
.idea
.vite
config.yml
build
build
database
database-wal
database-shm
6 changes: 3 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
},
"imports": {
"@oh/queue": "jsr:@oh/[email protected]",
"@oh/utils": "jsr:@oh/[email protected].10",
"@oh/utils": "jsr:@oh/[email protected].23",

"@da/bcrypt": "jsr:@da/[email protected]",
"@da/socket": "jsr:@da/[email protected]",

"modules/": "./src/modules/",
"shared/": "./src/shared/",

"loadenv": "https://deno.land/x/[email protected]/mod.ts",

"dayjs": "https://deno.land/x/[email protected]/mod.ts",
"bcrypt": "https://deno.land/x/[email protected]/mod.ts"
"dayjs": "https://deno.land/x/[email protected]/mod.ts"
},
"nodeModulesDir": "auto"
}
20 changes: 13 additions & 7 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/modules/api/v1/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { RequestType, getPathRequestList } from "@oh/utils";

import { versionRequest } from "./version.request.ts";

import { teleportsRequestList } from "./teleports/main.ts";

export const requestV1List: RequestType<unknown>[] = getPathRequestList({
requestList: [versionRequest],
requestList: [versionRequest, ...teleportsRequestList],
pathname: "/api/v1",
});
33 changes: 33 additions & 0 deletions src/modules/api/v1/teleports/get.request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
getResponse,
HttpStatusCode,
RequestMethod,
RequestType,
} from "@oh/utils";
import {
getHotelFromRequest,
isRequestAuthenticated,
} from "shared/utils/request.utils.ts";
import { System } from "modules/system/main.ts";
import { Scope } from "shared/enums/scope.enums.ts";

export const getRequest: RequestType<unknown> = {
method: RequestMethod.GET,
pathname: "/get",
func: async (request, url) => {
if (!isRequestAuthenticated(request))
return getResponse(HttpStatusCode.FORBIDDEN);

const teleportId = url.searchParams.get("teleportId");

if (!teleportId) return getResponse(HttpStatusCode.FORBIDDEN);

const teleport = await System.teleports.get(teleportId);

if (!teleport) return getResponse(HttpStatusCode.NOT_FOUND);

return getResponse(HttpStatusCode.OK, {
teleport,
});
},
};
40 changes: 40 additions & 0 deletions src/modules/api/v1/teleports/link.request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
getResponse,
HttpStatusCode,
RequestMethod,
RequestType,
} from "@oh/utils";
import {
getHotelFromRequest,
isRequestAuthenticated,
} from "shared/utils/request.utils.ts";
import { System } from "modules/system/main.ts";
import { Scope } from "shared/enums/scope.enums.ts";

export const linkRequest: RequestType<unknown> = {
method: RequestMethod.POST,
pathname: "/link",
func: async (request) => {
if (!isRequestAuthenticated(request))
return getResponse(HttpStatusCode.FORBIDDEN);

const { accountId, linkId, teleportId } = await request.json();

if (!accountId || !linkId || !teleportId)
return getResponse(HttpStatusCode.FORBIDDEN);

const hotel = getHotelFromRequest(request);

if (!(await hotel.hasAccountScopes(accountId, [Scope.ONET_TELEPORTS_LINK])))
return getResponse(HttpStatusCode.FORBIDDEN);

const isValid = await System.teleports.link(
linkId,
teleportId,
hotel.getHotelId(),
hotel.getIntegrationId(),
);

return getResponse(isValid ? HttpStatusCode.OK : HttpStatusCode.CONFLICT);
},
};
9 changes: 9 additions & 0 deletions src/modules/api/v1/teleports/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { RequestType, getPathRequestList } from "@oh/utils";

import { linkRequest } from "./link.request.ts";
import { getRequest } from "./get.request.ts";

export const teleportsRequestList: RequestType<unknown>[] = getPathRequestList({
requestList: [linkRequest, getRequest],
pathname: "/teleports",
});
14 changes: 9 additions & 5 deletions src/modules/api/v1/version.request.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { RequestType, RequestMethod } from "@oh/utils";
import {
RequestType,
RequestMethod,
getResponse,
HttpStatusCode,
} from "@oh/utils";
import { System } from "modules/system/main.ts";

export const versionRequest: RequestType<unknown> = {
method: RequestMethod.GET,
pathname: "/version",
func: (request, url) => {
return Response.json(
{ version: System.getEnvs().version },
{ status: 200 },
);
return getResponse(HttpStatusCode.OK, {
data: { version: System.getEnvs().version },
});
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Migration, DbMutable } from "@oh/utils";

export default {
id: "2024-12-12--15-17-remove-me",
description: "Initial test migration",
up: async (db: DbMutable) => {},
down: async (db: DbMutable) => {},
} as Migration;
15 changes: 15 additions & 0 deletions src/modules/migrations/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DbMutable } from "@oh/utils";

const MIGRATION_LIST = [
await import("./2024-12-12--15-17-remove-me.migration.ts"),
];

export const Migrations = (() => {
const load = async (db: DbMutable) => {
await db.migrations.load(MIGRATION_LIST.map((module) => module.default));
};

return {
load,
};
})();
4 changes: 2 additions & 2 deletions src/modules/system/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { System } from "modules/system/main.ts";
import { RequestMethod } from "@oh/utils";

type FetchProps = {
method: RequestMethod;
method?: RequestMethod;
pathname: string;
data?: unknown;
overrideEnabled?: boolean;
Expand Down Expand Up @@ -38,7 +38,7 @@ export const auth = () => {
};

const $fetch = async <Data>({
method,
method = RequestMethod.GET,
pathname,
data,
headers = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Event } from "shared/enums/event.enums.ts";

export const welcomeEvent: EventType<any> = {
event: Event.WELCOME,
func: ({ server, data }) => {
func: ({ hotel, data }) => {
console.log(
`>: welcome '${server.getHotelData().name}' (${server.getHotelId()})`,
`>: welcome '${hotel.getHotelData().name}' (${hotel.getHotelId()})`,
data,
);
},
Expand Down
116 changes: 116 additions & 0 deletions src/modules/system/hotels/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { System } from "modules/system/main.ts";
import { Hotel, HotelMutable } from "shared/types/hotel.types.ts";
import { eventList } from "./events/main.ts";
import { Event } from "shared/enums/event.enums.ts";
import { getRandomString } from "@oh/utils";
import * as bcrypt from "@da/bcrypt";
import { Scope } from "shared/enums/scope.enums.ts";

export const hotels = () => {
const $hotelMap: Record<string, HotelMutable> = {};
const $hotelApiTokenMap: Record<string, string> = {};

const $getHotel = (hotel: Hotel): HotelMutable | null => {
if (!hotel) return null;
const $hotel: Hotel = { ...hotel };

const load = () => {
for (const { event, func } of eventList)
getSocket().on(event, (data: any) =>
func({ hotel: $hotelMap[$hotel.hotelId], data }),
);

const apiToken = getRandomString(32) as string;
$hotelApiTokenMap[$hotel.hotelId] = bcrypt.hashSync(
apiToken,
bcrypt.genSaltSync(8),
);

emit(Event.WELCOME, { apiToken });
};

const getSocket = () => System.serverSocket.getClient($hotel.clientId);
const emit = <Data extends unknown>(
event: Event,
data: Data = {} as Data,
) => getSocket().emit(event, data);

const getHotelData = () => $hotel.hotelData;

const getHotelId = () => $hotel.hotelId;
const getIntegrationId = () => $hotel.integrationId;

const getToken = () => $hotel.licenseToken;

const verify = (apiToken: string): boolean =>
bcrypt.compareSync(apiToken, $hotelApiTokenMap[$hotel.hotelId]);

const hasAccountScopes = async (
accountId: string,
scopes: Scope[],
): Promise<boolean> => {
const { connection } = await System.auth.fetch<any>({
pathname: `/tokens/user/connection?accountId=${accountId}`,
});
return (
connection &&
connection.hotelId === getHotelId() &&
scopes.every((scope) => connection.scopes.includes(scope))
);
};

const getObject = () => $hotel;

return {
load,

getSocket,
emit,

getHotelData,

getHotelId,
getIntegrationId,

getToken,

verify,

hasAccountScopes,

getObject,
};
};

const add = (server: Hotel) => {
$hotelMap[server.hotelId] = $getHotel(server)!;
};

const remove = (server: Hotel) => {
const $server = $hotelMap[server.hotelId];
if (!$server) return;

delete $hotelMap[server.hotelId];
};

const get = ({
clientId,
hotelId,
}: Partial<Pick<Hotel, "hotelId" | "clientId">>) => {
if (hotelId) return $hotelMap[hotelId];
if (clientId)
return getList().find(
(server) => server.getObject().clientId === clientId,
);
return null;
};

const getList = () => Object.values($hotelMap);

return {
add,
get,
getList,
remove,
};
};
Loading
Loading