From a46c2ced7c3ddfc015719f3966849e7bc57274b2 Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 19 Dec 2022 07:26:07 +0100 Subject: [PATCH 1/8] refs #4975: Added last mdbVersion/last method --- loopback/locale/es.json | 9 +++-- modules/mdb/back/methods/mdbVersion/last.js | 40 +++++++++++++++++++ modules/mdb/back/methods/mdbVersion/upload.js | 6 +-- modules/mdb/back/models/mdbVersion.js | 1 + 4 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 modules/mdb/back/methods/mdbVersion/last.js diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 932dfe98f..478b0da15 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -247,7 +247,8 @@ "Empty data source": "Origen de datos vacio", "Email verify": "Correo de verificación", "Landing cannot be lesser than shipment": "Landing cannot be lesser than shipment", - "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" -} + "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", + "Not exist this app name": "No existe este nombre de aplicación" +} \ 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..91329be4e --- /dev/null +++ b/modules/mdb/back/methods/mdbVersion/last.js @@ -0,0 +1,40 @@ +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethodCtx('last', { + description: 'Upload and attach a access file', + accepts: [ + { + arg: 'appName', + type: 'string', + required: true, + description: 'The app name' + } + ], + returns: { + type: ['object'], + 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} + }); + + if (!versions.length) + throw new UserError('Not exist this app name'); + + let maxNumber = 0; + for (let mdb of versions) { + if (mdb.version > maxNumber) + maxNumber = mdb.version; + } + return maxNumber; + }; +}; diff --git a/modules/mdb/back/methods/mdbVersion/upload.js b/modules/mdb/back/methods/mdbVersion/upload.js index 57df35ce7..d165a7bb5 100644 --- a/modules/mdb/back/methods/mdbVersion/upload.js +++ b/modules/mdb/back/methods/mdbVersion/upload.js @@ -11,14 +11,12 @@ module.exports = Self => { type: 'string', required: true, description: 'The app name' - }, - { + }, { arg: 'newVersion', type: 'number', required: true, description: `The new version number` - }, - { + }, { arg: 'branch', type: 'string', required: true, 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); }; From dc82e610b7f58739221cd7c56505af83a52af5f2 Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 19 Dec 2022 11:24:18 +0100 Subject: [PATCH 2/8] refs #4975 Minor changes --- loopback/locale/es.json | 2 +- modules/mdb/back/methods/mdbVersion/last.js | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 478b0da15..c2dad5e03 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -250,5 +250,5 @@ "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", - "Not exist this app name": "No existe este nombre de aplicación" + "App name does not exist": "El nombre de aplicación no es válido" } \ No newline at end of file diff --git a/modules/mdb/back/methods/mdbVersion/last.js b/modules/mdb/back/methods/mdbVersion/last.js index 91329be4e..7e4a18ac3 100644 --- a/modules/mdb/back/methods/mdbVersion/last.js +++ b/modules/mdb/back/methods/mdbVersion/last.js @@ -12,7 +12,7 @@ module.exports = Self => { } ], returns: { - type: ['object'], + type: 'number', root: true }, http: { @@ -24,11 +24,12 @@ module.exports = Self => { Self.last = async(ctx, appName) => { const models = Self.app.models; const versions = await models.MdbVersion.find({ - where: {app: appName} + where: {app: appName}, + fields: ['version'] }); if (!versions.length) - throw new UserError('Not exist this app name'); + throw new UserError('App name does not exist'); let maxNumber = 0; for (let mdb of versions) { From d97a2738a4ba9bdd46f3e7eef953a324208cf201 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 22 Dec 2022 07:12:09 +0100 Subject: [PATCH 3/8] refs #4975 Upload method modified and addedd model --- modules/mdb/back/methods/mdbVersion/upload.js | 53 +++++++++++++------ modules/mdb/back/models/mdbVersionTree.json | 35 ++++++++++++ 2 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 modules/mdb/back/models/mdbVersionTree.json diff --git a/modules/mdb/back/methods/mdbVersion/upload.js b/modules/mdb/back/methods/mdbVersion/upload.js index d165a7bb5..8c77281b1 100644 --- a/modules/mdb/back/methods/mdbVersion/upload.js +++ b/modules/mdb/back/methods/mdbVersion/upload.js @@ -12,7 +12,7 @@ module.exports = Self => { required: true, description: 'The app name' }, { - arg: 'newVersion', + arg: 'toVersion', type: 'number', required: true, description: `The new version number` @@ -21,6 +21,11 @@ module.exports = Self => { type: 'string', required: true, description: `The branch name` + }, { + arg: 'fromVersion', + type: 'string', + required: true, + description: `The old version number` } ], returns: { @@ -33,14 +38,12 @@ module.exports = Self => { } }); - Self.upload = async(ctx, appName, newVersion, branch, options) => { + Self.upload = async(ctx, appName, toVersion, branch, fromVersion, options) => { const models = Self.app.models; const myOptions = {}; - const TempContainer = models.TempContainer; const AccessContainer = models.AccessContainer; const fileOptions = {}; - let tx; if (typeof options == 'object') @@ -53,6 +56,29 @@ module.exports = Self => { let srcFile; try { + const existBranch = await models.MdbBranch.findOne({ + where: {name: branch} + }, myOptions); + + if (!existBranch) + throw new UserError('Not exist this branch'); + + let lastVersion = await models.MdbBranch.findOne({ + where: {appName} + }, myOptions); + + if (lastVersion++ != toVersion) + throw new UserError('Try again'); + + const userId = ctx.req.accessToken.userId; + models.MdbVersionTree.create({ + app: appName, + version: toVersion, + branchFk: branch, + fromVersion, + userFk: userId + }); + 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 => { @@ -65,7 +91,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); @@ -75,18 +101,11 @@ module.exports = Self => { }); await fs.chmod(destinationFile, 0o644); - const existBranch = await models.MdbBranch.findOne({ - where: {name: branch} - }); - - if (!existBranch) - throw new UserError('Not exist this branch'); - const branchPath = path.join(accessContainer.client.root, 'branches', branch); 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) {} @@ -94,7 +113,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) {} @@ -105,15 +124,15 @@ module.exports = Self => { await models.MdbVersion.upsert({ app: appName, branchFk: branch, - version: newVersion - }); + version: toVersion + }, myOptions); if (tx) await tx.commit(); } catch (e) { if (tx) await tx.rollback(); if (fs.existsSync(srcFile)) - await fs.unlink(srcFile); + fs.unlink(srcFile); throw e; } 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 From 5a25b0ef4c85b2ec0a5d80e1513fb7d927cc78f9 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 22 Dec 2022 07:12:44 +0100 Subject: [PATCH 4/8] refs #4975 Added model MdbVersionTree --- modules/mdb/back/model-config.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/mdb/back/model-config.json b/modules/mdb/back/model-config.json index d5be8de87..1384aaf76 100644 --- a/modules/mdb/back/model-config.json +++ b/modules/mdb/back/model-config.json @@ -5,6 +5,9 @@ "MdbVersion": { "dataSource": "vn" }, + "MdbVersionTree": { + "dataSource": "vn" + }, "AccessContainer": { "dataSource": "accessStorage" } From 2803de94456bb90f87647bf3f1a93eb822c984d5 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 5 Jan 2023 14:20:47 +0100 Subject: [PATCH 5/8] refs #4975 Return JSON {version:xxx} --- modules/mdb/back/methods/mdbVersion/last.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/mdb/back/methods/mdbVersion/last.js b/modules/mdb/back/methods/mdbVersion/last.js index 7e4a18ac3..802899f76 100644 --- a/modules/mdb/back/methods/mdbVersion/last.js +++ b/modules/mdb/back/methods/mdbVersion/last.js @@ -36,6 +36,11 @@ module.exports = Self => { if (mdb.version > maxNumber) maxNumber = mdb.version; } - return maxNumber; + + let response = { + version: maxNumber + }; + + return response; }; }; From f23eb9f4ad85bde2d4afe2152807597db50ab24e Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 9 Jan 2023 18:28:09 +0100 Subject: [PATCH 6/8] refs #4975 Added call to last method in upload --- modules/mdb/back/methods/mdbVersion/upload.js | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/modules/mdb/back/methods/mdbVersion/upload.js b/modules/mdb/back/methods/mdbVersion/upload.js index 8c77281b1..fa196b58d 100644 --- a/modules/mdb/back/methods/mdbVersion/upload.js +++ b/modules/mdb/back/methods/mdbVersion/upload.js @@ -63,22 +63,13 @@ module.exports = Self => { if (!existBranch) throw new UserError('Not exist this branch'); - let lastVersion = await models.MdbBranch.findOne({ - where: {appName} - }, myOptions); + let lastMethod = await Self.last(ctx, appName, myOptions); + lastMethod.version++; - if (lastVersion++ != toVersion) + if (lastMethod.version != toVersion) throw new UserError('Try again'); const userId = ctx.req.accessToken.userId; - models.MdbVersionTree.create({ - app: appName, - version: toVersion, - branchFk: branch, - fromVersion, - userFk: userId - }); - 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 => { @@ -121,6 +112,14 @@ 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, From e0e34595f4d49b41c4de75cbf43b484c4a94a2e6 Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 9 Jan 2023 18:46:19 +0100 Subject: [PATCH 7/8] refs #4975 Added translations --- loopback/locale/es.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/loopback/locale/es.json b/loopback/locale/es.json index c2dad5e03..bc23e3c0f 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -250,5 +250,6 @@ "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" + "App name does not exist": "El nombre de aplicación no es válido", + "Try again": "Vuelve a intentarlo" } \ No newline at end of file From d448f41ac159493dcfe6de3d704500517d870fed Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 11 Jan 2023 15:39:33 +0100 Subject: [PATCH 8/8] refs #4975 Changed description of last method --- modules/mdb/back/methods/mdbVersion/last.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/mdb/back/methods/mdbVersion/last.js b/modules/mdb/back/methods/mdbVersion/last.js index 802899f76..5f89f10fb 100644 --- a/modules/mdb/back/methods/mdbVersion/last.js +++ b/modules/mdb/back/methods/mdbVersion/last.js @@ -2,7 +2,7 @@ const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('last', { - description: 'Upload and attach a access file', + description: 'Gets the latest version of a access file', accepts: [ { arg: 'appName',