feat: Events / audit log (#1008)

* feat: Record events in DB

* feat: events API

* First pass, hacky activity feed

* WIP

* Reset dashboard

* feat: audit log UI
feat: store ip address

* chore: Document events.list api

* fix: command specs

* await event create

* fix: backlinks service

* tidy

* fix: Hide audit log menu item if not admin
This commit is contained in:
Tom Moor
2019-08-05 20:38:31 -07:00
committed by GitHub
parent 75b03fdba2
commit fb4f6822a4
37 changed files with 911 additions and 148 deletions

View File

@ -1,18 +1,22 @@
// @flow
import { Document, Collection } from '../models';
import { Document, Collection, Event } from '../models';
import { sequelize } from '../sequelize';
import events from '../events';
import { type Context } from 'koa';
export default async function documentMover({
user,
document,
collectionId,
parentDocumentId,
index,
ip,
}: {
user: Context,
document: Document,
collectionId: string,
parentDocumentId: string,
index?: number,
ip: string,
}) {
let transaction;
const result = { collections: [], documents: [] };
@ -72,12 +76,18 @@ export default async function documentMover({
await transaction.commit();
events.add({
await Event.create({
name: 'documents.move',
modelId: document.id,
collectionIds: result.collections.map(c => c.id),
documentIds: result.documents.map(d => d.id),
actorId: user.id,
documentId: document.id,
collectionId,
teamId: document.teamId,
data: {
title: document.title,
collectionIds: result.collections.map(c => c.id),
documentIds: result.documents.map(d => d.id),
},
ip,
});
} catch (err) {
if (transaction) {

View File

@ -6,12 +6,16 @@ import { buildDocument, buildCollection } from '../test/factories';
beforeEach(flushdb);
describe('documentMover', async () => {
const ip = '127.0.0.1';
it('should move within a collection', async () => {
const { document, collection } = await seed();
const { document, user, collection } = await seed();
const response = await documentMover({
user,
document,
collectionId: collection.id,
ip,
});
expect(response.collections.length).toEqual(1);
@ -19,7 +23,7 @@ describe('documentMover', async () => {
});
it('should move with children', async () => {
const { document, collection } = await seed();
const { document, user, collection } = await seed();
const newDocument = await buildDocument({
parentDocumentId: document.id,
collectionId: collection.id,
@ -31,10 +35,12 @@ describe('documentMover', async () => {
await collection.addDocumentToStructure(newDocument);
const response = await documentMover({
user,
document,
collectionId: collection.id,
parentDocumentId: undefined,
index: 0,
ip,
});
expect(response.collections[0].documentStructure[0].children[0].id).toBe(
@ -45,7 +51,7 @@ describe('documentMover', async () => {
});
it('should move with children to another collection', async () => {
const { document, collection } = await seed();
const { document, user, collection } = await seed();
const newCollection = await buildCollection({
teamId: collection.teamId,
});
@ -60,10 +66,12 @@ describe('documentMover', async () => {
await collection.addDocumentToStructure(newDocument);
const response = await documentMover({
user,
document,
collectionId: newCollection.id,
parentDocumentId: undefined,
index: 0,
ip,
});
// check document ids where updated

View File

@ -1,7 +1,6 @@
// @flow
import { uniqBy } from 'lodash';
import { User, Team } from '../models';
import events from '../events';
import { User, Event, Team } from '../models';
import mailer from '../mailer';
type Invite = { name: string, email: string };
@ -9,9 +8,11 @@ type Invite = { name: string, email: string };
export default async function userInviter({
user,
invites,
ip,
}: {
user: User,
invites: Invite[],
ip: string,
}): Promise<{ sent: Invite[] }> {
const team = await Team.findByPk(user.teamId);
@ -35,23 +36,28 @@ export default async function userInviter({
);
// send and record invites
filteredInvites.forEach(async invite => {
await mailer.invite({
to: invite.email,
name: invite.name,
actorName: user.name,
actorEmail: user.email,
teamName: team.name,
teamUrl: team.url,
});
events.add({
name: 'users.invite',
actorId: user.id,
teamId: user.teamId,
email: invite.email,
});
});
await Promise.all(
filteredInvites.map(async invite => {
await Event.create({
name: 'users.invite',
actorId: user.id,
teamId: user.teamId,
data: {
email: invite.email,
name: invite.name,
},
ip,
});
await mailer.invite({
to: invite.email,
name: invite.name,
actorName: user.name,
actorEmail: user.email,
teamName: team.name,
teamUrl: team.url,
});
})
);
return { sent: filteredInvites };
}

View File

@ -6,11 +6,14 @@ import { buildUser } from '../test/factories';
beforeEach(flushdb);
describe('userInviter', async () => {
const ip = '127.0.0.1';
it('should return sent invites', async () => {
const user = await buildUser();
const response = await userInviter({
invites: [{ email: 'test@example.com', name: 'Test' }],
user,
ip,
});
expect(response.sent.length).toEqual(1);
});
@ -20,6 +23,7 @@ describe('userInviter', async () => {
const response = await userInviter({
invites: [{ email: ' ', name: 'Test' }],
user,
ip,
});
expect(response.sent.length).toEqual(0);
});
@ -29,6 +33,7 @@ describe('userInviter', async () => {
const response = await userInviter({
invites: [{ email: 'notanemail', name: 'Test' }],
user,
ip,
});
expect(response.sent.length).toEqual(0);
});
@ -41,6 +46,7 @@ describe('userInviter', async () => {
{ email: 'the@same.com', name: 'Test' },
],
user,
ip,
});
expect(response.sent.length).toEqual(1);
});
@ -50,6 +56,7 @@ describe('userInviter', async () => {
const response = await userInviter({
invites: [{ email: user.email, name: user.name }],
user,
ip,
});
expect(response.sent.length).toEqual(0);
});