Files
outline/server/commands/teamUpdater.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

116 lines
3.4 KiB
TypeScript

import { has, isEqual } from "es-toolkit/compat";
import { TeamPreference } from "@shared/types";
import env from "@server/env";
import { ValidationError } from "@server/errors";
import type { User } from "@server/models";
import { Collection, Team, TeamDomain } from "@server/models";
import { authorize } from "@server/policies";
import type { Context } from "@server/types";
type Props = {
params: Partial<Omit<Team, "allowedDomains">> & { allowedDomains?: string[] };
user: User;
team: Team;
};
const teamUpdater = async (ctx: Context, { params, user, team }: Props) => {
const { allowedDomains, preferences, subdomain, ...attributes } = params;
team.fill(attributes);
if (attributes.defaultCollectionId) {
const collection = await Collection.findOne({
attributes: ["id"],
where: {
id: attributes.defaultCollectionId,
teamId: team.id,
ownerId: null,
},
transaction: ctx.transaction,
});
if (!collection) {
throw ValidationError("Default collection not found");
}
}
if (subdomain !== undefined && env.isCloudHosted) {
const newSubdomain = subdomain === "" ? null : subdomain;
if (newSubdomain && newSubdomain !== team.subdomain) {
const existing = await Team.findOne({
where: { subdomain: newSubdomain },
paranoid: false,
transaction: ctx.transaction,
});
if (existing) {
throw ValidationError("Subdomain is already in use");
}
}
team.subdomain = newSubdomain;
}
if (allowedDomains !== undefined) {
const existingAllowedDomains = await TeamDomain.findAll({
where: { teamId: team.id },
transaction: ctx.transaction,
});
// Only keep existing domains if they are still in the list of allowed domains
const newAllowedDomains = team.allowedDomains.filter((existingTeamDomain) =>
allowedDomains.includes(existingTeamDomain.name)
);
// Add new domains
const existingDomains = team.allowedDomains.map((x) => x.name);
const newDomains = allowedDomains.filter(
(newDomain) => newDomain !== "" && !existingDomains.includes(newDomain)
);
// Created one at a time so that the limit check on each create counts the
// domains added earlier in the same request.
for (const newDomain of newDomains) {
newAllowedDomains.push(
await TeamDomain.createWithCtx(ctx, {
name: newDomain,
teamId: team.id,
createdById: user.id,
})
);
}
// Destroy the existing TeamDomains that were removed
const deletedDomains = existingAllowedDomains.filter(
(x) => !allowedDomains.includes(x.name)
);
await Promise.all(deletedDomains.map((x) => x.destroyWithCtx(ctx)));
team.allowedDomains = newAllowedDomains;
}
if (preferences) {
const shareExpiryDays = preferences[TeamPreference.DefaultShareExpiryDays];
// Removing the default stays possible after losing the entitlement.
if (
shareExpiryDays &&
shareExpiryDays !==
team.getPreference(TeamPreference.DefaultShareExpiryDays)
) {
authorize(user, "setDefaultShareExpiry", team);
}
for (const key of Object.values(TeamPreference)) {
if (
has(preferences, key) &&
!isEqual(preferences[key], team.getPreference(key))
) {
team.setPreference(key, preferences[key]);
}
}
}
return team.saveWithCtx(ctx);
};
export default teamUpdater;