79 lines
2.2 KiB
JavaScript
79 lines
2.2 KiB
JavaScript
let UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('new', {
|
|
description: 'Create a new order and returns the new ID',
|
|
accessType: 'WRITE',
|
|
accepts: [
|
|
{
|
|
arg: 'landed',
|
|
description: 'The landed for the order',
|
|
type: 'date',
|
|
required: true
|
|
}, {
|
|
arg: 'addressId',
|
|
description: 'The address for the order',
|
|
type: 'number',
|
|
required: true
|
|
}, {
|
|
arg: 'agencyModeId',
|
|
description: 'The agencyMode for the order',
|
|
type: 'number',
|
|
required: true
|
|
}
|
|
],
|
|
returns: {
|
|
type: 'number',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/new`,
|
|
verb: 'post'
|
|
}
|
|
});
|
|
|
|
Self.new = async(ctx, landed, addressId, agencyModeId, options) => {
|
|
const myOptions = {userId: ctx.req.accessToken.userId};
|
|
let tx;
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const address = await Self.app.models.Address.findOne({
|
|
where: {id: addressId},
|
|
fields: ['clientFk'],
|
|
include: [
|
|
{relation: 'client'
|
|
}
|
|
]
|
|
}, myOptions);
|
|
|
|
if (address.client().typeFk === 'normal') {
|
|
if (!address.client().isActive)
|
|
throw new UserError(`You can't create an order for an inactive client`);
|
|
}
|
|
|
|
query = `CALL vn.orderListCreate(?, ?, ?, ?);`;
|
|
[result] = await Self.rawSql(query, [
|
|
landed,
|
|
agencyModeId,
|
|
addressId,
|
|
'SALIX'
|
|
], myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return result[0].vOrderId;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|