Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat(web): add Slack installations adapter
Richie McIlroyc1b9db19a541
diff --git a/apps/web/__tests__/unit/integration-installations.test.ts b/apps/web/__tests__/unit/integration-installations.test.tsnew file mode 100644index 000000000..368602930--- /dev/null+++ b/apps/web/__tests__/unit/integration-installations.test.ts@@ -0,0 +1,123 @@+import { Organisation, User } from "@cap/web-domain";+import { beforeEach, describe, expect, it, vi } from "vitest";++const integrationMocks = vi.hoisted(() => ({+ deleteIntegrationInstallation: vi.fn(async () => undefined),+ deleteIntegrationInstallationByExternalId: vi.fn(async () => undefined),+ getIntegrationCredentials: vi.fn(),+ listIntegrationInstallations: vi.fn(),+ saveIntegrationInstallation: vi.fn(async () => undefined),+}));++vi.mock("@/lib/integrations/installations", () => integrationMocks);++import {+ deleteSlackInstallation,+ deleteSlackInstallationByTeamId,+ getSlackInstallationToken,+ listSlackInstallations,+ saveSlackInstallation,+} from "@/lib/slack/installations";++const organizationId = Organisation.OrganisationId.make("org123");+const installedByUserId = User.UserId.make("user123");++describe("integration installations", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ });++ it("stores Slack through the provider-agnostic installation boundary", async () => {+ await saveSlackInstallation({+ organizationId,+ installedByUserId,+ teamId: "T123",+ teamName: "Cap",+ enterpriseId: "E123",+ botUserId: "U123",+ accessToken: "xoxb-token",+ scope: "links:read, links:write,links.embed:write",+ });++ expect(integrationMocks.saveIntegrationInstallation).toHaveBeenCalledWith({+ provider: "slack",+ externalId: "T123",+ displayName: "Cap",+ organizationId,+ installedByUserId,+ credentials: { accessToken: "xoxb-token" },+ metadata: {+ enterpriseId: "E123",+ botUserId: "U123",+ scopes: ["links:read", "links:write", "links.embed:write"],+ },+ });+ });++ it("maps generic installation summaries to Slack workspaces", async () => {+ const createdAt = new Date("2026-01-01T00:00:00Z");+ const updatedAt = new Date("2026-01-02T00:00:00Z");+ integrationMocks.listIntegrationInstallations.mockResolvedValue([+ {+ id: "installation123",+ externalId: "T123",+ displayName: "Cap",+ createdAt,+ updatedAt,+ },+ ]);++ await expect(listSlackInstallations(organizationId)).resolves.toEqual([+ {+ id: "installation123",+ teamId: "T123",+ teamName: "Cap",+ createdAt,+ updatedAt,+ },+ ]);+ expect(integrationMocks.listIntegrationInstallations).toHaveBeenCalledWith({+ organizationId,+ provider: "slack",+ });+ });++ it("reads only valid provider credentials", async () => {+ integrationMocks.getIntegrationCredentials.mockResolvedValue({+ accessToken: "xoxb-token",+ });+ await expect(getSlackInstallationToken("T123")).resolves.toBe("xoxb-token");++ integrationMocks.getIntegrationCredentials.mockResolvedValue(null);+ await expect(getSlackInstallationToken("T123")).resolves.toBeNull();++ integrationMocks.getIntegrationCredentials.mockResolvedValue({+ accessToken: 123,+ });+ await expect(getSlackInstallationToken("T123")).rejects.toThrow(+ "Slack installation credentials are invalid",+ );+ });++ it("keeps all deletion paths provider-scoped", async () => {+ await deleteSlackInstallation({+ organizationId,+ installationId: "installation123",+ });+ await deleteSlackInstallationByTeamId("T123");++ expect(integrationMocks.deleteIntegrationInstallation).toHaveBeenCalledWith(+ {+ provider: "slack",+ organizationId,+ installationId: "installation123",+ },+ );+ expect(+ integrationMocks.deleteIntegrationInstallationByExternalId,+ ).toHaveBeenCalledWith({+ provider: "slack",+ externalId: "T123",+ });+ });+});diff --git a/apps/web/lib/slack/installations.ts b/apps/web/lib/slack/installations.tsnew file mode 100644index 000000000..fd73b089b--- /dev/null+++ b/apps/web/lib/slack/installations.ts@@ -0,0 +1,108 @@+import type { Organisation, User } from "@cap/web-domain";+import {+ deleteIntegrationInstallation,+ deleteIntegrationInstallationByExternalId,+ getIntegrationCredentials,+ listIntegrationInstallations,+ saveIntegrationInstallation,+} from "@/lib/integrations/installations";++const SLACK_PROVIDER = "slack";++export type SlackInstallationSummary = {+ id: string;+ teamId: string;+ teamName: string;+ createdAt: Date;+ updatedAt: Date;+};++export const saveSlackInstallation = async ({+ organizationId,+ installedByUserId,+ teamId,+ teamName,+ enterpriseId,+ botUserId,+ accessToken,+ scope,+}: {+ organizationId: Organisation.OrganisationId;+ installedByUserId: User.UserId;+ teamId: string;+ teamName: string;+ enterpriseId: string | null;+ botUserId: string | null;+ accessToken: string;+ scope: string;+}) => {+ await saveIntegrationInstallation({+ provider: SLACK_PROVIDER,+ externalId: teamId,+ displayName: teamName,+ organizationId,+ installedByUserId,+ credentials: { accessToken },+ metadata: {+ enterpriseId,+ botUserId,+ scopes: scope+ .split(",")+ .map((value) => value.trim())+ .filter(Boolean),+ },+ });+};++export const listSlackInstallations = async (+ organizationId: Organisation.OrganisationId,+): Promise<SlackInstallationSummary[]> =>+ listIntegrationInstallations({+ organizationId,+ provider: SLACK_PROVIDER,+ }).then((installations) =>+ installations.map((installation) => ({+ id: installation.id,+ teamId: installation.externalId,+ teamName: installation.displayName,+ createdAt: installation.createdAt,+ updatedAt: installation.updatedAt,+ })),+ );++export const getSlackInstallationToken = async (teamId: string) => {+ const credentials = await getIntegrationCredentials({+ provider: SLACK_PROVIDER,+ externalId: teamId,+ });+ if (credentials === null) return null;+ if (+ typeof credentials !== "object" ||+ Array.isArray(credentials) ||+ typeof (credentials as { accessToken?: unknown }).accessToken !== "string"+ ) {+ throw new Error("Slack installation credentials are invalid");+ }+ return (credentials as { accessToken: string }).accessToken;+};++export const deleteSlackInstallation = async ({+ organizationId,+ installationId,+}: {+ organizationId: Organisation.OrganisationId;+ installationId: string;+}) => {+ await deleteIntegrationInstallation({+ provider: SLACK_PROVIDER,+ organizationId,+ installationId,+ });+};++export const deleteSlackInstallationByTeamId = async (teamId: string) => {+ await deleteIntegrationInstallationByExternalId({+ provider: SLACK_PROVIDER,+ externalId: teamId,+ });+};