@@ -2,6 +2,8 @@ var db = require('../models')
2
2
var bCrypt = require ( 'bcrypt' )
3
3
const exec = require ( 'child_process' ) . exec ;
4
4
var mathjs = require ( 'mathjs' )
5
+ var libxmljs = require ( "libxmljs" ) ;
6
+ var serialize = require ( "node-serialize" )
5
7
const Op = db . Sequelize . Op
6
8
7
9
module . exports . userSearch = function ( req , res ) {
@@ -209,3 +211,35 @@ module.exports.listUsersAPI = function (req, res) {
209
211
} )
210
212
} )
211
213
}
214
+
215
+ module . exports . bulkProducts = function ( req , res ) {
216
+ // TODO: Deprecate this soon
217
+ if ( req . query . legacy && req . files . products ) {
218
+ var products = serialize . unserialize ( req . files . products . data . toString ( 'utf8' ) )
219
+ console . log ( products )
220
+ products . forEach ( function ( product ) {
221
+ var newProduct = new db . Product ( )
222
+ newProduct . name = product . name
223
+ newProduct . code = product . code
224
+ newProduct . tags = product . tags
225
+ newProduct . description = product . description
226
+
227
+ newProduct . save ( )
228
+ } )
229
+ res . redirect ( '/app/products' )
230
+ }
231
+ else if ( req . files . products && req . files . products . mimetype == 'text/xml' ) {
232
+ var products = libxmljs . parseXmlString ( req . files . products . data . toString ( 'utf8' ) , { noent :true , noblanks :true } )
233
+ products . root ( ) . childNodes ( ) . forEach ( product => {
234
+ var newProduct = new db . Product ( )
235
+ newProduct . name = product . childNodes ( ) [ 0 ] . text ( )
236
+ newProduct . code = product . childNodes ( ) [ 1 ] . text ( )
237
+ newProduct . tags = product . childNodes ( ) [ 2 ] . text ( )
238
+ newProduct . description = product . childNodes ( ) [ 3 ] . text ( )
239
+ newProduct . save ( )
240
+ } )
241
+ res . redirect ( '/app/products' )
242
+ } else {
243
+ res . render ( 'app/bulkproducts' , { messages :{ danger :'Invalid file' } } )
244
+ }
245
+ }
0 commit comments