salix/services/loopback/common/methods/client/activate.js

63 lines
1.9 KiB
JavaScript

var request = require('request');
module.exports = function(Self) {
Self.remoteMethod('activate', {
description: 'Activate or deactive client',
accepts: [
{
arg: 'id',
type: 'number',
required: true,
description: 'Model id',
http: {source: 'path'}
}, {
arg: 'context',
type: 'object',
http: function(ctx) {
return ctx;
}
}
],
returns: {
arg: 'active',
type: 'boolean'
},
http: {
verb: 'put',
path: '/:id/activate'
}
});
Self.activate = function(id, ctx, cb) {
Self.findById(id, function(err, client) {
if (err) return cb(err);
Self.update({id: client.id}, {active: !client.active});
let filter = {where: {clientFk: client.id}, fields: ['started', 'ended']};
Self.app.models.CreditClassification.findOne(filter, function(error, data) {
if (error) return;
let currentDate = new Date();
if (data && client.active && (data.ended >= currentDate || data.ended == null)) {
let referer = ctx.req.headers.referer;
var options = {
url: `${referer}/mailer/notification/client-deactivate/${client.id}`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: ctx.req.headers.authorization
},
json: {}
};
request(options);
}
});
cb(null, !client.active);
});
};
};