4108-mdb_backend #987

Merged
juan merged 9 commits from 4108-mdb_backend into dev 2022-06-02 07:33:24 +00:00
9 changed files with 85 additions and 104 deletions
Showing only changes of commit aa7074f303 - Show all commits

View File

@ -2585,4 +2585,10 @@ INSERT INTO `vn`.`mdbBranch` (`name`)
INSERT INTO `vn`.`mdbVersion` (`app`, `branchFk`, `version`)
VALUES
('tpv', 'test', '1'),
('lab', 'master', '2');
('lab', 'master', '1'),
('enc', 'test', '1'),
('ent', 'master', '1'),
('com', 'test', '1'),
('eti', 'master', '1'),
('ser', 'test', '1'),
('lib', 'master', '1');

View File

@ -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"
]
}
}

View File

@ -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,

View File

@ -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;
}
});
});

View File

@ -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`);
});
});

View File

@ -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({
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;
}
};
};

View File

@ -5,7 +5,7 @@
"MdbVersion": {
"dataSource": "vn"
},
"MdbContainer": {
"dataSource": "mdbStorage"
"AccessContainer": {
"dataSource": "accessStorage"
}
}

View File

@ -1,5 +1,5 @@
{
"name": "MdbContainer",
"name": "AccessContainer",
"base": "Container",
"acls": [{
"accessType": "*",

View File

@ -19,7 +19,7 @@
"branch": {
"type": "belongsTo",
"model": "MdbBranch",
"foreignKey": "name"
"foreignKey": "branchFk"
}
}
}