From c0de02a4caeb53733278d732fd373774e2b75de8 Mon Sep 17 00:00:00 2001 From: nelo Date: Wed, 25 Jan 2017 11:46:15 +0100 Subject: [PATCH] Endpoint desactivar --- .../crud/src/client/descriptor/descriptor.js | 22 ++------------- services/client/common/models/Client.js | 27 ++++++++++++++++--- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/@salix/crud/src/client/descriptor/descriptor.js b/@salix/crud/src/client/descriptor/descriptor.js index beaaf941d..ca4bf7e94 100644 --- a/@salix/crud/src/client/descriptor/descriptor.js +++ b/@salix/crud/src/client/descriptor/descriptor.js @@ -11,28 +11,10 @@ export const COMPONENT = { }, controller: function($http, $scope, copyObject) { var self = this; - this.$onChanges = function(changes) { - if (this.client) { - this.copyClient(); - } - }; - $scope.$watch('descriptor.client.active', function(newValue, oldValue) { - if (self.client && self.clientOld && self.client.active !== self.clientOld.active) { - var newClient = {id: self.client.id, active: self.client.active, modify: "Active"}; - $http.put('/client/api/Clients', newClient).then( - json => { - self.client = json.data; - self.copyClient(); - } - ); - } + if (oldValue !== undefined) + $http.put(`/client/api/Clients/${self.client.id}/activate`, {}); }); - - this.copyClient = function() { - this.clientOld = {}; - copyObject(this.client, this.clientOld); - }; } }; COMPONENT.controller.$inject = ['$http', '$scope', 'copyObject']; diff --git a/services/client/common/models/Client.js b/services/client/common/models/Client.js index 0fae09502..593e5f31e 100644 --- a/services/client/common/models/Client.js +++ b/services/client/common/models/Client.js @@ -6,12 +6,33 @@ module.exports = function(Client) { Client.validate('payMethod',hasCC,{message: 'Introduzca el iban del cliente'}); function hasCC(err) { if (this.payMethod == 2 && !this.iban) err(); - } + }; Client.validate('payMethod',hasSalesMan,{message: 'No se puede cambiar la forma de pago si no hay comercial asignado'}); function hasSalesMan(err) { if (this.payMethod && !this.salesPerson) err(); - } + }; + + Client.remoteMethod('activate', + { + returns: { + arg: 'active', + type: 'boolean' + }, + http: { + path: '/:id/activate', verb: 'put' + } + }); + + Client.activate = function(cb){ + var id = 12; + Client.findById(id, function(err, client) { + if (!err) { + Client.update({id: client.id}, {active: !client.active}); + cb(null, !client.active); + } + }) + }; // Hooks Client.observe('before save', function (ctx, next) { @@ -27,8 +48,6 @@ module.exports = function(Client) { if (item.payMethod != ctx.data.payMethod && item.dueDay == ctx.data.dueDay) { ctx.data.dueDay = 5; } - if(!ctx.data.name) - ctx.data.name = item.name; } next(); }