Files
outline/server/routes/api/integrations/integrations.ts
T
Tom MoorandClaude Opus 5.5 00b124d03c chore: Rename APIContext to Context and flatten its fields (#14684)
* chore: Rename APIContext to Context and flatten its fields

Auth and transaction now live directly on the context instead of in
ctx.state and a nested ctx.context, and model helpers read them directly.
APIContext remains as a deprecated alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove deprecated APIContext alias

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* fix: Read auth from context in OAuth provider callbacks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Add withUser and withTransaction context helpers

Replaces manual createContext rebuilds that copy fields from an existing context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove arbitrary property access from Koa context types

AppContext and Context no longer inherit the any index signature from
Koa's DefaultContext, custom fields are declared explicitly, and importing
Koa's loose Context type is disallowed by lint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-23 23:17:06 -05:00

182 lines
4.8 KiB
TypeScript

import Router from "@koa/router";
import type { WhereOptions } from "sequelize";
import { Op } from "sequelize";
import { IntegrationType, UserRole } from "@shared/types";
import auth from "@server/middlewares/authentication";
import { rateLimiter } from "@server/middlewares/rateLimiter";
import { transaction } from "@server/middlewares/transaction";
import validate from "@server/middlewares/validate";
import Integration from "@server/models/Integration";
import { authorize } from "@server/policies";
import { presentIntegration, presentPolicies } from "@server/presenters";
import type { Context } from "@server/types";
import { RateLimiterStrategy } from "@server/utils/RateLimiter";
import pagination from "../middlewares/pagination";
import * as T from "./schema";
const router = new Router();
router.post(
"integrations.list",
auth(),
pagination(),
validate(T.IntegrationsListSchema),
async (ctx: Context<T.IntegrationsListReq>) => {
const { direction, service, type, sort } = ctx.input.body;
const { user } = ctx.auth;
let where: WhereOptions<Integration> = {
teamId: user.teamId,
};
if (type) {
where = { ...where, type };
}
if (service) {
where = { ...where, service };
}
// Linked account is special as these are user-specific, other integrations
// are workspace-wide. Guests only receive embeds, which the editor needs.
where = {
...where,
[Op.or]: [
{ userId: user.id, type: IntegrationType.LinkedAccount },
{
type: user.isGuest
? IntegrationType.Embed
: { [Op.not]: IntegrationType.LinkedAccount },
},
],
};
const [integrations, total] = await Promise.all([
Integration.findAll({
where,
order: [[sort, direction]],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
}),
Integration.count({
where,
}),
]);
ctx.body = {
pagination: { ...ctx.state.pagination, total },
data: integrations.map((integration) =>
presentIntegration(ctx, integration)
),
policies: presentPolicies(user, integrations),
};
}
);
router.post(
"integrations.create",
rateLimiter(RateLimiterStrategy.TwentyFivePerMinute),
auth({ role: UserRole.Admin }),
validate(T.IntegrationsCreateSchema),
transaction(),
async (ctx: Context<T.IntegrationsCreateReq>) => {
const { type, service, settings } = ctx.input.body;
const { user } = ctx.auth;
authorize(user, "createIntegration", user.team);
const integration = await Integration.createWithCtx(ctx, {
userId: user.id,
teamId: user.teamId,
service,
settings,
type,
});
ctx.body = {
data: presentIntegration(ctx, integration),
policies: presentPolicies(user, [integration]),
};
}
);
router.post(
"integrations.info",
auth(),
validate(T.IntegrationsInfoSchema),
async (ctx: Context<T.IntegrationsInfoReq>) => {
const { id } = ctx.input.body;
const { user } = ctx.auth;
const integration = await Integration.findByPk(id, {
rejectOnEmpty: true,
});
authorize(user, "read", integration);
ctx.body = {
data: presentIntegration(ctx, integration),
policies: presentPolicies(user, [integration]),
};
}
);
router.post(
"integrations.update",
auth({ role: UserRole.Admin }),
validate(T.IntegrationsUpdateSchema),
transaction(),
async (ctx: Context<T.IntegrationsUpdateReq>) => {
const { id, events, settings } = ctx.input.body;
const { user } = ctx.auth;
const { transaction } = ctx;
const integration = await Integration.findByPk(id, {
transaction,
lock: transaction.LOCK.NO_KEY_UPDATE,
});
authorize(user, "update", integration);
if (events !== undefined && integration.type === IntegrationType.Post) {
integration.events = events.filter((event: string) =>
["documents.update", "documents.publish"].includes(event)
);
}
if (settings !== undefined) {
integration.settings = settings;
}
await integration.saveWithCtx(ctx);
ctx.body = {
data: presentIntegration(ctx, integration),
policies: presentPolicies(user, [integration]),
};
}
);
router.post(
"integrations.delete",
auth(),
validate(T.IntegrationsDeleteSchema),
transaction(),
async (ctx: Context<T.IntegrationsDeleteReq>) => {
const { id } = ctx.input.body;
const { user } = ctx.auth;
const { transaction } = ctx;
const integration = await Integration.findByPk(id, {
rejectOnEmpty: true,
transaction,
lock: transaction.LOCK.NO_KEY_UPDATE,
});
authorize(user, "delete", integration);
await integration.destroyWithCtx(ctx);
ctx.body = {
success: true,
};
}
);
export default router;