diff --git a/loopback/locale/es.json b/loopback/locale/es.json index fe256e2ea..f6f305dc3 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -252,6 +252,9 @@ "Receipt's bank was not found": "No se encontró el banco del recibo", "This receipt was not compensated": "Este recibo no ha sido compensado", "Client's email was not found": "No se encontró el email del cliente", + "App name does not exist": "El nombre de aplicación no es válido", + "Try again": "Vuelve a intentarlo", + "Aplicación bloqueada por el usuario 9": "Aplicación bloqueada por el usuario 9", "Failed to upload file": "Error al subir archivo", "The DOCUWARE PDF document does not exists": "The DOCUWARE PDF document does not exists" } \ No newline at end of file diff --git a/modules/mdb/back/methods/mdbVersion/last.js b/modules/mdb/back/methods/mdbVersion/last.js new file mode 100644 index 000000000..5f89f10fb --- /dev/null +++ b/modules/mdb/back/methods/mdbVersion/last.js @@ -0,0 +1,46 @@ +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethodCtx('last', { + description: 'Gets the latest version of a access file', + accepts: [ + { + arg: 'appName', + type: 'string', + required: true, + description: 'The app name' + } + ], + returns: { + type: 'number', + root: true + }, + http: { + path: `/:appName/last`, + verb: 'GET' + } + }); + + Self.last = async(ctx, appName) => { + const models = Self.app.models; + const versions = await models.MdbVersion.find({ + where: {app: appName}, + fields: ['version'] + }); + + if (!versions.length) + throw new UserError('App name does not exist'); + + let maxNumber = 0; + for (let mdb of versions) { + if (mdb.version > maxNumber) + maxNumber = mdb.version; + } + + let response = { + version: maxNumber + }; + + return response; + }; +}; diff --git a/modules/mdb/back/methods/mdbVersion/upload.js b/modules/mdb/back/methods/mdbVersion/upload.js index ea88c58f7..1df4365a9 100644 --- a/modules/mdb/back/methods/mdbVersion/upload.js +++ b/modules/mdb/back/methods/mdbVersion/upload.js @@ -11,20 +11,22 @@ module.exports = Self => { type: 'string', required: true, description: 'The app name' - }, - { - arg: 'newVersion', + }, { + arg: 'toVersion', type: 'number', required: true, description: `The new version number` - }, - { + }, { arg: 'branch', type: 'string', required: true, description: `The branch name` - }, - { + }, { + arg: 'fromVersion', + type: 'string', + required: true, + description: `The old version number` + }, { arg: 'unlock', type: 'boolean', required: false, @@ -41,16 +43,13 @@ module.exports = Self => { } }); - Self.upload = async(ctx, appName, newVersion, branch, unlock, options) => { + Self.upload = async(ctx, appName, toVersion, branch, fromVersion, unlock, options) => { const models = Self.app.models; - const userId = ctx.req.accessToken.userId; const myOptions = {}; const $t = ctx.req.__; // $translate - const TempContainer = models.TempContainer; const AccessContainer = models.AccessContainer; const fileOptions = {}; - let tx; if (typeof options == 'object') @@ -63,6 +62,7 @@ module.exports = Self => { let srcFile; try { + const userId = ctx.req.accessToken.userId; const mdbApp = await models.MdbApp.findById(appName, null, myOptions); if (mdbApp && mdbApp.locked && mdbApp.userFk != userId) { @@ -71,6 +71,19 @@ module.exports = Self => { })); } + const existBranch = await models.MdbBranch.findOne({ + where: {name: branch} + }, myOptions); + + if (!existBranch) + throw new UserError('Not exist this branch'); + + let lastMethod = await Self.last(ctx, appName, myOptions); + lastMethod.version++; + + if (lastMethod.version != toVersion) + throw new UserError('Try again'); + const tempContainer = await TempContainer.container('access'); const uploaded = await TempContainer.upload(tempContainer.name, ctx.req, ctx.result, fileOptions); const files = Object.values(uploaded.files).map(file => { @@ -83,7 +96,7 @@ module.exports = Self => { const accessContainer = await AccessContainer.container('.archive'); const destinationFile = path.join( - accessContainer.client.root, accessContainer.name, appName, `${newVersion}.7z`); + accessContainer.client.root, accessContainer.name, appName, `${toVersion}.7z`); if (process.env.NODE_ENV == 'test') await fs.unlink(srcFile); @@ -104,7 +117,7 @@ module.exports = Self => { await fs.mkdir(branchPath, {recursive: true}); const destinationBranch = path.join(branchPath, `${appName}.7z`); - const destinationRelative = `../../.archive/${appName}/${newVersion}.7z`; + const destinationRelative = `../../.archive/${appName}/${toVersion}.7z`; try { await fs.unlink(destinationBranch); } catch (e) {} @@ -112,7 +125,7 @@ module.exports = Self => { if (branch == 'master') { const destinationRoot = path.join(accessContainer.client.root, `${appName}.7z`); - const rootRelative = `./.archive/${appName}/${newVersion}.7z`; + const rootRelative = `./.archive/${appName}/${toVersion}.7z`; try { await fs.unlink(destinationRoot); } catch (e) {} @@ -120,10 +133,18 @@ module.exports = Self => { } } + await models.MdbVersionTree.create({ + app: appName, + version: toVersion, + branchFk: branch, + fromVersion, + userFk: userId + }, myOptions); + await models.MdbVersion.upsert({ app: appName, branchFk: branch, - version: newVersion + version: toVersion }, myOptions); if (unlock) await models.MdbApp.unlock(ctx, appName, myOptions); @@ -133,7 +154,7 @@ module.exports = Self => { if (tx) await tx.rollback(); if (fs.existsSync(srcFile)) - await fs.unlink(srcFile); + fs.unlink(srcFile); throw e; } diff --git a/modules/mdb/back/model-config.json b/modules/mdb/back/model-config.json index 6107f8790..8010afca2 100644 --- a/modules/mdb/back/model-config.json +++ b/modules/mdb/back/model-config.json @@ -8,6 +8,9 @@ "MdbVersion": { "dataSource": "vn" }, + "MdbVersionTree": { + "dataSource": "vn" + }, "AccessContainer": { "dataSource": "accessStorage" } diff --git a/modules/mdb/back/models/mdbVersion.js b/modules/mdb/back/models/mdbVersion.js index b36ee2a60..3a7a0c6f3 100644 --- a/modules/mdb/back/models/mdbVersion.js +++ b/modules/mdb/back/models/mdbVersion.js @@ -1,3 +1,4 @@ module.exports = Self => { require('../methods/mdbVersion/upload')(Self); + require('../methods/mdbVersion/last')(Self); }; diff --git a/modules/mdb/back/models/mdbVersionTree.json b/modules/mdb/back/models/mdbVersionTree.json new file mode 100644 index 000000000..8c0260e54 --- /dev/null +++ b/modules/mdb/back/models/mdbVersionTree.json @@ -0,0 +1,35 @@ +{ + "name": "MdbVersionTree", + "base": "VnModel", + "options": { + "mysql": { + "table": "mdbVersionTree" + } + }, + "properties": { + "app": { + "type": "string", + "description": "The app name", + "id": true + }, + "version": { + "type": "number" + }, + "branchFk": { + "type": "string" + }, + "fromVersion": { + "type": "number" + }, + "userFk": { + "type": "number" + } + }, + "relations": { + "branch": { + "type": "belongsTo", + "model": "MdbBranch", + "foreignKey": "branchFk" + } + } +} \ No newline at end of file