43
server/commands/revisionCreator.js
Normal file
43
server/commands/revisionCreator.js
Normal file
@ -0,0 +1,43 @@
|
||||
// @flow
|
||||
import { Document, User, Event, Revision } from "../models";
|
||||
import { sequelize } from "../sequelize";
|
||||
|
||||
export default async function revisionCreator({
|
||||
document,
|
||||
user,
|
||||
ip,
|
||||
}: {
|
||||
document: Document,
|
||||
user: User,
|
||||
ip?: string,
|
||||
}) {
|
||||
let transaction;
|
||||
|
||||
try {
|
||||
transaction = await sequelize.transaction();
|
||||
|
||||
const revision = await Revision.createFromDocument(document, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
await Event.create(
|
||||
{
|
||||
name: "revisions.create",
|
||||
documentId: document.id,
|
||||
modelId: revision.id,
|
||||
teamId: document.teamId,
|
||||
actorId: user.id,
|
||||
ip: ip || user.lastActiveIp,
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
await transaction.commit();
|
||||
|
||||
return revision;
|
||||
} catch (err) {
|
||||
if (transaction) {
|
||||
await transaction.rollback();
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
27
server/commands/revisionCreator.test.js
Normal file
27
server/commands/revisionCreator.test.js
Normal file
@ -0,0 +1,27 @@
|
||||
// @flow
|
||||
import { Event } from "../models";
|
||||
import { buildDocument, buildUser } from "../test/factories";
|
||||
import { flushdb } from "../test/support";
|
||||
import revisionCreator from "./revisionCreator";
|
||||
|
||||
beforeEach(() => flushdb());
|
||||
|
||||
describe("revisionCreator", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
it("should create revision model from document", async () => {
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
const revision = await revisionCreator({ document, user, ip });
|
||||
const event = await Event.findOne();
|
||||
|
||||
expect(revision.documentId).toEqual(document.id);
|
||||
expect(revision.userId).toEqual(user.id);
|
||||
expect(event.name).toEqual("revisions.create");
|
||||
expect(event.modelId).toEqual(revision.id);
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user