Commit 9d0c0ea7dda6c2c38c5e0d29814709c4021e1f77

Authored by Marcelo Puebla
Exists in master and in 2 other branches develop, lab

Merge branch 'master' into 'master'

Master

See merge request !3
1 1
2 var bookshelf = require('bookshelf')(knex); 2 var bookshelf = require('bookshelf')(knex);
3 bookshelf.plugin('pagination'); 3 bookshelf.plugin('pagination');
4 4
5 const ArticuloImagen = bookshelf.Model.extend({ 5 const ArticuloImagen = bookshelf.Model.extend({
6 tableName: 'ARTICULOS_IMAGEN' 6 tableName: 'ARTICULOS_IMAGEN'
7 }); 7 });
8 8
9 const Articulo = bookshelf.Model.extend({ 9 const Articulo = bookshelf.Model.extend({
10 tableName: 'ARTICULOS', 10 tableName: 'ARTICULOS',
11 imagenes: function () { 11 imagenes: function () {
12 return this.hasMany(ArticuloImagen, 'id_articulo', 'id'); 12 return this.hasMany(ArticuloImagen, 'id_articulo', 'id');
13 } 13 }
14 }); 14 });
15 15
16 router.get('/articulos/:page', (req, res) => { 16 router.get('/articulos/:page', (req, res) => {
17 17
18 Articulo 18 Articulo
19 .query(() => {}) 19 .query(() => {})
20 .orderBy('id') 20 .orderBy('id')
21 .fetchPage({ 21 .fetchPage({
22 pageSize: 150, 22 pageSize: 150,
23 page: req.params.page, 23 page: req.params.page,
24 withRelated: ['imagenes'] 24 withRelated: ['imagenes']
25 }) 25 })
26 .then(data => { 26 .then(data => {
27 27
28 var promisesCodBar = []; 28 res.status(200).send(
29 29 {
30 data.forEach(articulo => { 30 data: data,
31 31 pagination: data.pagination,
32 promisesCodBar.push(
33 knex('CODBAR').where({
34 CodSec: articulo.get('CodSec'),
35 CodArt: articulo.get('CodArt')
36 })
37 );
38 });
39
40 Promise.all(promisesCodBar).then((codigoBarra) => {
41
42 codigoBarra.forEach(codigo => {
43 codigo = codigo[0];
44
45 if (codigo) {
46
47 let articulo = data.filter(art => {
48 return art.get('CodArt') == codigo.CodArt;
49 });
50
51 articulo[0].set('codigoBarra', codigo.CodBar);
52 }
53 }); 32 });
54 res.status(200).send(
55 {
56 data: data,
57 pagination: data.pagination,
58 });
59 });
60 33
61 }); 34 });
62 }); 35 });
63 36
64 module.exports = router; 37 module.exports = router;
65 38