From 79c42d04f9fc5a0574d36f45e7166bb77a74f48f Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 25 May 2022 14:59:28 +0200 Subject: [PATCH 1/8] feat(mdb): start section --- db/changes/10470-family/00-aclMdb.sql | 14 ++++ db/changes/10470-family/delete.keep | 0 db/dump/fixtures.sql | 9 +++ docker-compose.yml | 1 + loopback/server/datasources.json | 20 +++++ .../mdbVersion/specs/filterValue.spec.js | 76 +++++++++++++++++++ modules/mdb/back/methods/mdbVersion/upload.js | 50 ++++++++++++ modules/mdb/back/model-config.json | 11 +++ modules/mdb/back/models/mdb-container.json | 10 +++ modules/mdb/back/models/mdbBranch.json | 16 ++++ modules/mdb/back/models/mdbVersion.js | 3 + modules/mdb/back/models/mdbVersion.json | 25 ++++++ 12 files changed, 235 insertions(+) create mode 100644 db/changes/10470-family/00-aclMdb.sql delete mode 100644 db/changes/10470-family/delete.keep create mode 100644 modules/mdb/back/methods/mdbVersion/specs/filterValue.spec.js create mode 100644 modules/mdb/back/methods/mdbVersion/upload.js create mode 100644 modules/mdb/back/model-config.json create mode 100644 modules/mdb/back/models/mdb-container.json create mode 100644 modules/mdb/back/models/mdbBranch.json create mode 100644 modules/mdb/back/models/mdbVersion.js create mode 100644 modules/mdb/back/models/mdbVersion.json diff --git a/db/changes/10470-family/00-aclMdb.sql b/db/changes/10470-family/00-aclMdb.sql new file mode 100644 index 0000000000..e14fa3daad --- /dev/null +++ b/db/changes/10470-family/00-aclMdb.sql @@ -0,0 +1,14 @@ +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES('MdbVersion', '*', '*', 'ALLOW', 'ROLE', 'developer'); + +CREATE TABLE `vn`.`mdbBranch` ( + `name` VARCHAR(255), + PRIMARY KEY(`name`) +); + +CREATE TABLE `vn`.`mdbVersion` ( + `app` VARCHAR(255) NOT NULL, + `branchFk` VARCHAR(255) NOT NULL, + `version` INT, + CONSTRAINT `mdbVersion_branchFk` FOREIGN KEY (`branchFk`) REFERENCES `vn`.`mdbBranch` (`name`) ON DELETE CASCADE ON UPDATE CASCADE +); diff --git a/db/changes/10470-family/delete.keep b/db/changes/10470-family/delete.keep deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index c69012f414..087d66ac5e 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2577,3 +2577,12 @@ INSERT INTO `vn`.`machineWorker` (`workerFk`, `machineFk`, `inTimed`, `outTimed` (1106, 2, CURDATE(), NULL), (1106, 2, DATE_ADD(CURDATE(), INTERVAL + 1 DAY), DATE_ADD(CURDATE(), INTERVAL +1 DAY)); +INSERT INTO `vn`.`mdbBranch` (`name`) + VALUES + ('test'), + ('master'); + +INSERT INTO `vn`.`mdbVersion` (`app`, `branchFk`, `version`) + VALUES + ('tpv', 'test', '1'), + ('lab', 'master', '2'); \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 1d80a4b62d..4fc5dc8117 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -32,6 +32,7 @@ services: - /mnt/appdata/pdfs:/var/lib/salix/pdfs - /mnt/appdata/dms:/var/lib/salix/dms - /mnt/appdata/image:/var/lib/salix/image + - /mnt/appdata/vn-access:/var/lib/salix/vn-access deploy: replicas: ${BACK_REPLICAS:?} placement: diff --git a/loopback/server/datasources.json b/loopback/server/datasources.json index f51beeb194..73f3a7bf0d 100644 --- a/loopback/server/datasources.json +++ b/loopback/server/datasources.json @@ -98,5 +98,25 @@ "image/jpg", "video/mp4" ] + }, + "mdbStorage": { + "name": "mdbStorage", + "connector": "loopback-component-storage", + "provider": "filesystem", + "root": "./storage/mdb", + "maxFileSize": "262144000", + "allowedContentTypes": [ + "application/x-7z-compressed", + "application/x-zip-compressed", + "application/x-rar-compressed", + "application/octet-stream", + "application/pdf", + "application/zip", + "application/rar", + "multipart/x-zip", + "image/png", + "image/jpeg", + "image/jpg" + ] } } \ No newline at end of file diff --git a/modules/mdb/back/methods/mdbVersion/specs/filterValue.spec.js b/modules/mdb/back/methods/mdbVersion/specs/filterValue.spec.js new file mode 100644 index 0000000000..9e5cf2da26 --- /dev/null +++ b/modules/mdb/back/methods/mdbVersion/specs/filterValue.spec.js @@ -0,0 +1,76 @@ +const models = require('vn-loopback/server/server').models; + +describe('tag filterValue()', () => { + const colorTagId = 1; + it('should return a list of color values', async() => { + const tx = await models.Item.beginTransaction({}); + const options = {transaction: tx}; + + try { + const filter = {limit: 5}; + const result = await models.Tag.filterValue(colorTagId, filter, options); + + expect(result.length).toEqual(5); + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it('should return the values matching color "Blue"', async() => { + const tx = await models.Item.beginTransaction({}); + const options = {transaction: tx}; + + try { + const filter = {where: {value: 'Blue'}, limit: 5}; + const result = await models.Tag.filterValue(colorTagId, filter, options); + + expect(result.length).toEqual(2); + expect(result[0].value).toEqual('Blue'); + expect(result[1].value).toEqual('Blue/Silver'); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it('should return the values matching color "Blue/Silver"', async() => { + const tx = await models.Item.beginTransaction({}); + const options = {transaction: tx}; + + try { + const filter = {where: {value: 'Blue/Silver'}, limit: 5}; + const result = await models.Tag.filterValue(colorTagId, filter, options); + + expect(result.length).toEqual(1); + expect(result[0].value).toEqual('Blue/Silver'); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it('should return the values matching color "Silver"', async() => { + const tx = await models.Item.beginTransaction({}); + const options = {transaction: tx}; + + try { + const filter = {where: {value: 'Silver'}, limit: 5}; + const result = await models.Tag.filterValue(colorTagId, filter, options); + + expect(result.length).toEqual(2); + expect(result[0].value).toEqual('Silver'); + expect(result[1].value).toEqual('Blue/Silver'); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/mdb/back/methods/mdbVersion/upload.js b/modules/mdb/back/methods/mdbVersion/upload.js new file mode 100644 index 0000000000..96b8347b21 --- /dev/null +++ b/modules/mdb/back/methods/mdbVersion/upload.js @@ -0,0 +1,50 @@ +module.exports = Self => { + Self.remoteMethodCtx('upload', { + description: 'Returns a list of tag values', + accepts: [ + { + arg: 'appName', + 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, + description: `The branch name` + } + ], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/upload`, + verb: 'POST' + } + }); + + Self.upload = async(ctx, appName, newVersion, branch, options) => { + const models = Self.app.models; + const myOptions = {}; + + const moduleFile = ctx.req; + if (!moduleFile) return; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + const newMdb = await models.mdbVersion.create({ + app: appName, + version: newVersion, + branchFk: branch + }, myOptions); + }; +}; diff --git a/modules/mdb/back/model-config.json b/modules/mdb/back/model-config.json new file mode 100644 index 0000000000..fbb7e1a337 --- /dev/null +++ b/modules/mdb/back/model-config.json @@ -0,0 +1,11 @@ +{ + "MdbBranch": { + "dataSource": "vn" + }, + "MdbVersion": { + "dataSource": "vn" + }, + "MdbContainer": { + "dataSource": "mdbStorage" + } +} diff --git a/modules/mdb/back/models/mdb-container.json b/modules/mdb/back/models/mdb-container.json new file mode 100644 index 0000000000..9d8052d802 --- /dev/null +++ b/modules/mdb/back/models/mdb-container.json @@ -0,0 +1,10 @@ +{ + "name": "MdbContainer", + "base": "Container", + "acls": [{ + "accessType": "*", + "principalType": "ROLE", + "principalId": "developer", + "permission": "ALLOW" + }] +} \ No newline at end of file diff --git a/modules/mdb/back/models/mdbBranch.json b/modules/mdb/back/models/mdbBranch.json new file mode 100644 index 0000000000..486dfaf25e --- /dev/null +++ b/modules/mdb/back/models/mdbBranch.json @@ -0,0 +1,16 @@ +{ + "name": "MdbBranch", + "base": "VnModel", + "options": { + "mysql": { + "table": "mdbBranch" + } + }, + "properties": { + "name": { + "id": true, + "type": "string", + "description": "Identifier" + } + } +} \ No newline at end of file diff --git a/modules/mdb/back/models/mdbVersion.js b/modules/mdb/back/models/mdbVersion.js new file mode 100644 index 0000000000..b36ee2a601 --- /dev/null +++ b/modules/mdb/back/models/mdbVersion.js @@ -0,0 +1,3 @@ +module.exports = Self => { + require('../methods/mdbVersion/upload')(Self); +}; diff --git a/modules/mdb/back/models/mdbVersion.json b/modules/mdb/back/models/mdbVersion.json new file mode 100644 index 0000000000..dc17531d63 --- /dev/null +++ b/modules/mdb/back/models/mdbVersion.json @@ -0,0 +1,25 @@ +{ + "name": "MdbVersion", + "base": "VnModel", + "options": { + "mysql": { + "table": "mdbVersion" + } + }, + "properties": { + "app": { + "type": "string", + "description": "The app name" + }, + "version": { + "type": "number" + } + }, + "relations": { + "branch": { + "type": "belongsTo", + "model": "MdbBranch", + "foreignKey": "name" + } + } +} \ No newline at end of file From aa7074f3039399d7a3bfcf3704bb28c9a911d7dd Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 27 May 2022 08:23:52 +0200 Subject: [PATCH 2/8] test(mdb): back test --- db/dump/fixtures.sql | 8 +- loopback/server/datasources.json | 20 ++--- modules/claim/front/photos/index.js | 2 +- .../mdbVersion/specs/filterValue.spec.js | 76 ------------------- .../methods/mdbVersion/specs/upload.spec.js | 27 +++++++ modules/mdb/back/methods/mdbVersion/upload.js | 48 ++++++++++-- modules/mdb/back/model-config.json | 4 +- modules/mdb/back/models/mdb-container.json | 2 +- modules/mdb/back/models/mdbVersion.json | 2 +- 9 files changed, 85 insertions(+), 104 deletions(-) delete mode 100644 modules/mdb/back/methods/mdbVersion/specs/filterValue.spec.js create mode 100644 modules/mdb/back/methods/mdbVersion/specs/upload.spec.js diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 087d66ac5e..ca8791c214 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2585,4 +2585,10 @@ INSERT INTO `vn`.`mdbBranch` (`name`) INSERT INTO `vn`.`mdbVersion` (`app`, `branchFk`, `version`) VALUES ('tpv', 'test', '1'), - ('lab', 'master', '2'); \ No newline at end of file + ('lab', 'master', '1'), + ('enc', 'test', '1'), + ('ent', 'master', '1'), + ('com', 'test', '1'), + ('eti', 'master', '1'), + ('ser', 'test', '1'), + ('lib', 'master', '1'); \ No newline at end of file diff --git a/loopback/server/datasources.json b/loopback/server/datasources.json index 73f3a7bf0d..7d2a6c1178 100644 --- a/loopback/server/datasources.json +++ b/loopback/server/datasources.json @@ -99,24 +99,14 @@ "video/mp4" ] }, - "mdbStorage": { - "name": "mdbStorage", + "accessStorage": { + "name": "accessStorage", "connector": "loopback-component-storage", "provider": "filesystem", - "root": "./storage/mdb", - "maxFileSize": "262144000", + "root": "./storage/access/branches", + "maxFileSize": "2621440000", "allowedContentTypes": [ - "application/x-7z-compressed", - "application/x-zip-compressed", - "application/x-rar-compressed", - "application/octet-stream", - "application/pdf", - "application/zip", - "application/rar", - "multipart/x-zip", - "image/png", - "image/jpeg", - "image/jpg" + "application/x-7z-compressed" ] } } \ No newline at end of file diff --git a/modules/claim/front/photos/index.js b/modules/claim/front/photos/index.js index 62e439a910..5cde18f52e 100644 --- a/modules/claim/front/photos/index.js +++ b/modules/claim/front/photos/index.js @@ -71,7 +71,7 @@ class Controller extends Section { } create() { - const query = `claims/${this.claim.id}/uploadFile`; + const query = `mdbVersions/upload`; const options = { method: 'POST', url: query, diff --git a/modules/mdb/back/methods/mdbVersion/specs/filterValue.spec.js b/modules/mdb/back/methods/mdbVersion/specs/filterValue.spec.js deleted file mode 100644 index 9e5cf2da26..0000000000 --- a/modules/mdb/back/methods/mdbVersion/specs/filterValue.spec.js +++ /dev/null @@ -1,76 +0,0 @@ -const models = require('vn-loopback/server/server').models; - -describe('tag filterValue()', () => { - const colorTagId = 1; - it('should return a list of color values', async() => { - const tx = await models.Item.beginTransaction({}); - const options = {transaction: tx}; - - try { - const filter = {limit: 5}; - const result = await models.Tag.filterValue(colorTagId, filter, options); - - expect(result.length).toEqual(5); - await tx.rollback(); - } catch (e) { - await tx.rollback(); - throw e; - } - }); - - it('should return the values matching color "Blue"', async() => { - const tx = await models.Item.beginTransaction({}); - const options = {transaction: tx}; - - try { - const filter = {where: {value: 'Blue'}, limit: 5}; - const result = await models.Tag.filterValue(colorTagId, filter, options); - - expect(result.length).toEqual(2); - expect(result[0].value).toEqual('Blue'); - expect(result[1].value).toEqual('Blue/Silver'); - - await tx.rollback(); - } catch (e) { - await tx.rollback(); - throw e; - } - }); - - it('should return the values matching color "Blue/Silver"', async() => { - const tx = await models.Item.beginTransaction({}); - const options = {transaction: tx}; - - try { - const filter = {where: {value: 'Blue/Silver'}, limit: 5}; - const result = await models.Tag.filterValue(colorTagId, filter, options); - - expect(result.length).toEqual(1); - expect(result[0].value).toEqual('Blue/Silver'); - - await tx.rollback(); - } catch (e) { - await tx.rollback(); - throw e; - } - }); - - it('should return the values matching color "Silver"', async() => { - const tx = await models.Item.beginTransaction({}); - const options = {transaction: tx}; - - try { - const filter = {where: {value: 'Silver'}, limit: 5}; - const result = await models.Tag.filterValue(colorTagId, filter, options); - - expect(result.length).toEqual(2); - expect(result[0].value).toEqual('Silver'); - expect(result[1].value).toEqual('Blue/Silver'); - - await tx.rollback(); - } catch (e) { - await tx.rollback(); - throw e; - } - }); -}); diff --git a/modules/mdb/back/methods/mdbVersion/specs/upload.spec.js b/modules/mdb/back/methods/mdbVersion/specs/upload.spec.js new file mode 100644 index 0000000000..f62886760c --- /dev/null +++ b/modules/mdb/back/methods/mdbVersion/specs/upload.spec.js @@ -0,0 +1,27 @@ +const models = require('vn-loopback/server/server').models; + +fdescribe('Mdb upload()', () => { + it(`should return an error for a user without enough privileges`, async() => { + const tx = await models.MdbVersion.beginTransaction({}); + + let error; + + try { + const options = {transaction: tx}; + const currentUserId = 1101; + const appName = 'tpv'; + const newVersion = '123'; + const branch = 'test'; + const ctx = {req: {accessToken: {userId: currentUserId}}}; + + await models.MdbVersion.upload(ctx, appName, newVersion, branch, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + error = e; + } + + expect(error.message).toEqual(`You don't have enough privileges`); + }); +}); diff --git a/modules/mdb/back/methods/mdbVersion/upload.js b/modules/mdb/back/methods/mdbVersion/upload.js index 96b8347b21..d5725edbe6 100644 --- a/modules/mdb/back/methods/mdbVersion/upload.js +++ b/modules/mdb/back/methods/mdbVersion/upload.js @@ -1,3 +1,6 @@ +const fs = require('fs-extra'); +const path = require('path'); + module.exports = Self => { Self.remoteMethodCtx('upload', { description: 'Returns a list of tag values', @@ -34,17 +37,48 @@ module.exports = Self => { Self.upload = async(ctx, appName, newVersion, branch, options) => { const models = Self.app.models; const myOptions = {}; + console.log(ctx); - const moduleFile = ctx.req; - if (!moduleFile) return; + const TempContainer = models.TempContainer; + const AccessContainer = models.AccessContainer; + const fileOptions = {}; + + let tx; if (typeof options == 'object') Object.assign(myOptions, options); - const newMdb = await models.mdbVersion.create({ - app: appName, - version: newVersion, - branchFk: branch - }, myOptions); + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + 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 => { + return file[0]; + }); + const uploadedFile = files[0]; + const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name); + const srcFile = path.join(file.client.root, file.container, file.name); + const accessContainer = await AccessContainer.container(branch); + const destinationFile = path.join(accessContainer.client.root, branch, `${appName}${newVersion}.7z`); + + await fs.move(srcFile, destinationFile, { + overwrite: true + }); + + await models.MdbVersion.create({ + app: appName, + version: newVersion, + branchFk: branch + }, myOptions); + + if (tx) await tx.commit(); + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; }; diff --git a/modules/mdb/back/model-config.json b/modules/mdb/back/model-config.json index fbb7e1a337..d5be8de879 100644 --- a/modules/mdb/back/model-config.json +++ b/modules/mdb/back/model-config.json @@ -5,7 +5,7 @@ "MdbVersion": { "dataSource": "vn" }, - "MdbContainer": { - "dataSource": "mdbStorage" + "AccessContainer": { + "dataSource": "accessStorage" } } diff --git a/modules/mdb/back/models/mdb-container.json b/modules/mdb/back/models/mdb-container.json index 9d8052d802..a927b30f1f 100644 --- a/modules/mdb/back/models/mdb-container.json +++ b/modules/mdb/back/models/mdb-container.json @@ -1,5 +1,5 @@ { - "name": "MdbContainer", + "name": "AccessContainer", "base": "Container", "acls": [{ "accessType": "*", diff --git a/modules/mdb/back/models/mdbVersion.json b/modules/mdb/back/models/mdbVersion.json index dc17531d63..0fd86e4a5d 100644 --- a/modules/mdb/back/models/mdbVersion.json +++ b/modules/mdb/back/models/mdbVersion.json @@ -19,7 +19,7 @@ "branch": { "type": "belongsTo", "model": "MdbBranch", - "foreignKey": "name" + "foreignKey": "branchFk" } } } \ No newline at end of file From 863977915ea0bf5f2be9bcf99062e01ae31171ba Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 27 May 2022 13:27:05 +0200 Subject: [PATCH 3/8] fix --- db/changes/10470-family/00-aclMdb.sql | 6 +++--- docker-compose.yml | 2 +- modules/claim/front/photos/index.js | 2 +- modules/mdb/back/methods/mdbVersion/upload.js | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/db/changes/10470-family/00-aclMdb.sql b/db/changes/10470-family/00-aclMdb.sql index e14fa3daad..c57f60eb37 100644 --- a/db/changes/10470-family/00-aclMdb.sql +++ b/db/changes/10470-family/00-aclMdb.sql @@ -1,6 +1,3 @@ -INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) - VALUES('MdbVersion', '*', '*', 'ALLOW', 'ROLE', 'developer'); - CREATE TABLE `vn`.`mdbBranch` ( `name` VARCHAR(255), PRIMARY KEY(`name`) @@ -12,3 +9,6 @@ CREATE TABLE `vn`.`mdbVersion` ( `version` INT, CONSTRAINT `mdbVersion_branchFk` FOREIGN KEY (`branchFk`) REFERENCES `vn`.`mdbBranch` (`name`) ON DELETE CASCADE ON UPDATE CASCADE ); + +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES('MdbVersion', '*', '*', 'ALLOW', 'ROLE', 'developer'); diff --git a/docker-compose.yml b/docker-compose.yml index 4fc5dc8117..e6e0dbdab0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -32,7 +32,7 @@ services: - /mnt/appdata/pdfs:/var/lib/salix/pdfs - /mnt/appdata/dms:/var/lib/salix/dms - /mnt/appdata/image:/var/lib/salix/image - - /mnt/appdata/vn-access:/var/lib/salix/vn-access + - /mnt/appdata/access:/var/lib/salix/access deploy: replicas: ${BACK_REPLICAS:?} placement: diff --git a/modules/claim/front/photos/index.js b/modules/claim/front/photos/index.js index 5cde18f52e..62e439a910 100644 --- a/modules/claim/front/photos/index.js +++ b/modules/claim/front/photos/index.js @@ -71,7 +71,7 @@ class Controller extends Section { } create() { - const query = `mdbVersions/upload`; + const query = `claims/${this.claim.id}/uploadFile`; const options = { method: 'POST', url: query, diff --git a/modules/mdb/back/methods/mdbVersion/upload.js b/modules/mdb/back/methods/mdbVersion/upload.js index d5725edbe6..904f9f1d9b 100644 --- a/modules/mdb/back/methods/mdbVersion/upload.js +++ b/modules/mdb/back/methods/mdbVersion/upload.js @@ -37,7 +37,6 @@ module.exports = Self => { Self.upload = async(ctx, appName, newVersion, branch, options) => { const models = Self.app.models; const myOptions = {}; - console.log(ctx); const TempContainer = models.TempContainer; const AccessContainer = models.AccessContainer; @@ -60,6 +59,7 @@ module.exports = Self => { return file[0]; }); const uploadedFile = files[0]; + const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name); const srcFile = path.join(file.client.root, file.container, file.name); const accessContainer = await AccessContainer.container(branch); From 1e2f827233b3fd1cc65ffab7e7ee623913269b99 Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 27 May 2022 13:39:33 +0200 Subject: [PATCH 4/8] fix --- .../methods/mdbVersion/specs/upload.spec.js | 27 ------------------- modules/mdb/back/methods/mdbVersion/upload.js | 2 +- 2 files changed, 1 insertion(+), 28 deletions(-) delete mode 100644 modules/mdb/back/methods/mdbVersion/specs/upload.spec.js diff --git a/modules/mdb/back/methods/mdbVersion/specs/upload.spec.js b/modules/mdb/back/methods/mdbVersion/specs/upload.spec.js deleted file mode 100644 index f62886760c..0000000000 --- a/modules/mdb/back/methods/mdbVersion/specs/upload.spec.js +++ /dev/null @@ -1,27 +0,0 @@ -const models = require('vn-loopback/server/server').models; - -fdescribe('Mdb upload()', () => { - it(`should return an error for a user without enough privileges`, async() => { - const tx = await models.MdbVersion.beginTransaction({}); - - let error; - - try { - const options = {transaction: tx}; - const currentUserId = 1101; - const appName = 'tpv'; - const newVersion = '123'; - const branch = 'test'; - const ctx = {req: {accessToken: {userId: currentUserId}}}; - - await models.MdbVersion.upload(ctx, appName, newVersion, branch, options); - - await tx.rollback(); - } catch (e) { - await tx.rollback(); - error = e; - } - - expect(error.message).toEqual(`You don't have enough privileges`); - }); -}); diff --git a/modules/mdb/back/methods/mdbVersion/upload.js b/modules/mdb/back/methods/mdbVersion/upload.js index 904f9f1d9b..2109f2b4fb 100644 --- a/modules/mdb/back/methods/mdbVersion/upload.js +++ b/modules/mdb/back/methods/mdbVersion/upload.js @@ -3,7 +3,7 @@ const path = require('path'); module.exports = Self => { Self.remoteMethodCtx('upload', { - description: 'Returns a list of tag values', + description: 'Upload and attach a access file', accepts: [ { arg: 'appName', From cac4e74793c4b0a846bf5375b4bcdbd54cc2b816 Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 31 May 2022 09:06:15 +0200 Subject: [PATCH 5/8] refactor --- docker-compose.yml | 2 +- loopback/server/datasources.json | 2 +- modules/mdb/back/methods/mdbVersion/upload.js | 62 ++++++++++++++++--- modules/mdb/back/models/mdbVersion.json | 3 +- 4 files changed, 56 insertions(+), 13 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index e6e0dbdab0..4fc5dc8117 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -32,7 +32,7 @@ services: - /mnt/appdata/pdfs:/var/lib/salix/pdfs - /mnt/appdata/dms:/var/lib/salix/dms - /mnt/appdata/image:/var/lib/salix/image - - /mnt/appdata/access:/var/lib/salix/access + - /mnt/appdata/vn-access:/var/lib/salix/vn-access deploy: replicas: ${BACK_REPLICAS:?} placement: diff --git a/loopback/server/datasources.json b/loopback/server/datasources.json index 7d2a6c1178..86ec7d10ff 100644 --- a/loopback/server/datasources.json +++ b/loopback/server/datasources.json @@ -103,7 +103,7 @@ "name": "accessStorage", "connector": "loopback-component-storage", "provider": "filesystem", - "root": "./storage/access/branches", + "root": "./storage/access", "maxFileSize": "2621440000", "allowedContentTypes": [ "application/x-7z-compressed" diff --git a/modules/mdb/back/methods/mdbVersion/upload.js b/modules/mdb/back/methods/mdbVersion/upload.js index 2109f2b4fb..fa32fca977 100644 --- a/modules/mdb/back/methods/mdbVersion/upload.js +++ b/modules/mdb/back/methods/mdbVersion/upload.js @@ -52,6 +52,7 @@ module.exports = Self => { myOptions.transaction = tx; } + let srcFile; try { const tempContainer = await TempContainer.container('access'); const uploaded = await TempContainer.upload(tempContainer.name, ctx.req, ctx.result, fileOptions); @@ -61,23 +62,64 @@ module.exports = Self => { const uploadedFile = files[0]; const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name); - const srcFile = path.join(file.client.root, file.container, file.name); - const accessContainer = await AccessContainer.container(branch); - const destinationFile = path.join(accessContainer.client.root, branch, `${appName}${newVersion}.7z`); + srcFile = path.join(file.client.root, file.container, file.name); - await fs.move(srcFile, destinationFile, { - overwrite: true - }); + const accessContainer = await AccessContainer.container('.archive'); + const destinationFile = path.join( + accessContainer.client.root, accessContainer.name, appName, `${newVersion}.7z`); - await models.MdbVersion.create({ - app: appName, - version: newVersion, - branchFk: branch + if (process.env.NODE_ENV == 'test') { + try { + await fs.unlink(srcFile); + } catch (e) {} + } else { + await fs.move(srcFile, destinationFile, { + overwrite: true + }); + await fs.chmod(destinationFile, 0o644); + + const branchPath = path.join(accessContainer.client.root, 'branches', branch); + await fs.mkdir(branchPath, {recursive: true}); + + const destinationBranch = path.join(branchPath, `${appName}.7z`); + const destinationRoot = path.join(accessContainer.client.root, `${appName}.7z`); + try { + await fs.unlink(destinationBranch); + } catch (e) {} + await fs.symlink(destinationFile, destinationBranch); + + if (branch == 'master') { + try { + await fs.unlink(destinationRoot); + } catch (e) {} + await fs.symlink(destinationFile, destinationRoot); + } + } + + const version = await models.MdbVersion.findOne({ + where: { + app: appName, + branchFk: branch + } }, myOptions); + if (!version) { + return await models.MdbVersion.create({ + app: appName, + branchFk: branch, + version: newVersion + }); + } + + await version.updateAttributes({version: newVersion}, myOptions); + if (tx) await tx.commit(); } catch (e) { if (tx) await tx.rollback(); + + if (await fs.stat(srcFile)) + await fs.unlink(srcFile); + throw e; } }; diff --git a/modules/mdb/back/models/mdbVersion.json b/modules/mdb/back/models/mdbVersion.json index 0fd86e4a5d..02635ff8a1 100644 --- a/modules/mdb/back/models/mdbVersion.json +++ b/modules/mdb/back/models/mdbVersion.json @@ -9,7 +9,8 @@ "properties": { "app": { "type": "string", - "description": "The app name" + "description": "The app name", + "id": true }, "version": { "type": "number" From 8340041b33381aa995d96615b340d7e8cc7c9cc0 Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 1 Jun 2022 07:57:32 +0200 Subject: [PATCH 6/8] throw userError and maxFileSize --- db/dump/fixtures.sql | 8 +--- loopback/server/datasources.json | 2 +- modules/mdb/back/methods/mdbVersion/upload.js | 39 ++++++++----------- 3 files changed, 19 insertions(+), 30 deletions(-) diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index ca8791c214..c91ba71c28 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2585,10 +2585,4 @@ INSERT INTO `vn`.`mdbBranch` (`name`) INSERT INTO `vn`.`mdbVersion` (`app`, `branchFk`, `version`) VALUES ('tpv', 'test', '1'), - ('lab', 'master', '1'), - ('enc', 'test', '1'), - ('ent', 'master', '1'), - ('com', 'test', '1'), - ('eti', 'master', '1'), - ('ser', 'test', '1'), - ('lib', 'master', '1'); \ No newline at end of file + ('lab', 'master', '1'); \ No newline at end of file diff --git a/loopback/server/datasources.json b/loopback/server/datasources.json index 86ec7d10ff..5dade9c2e9 100644 --- a/loopback/server/datasources.json +++ b/loopback/server/datasources.json @@ -104,7 +104,7 @@ "connector": "loopback-component-storage", "provider": "filesystem", "root": "./storage/access", - "maxFileSize": "2621440000", + "maxFileSize": "524288000", "allowedContentTypes": [ "application/x-7z-compressed" ] diff --git a/modules/mdb/back/methods/mdbVersion/upload.js b/modules/mdb/back/methods/mdbVersion/upload.js index fa32fca977..3d54c02500 100644 --- a/modules/mdb/back/methods/mdbVersion/upload.js +++ b/modules/mdb/back/methods/mdbVersion/upload.js @@ -1,5 +1,6 @@ const fs = require('fs-extra'); const path = require('path'); +const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('upload', { @@ -68,16 +69,21 @@ module.exports = Self => { const destinationFile = path.join( accessContainer.client.root, accessContainer.name, appName, `${newVersion}.7z`); - if (process.env.NODE_ENV == 'test') { - try { - await fs.unlink(srcFile); - } catch (e) {} - } else { + if (process.env.NODE_ENV == 'test') + await fs.unlink(srcFile); + else { await fs.move(srcFile, destinationFile, { overwrite: true }); 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}); @@ -96,28 +102,17 @@ module.exports = Self => { } } - const version = await models.MdbVersion.findOne({ - where: { - app: appName, - branchFk: branch - } - }, myOptions); - - if (!version) { - return await models.MdbVersion.create({ - app: appName, - branchFk: branch, - version: newVersion - }); - } - - await version.updateAttributes({version: newVersion}, myOptions); + await models.MdbVersion.upsert({ + app: appName, + branchFk: branch, + version: newVersion + }); if (tx) await tx.commit(); } catch (e) { if (tx) await tx.rollback(); - if (await fs.stat(srcFile)) + if (fs.existsSync(srcFile)) await fs.unlink(srcFile); throw e; From 13c558e526d15beadf2ea9e7c6b2e4db19b7c70a Mon Sep 17 00:00:00 2001 From: alexm Date: Thu, 2 Jun 2022 08:52:51 +0200 Subject: [PATCH 7/8] add folder storage/access --- storage/access/.keep | 1 + 1 file changed, 1 insertion(+) create mode 100644 storage/access/.keep diff --git a/storage/access/.keep b/storage/access/.keep new file mode 100644 index 0000000000..8e25568966 --- /dev/null +++ b/storage/access/.keep @@ -0,0 +1 @@ +Forces tmp folder creation! \ No newline at end of file From 9a25bdad06f04b1fb20d0cf57b9133ff2d1ed7e6 Mon Sep 17 00:00:00 2001 From: alexm Date: Thu, 2 Jun 2022 09:01:34 +0200 Subject: [PATCH 8/8] relaunch tests --- back/methods/account/change-password.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/back/methods/account/change-password.js b/back/methods/account/change-password.js index 25b63b9a8e..c0956b1937 100644 --- a/back/methods/account/change-password.js +++ b/back/methods/account/change-password.js @@ -5,17 +5,17 @@ module.exports = Self => { accepts: [ { arg: 'id', - type: 'Number', + type: 'number', description: 'The user id', http: {source: 'path'} }, { arg: 'oldPassword', - type: 'String', + type: 'string', description: 'The old password', required: true }, { arg: 'newPassword', - type: 'String', + type: 'string', description: 'The new password', required: true }