Added back unit test plus fixes
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2021-03-10 09:01:32 +01:00
parent 31205f4556
commit 2a2e276c6b
9 changed files with 153 additions and 49 deletions

View File

@ -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() {

View File

@ -13,7 +13,7 @@
</section>
<input
type="file"
accept="{{$ctrl.accept}}"
accept="{{$ctrl.acceptTypes}}"
style="display: none;">
</input>
</div>

View File

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

View File

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

View File

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

View File

@ -10,7 +10,7 @@
<div class="vn-w-lg">
<vn-card class="vn-pa-lg">
<vn-horizontal>
<vn-textfield
<vn-textfield vn-focus
vn-one
label="Reference"
ng-model="$ctrl.import.ref">
@ -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">
<append>
<vn-icon vn-none
color-marginal
title="{{$ctrl.contentTypesInfo}}"
title="{{'JSON files only' | translate}}"
icon="info">
</vn-icon>
</append>
</vn-input-file>
</vn-horizontal>
<vn-horizontal>
<vn-horizontal ng-show="$ctrl.import.buys.length > 0">
<table class="vn-table">
<thead>
<tr>
<th translate expand>Item</th>
<th translate>Item</th>
<th translate expand>Description</th>
<th translate center>Size</th>
<th translate center>Packing</th>
@ -56,7 +56,7 @@
</thead>
<tbody ng-repeat="buy in $ctrl.import.buys">
<tr>
<td title="{{::buy.itemFk}}" expand>
<td title="{{::buy.itemFk}}">
<vn-autocomplete
class="dense"
vn-focus
@ -103,7 +103,7 @@
</vn-card>
<vn-button-bar>
<vn-submit
label="Import">
label="Import buys">
</vn-submit>
<vn-button
class="cancel"

View File

@ -1,2 +0,0 @@
#Ordenar alfabeticamente
reference: Referencia

View File

@ -1,7 +1,5 @@
vn-ticket-request {
.vn-textfield {
margin: 0!important;
max-width: 100px;
vn-entry-buy-import {
.vn-table > tbody td:nth-child(1) {
width: 250px
}
}
}

View File

@ -0,0 +1,4 @@
reference: Referencia
Observation: Observación
Box: Embalage
Import buys: Importar compras