diff --git a/client/production/src/production-actions/production-actions.js b/client/production/src/production-actions/production-actions.js index 79552ce6b..b3411a1ae 100644 --- a/client/production/src/production-actions/production-actions.js +++ b/client/production/src/production-actions/production-actions.js @@ -48,7 +48,7 @@ export default class ProductionActions { ); } _sendMessage(tickets) { - this.$http.post(`/production/api/TicketStates/messageSend`, {tickets: tickets}).then( + this.$http.post(`/production/api/FakeProductions/messageSend`, {tickets: tickets}).then( () => { this.vnApp.showMessage(this.$translate.instant('Success: message send!')); } diff --git a/services/client/common/models/user.json b/services/client/common/models/user.json index f13d4c168..2f79cebdb 100644 --- a/services/client/common/models/user.json +++ b/services/client/common/models/user.json @@ -6,6 +6,9 @@ "id": true, "type": "Number", "forceId": false + }, + "username":{ + "type": "string" } } } \ No newline at end of file diff --git a/services/production/common/methods/fake-production/message-send.js b/services/production/common/methods/fake-production/message-send.js index d17073bd4..332f51d08 100644 --- a/services/production/common/methods/fake-production/message-send.js +++ b/services/production/common/methods/fake-production/message-send.js @@ -1,3 +1,4 @@ +var uuid = require('uuid'); module.exports = function(FakeProduction) { FakeProduction.remoteMethodCtx('messageSend', { description: 'Send message to salesPerson of one array of tickets', @@ -6,26 +7,65 @@ module.exports = function(FakeProduction) { type: 'message' }, http: { - path: '/messageSend' + path: '/messageSend', + verb: 'post' } }); FakeProduction.messageSend = function(ctx, cb) { var tickets = ctx.req.body.tickets; - var elements = []; - tickets.forEach(function(t) { - elements.push({salesPerson: 'nelo', message: `Revisa el tickete ${t.ticketFk}`}); - }, this); - messageSend(elements, cb); + var userId = ctx.req.accessToken.$userId; + var User = FakeProduction.app.models.User; + + User.findById(userId, function (err, user) { + var elements = []; + tickets.forEach(function(t) { + elements.push({sender: user.username, salesPerson: t.salesPerson, message: `Revisa el tickete ${t.ticketFk}`}); + }, this); + messageSend(elements, cb); + }); }; var messageSend = function(elements, cb){ + var messages = []; + var messagesInbox = []; elements.forEach(function(e) { - var query = `select messageSend(?, ?)`; - var params = [e.salesPerson, e.message]; - FakeProduction.rawSql(query, params, cb); + var id = uuid.v1(); + var message = {uuid: id, sender: e.username, recipient: e.salesPerson, message: e.message}; + var messageInbox = {uuid: id, sender: e.username, recipient: e.salesPerson, finalRecipient: e.salesPerson, message: e.message}; + messages.push(message); + messagesInbox.push(messageInbox); }, this); - - cb(null, "Mensaje enviado"); + createMessage(messages, messagesInbox, cb); } -} \ No newline at end of file + + var createMessage = function(messages, messagesInbox, cb) { + var Message = FakeProduction.app.models.Message; + FakeProduction.beginTransaction({isolationLevel: FakeProduction.Transaction.READ_COMMITTED}, function(err, tx) { + Message.create(messages, {transaction: tx}, function (err, res) { + if(err){ + transaction.rollback(function(error) { + (error) ? cb(error, null) : cb(err, null); + }); + } else{ + createMessageInbox(messagesInbox, tx, cb); + } + }); + }); + } + + var createMessageInbox = function(messagesInbox, tx, cb) { + var MessageInbox = FakeProduction.app.models.MessageInbox; + MessageInbox.create(messagesInbox, {transaction: tx}, function (err, res) { + if(err){ + tx.rollback(function(error) { + (error) ? cb(error, null) : cb(err, null); + }); + } else{ + tx.commit(function(err) { + (err) ? cb(err, null) : cb(null, res); + }); + } + }); + } +} diff --git a/services/production/common/methods/ticket/change-time.js b/services/production/common/methods/ticket/change-time.js index f6ff225bb..e877c0051 100644 --- a/services/production/common/methods/ticket/change-time.js +++ b/services/production/common/methods/ticket/change-time.js @@ -34,5 +34,6 @@ module.exports = function(Ticket) { cb(null, response); }); }); + }; } \ No newline at end of file diff --git a/services/production/common/models/message-inbox.json b/services/production/common/models/message-inbox.json new file mode 100644 index 000000000..6cf1498ab --- /dev/null +++ b/services/production/common/models/message-inbox.json @@ -0,0 +1,44 @@ +{ + "name": "MessageInbox", + "base": "MyModel", + "validateUpsert": true, + "properties": { + "id": { + "type": "Number", + "id": true, + "description": "Identifier" + }, + "uuid":{ + "type": "String" + }, + "sender":{ + "type": "String" + }, + "recipient":{ + "type": "String" + }, + "finalRecipient":{ + "type": "String" + }, + "message":{ + "type": "String" + }, + "sendDate":{ + "type": "date" + } + }, + "acls": [ + { + "accessType": "*", + "principalType": "ROLE", + "principalId": "$everyone", + "permission": "DENY" + }, + { + "accessType": "*", + "principalType": "ROLE", + "principalId": "root", + "permission": "ALLOW" + } + ] +} diff --git a/services/production/common/models/message.json b/services/production/common/models/message.json new file mode 100644 index 000000000..4beacc4ca --- /dev/null +++ b/services/production/common/models/message.json @@ -0,0 +1,41 @@ +{ + "name": "Message", + "base": "MyModel", + "validateUpsert": true, + "properties": { + "id": { + "type": "Number", + "id": true, + "description": "Identifier" + }, + "uuid":{ + "type": "String" + }, + "sender":{ + "type": "String" + }, + "recipient":{ + "type": "String" + }, + "message":{ + "type": "String" + }, + "sendDate":{ + "type": "date" + } + }, + "acls": [ + { + "accessType": "*", + "principalType": "ROLE", + "principalId": "$everyone", + "permission": "DENY" + }, + { + "accessType": "*", + "principalType": "ROLE", + "principalId": "root", + "permission": "ALLOW" + } + ] +} diff --git a/services/production/common/models/user.json b/services/production/common/models/user.json index f13d4c168..2f79cebdb 100644 --- a/services/production/common/models/user.json +++ b/services/production/common/models/user.json @@ -6,6 +6,9 @@ "id": true, "type": "Number", "forceId": false + }, + "username":{ + "type": "string" } } } \ No newline at end of file diff --git a/services/production/server/model-config.json b/services/production/server/model-config.json index 64efcbc90..2dd14490c 100644 --- a/services/production/server/model-config.json +++ b/services/production/server/model-config.json @@ -76,5 +76,14 @@ "FakeProduction": { "dataSource": "vn", "public": true + }, + "Message": { + "dataSource": "vn", + "public": true + }, + "MessageInbox": { + "dataSource": "vn", + "public": true } + } diff --git a/services/service/models/user.json b/services/service/models/user.json index f13d4c168..2f79cebdb 100644 --- a/services/service/models/user.json +++ b/services/service/models/user.json @@ -6,6 +6,9 @@ "id": true, "type": "Number", "forceId": false + }, + "username":{ + "type": "string" } } } \ No newline at end of file