From 2a2e276c6bbeb5883bd0692d11ca11ef27d4dacd Mon Sep 17 00:00:00 2001 From: joan Date: Wed, 10 Mar 2021 09:01:32 +0100 Subject: [PATCH] Added back unit test plus fixes --- front/core/components/field/index.js | 3 + front/core/components/input-file/index.html | 2 +- front/core/components/input-file/index.js | 10 +++ modules/entry/back/methods/entry/import.js | 77 ++++++++++-------- .../back/methods/entry/specs/import.spec.js | 78 +++++++++++++++++++ modules/entry/front/buy/import/index.html | 16 ++-- modules/entry/front/buy/import/locale/es.yml | 2 - modules/entry/front/buy/import/style.scss | 10 +-- modules/entry/front/buy/locale/es.yml | 4 + 9 files changed, 153 insertions(+), 49 deletions(-) create mode 100644 modules/entry/back/methods/entry/specs/import.spec.js delete mode 100644 modules/entry/front/buy/import/locale/es.yml create mode 100644 modules/entry/front/buy/locale/es.yml diff --git a/front/core/components/field/index.js b/front/core/components/field/index.js index 40ea01e47..e10ef6383 100644 --- a/front/core/components/field/index.js +++ b/front/core/components/field/index.js @@ -83,6 +83,9 @@ export default class Field extends FormInput { this._required = value; let required = this.element.querySelector('.required'); display(required, this._required); + + this.$.$applyAsync(() => + this.input.setAttribute('required', value)); } get required() { diff --git a/front/core/components/input-file/index.html b/front/core/components/input-file/index.html index 5ec7e1da4..bdfdcdcb4 100644 --- a/front/core/components/input-file/index.html +++ b/front/core/components/input-file/index.html @@ -13,7 +13,7 @@ diff --git a/front/core/components/input-file/index.js b/front/core/components/input-file/index.js index 4d057f3cc..962f38b73 100644 --- a/front/core/components/input-file/index.js +++ b/front/core/components/input-file/index.js @@ -78,6 +78,16 @@ export default class InputFile extends Field { $event: $event }); } + + get accept() { + return this._accept; + } + + set accept(value) { + this._accept = value; + this.$.$applyAsync(() => + this.input.setAttribute('accept', value)); + } } ngModule.vnComponent('vnInputFile', { diff --git a/modules/entry/back/methods/entry/import.js b/modules/entry/back/methods/entry/import.js index cbb2e9ace..21d4ef0e1 100644 --- a/modules/entry/back/methods/entry/import.js +++ b/modules/entry/back/methods/entry/import.js @@ -36,47 +36,60 @@ module.exports = Self => { } }); - Self.import = async(ctx, id) => { + Self.import = async(ctx, id, options) => { const conn = Self.dataSource.connector; const args = ctx.args; const models = Self.app.models; - const entry = await models.Entry.findById(id); - await entry.updateAttributes({ - observation: args.observation - }); - - const buys = []; - for (let buy of args.buys) { - buys.push({ - entryFk: entry.id, - itemFk: buy.itemFk, - stickers: 1, - quantity: 1, - packing: buy.packing, - grouping: buy.grouping, - buyingValue: buy.buyingValue, - packageFk: 1 - }); + let tx; + if (!options.transaction) { + tx = await Self.beginTransaction({}); + options.transaction = tx; } - const createdBuys = await models.Buy.create(buys); - const buyIds = createdBuys.map(buy => buy.id); + try { + const entry = await models.Entry.findById(id, null, options); + await entry.updateAttributes({ + observation: args.observation, + ref: args.ref + }, options); - let stmts = []; - let stmt; + const buys = []; + for (let buy of args.buys) { + buys.push({ + entryFk: entry.id, + itemFk: buy.itemFk, + stickers: 1, + quantity: 1, + packing: buy.packing, + grouping: buy.grouping, + buyingValue: buy.buyingValue, + packageFk: 1 + }); + } - stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.buyRecalc'); - stmt = new ParameterizedSQL( - `CREATE TEMPORARY TABLE tmp.buyRecalc - (INDEX (id)) - ENGINE = MEMORY - SELECT ? AS id`, [buyIds]); + const createdBuys = await models.Buy.create(buys, options); + const buyIds = createdBuys.map(buy => buy.id); - stmts.push(stmt); - stmts.push('CALL buy_recalcPrices()'); + let stmts = []; + let stmt; - const sql = ParameterizedSQL.join(stmts, ';'); - await conn.executeStmt(sql); + stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.buyRecalc'); + stmt = new ParameterizedSQL( + `CREATE TEMPORARY TABLE tmp.buyRecalc + (INDEX (id)) + ENGINE = MEMORY + SELECT ? AS id`, [buyIds]); + + stmts.push(stmt); + stmts.push('CALL buy_recalcPrices()'); + + const sql = ParameterizedSQL.join(stmts, ';'); + await conn.executeStmt(sql, options); + if (tx) await tx.commit(); + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; }; diff --git a/modules/entry/back/methods/entry/specs/import.spec.js b/modules/entry/back/methods/entry/specs/import.spec.js new file mode 100644 index 000000000..882795065 --- /dev/null +++ b/modules/entry/back/methods/entry/specs/import.spec.js @@ -0,0 +1,78 @@ +const app = require('vn-loopback/server/server'); +const LoopBackContext = require('loopback-context'); + +describe('entry import()', () => { + let newEntry; + const buyerId = 35; + const companyId = 442; + const travelId = 1; + const supplierId = 1; + const activeCtx = { + accessToken: {userId: buyerId}, + }; + + beforeAll(async done => { + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx + }); + + done(); + }); + + it('should import the buy rows', async() => { + const ctx = { + req: activeCtx, + args: { + observation: '123456', + ref: '1, 2', + buys: [ + { + itemFk: 1, + buyingValue: 5.77, + description: 'Bow', + grouping: 1, + packing: 1, + size: 1, + volume: 1200 + }, + { + itemFk: 4, + buyingValue: 2.16, + description: 'Arrow', + grouping: 1, + packing: 1, + size: 25, + volume: 1125 + } + ] + } + }; + const tx = await app.models.Entry.beginTransaction({}); + const options = {transaction: tx}; + + newEntry = await app.models.Entry.create({ + dated: new Date(), + supplierFk: supplierId, + travelFk: travelId, + companyFk: companyId, + observation: 'The entry', + ref: 'Entry ref' + }, options); + + await app.models.Entry.import(ctx, newEntry.id, options); + + const updatedEntry = await app.models.Entry.findById(newEntry.id, null, options); + const entryBuys = await app.models.Buy.find({ + where: {entryFk: newEntry.id} + }, options); + + expect(updatedEntry.observation).toEqual('123456'); + expect(updatedEntry.ref).toEqual('1, 2'); + expect(entryBuys.length).toEqual(2); + + // Restores + await newEntry.destroy(options); + + await tx.rollback(); + }); +}); diff --git a/modules/entry/front/buy/import/index.html b/modules/entry/front/buy/import/index.html index ad812244a..00fa709b2 100644 --- a/modules/entry/front/buy/import/index.html +++ b/modules/entry/front/buy/import/index.html @@ -10,7 +10,7 @@
- @@ -29,22 +29,22 @@ label="File" ng-model="$ctrl.import.file" on-change="$ctrl.onFileChange($event)" - required="true" - multiple="true"> + accept="application/json" + required="true"> - + - + @@ -56,7 +56,7 @@ -
ItemItem Description Size Packing
+ + label="Import buys"> tbody td:nth-child(1) { + width: 250px } -} - +} \ No newline at end of file diff --git a/modules/entry/front/buy/locale/es.yml b/modules/entry/front/buy/locale/es.yml new file mode 100644 index 000000000..8d035e9a7 --- /dev/null +++ b/modules/entry/front/buy/locale/es.yml @@ -0,0 +1,4 @@ +reference: Referencia +Observation: Observación +Box: Embalage +Import buys: Importar compras \ No newline at end of file