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`) INSERT INTO `vn`.`mdbVersion` (`app`, `branchFk`, `version`)
VALUES VALUES
('tpv', 'test', '1'), ('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');
alexm marked this conversation as resolved Outdated
Outdated
Review

Per a fixtures, deixa 2 registres en esta taula, uno amb test i un altre amb master. Quantes menys dades tingam en fixtures millor.

Per a fixtures, deixa 2 registres en esta taula, uno amb test i un altre amb master. Quantes menys dades tingam en fixtures millor.

View File

@ -99,24 +99,14 @@
"video/mp4" "video/mp4"
] ]
}, },
"mdbStorage": { "accessStorage": {
"name": "mdbStorage", "name": "accessStorage",
"connector": "loopback-component-storage", "connector": "loopback-component-storage",
"provider": "filesystem", "provider": "filesystem",
"root": "./storage/mdb", "root": "./storage/access/branches",
"maxFileSize": "262144000", "maxFileSize": "2621440000",
alexm marked this conversation as resolved Outdated
Outdated
Review

Fica màxim 500MB, crec que el valor està en bytes, comprova-ho en la documentació de looback storage.

Fica màxim 500MB, crec que el valor està en bytes, comprova-ho en la documentació de looback storage.
"allowedContentTypes": [ "allowedContentTypes": [
"application/x-7z-compressed", "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"
] ]
} }
} }

View File

@ -71,7 +71,7 @@ class Controller extends Section {
} }
create() { create() {
const query = `claims/${this.claim.id}/uploadFile`; const query = `mdbVersions/upload`;
const options = { const options = {
method: 'POST', method: 'POST',
url: query, 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 => { module.exports = Self => {
Self.remoteMethodCtx('upload', { Self.remoteMethodCtx('upload', {
description: 'Returns a list of tag values', description: 'Returns a list of tag values',
@ -34,17 +37,48 @@ module.exports = Self => {
Self.upload = async(ctx, appName, newVersion, branch, options) => { Self.upload = async(ctx, appName, newVersion, branch, options) => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {}; const myOptions = {};
console.log(ctx);
const moduleFile = ctx.req; const TempContainer = models.TempContainer;
if (!moduleFile) return; const AccessContainer = models.AccessContainer;
const fileOptions = {};
let tx;
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); 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, {
juan marked this conversation as resolved Outdated
Outdated
Review

Cuando NODE_ENV es test no hay que guardar el fichero, si no sobreescribira el de producción. Añadir un if para evitar el guardado en ese caso.

Cuando `NODE_ENV` es `test` no hay que guardar el fichero, si no sobreescribira el de producción. Añadir un if para evitar el guardado en ese caso.
overwrite: true
});
await models.MdbVersion.create({
alexm marked this conversation as resolved Outdated
Outdated
Review

Lleva el try/catch, si intenta borrar el fitxer i no pot, ha de tornar el error.

Lleva el try/catch, si intenta borrar el fitxer i no pot, ha de tornar el error.
app: appName, app: appName,
version: newVersion, version: newVersion,
branchFk: branch branchFk: branch
}, myOptions); }, myOptions);
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw e;
alexm marked this conversation as resolved Outdated
Outdated
Review

Abans de crear el directori, comprovar que la rama existeix en la taula mdbBranch, en cas contrari llançar un error notificant que la rama no existeix

Abans de crear el directori, comprovar que la rama existeix en la taula mdbBranch, en cas contrari llançar un error notificant que la rama no existeix
}
}; };
}; };

View File

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

View File

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

View File

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