mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-29 12:13:43 -05:00
Merge pull request #16617 from overleaf/dp-mongoose-callback
Convert ProjectHistoryHandler from callbacks to promises GitOrigin-RevId: 6b1841269f6976deb4da663f614c423de7b962b7
This commit is contained in:
parent
7f113250aa
commit
4c973755fa
2 changed files with 92 additions and 112 deletions
|
@ -2,81 +2,61 @@ const { Project } = require('../../models/Project')
|
||||||
const ProjectDetailsHandler = require('./ProjectDetailsHandler')
|
const ProjectDetailsHandler = require('./ProjectDetailsHandler')
|
||||||
const HistoryManager = require('../History/HistoryManager')
|
const HistoryManager = require('../History/HistoryManager')
|
||||||
const ProjectEntityUpdateHandler = require('./ProjectEntityUpdateHandler')
|
const ProjectEntityUpdateHandler = require('./ProjectEntityUpdateHandler')
|
||||||
const { promisifyAll } = require('@overleaf/promise-utils')
|
const { callbackify } = require('util')
|
||||||
|
|
||||||
const ProjectHistoryHandler = {
|
const ProjectHistoryHandler = {
|
||||||
setHistoryId(projectId, historyId, callback) {
|
async setHistoryId(projectId, historyId) {
|
||||||
// reject invalid history ids
|
// reject invalid history ids
|
||||||
if (historyId == null) {
|
if (historyId == null) {
|
||||||
return callback(new Error('missing history id'))
|
throw new Error('missing history id')
|
||||||
}
|
}
|
||||||
|
|
||||||
// use $exists:false to prevent overwriting any existing history id, atomically
|
// use $exists:false to prevent overwriting any existing history id, atomically
|
||||||
Project.updateOne(
|
const result = await Project.updateOne(
|
||||||
{ _id: projectId, 'overleaf.history.id': { $exists: false } },
|
{ _id: projectId, 'overleaf.history.id': { $exists: false } },
|
||||||
{ 'overleaf.history.id': historyId },
|
{ 'overleaf.history.id': historyId }
|
||||||
function (err, result) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err)
|
|
||||||
}
|
|
||||||
if (result.matchedCount === 0) {
|
|
||||||
return callback(new Error('history exists'))
|
|
||||||
}
|
|
||||||
callback()
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (result.matchedCount === 0) {
|
||||||
|
throw new Error('history exists')
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
getHistoryId(projectId, callback) {
|
async getHistoryId(projectId) {
|
||||||
ProjectDetailsHandler.getDetails(projectId, function (err, project) {
|
const project = await ProjectDetailsHandler.promises.getDetails(projectId)
|
||||||
if (err) {
|
return project?.overleaf?.history?.id
|
||||||
return callback(err)
|
|
||||||
} // n.b. getDetails returns an error if the project doesn't exist
|
|
||||||
callback(null, project?.overleaf?.history?.id)
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
|
|
||||||
ensureHistoryExistsForProject(projectId, callback) {
|
async ensureHistoryExistsForProject(projectId) {
|
||||||
// We can only set a history id for a project that doesn't have one. The
|
// 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
|
// history id is cached in the project history service, and changing an
|
||||||
// existing value corrupts the history, leaving it in an irrecoverable
|
// existing value corrupts the history, leaving it in an irrecoverable
|
||||||
// state. Setting a history id when one wasn't present before is ok,
|
// state. Setting a history id when one wasn't present before is ok,
|
||||||
// because undefined history ids aren't cached.
|
// because undefined history ids aren't cached.
|
||||||
ProjectHistoryHandler.getHistoryId(projectId, function (err, historyId) {
|
let historyId = await ProjectHistoryHandler.getHistoryId(projectId)
|
||||||
if (err) {
|
|
||||||
return callback(err)
|
if (historyId != null) {
|
||||||
}
|
return
|
||||||
if (historyId != null) {
|
}
|
||||||
return callback()
|
|
||||||
} // history already exists, success
|
historyId = await HistoryManager.promises.initializeProject(projectId)
|
||||||
HistoryManager.initializeProject(projectId, function (err, historyId) {
|
if (historyId == null) {
|
||||||
if (err) {
|
throw new Error('failed to initialize history id')
|
||||||
return callback(err)
|
}
|
||||||
}
|
|
||||||
if (historyId == null) {
|
await ProjectHistoryHandler.setHistoryId(projectId, historyId)
|
||||||
return callback(new Error('failed to initialize history id'))
|
|
||||||
}
|
await ProjectEntityUpdateHandler.promises.resyncProjectHistory(projectId)
|
||||||
ProjectHistoryHandler.setHistoryId(
|
|
||||||
projectId,
|
await HistoryManager.promises.flushProject(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 = {
|
||||||
module.exports = ProjectHistoryHandler
|
setHistoryId: callbackify(ProjectHistoryHandler.setHistoryId),
|
||||||
|
getHistoryId: callbackify(ProjectHistoryHandler.getHistoryId),
|
||||||
|
ensureHistoryExistsForProject: callbackify(
|
||||||
|
ProjectHistoryHandler.ensureHistoryExistsForProject
|
||||||
|
),
|
||||||
|
promises: ProjectHistoryHandler,
|
||||||
|
}
|
||||||
|
|
|
@ -48,50 +48,55 @@ describe('ProjectHistoryHandler', function () {
|
||||||
'../../models/Project': {
|
'../../models/Project': {
|
||||||
Project: this.ProjectModel,
|
Project: this.ProjectModel,
|
||||||
},
|
},
|
||||||
'./ProjectDetailsHandler': (this.ProjectDetailsHandler = {}),
|
'./ProjectDetailsHandler': (this.ProjectDetailsHandler = {
|
||||||
'../History/HistoryManager': (this.HistoryManager = {}),
|
promises: {},
|
||||||
'./ProjectEntityUpdateHandler': (this.ProjectEntityUpdateHandler = {}),
|
}),
|
||||||
|
'../History/HistoryManager': (this.HistoryManager = {
|
||||||
|
promises: {},
|
||||||
|
}),
|
||||||
|
'./ProjectEntityUpdateHandler': (this.ProjectEntityUpdateHandler = {
|
||||||
|
promises: {},
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('starting history for an existing project', function () {
|
describe('starting history for an existing project', function () {
|
||||||
beforeEach(function () {
|
beforeEach(async function () {
|
||||||
this.HistoryManager.initializeProject = sinon
|
this.HistoryManager.promises.initializeProject = sinon
|
||||||
.stub()
|
.stub()
|
||||||
.yields(null, this.historyId)
|
.resolves(this.historyId)
|
||||||
this.HistoryManager.flushProject = sinon.stub().callsArg(1)
|
this.HistoryManager.promises.flushProject = sinon.stub()
|
||||||
return (this.ProjectEntityUpdateHandler.resyncProjectHistory = sinon
|
|
||||||
.stub()
|
return (this.ProjectEntityUpdateHandler.promises.resyncProjectHistory =
|
||||||
.callsArg(1))
|
sinon.stub())
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('when the history does not already exist', function () {
|
describe('when the history does not already exist', function () {
|
||||||
beforeEach(function () {
|
beforeEach(async function () {
|
||||||
this.ProjectDetailsHandler.getDetails = sinon
|
this.ProjectDetailsHandler.promises.getDetails = sinon
|
||||||
.stub()
|
.stub()
|
||||||
.withArgs(projectId)
|
.withArgs(projectId)
|
||||||
.callsArgWith(1, null, this.project)
|
.resolves(this.project)
|
||||||
this.ProjectModel.updateOne = sinon
|
this.ProjectModel.updateOne = sinon.stub().resolves({ matchedCount: 1 })
|
||||||
.stub()
|
return this.ProjectHistoryHandler.promises.ensureHistoryExistsForProject(
|
||||||
.callsArgWith(2, null, { matchedCount: 1 })
|
projectId
|
||||||
return this.ProjectHistoryHandler.ensureHistoryExistsForProject(
|
|
||||||
projectId,
|
|
||||||
this.callback
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should get any existing history id for the project', function () {
|
it('should get any existing history id for the project', async function () {
|
||||||
return this.ProjectDetailsHandler.getDetails
|
return this.ProjectDetailsHandler.promises.getDetails
|
||||||
.calledWith(projectId)
|
.calledWith(projectId)
|
||||||
.should.equal(true)
|
.should.equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should initialize a new history in the v1 history service', function () {
|
it('should initialize a new history in the v1 history service', async function () {
|
||||||
return this.HistoryManager.initializeProject.called.should.equal(true)
|
return this.HistoryManager.promises.initializeProject.called.should.equal(
|
||||||
|
true
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should set the new history id on the project', function () {
|
it('should set the new history id on the project', async function () {
|
||||||
return this.ProjectModel.updateOne
|
return this.ProjectModel.updateOne
|
||||||
.calledWith(
|
.calledWith(
|
||||||
{ _id: projectId, 'overleaf.history.id': { $exists: false } },
|
{ _id: projectId, 'overleaf.history.id': { $exists: false } },
|
||||||
|
@ -100,63 +105,58 @@ describe('ProjectHistoryHandler', function () {
|
||||||
.should.equal(true)
|
.should.equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should resync the project history', function () {
|
it('should resync the project history', async function () {
|
||||||
return this.ProjectEntityUpdateHandler.resyncProjectHistory
|
return this.ProjectEntityUpdateHandler.promises.resyncProjectHistory
|
||||||
.calledWith(projectId)
|
.calledWith(projectId)
|
||||||
.should.equal(true)
|
.should.equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should flush the project history', function () {
|
it('should flush the project history', async function () {
|
||||||
return this.HistoryManager.flushProject
|
return this.HistoryManager.promises.flushProject
|
||||||
.calledWith(projectId)
|
.calledWith(projectId)
|
||||||
.should.equal(true)
|
.should.equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should call the callback without an error', function () {
|
|
||||||
return this.callback.called.should.equal(true)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('when the history already exists', function () {
|
describe('when the history already exists', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
this.project.overleaf = { history: { id: 1234 } }
|
this.project.overleaf = { history: { id: 1234 } }
|
||||||
this.ProjectDetailsHandler.getDetails = sinon
|
this.ProjectDetailsHandler.promises.getDetails = sinon
|
||||||
.stub()
|
.stub()
|
||||||
.withArgs(projectId)
|
.withArgs(projectId)
|
||||||
.callsArgWith(1, null, this.project)
|
.resolves(this.project)
|
||||||
this.ProjectModel.updateOne = sinon.stub()
|
this.ProjectModel.updateOne = sinon.stub().resolves({ matchedCount: 1 })
|
||||||
return this.ProjectHistoryHandler.ensureHistoryExistsForProject(
|
return this.ProjectHistoryHandler.promises.ensureHistoryExistsForProject(
|
||||||
projectId,
|
projectId
|
||||||
this.callback
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should get any existing history id for the project', function () {
|
it('should get any existing history id for the project', async function () {
|
||||||
return this.ProjectDetailsHandler.getDetails
|
return this.ProjectDetailsHandler.promises.getDetails
|
||||||
.calledWith(projectId)
|
.calledWith(projectId)
|
||||||
.should.equal(true)
|
.should.equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not initialize a new history in the v1 history service', function () {
|
it('should not initialize a new history in the v1 history service', async function () {
|
||||||
return this.HistoryManager.initializeProject.called.should.equal(false)
|
return this.HistoryManager.promises.initializeProject.called.should.equal(
|
||||||
})
|
|
||||||
|
|
||||||
it('should not set the new history id on the project', function () {
|
|
||||||
return this.ProjectModel.updateOne.called.should.equal(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should not resync the project history', function () {
|
|
||||||
return this.ProjectEntityUpdateHandler.resyncProjectHistory.called.should.equal(
|
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not flush the project history', function () {
|
it('should not set the new history id on the project', async function () {
|
||||||
return this.HistoryManager.flushProject.called.should.equal(false)
|
return this.ProjectModel.updateOne.called.should.equal(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should call the callback', function () {
|
it('should not resync the project history', async function () {
|
||||||
return this.callback.calledWith().should.equal(true)
|
return this.ProjectEntityUpdateHandler.promises.resyncProjectHistory.called.should.equal(
|
||||||
|
false
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not flush the project history', async function () {
|
||||||
|
return this.HistoryManager.promises.flushProject.called.should.equal(
|
||||||
|
false
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue