Files
outline/server/models/AccessRequest.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

167 lines
3.5 KiB
TypeScript

import type {
InferAttributes,
InferCreationAttributes,
SaveOptions,
} from "sequelize";
import {
Table,
ForeignKey,
Column,
BelongsTo,
DataType,
Default,
AllowNull,
DefaultScope,
BeforeCreate,
IsIn,
} from "sequelize-typescript";
import Document from "./Document";
import Team from "./Team";
import User from "./User";
import IdModel from "./base/IdModel";
import { ValidationError } from "@server/errors";
import type { Context } from "@server/types";
export enum AccessRequestStatus {
Pending = "pending",
Approved = "approved",
Dismissed = "dismissed",
}
@DefaultScope(() => ({
include: [
{
association: "user",
required: true,
},
{
association: "responder",
required: false,
},
],
}))
@Table({
tableName: "access_requests",
modelName: "access_request",
})
class AccessRequest extends IdModel<
InferAttributes<AccessRequest>,
Partial<InferCreationAttributes<AccessRequest>>
> {
@Default(AccessRequestStatus.Pending)
@IsIn([Object.values(AccessRequestStatus)])
@Column(DataType.STRING)
status: AccessRequestStatus;
@AllowNull
@Column(DataType.DATE)
respondedAt: Date | null;
// associations
@BelongsTo(() => Document, "documentId")
document: Document;
@ForeignKey(() => Document)
@Column(DataType.UUID)
documentId: string;
@BelongsTo(() => User, "userId")
user: User;
@ForeignKey(() => User)
@Column(DataType.UUID)
userId: string;
@BelongsTo(() => Team, "teamId")
team: Team;
@ForeignKey(() => Team)
@Column(DataType.UUID)
teamId: string;
@BelongsTo(() => User, "responderId")
responder: User | null;
@AllowNull
@ForeignKey(() => User)
@Column(DataType.UUID)
responderId: string | null;
@BeforeCreate
static async validateNoDuplicatePendingRequest(
instance: AccessRequest,
options: SaveOptions<AccessRequest>
) {
const { documentId, userId } = instance;
const existingRequest = await this.findOne({
where: {
documentId,
userId,
status: AccessRequestStatus.Pending,
},
transaction: options.transaction,
});
if (existingRequest) {
throw ValidationError(
"A pending access request already exists for this document and user."
);
}
}
/**
* Approve this access request, setting the status and responder, and persist.
*
* @param ctx the context; the authenticated user is recorded as responder.
*/
public approve(ctx: Context) {
this.status = AccessRequestStatus.Approved;
this.responderId = ctx.auth.user.id;
this.respondedAt = new Date();
return this.saveWithCtx(ctx);
}
/**
* Dismiss this access request, setting the status and responder, and persist.
*
* @param ctx the context; the authenticated user is recorded as responder.
*/
public dismiss(ctx: Context) {
this.status = AccessRequestStatus.Dismissed;
this.responderId = ctx.auth.user.id;
this.respondedAt = new Date();
return this.saveWithCtx(ctx);
}
/**
* get the user's pending request.
*
* @param documentId The document ID or slug.
* @param userId The user ID.
*
* @returns the pending request or null.
*/
public static async findPendingForUser({
documentId,
userId,
}: {
documentId?: string;
userId?: string;
}): Promise<AccessRequest | null> {
if (!documentId || !userId) {
return null;
}
return this.findOne({
where: {
documentId,
userId,
status: AccessRequestStatus.Pending,
},
});
}
}
export default AccessRequest;