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

85 lines
2.4 KiB
TypeScript

import { toError } from "@shared/utils/error";
import Logger from "@server/logging/Logger";
import { Collection } from "@server/models";
import DuplicateCollectionDocumentsTask from "@server/queues/tasks/DuplicateCollectionDocumentsTask";
import { afterCommit } from "@server/storage/transaction";
import type { Context } from "@server/types";
type Props = {
/** The collection to duplicate */
collection: Collection;
/** Override of the duplicated collection name */
name?: string;
};
/**
* Duplicates a collection into a new collection owned by the acting user. The
* published documents within the collection are duplicated asynchronously by a
* background task once the new collection is committed.
*
* @param ctx the context containing the acting user and transaction.
* @param props the collection to duplicate and optional overrides.
* @returns the duplicated collection.
*/
export default async function collectionDuplicator(
ctx: Context,
{ collection, name }: Props
): Promise<Collection> {
const { user } = ctx.auth;
const { transaction } = ctx;
const duplicated = Collection.build({
name: name ?? collection.name,
content: collection.content,
description: collection.description,
icon: collection.icon,
color: collection.color,
teamId: user.teamId,
createdById: user.id,
permission: collection.permission,
sharing: collection.sharing,
sort: collection.sort,
commenting: collection.commenting,
templateManagement: collection.templateManagement,
sourceMetadata: {
...collection.sourceMetadata,
originalCollectionId: collection.id,
},
});
await duplicated.saveWithCtx(ctx);
const scheduleTask = () =>
new DuplicateCollectionDocumentsTask().schedule({
collectionId: duplicated.id,
originalCollectionId: collection.id,
actorId: user.id,
ip: ctx.ip ?? null,
});
if (transaction) {
await afterCommit(transaction, async () => {
try {
await scheduleTask();
} catch (err) {
Logger.error(
"Failed to schedule duplicate collection documents task",
toError(err),
{
collectionId: duplicated.id,
}
);
}
});
} else {
await scheduleTask();
}
// we must reload the collection to get memberships for policy presenter
return Collection.findByPk(duplicated.id, {
userId: user.id,
transaction,
rejectOnEmpty: true,
});
}