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