mirror of
https://github.com/outline/outline.git
synced 2026-05-04 16:50:11 -05:00
documentUpdater
This commit is contained in:
@@ -1,24 +1,20 @@
|
||||
import { Event } from "@server/models";
|
||||
import { sequelize } from "@server/storage/database";
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { withAPIContext } from "@server/test/support";
|
||||
import documentUpdater from "./documentUpdater";
|
||||
|
||||
describe("documentUpdater", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
it("should change lastModifiedById", async () => {
|
||||
const user = await buildUser();
|
||||
let document = await buildDocument({
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
document = await sequelize.transaction(async (transaction) =>
|
||||
documentUpdater({
|
||||
document = await withAPIContext(user, (ctx) =>
|
||||
documentUpdater(ctx, {
|
||||
text: "Changed",
|
||||
document,
|
||||
user,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
@@ -36,13 +32,11 @@ describe("documentUpdater", () => {
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
document = await sequelize.transaction(async (transaction) =>
|
||||
documentUpdater({
|
||||
document = await withAPIContext(user, (ctx) =>
|
||||
documentUpdater(ctx, {
|
||||
title: document.title,
|
||||
document,
|
||||
user,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
@@ -55,13 +49,11 @@ describe("documentUpdater", () => {
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
document = await sequelize.transaction(async (transaction) =>
|
||||
documentUpdater({
|
||||
document = await withAPIContext(user, (ctx) =>
|
||||
documentUpdater(ctx, {
|
||||
text: "Changed",
|
||||
document,
|
||||
user,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Transaction } from "sequelize";
|
||||
import { Event, Document, User } from "@server/models";
|
||||
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
|
||||
import { APIContext } from "@server/types";
|
||||
|
||||
type Props = {
|
||||
/** The user updating the document */
|
||||
@@ -31,10 +31,6 @@ type Props = {
|
||||
publish?: boolean;
|
||||
/** The ID of the collection to publish the document to */
|
||||
collectionId?: string | null;
|
||||
/** The IP address of the user creating the document */
|
||||
ip: string;
|
||||
/** The database transaction to run within */
|
||||
transaction: Transaction;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -44,24 +40,26 @@ type Props = {
|
||||
* @param Props The properties of the document to update
|
||||
* @returns Document The updated document
|
||||
*/
|
||||
export default async function documentUpdater({
|
||||
user,
|
||||
document,
|
||||
title,
|
||||
icon,
|
||||
color,
|
||||
text,
|
||||
editorVersion,
|
||||
templateId,
|
||||
fullWidth,
|
||||
insightsEnabled,
|
||||
append,
|
||||
publish,
|
||||
collectionId,
|
||||
done,
|
||||
transaction,
|
||||
ip,
|
||||
}: Props): Promise<Document> {
|
||||
export default async function documentUpdater(
|
||||
ctx: APIContext,
|
||||
{
|
||||
user,
|
||||
document,
|
||||
title,
|
||||
icon,
|
||||
color,
|
||||
text,
|
||||
editorVersion,
|
||||
templateId,
|
||||
fullWidth,
|
||||
insightsEnabled,
|
||||
append,
|
||||
publish,
|
||||
collectionId,
|
||||
done,
|
||||
}: Props
|
||||
): Promise<Document> {
|
||||
const { transaction } = ctx.state;
|
||||
const previousTitle = document.title;
|
||||
const cId = collectionId || document.collectionId;
|
||||
|
||||
@@ -96,13 +94,10 @@ export default async function documentUpdater({
|
||||
name: "documents.update",
|
||||
documentId: document.id,
|
||||
collectionId: cId,
|
||||
teamId: document.teamId,
|
||||
actorId: user.id,
|
||||
data: {
|
||||
done,
|
||||
title: document.title,
|
||||
},
|
||||
ip,
|
||||
};
|
||||
|
||||
if (publish && (document.template || cId)) {
|
||||
@@ -111,19 +106,16 @@ export default async function documentUpdater({
|
||||
}
|
||||
await document.publish(user, cId, { transaction });
|
||||
|
||||
await Event.create(
|
||||
{
|
||||
...event,
|
||||
name: "documents.publish",
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
await Event.createFromContext(ctx, {
|
||||
...event,
|
||||
name: "documents.publish",
|
||||
});
|
||||
} else if (changed) {
|
||||
document.lastModifiedById = user.id;
|
||||
document.updatedBy = user;
|
||||
await document.save({ transaction });
|
||||
|
||||
await Event.create(event, { transaction });
|
||||
await Event.createFromContext(ctx, event);
|
||||
} else if (done) {
|
||||
await Event.schedule(event);
|
||||
}
|
||||
@@ -139,7 +131,7 @@ export default async function documentUpdater({
|
||||
previousTitle,
|
||||
title: document.title,
|
||||
},
|
||||
ip,
|
||||
ip: ctx.request.ip,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1205,7 +1205,7 @@ router.post(
|
||||
}
|
||||
}
|
||||
|
||||
document = await documentUpdater({
|
||||
document = await documentUpdater(ctx, {
|
||||
document,
|
||||
user,
|
||||
...input,
|
||||
@@ -1213,8 +1213,6 @@ router.post(
|
||||
collectionId,
|
||||
insightsEnabled,
|
||||
editorVersion,
|
||||
transaction,
|
||||
ip: ctx.request.ip,
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
|
||||
@@ -31,10 +31,10 @@ export function setSelfHosted() {
|
||||
env.URL = sharedEnv.URL = `https://${faker.internet.domainName()}`;
|
||||
}
|
||||
|
||||
export function withAPIContext(
|
||||
export function withAPIContext<T>(
|
||||
user: User,
|
||||
fn: (ctx: APIContext) => unknown
|
||||
): Promise<unknown> {
|
||||
fn: (ctx: APIContext) => T
|
||||
): Promise<T> {
|
||||
return sequelize.transaction(async (transaction: Transaction) => {
|
||||
const state = {
|
||||
auth: {
|
||||
|
||||
Reference in New Issue
Block a user