Compare View
Commits (5)
-
Master(mpuebla) See merge request !1
Showing
5 changed files
Show diff stats
index.js
package-lock.json
... | ... | @@ -2296,9 +2296,9 @@ |
2296 | 2296 | "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" |
2297 | 2297 | }, |
2298 | 2298 | "nodemon": { |
2299 | - "version": "1.19.1", | |
2300 | - "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-1.19.1.tgz", | |
2301 | - "integrity": "sha512-/DXLzd/GhiaDXXbGId5BzxP1GlsqtMGM9zTmkWrgXtSqjKmGSbLicM/oAy4FR0YWm14jCHRwnR31AHS2dYFHrg==", | |
2299 | + "version": "1.19.2", | |
2300 | + "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-1.19.2.tgz", | |
2301 | + "integrity": "sha512-hRLYaw5Ihyw9zK7NF+9EUzVyS6Cvgc14yh8CAYr38tPxJa6UrOxwAQ351GwrgoanHCF0FalQFn6w5eoX/LGdJw==", | |
2302 | 2302 | "requires": { |
2303 | 2303 | "chokidar": "^2.1.5", |
2304 | 2304 | "debug": "^3.1.0", |
... | ... | @@ -3304,9 +3304,9 @@ |
3304 | 3304 | "integrity": "sha1-0vD3N9FrBhXnKmk17QQhRXLVb5c=" |
3305 | 3305 | }, |
3306 | 3306 | "upath": { |
3307 | - "version": "1.1.2", | |
3308 | - "resolved": "https://registry.npmjs.org/upath/-/upath-1.1.2.tgz", | |
3309 | - "integrity": "sha512-kXpym8nmDmlCBr7nKdIx8P2jNBa+pBpIUFRnKJ4dr8htyYGJFokkr2ZvERRtUN+9SY+JqXouNgUPtv6JQva/2Q==" | |
3307 | + "version": "1.2.0", | |
3308 | + "resolved": "https://registry.npmjs.org/upath/-/upath-1.2.0.tgz", | |
3309 | + "integrity": "sha512-aZwGpamFO61g3OlfT7OQCHqhGnW43ieH9WZeP7QxN/G/jS4jfqUkZxoryvJgVPEcrl5NL/ggHsSmLMHuH64Lhg==" | |
3310 | 3310 | }, |
3311 | 3311 | "update-notifier": { |
3312 | 3312 | "version": "2.5.0", |
package.json
rutas/articulos.js
1 | 1 | |
2 | 2 | var bookshelf = require('bookshelf')(knex); |
3 | +bookshelf.plugin('pagination'); | |
3 | 4 | |
4 | 5 | const ArticuloImagen = bookshelf.Model.extend({ |
5 | 6 | tableName: 'ARTICULOS_IMAGEN' |
... | ... | @@ -7,44 +8,26 @@ const ArticuloImagen = bookshelf.Model.extend({ |
7 | 8 | |
8 | 9 | const Articulo = bookshelf.Model.extend({ |
9 | 10 | tableName: 'ARTICULOS', |
10 | - imagenes: function() { | |
11 | + imagenes: function () { | |
11 | 12 | return this.hasMany(ArticuloImagen, 'id_articulo', 'id'); |
12 | 13 | } |
13 | 14 | }); |
14 | 15 | |
15 | -router.get('/articulos', (req , res) => { | |
16 | +router.get('/articulos/:page', (req, res) => { | |
16 | 17 | |
17 | - let cantidadesVendidas = knex.schema | |
18 | - .raw(`select stock.SEC, stock.ART, SUM(stock.can) as cantidadVendida | |
19 | - from AMOVSTOC stock | |
20 | - join ARTICULOS art on art.CodSec = stock.SEC and art.CodArt = stock.ART and | |
21 | - art.categoria_selfservice > 0 and art.PreVen > 0 | |
22 | - where stock.FEC >= dateadd(month, -2, getdate()) and stock.CYV = 'V' and TCO = 'FT' | |
23 | - group by stock.sec, stock.art`) | |
24 | - | |
25 | - var articulos = Articulo | |
26 | - .where((query) => { | |
27 | - query.where('NHA', 0); | |
28 | - query.where('PreVen', '>', 0); | |
29 | - query.where('categoria_selfservice', '>', 0); | |
18 | + Articulo | |
19 | + .query(() => {}) | |
20 | + .orderBy('id') | |
21 | + .fetchPage({ | |
22 | + pageSize: 150, | |
23 | + page: req.params.page, | |
24 | + withRelated: ['imagenes'] | |
30 | 25 | }) |
31 | - .fetchAll({withRelated: ['imagenes']}) | |
32 | - | |
33 | - Promise.all([articulos, cantidadesVendidas]) | |
34 | - .then(values => { | |
35 | - | |
36 | - let articulos = values[0]; | |
37 | - let cantidadesVendidas = values[1]; | |
26 | + .then(data => { | |
38 | 27 | |
39 | 28 | var promisesCodBar = []; |
40 | 29 | |
41 | - articulos.forEach(articulo => { | |
42 | - | |
43 | - let cantidadVendida = cantidadesVendidas.filter(art => { | |
44 | - return articulo.get('CodArt') == art.ART && articulo.get('CodSec') == art.SEC; | |
45 | - }); | |
46 | - | |
47 | - articulo.set('cantidadVendida', cantidadVendida[0] ? cantidadVendida[0].cantidadVendida : 0); | |
30 | + data.forEach(articulo => { | |
48 | 31 | |
49 | 32 | promisesCodBar.push( |
50 | 33 | knex('CODBAR').where({ |
... | ... | @@ -59,25 +42,23 @@ router.get('/articulos', (req , res) => { |
59 | 42 | codigoBarra.forEach(codigo => { |
60 | 43 | codigo = codigo[0]; |
61 | 44 | |
62 | - let articulo = articulos.filter(art => { | |
63 | - return art.get('CodArt') == codigo.CodArt; | |
64 | - }); | |
45 | + if (codigo) { | |
65 | 46 | |
66 | - articulo[0].set('codigoBarra', codigo.CodBar); | |
67 | - }); | |
47 | + let articulo = data.filter(art => { | |
48 | + return art.get('CodArt') == codigo.CodArt; | |
49 | + }); | |
68 | 50 | |
69 | - res.status(200).send(articulos); | |
51 | + articulo[0].set('codigoBarra', codigo.CodBar); | |
52 | + } | |
53 | + }); | |
54 | + res.status(200).send( | |
55 | + { | |
56 | + data: data, | |
57 | + pagination: data.pagination, | |
58 | + }); | |
70 | 59 | }); |
71 | 60 | |
72 | 61 | }); |
73 | 62 | }); |
74 | 63 | |
75 | -router.get('/articulos/:id', (req, res) => { | |
76 | - Articulo | |
77 | - .where({id: req.params.id}) | |
78 | - .fetch({withRelated: 'imagenes'}) | |
79 | - .then(articulo => res.status(200).send(articulo)) | |
80 | - .catch(err => { res.status(500).send(err)}) | |
81 | -}); | |
82 | - | |
83 | 64 | module.exports = router; |
rutas/imagenes.js
... | ... | @@ -56,4 +56,24 @@ router.post('/imagenes/guardar', (req, res) => { |
56 | 56 | |
57 | 57 | }); |
58 | 58 | |
59 | +router.post('/imagen/borrar', (req, res) => { | |
60 | + | |
61 | + let deleted = false | |
62 | + let path = `${__dirname}/../img/${req.body.name}`; | |
63 | + | |
64 | + knex('ARTICULOS_IMAGEN') | |
65 | + .where('id_articulo', req.body.id_articulo) | |
66 | + .andWhere('id', req.body.id) | |
67 | + .update({ | |
68 | + id_articulo: 0, | |
69 | + }) | |
70 | + .then(() => { | |
71 | + try { | |
72 | + require('fs').unlinkSync(path); | |
73 | + deleted = true; | |
74 | + res.status(200).send(deleted); | |
75 | + } catch (err) { } | |
76 | + }); | |
77 | +}); | |
78 | + | |
59 | 79 | module.exports = router; |