Tarea #438 order.line

This commit is contained in:
gerard 2018-08-07 15:48:55 +02:00
parent 4c45db1c16
commit da9a783843
7 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,36 @@
const UserError = require('vn-loopback/common/helpers').UserError;
module.exports = Self => {
Self.remoteMethod('removes', {
description: 'Delete an Order Row',
accessType: '',
accepts: [{
arg: 'params',
type: 'object',
required: true,
description: '[Row IDs], actualOrderId',
http: {source: 'body'}
}],
returns: {
type: 'string',
root: true
},
http: {
path: `/removes`,
verb: 'post'
}
});
Self.removes = async params => {
if (!params.rows || !params.rows.length)
throw new Error('There is nothing delete');
await Self.app.models.Order.isEditable(params.actualOrderId);
let promises = [];
for (let i = 0; i < params.rows.length; i++) {
promises.push(Self.app.models.OrderRow.destroyById(params.rows[i]));
}
return await Promise.all(promises);
};
};

View File

@ -0,0 +1,25 @@
const app = require(`../../../../server/server`);
describe('order removes()', () => {
it('should throw an error if rows property is empty', async() => {
let error;
try {
await app.models.OrderRow.removes({rows: []});
} catch (e) {
error = e;
}
expect(error).toEqual(new Error('There is nothing delete'));
});
it('should throw an error if the row selected is not editable', async() => {
let error;
try {
await app.models.OrderRow.removes({rows: [2]});
} catch (e) {
error = e;
}
expect(error).toEqual(new Error('This order is not editable'));
});
});

View File

@ -0,0 +1,30 @@
const UserError = require('vn-loopback/common/helpers').UserError;
module.exports = Self => {
Self.remoteMethod('isEditable', {
description: 'Check if an order is editable',
accessType: 'READ',
accepts: [{
arg: 'orderId',
type: 'number',
required: true,
description: 'orderId',
http: {source: 'path'}
}],
returns: {
type: 'boolean',
root: true
},
http: {
path: `/:orderId/isEditable`,
verb: 'get'
}
});
Self.isEditable = async orderId => {
let exists = await Self.app.models.Order.findOne({where: {id: orderId}, fields: ['isConfirmed']});
if (!exists || exists.isConfirmed === 1)
throw new UserError('This order is not editable');
};
};

View File

@ -0,0 +1,26 @@
const app = require(`../../../../server/server`);
const UserError = require('vn-loopback/common/helpers').UserError;
describe('order isEditable()', () => {
it('should throw an error if the order given is not editable', async() => {
let error;
try {
await app.models.Order.isEditable(2);
} catch (e) {
error = e;
}
expect(error).toEqual(new UserError('This order is not editable'));
});
it('should throw an error if the order given does not exists', async() => {
let error;
try {
await app.models.Order.isEditable(99999);
} catch (e) {
error = e;
}
expect(error).toEqual(new UserError('This order is not editable'));
});
});

View File

@ -0,0 +1,4 @@
module.exports = Self => {
//require('../methods/order-row/new')(Self);
require('../methods/order-row/removes')(Self);
};

View File

@ -50,6 +50,11 @@
"type": "belongsTo",
"model": "Order",
"foreignKey": "orderFk"
},
"warehouse": {
"type": "belongsTo",
"model": "Warehouse",
"foreignKey": "warehouseFk"
}
}
}

View File

@ -2,6 +2,8 @@ module.exports = Self => {
require('../methods/order/new')(Self);
require('../methods/order/getTotalVolume')(Self);
require('../methods/order/getVolumes')(Self);
//require('../methods/order/getTaxes')(Self);
require('../methods/order/isEditable')(Self);
require('../methods/order/getTotal')(Self);
require('../methods/order/itemFilter')(Self);
};