feat: purge history utility

This commit is contained in:
NGPixel
2020-09-05 23:32:00 -04:00
parent 8490fc1267
commit ef739de970
4 changed files with 108 additions and 2 deletions

View File

@@ -562,11 +562,24 @@ module.exports = {
} catch (err) {
return graphHelper.generateError(err)
}
},
/**
* Purge history
*/
async purgeHistory (obj, args, context) {
try {
await WIKI.models.pageHistory.purge(args.olderThan)
return {
responseResult: graphHelper.generateSuccess('Page history purged successfully.')
}
} catch (err) {
return graphHelper.generateError(err)
}
}
},
Page: {
async tags (obj) {
return await WIKI.models.pages.relatedQuery('tags').for(obj.id)
return WIKI.models.pages.relatedQuery('tags').for(obj.id)
}
// comments(pg) {
// return pg.$relatedQuery('comments')

View File

@@ -149,6 +149,10 @@ type PageMutation {
pageId: Int!
versionId: Int!
): DefaultResponse @auth(requires: ["write:pages", "manage:pages", "manage:system"])
purgeHistory (
olderThan: String!
): DefaultResponse @auth(requires: ["manage:system"])
}
# -----------------------------------------------

View File

@@ -1,5 +1,6 @@
const Model = require('objection').Model
const _ = require('lodash')
const { DateTime, Duration } = require('luxon')
/* global WIKI */
@@ -228,4 +229,15 @@ module.exports = class PageHistory extends Model {
total: history.total
}
}
/**
* Purge history older than X
*
* @param {String} olderThan ISO 8601 Duration
*/
static async purge (olderThan) {
const dur = Duration.fromISO(olderThan)
const olderThanISO = DateTime.utc().minus(dur)
await WIKI.models.pageHistory.query().where('versionDate', '<', olderThanISO.toISO()).del()
}
}