Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+43
@@ -0,0 +1,43 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { CatalogError } from "@hivegrid/foundation";
|
||||
|
||||
import {
|
||||
TELEPHONY_PERMISSIONS,
|
||||
canReadAllTelephonyCalls,
|
||||
requireTelephonyCallVisibility,
|
||||
requireTelephonyPermission,
|
||||
telephonyHasPermission,
|
||||
type TelephonyRouteContext,
|
||||
} from "./telephonyAccess.server";
|
||||
|
||||
function context(permissions: readonly string[], userId = "user-1"): TelephonyRouteContext {
|
||||
return { tenantId: "tenant-1", userId, tenantUserId: `membership-${userId}`, permissions, hasModule: true };
|
||||
}
|
||||
|
||||
describe("telephony access", () => {
|
||||
it("lets manage cover every telephony capability", () => {
|
||||
const ctx = context([TELEPHONY_PERMISSIONS.manage]);
|
||||
expect(telephonyHasPermission(ctx, TELEPHONY_PERMISSIONS.readOwn)).toBe(true);
|
||||
expect(telephonyHasPermission(ctx, TELEPHONY_PERMISSIONS.readAll)).toBe(true);
|
||||
expect(telephonyHasPermission(ctx, TELEPHONY_PERMISSIONS.call)).toBe(true);
|
||||
});
|
||||
|
||||
it("lets read_all cover read_own without granting call or manage", () => {
|
||||
const ctx = context([TELEPHONY_PERMISSIONS.readAll]);
|
||||
expect(telephonyHasPermission(ctx, TELEPHONY_PERMISSIONS.readOwn)).toBe(true);
|
||||
expect(canReadAllTelephonyCalls(ctx)).toBe(true);
|
||||
expect(telephonyHasPermission(ctx, TELEPHONY_PERMISSIONS.call)).toBe(false);
|
||||
expect(telephonyHasPermission(ctx, TELEPHONY_PERMISSIONS.manage)).toBe(false);
|
||||
});
|
||||
|
||||
it("allows an own-call reader to see only their bound calls", () => {
|
||||
const ctx = context([TELEPHONY_PERMISSIONS.readOwn]);
|
||||
expect(requireTelephonyCallVisibility(ctx, "membership-user-1")).toBe(ctx);
|
||||
expect(() => requireTelephonyCallVisibility(ctx, "membership-user-2")).toThrow(CatalogError);
|
||||
expect(() => requireTelephonyCallVisibility(ctx, null)).toThrow(CatalogError);
|
||||
});
|
||||
|
||||
it("returns the not-found catalog error for a missing capability", () => {
|
||||
expect(() => requireTelephonyPermission(context([]), TELEPHONY_PERMISSIONS.call)).toThrow(CatalogError);
|
||||
});
|
||||
});
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
import { CatalogError, foundationErrors, requirePermission } from "@hivegrid/foundation";
|
||||
|
||||
import { tenantHasModule } from "~/utils/connector/tenantConnector.server";
|
||||
import { getPermissionsByUser } from "~/utils/db/permissions/userRoles.db.server";
|
||||
import { withTenantRLS } from "~/utils/db/.server/tenantRls.server";
|
||||
import { requireAuth } from "~/utils/loaders.middleware";
|
||||
import { getUserInfo } from "~/utils/session.server";
|
||||
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
||||
|
||||
import { TELEPHONY_MODULE_KEY } from "./telephonyRoutePaths";
|
||||
|
||||
export const TELEPHONY_PERMISSIONS = {
|
||||
readOwn: "telephony.read_own",
|
||||
readAll: "telephony.read_all",
|
||||
call: "telephony.call",
|
||||
manage: "telephony.manage",
|
||||
} as const;
|
||||
|
||||
export type TelephonyPermission = (typeof TELEPHONY_PERMISSIONS)[keyof typeof TELEPHONY_PERMISSIONS];
|
||||
|
||||
export type TelephonyRouteContext = {
|
||||
tenantId: string;
|
||||
userId: string;
|
||||
tenantUserId: string;
|
||||
permissions: readonly string[];
|
||||
hasModule: boolean;
|
||||
};
|
||||
|
||||
export async function loadTelephonyContext({
|
||||
request,
|
||||
params,
|
||||
requireModule = false,
|
||||
}: {
|
||||
request: Request;
|
||||
params: Record<string, string | undefined>;
|
||||
requireModule?: boolean;
|
||||
}): Promise<TelephonyRouteContext> {
|
||||
await requireAuth({ request, params });
|
||||
const [tenantId, userInfo] = await Promise.all([getTenantIdFromUrl(params), getUserInfo(request)]);
|
||||
if (!tenantId || !userInfo.userId) {
|
||||
throw new CatalogError(foundationErrors.notFound);
|
||||
}
|
||||
|
||||
const [hasModule, permissions, membership] = await Promise.all([
|
||||
tenantHasModule({ tenantId, userId: userInfo.userId, moduleKey: TELEPHONY_MODULE_KEY }),
|
||||
getPermissionsByUser(userInfo.userId, tenantId),
|
||||
withTenantRLS(tenantId, (tx) =>
|
||||
tx.tenantUser.findFirst({ where: { tenantId, userId: userInfo.userId }, select: { id: true } }),
|
||||
),
|
||||
]);
|
||||
if (!membership) {
|
||||
throw new CatalogError(foundationErrors.notFound);
|
||||
}
|
||||
if (requireModule && !hasModule) {
|
||||
throw new CatalogError(foundationErrors.notFound, { module: TELEPHONY_MODULE_KEY });
|
||||
}
|
||||
return { tenantId, userId: userInfo.userId, tenantUserId: membership.id, permissions, hasModule };
|
||||
}
|
||||
|
||||
export function telephonyHasPermission(ctx: TelephonyRouteContext, permission: TelephonyPermission): boolean {
|
||||
if (ctx.permissions.includes(TELEPHONY_PERMISSIONS.manage)) return true;
|
||||
if (permission === TELEPHONY_PERMISSIONS.readOwn && ctx.permissions.includes(TELEPHONY_PERMISSIONS.readAll)) return true;
|
||||
return ctx.permissions.includes(permission);
|
||||
}
|
||||
|
||||
export function requireTelephonyPermission(ctx: TelephonyRouteContext, permission: TelephonyPermission): TelephonyRouteContext {
|
||||
if (!telephonyHasPermission(ctx, permission)) {
|
||||
throw new CatalogError(foundationErrors.notFound, { permission });
|
||||
}
|
||||
if (ctx.permissions.includes(permission)) {
|
||||
requirePermission({ tenantId: ctx.tenantId, userId: ctx.userId, permissions: ctx.permissions }, permission);
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
export function canReadAllTelephonyCalls(ctx: TelephonyRouteContext): boolean {
|
||||
return telephonyHasPermission(ctx, TELEPHONY_PERMISSIONS.readAll);
|
||||
}
|
||||
|
||||
export function requireTelephonyCallVisibility(ctx: TelephonyRouteContext, boundUserId: string | null): TelephonyRouteContext {
|
||||
if (canReadAllTelephonyCalls(ctx)) return ctx;
|
||||
if (boundUserId === ctx.tenantUserId && telephonyHasPermission(ctx, TELEPHONY_PERMISSIONS.readOwn)) return ctx;
|
||||
throw new CatalogError(foundationErrors.notFound, { call: "not-visible" });
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
AIRCALL_OAUTH_CALLBACK_PATH,
|
||||
AIRCALL_OAUTH_INSTALL_PATH,
|
||||
AIRCALL_WEBHOOK_PATH,
|
||||
telephonyCallDetailPath,
|
||||
telephonyCallsPath,
|
||||
telephonySettingsPath,
|
||||
} from "./telephonyRoutePaths";
|
||||
|
||||
describe("telephony route paths", () => {
|
||||
it("keeps tenant calls and settings routes aligned with navigation", () => {
|
||||
expect(telephonyCallsPath("acme")).toBe("/acme/calls");
|
||||
expect(telephonyCallDetailPath("acme", "call-1")).toBe("/acme/calls/call-1");
|
||||
expect(telephonySettingsPath("acme")).toBe("/acme/settings/integrations/telephony");
|
||||
});
|
||||
|
||||
it("keeps the provider endpoints tenant-neutral", () => {
|
||||
expect(AIRCALL_OAUTH_INSTALL_PATH).toBe("/integrations/aircall/oauth/install");
|
||||
expect(AIRCALL_OAUTH_CALLBACK_PATH).toBe("/integrations/aircall/oauth/callback");
|
||||
expect(AIRCALL_WEBHOOK_PATH).toBe("/v1/webhooks/telephony/aircall");
|
||||
});
|
||||
});
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
export const TELEPHONY_MODULE_KEY = "telephony";
|
||||
|
||||
export function telephonyCallsPath(tenantSlug: string): string {
|
||||
return `/${tenantSlug}/calls`;
|
||||
}
|
||||
|
||||
export function telephonyCallDetailPath(tenantSlug: string, callId: string): string {
|
||||
return `/${tenantSlug}/calls/${callId}`;
|
||||
}
|
||||
|
||||
export function telephonySettingsPath(tenantSlug: string): string {
|
||||
return `/${tenantSlug}/settings/integrations/telephony`;
|
||||
}
|
||||
|
||||
export const AIRCALL_OAUTH_INSTALL_PATH = "/integrations/aircall/oauth/install";
|
||||
export const AIRCALL_OAUTH_CALLBACK_PATH = "/integrations/aircall/oauth/callback";
|
||||
export const AIRCALL_WEBHOOK_PATH = "/v1/webhooks/telephony/aircall";
|
||||
|
||||
export function telephonyNotFoundResponse(): Response {
|
||||
return new Response("Not Found", { status: 404 });
|
||||
}
|
||||
Reference in new issue
Block a user