[web] Promisify TemplatesController (#18849)

* Promisify TemplatesController

* Update TemplatesControllerTests

* Fix `templateVersionId` in `getV1Template` (!!)

GitOrigin-RevId: bdaa59ed3cff81d919a8b3d19d5be555a2790f55
This commit is contained in:
Antoine Clausse 2024-06-27 10:08:32 +02:00 committed by Copybot
parent 0ecb266e32
commit 1dbbe5af9d
2 changed files with 40 additions and 54 deletions

View file

@ -1,23 +1,11 @@
/* eslint-disable
max-len,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let TemplatesController
const path = require('path')
const SessionManager = require('../Authentication/SessionManager')
const TemplatesManager = require('./TemplatesManager')
const ProjectHelper = require('../Project/ProjectHelper')
const logger = require('@overleaf/logger')
const { expressify } = require('@overleaf/promise-utils')
module.exports = TemplatesController = {
const TemplatesController = {
getV1Template(req, res) {
const templateVersionId = req.params.Template_version_id
const templateId = req.query.id
@ -28,14 +16,15 @@ module.exports = TemplatesController = {
)
return res.sendStatus(400)
}
const data = {}
data.templateVersionId = templateVersionId
data.templateId = templateId
data.name = req.query.templateName
data.compiler = ProjectHelper.compilerFromV1Engine(req.query.latexEngine)
data.imageName = req.query.texImage
data.mainFile = req.query.mainFile
data.brandVariationId = req.query.brandVariationId
const data = {
templateVersionId,
templateId,
name: req.query.templateName,
compiler: ProjectHelper.compilerFromV1Engine(req.query.latexEngine),
imageName: req.query.texImage,
mainFile: req.query.mainFile,
brandVariationId: req.query.brandVariationId,
}
return res.render(
path.resolve(
__dirname,
@ -45,9 +34,9 @@ module.exports = TemplatesController = {
)
},
createProjectFromV1Template(req, res, next) {
async createProjectFromV1Template(req, res) {
const userId = SessionManager.getLoggedInUserId(req.session)
return TemplatesManager.createProjectFromV1Template(
const project = await TemplatesManager.promises.createProjectFromV1Template(
req.body.brandVariationId,
req.body.compiler,
req.body.mainFile,
@ -55,14 +44,19 @@ module.exports = TemplatesController = {
req.body.templateName,
req.body.templateVersionId,
userId,
req.body.imageName,
function (err, project) {
if (err != null) {
return next(err)
}
delete req.session.templateData
return res.redirect(`/project/${project._id}`)
}
req.body.imageName
)
delete req.session.templateData
if (!project) {
throw new Error('failed to create project from template')
}
return res.redirect(`/project/${project._id}`)
},
}
module.exports = {
getV1Template: TemplatesController.getV1Template,
createProjectFromV1Template: expressify(
TemplatesController.createProjectFromV1Template
),
}

View file

@ -1,17 +1,4 @@
/* eslint-disable
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const SandboxedModule = require('sandboxed-module')
const assert = require('assert')
const { expect } = require('chai')
const sinon = require('sinon')
const ProjectHelper = require('../../../../app/src/Features/Project/ProjectHelper')
@ -29,7 +16,7 @@ describe('TemplatesController', function () {
getLoggedInUserId: sinon.stub().returns(this.user_id),
}),
'./TemplatesManager': (this.TemplatesManager = {
createProjectFromV1Template: sinon.stub(),
promises: { createProjectFromV1Template: sinon.stub() },
}),
},
})
@ -55,13 +42,13 @@ describe('TemplatesController', function () {
describe('createProjectFromV1Template', function () {
describe('on success', function () {
beforeEach(function () {
beforeEach(function (done) {
this.project = { _id: 'project-id' }
this.TemplatesManager.createProjectFromV1Template.yields(
null,
this.TemplatesManager.promises.createProjectFromV1Template.resolves(
this.project
)
return this.TemplatesController.createProjectFromV1Template(
this.res.redirect.callsFake(() => done())
this.TemplatesController.createProjectFromV1Template(
this.req,
this.res,
this.next
@ -69,7 +56,7 @@ describe('TemplatesController', function () {
})
it('should call TemplatesManager', function () {
return this.TemplatesManager.createProjectFromV1Template.should.have.been.calledWithMatch(
return this.TemplatesManager.promises.createProjectFromV1Template.should.have.been.calledWithMatch(
'brand-variation-id',
'compiler',
'main-file',
@ -92,8 +79,11 @@ describe('TemplatesController', function () {
})
describe('on error', function () {
beforeEach(function () {
this.TemplatesManager.createProjectFromV1Template.yields('error')
beforeEach(function (done) {
this.TemplatesManager.promises.createProjectFromV1Template.rejects(
'error'
)
this.next.callsFake(() => done())
return this.TemplatesController.createProjectFromV1Template(
this.req,
this.res,
@ -102,7 +92,9 @@ describe('TemplatesController', function () {
})
it('should call next with error', function () {
return this.next.should.have.been.calledWith('error')
return this.next.should.have.been.calledWithMatch(
sinon.match.instanceOf(Error)
)
})
it('should not redirect', function () {