salix/modules/supplier/back/methods/supplier/newSupplier.js

37 lines
1002 B
JavaScript

const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('newSupplier', {
description: 'Creates a new supplier and returns it',
accessType: 'WRITE',
accepts: [{
arg: 'name',
type: 'string'
}],
returns: {
type: 'string',
root: true
},
http: {
path: `/newSupplier`,
verb: 'POST'
}
});
Self.newSupplier = async(ctx, options) => {
const models = Self.app.models;
const args = ctx.args;
const myOptions = {validate: false};
if (typeof options == 'object')
Object.assign(myOptions, options);
delete args.ctx;
if (!args.name) throw new UserError('The social name cannot be empty');
const data = {...args, ...{nickname: args.name}};
const supplier = await models.Supplier.create(data, myOptions);
return supplier;
};
};