mirror of
https://github.com/directus/directus.git
synced 2026-01-26 14:07:56 -05:00
Add revisions for update
This commit is contained in:
@@ -63,10 +63,14 @@ router.patch(
|
||||
if (!req.accountability?.user) {
|
||||
throw new InvalidCredentialsException();
|
||||
}
|
||||
|
||||
/**
|
||||
* @TODO implement skip-activity query param so we don't end up saving activity / revisions
|
||||
* for every single page navigation that was done
|
||||
*/
|
||||
|
||||
const service = new UsersService({ accountability: req.accountability });
|
||||
|
||||
const primaryKey = await service.update(req.body, req.accountability.user);
|
||||
|
||||
const item = await service.readByKey(primaryKey, req.sanitizedQuery);
|
||||
|
||||
return res.json({ data: item || null });
|
||||
|
||||
@@ -218,20 +218,52 @@ export default class ItemsService implements AbstractService {
|
||||
accountability: this.accountability,
|
||||
knex: trx,
|
||||
});
|
||||
|
||||
payload = await payloadService.processM2O(payload);
|
||||
payload = await payloadService.processValues('update', payload);
|
||||
|
||||
const payloadWithoutAliases = pick(
|
||||
payload,
|
||||
columns.map(({ column }) => column)
|
||||
);
|
||||
|
||||
await trx(this.collection)
|
||||
.update(payloadWithoutAliases)
|
||||
.whereIn(primaryKeyField, keys);
|
||||
|
||||
await payloadService.processO2M(payload);
|
||||
|
||||
/**
|
||||
* @todo save activity
|
||||
*/
|
||||
if (this.accountability) {
|
||||
const activityRecords = keys.map((key) => ({
|
||||
action: Action.UPDATE,
|
||||
action_by: this.accountability!.user,
|
||||
collection: this.collection,
|
||||
ip: this.accountability!.ip,
|
||||
user_agent: this.accountability!.userAgent,
|
||||
item: key,
|
||||
}));
|
||||
|
||||
const activityPrimaryKeys: PrimaryKey[] = [];
|
||||
|
||||
for (const activityRecord of activityRecords) {
|
||||
const result = await trx.insert(activityRecord).into('directus_activity');
|
||||
activityPrimaryKeys.push(result[0]);
|
||||
}
|
||||
|
||||
const itemsService = new ItemsService(this.collection, { knex: trx });
|
||||
|
||||
const snapshots = await itemsService.readByKey(keys);
|
||||
|
||||
const revisionRecords = activityPrimaryKeys.map((key, index) => ({
|
||||
activity: key,
|
||||
collection: this.collection,
|
||||
item: keys[index],
|
||||
data: JSON.stringify(snapshots[index]),
|
||||
delta: JSON.stringify(payloadWithoutAliases),
|
||||
}));
|
||||
|
||||
await trx.insert(revisionRecords).into('directus_revisions');
|
||||
}
|
||||
});
|
||||
|
||||
return key;
|
||||
|
||||
Reference in New Issue
Block a user