Allow disabling activity/revisions (#5112)

* Add accountability column

* Add field info for accountability

* Add accountability to collection type

* Fetch accountability info from collection meta

* Add field name translation for accountability field

* Hide revisions drawer detail if revisions aren't available

* Only save activity where accountability flag matches

* Disable revisions for directus_presets

Fixes #3767

* Tweak field option naming
This commit is contained in:
Rijk van Zanten
2021-04-16 16:26:18 -04:00
committed by GitHub
parent 88bf146fbb
commit 40b9fb0fe6
13 changed files with 101 additions and 37 deletions

View File

@@ -129,7 +129,7 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
await payloadService.processO2M(payloads, key);
}
if (this.accountability) {
if (this.accountability && this.schema.collections[this.collection].accountability !== null) {
const activityRecords = primaryKeys.map((key) => ({
action: Action.CREATE,
user: this.accountability!.user,
@@ -153,16 +153,18 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
activityPrimaryKeys.push(primaryKey);
}
const revisionRecords = activityPrimaryKeys.map((key, index) => ({
activity: key,
collection: this.collection,
item: primaryKeys[index],
data: JSON.stringify(payloads[index]),
delta: JSON.stringify(payloads[index]),
}));
if (this.schema.collections[this.collection].accountability === 'all') {
const revisionRecords = activityPrimaryKeys.map((key, index) => ({
activity: key,
collection: this.collection,
item: primaryKeys[index],
data: JSON.stringify(payloads[index]),
delta: JSON.stringify(payloads[index]),
}));
if (revisionRecords.length > 0) {
await trx.insert(revisionRecords).into('directus_revisions');
if (revisionRecords.length > 0) {
await trx.insert(revisionRecords).into('directus_revisions');
}
}
}
@@ -335,7 +337,7 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
await payloadService.processO2M(payload, key);
}
if (this.accountability) {
if (this.accountability && this.schema.collections[this.collection].accountability !== null) {
const activityRecords = keys.map((key) => ({
action: Action.UPDATE,
user: this.accountability!.user,
@@ -357,24 +359,26 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
activityPrimaryKeys.push(primaryKey);
}
const itemsService = new ItemsService(this.collection, {
knex: trx,
schema: this.schema,
});
if (this.schema.collections[this.collection].accountability === 'all') {
const itemsService = new ItemsService(this.collection, {
knex: trx,
schema: this.schema,
});
const snapshots = await itemsService.readByKey(keys);
const snapshots = await itemsService.readByKey(keys);
const revisionRecords = activityPrimaryKeys.map((key, index) => ({
activity: key,
collection: this.collection,
item: keys[index],
data:
snapshots && Array.isArray(snapshots) ? JSON.stringify(snapshots?.[index]) : JSON.stringify(snapshots),
delta: JSON.stringify(payloadWithoutAliasAndPK),
}));
const revisionRecords = activityPrimaryKeys.map((key, index) => ({
activity: key,
collection: this.collection,
item: keys[index],
data:
snapshots && Array.isArray(snapshots) ? JSON.stringify(snapshots?.[index]) : JSON.stringify(snapshots),
delta: JSON.stringify(payloadWithoutAliasAndPK),
}));
if (revisionRecords.length > 0) {
await trx.insert(revisionRecords).into('directus_revisions');
if (revisionRecords.length > 0) {
await trx.insert(revisionRecords).into('directus_revisions');
}
}
}
});
@@ -502,7 +506,7 @@ export class ItemsService<Item extends AnyItem = AnyItem> implements AbstractSer
await this.knex.transaction(async (trx) => {
await trx(this.collection).whereIn(primaryKeyField, keys).delete();
if (this.accountability) {
if (this.accountability && this.schema.collections[this.collection].accountability !== null) {
const activityRecords = keys.map((key) => ({
action: Action.DELETE,
user: this.accountability!.user,