Files
Verso/services/web/app/src/Features/Project/ProjectHistoryHandler.js
T
Eric Mc Sween 680ebae30b Merge pull request #15172 from overleaf/em-promise-utils
Move util/promises from web into a shared library

GitOrigin-RevId: fe1980dc57b9dc8ce86fa1fad6a8a817e9505b3d
2023-10-20 08:04:05 +00:00

83 lines
2.7 KiB
JavaScript

const { Project } = require('../../models/Project')
const ProjectDetailsHandler = require('./ProjectDetailsHandler')
const HistoryManager = require('../History/HistoryManager')
const ProjectEntityUpdateHandler = require('./ProjectEntityUpdateHandler')
const { promisifyAll } = require('@overleaf/promise-utils')
const ProjectHistoryHandler = {
setHistoryId(projectId, historyId, callback) {
// reject invalid history ids
if (historyId == null) {
return callback(new Error('missing history id'))
}
// use $exists:false to prevent overwriting any existing history id, atomically
Project.updateOne(
{ _id: projectId, 'overleaf.history.id': { $exists: false } },
{ 'overleaf.history.id': historyId },
function (err, result) {
if (err) {
return callback(err)
}
if (result.matchedCount === 0) {
return callback(new Error('history exists'))
}
callback()
}
)
},
getHistoryId(projectId, callback) {
ProjectDetailsHandler.getDetails(projectId, function (err, project) {
if (err) {
return callback(err)
} // n.b. getDetails returns an error if the project doesn't exist
callback(null, project?.overleaf?.history?.id)
})
},
ensureHistoryExistsForProject(projectId, callback) {
// We can only set a history id for a project that doesn't have one. The
// history id is cached in the project history service, and changing an
// existing value corrupts the history, leaving it in an irrecoverable
// state. Setting a history id when one wasn't present before is ok,
// because undefined history ids aren't cached.
ProjectHistoryHandler.getHistoryId(projectId, function (err, historyId) {
if (err) {
return callback(err)
}
if (historyId != null) {
return callback()
} // history already exists, success
HistoryManager.initializeProject(projectId, function (err, historyId) {
if (err) {
return callback(err)
}
if (historyId == null) {
return callback(new Error('failed to initialize history id'))
}
ProjectHistoryHandler.setHistoryId(
projectId,
historyId,
function (err) {
if (err) {
return callback(err)
}
ProjectEntityUpdateHandler.resyncProjectHistory(
projectId,
function (err) {
if (err) {
return callback(err)
}
HistoryManager.flushProject(projectId, callback)
}
)
}
)
})
})
},
}
ProjectHistoryHandler.promises = promisifyAll(ProjectHistoryHandler)
module.exports = ProjectHistoryHandler