From 57acca2f5eafebdc3325ec435c0fbb5ba5bc9614 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Thu, 23 Jan 2020 13:31:07 +0100 Subject: [PATCH] refactor & updated tests --- back/methods/chat/send.js | 100 +++++++++++++----- back/methods/message/send.js | 48 --------- back/methods/message/specs/send.spec.js | 14 --- back/model-config.json | 6 -- back/models/message-inbox.json | 43 -------- back/models/message.js | 3 - back/models/message.json | 39 ------- loopback/locale/en.json | 6 +- loopback/locale/es.json | 6 +- .../back/methods/claim/regularizeClaim.js | 32 +++--- .../claim/specs/regularizeClaim.spec.js | 8 +- .../createWithInsurance.js | 18 ++-- .../createWithInsurance.spec.js | 21 +++- modules/client/back/models/client.js | 32 +++--- .../client/back/models/credit-insurance.js | 75 +++++-------- .../client/back/models/credit-insurance.json | 26 +++++ .../back/methods/ticket-request/confirm.js | 24 +++-- .../ticket-request/specs/confirm.spec.js | 43 ++++---- print/methods/closure.js | 4 +- 19 files changed, 242 insertions(+), 306 deletions(-) delete mode 100644 back/methods/message/send.js delete mode 100644 back/methods/message/specs/send.spec.js delete mode 100644 back/models/message-inbox.json delete mode 100644 back/models/message.js delete mode 100644 back/models/message.json diff --git a/back/methods/chat/send.js b/back/methods/chat/send.js index 7a441fcba..c36178b0f 100644 --- a/back/methods/chat/send.js +++ b/back/methods/chat/send.js @@ -7,7 +7,7 @@ module.exports = Self => { arg: 'to', type: 'String', required: true, - description: 'user (@) or channel (#) to send the message' + description: 'User (@) or channel (#) to send the message' }, { arg: 'message', type: 'String', @@ -31,26 +31,23 @@ module.exports = Self => { const recipient = to.replace('@', ''); if (sender.name != recipient) - return sendMessage(to, `@${sender.name}: ${message}`); + return sendMessage(sender, to, `@${sender.name}: ${message} `); }; - async function sendMessage(name, message) { - const models = Self.app.models; - const chatConfig = await models.ChatConfig.findOne(); + async function sendMessage(sender, channel, message) { + const config = await getConfig(); + const avatar = `${config.host}/avatar/${sender.name}`; + const uri = `${config.api}/chat.postMessage`; - if (!Self.token) - Self.token = await login(); - - const uri = `${chatConfig.uri}/chat.postMessage`; - return makeRequest(uri, { - 'channel': name, + return sendAuth(uri, { + 'channel': channel, + 'avatar': avatar, 'text': message }).catch(async error => { - if (error.statusCode === 401 && !Self.loginAttempted) { - Self.token = await login(); - Self.loginAttempted = true; + if (error.statusCode === 401 && !this.resendAttempted) { + this.resendAttempted = true; - return sendMessage(name, message); + return sendMessage(sender, channel, message); } throw new Error(error.message); @@ -61,24 +58,51 @@ module.exports = Self => { * Returns a rocketchat token * @return {Object} userId and authToken */ - async function login() { - const models = Self.app.models; - const chatConfig = await models.ChatConfig.findOne(); - const uri = `${chatConfig.uri}/login`; - return makeRequest(uri, { - user: chatConfig.user, - password: chatConfig.password - }).then(res => res.data); + async function getAuthToken() { + if (!this.auth || this.auth && !this.auth.authToken) { + const config = await getConfig(); + const uri = `${config.api}/login`; + const res = await send(uri, { + user: config.user, + password: config.password + }); + + this.auth = res.data; + } + + return this.auth; } - function makeRequest(uri, body) { + /** + * Returns a rocketchat config + * @return {Object} Auth config + */ + async function getConfig() { + if (!this.chatConfig) { + const models = Self.app.models; + + this.chatConfig = await models.ChatConfig.findOne(); + } + + return this.chatConfig; + } + + /** + * Send unauthenticated request + * @param {*} uri - Request uri + * @param {*} body - Request params + * @param {*} options - Request options + * + * @return {Object} Request response + */ + async function send(uri, body, options) { if (process.env.NODE_ENV !== 'production') { return new Promise(resolve => { return resolve({statusCode: 200, message: 'Fake notification sent'}); }); } - const options = { + const defaultOptions = { method: 'POST', uri: uri, body: body, @@ -86,11 +110,29 @@ module.exports = Self => { json: true }; - if (Self.token) { - options.headers['X-Auth-Token'] = Self.token.authToken; - options.headers['X-User-Id'] = Self.token.userId; + if (options) Object.assign(defaultOptions, options); + + return request(defaultOptions); + } + + /** + * Send authenticated request + * @param {*} uri - Request uri + * @param {*} body - Request params + * + * @return {Object} Request response + */ + async function sendAuth(uri, body) { + const login = await getAuthToken(); + const options = { + headers: {'content-type': 'application/json'} + }; + + if (login) { + options.headers['X-Auth-Token'] = login.authToken; + options.headers['X-User-Id'] = login.userId; } - return request(options); + return send(uri, body, options); } }; diff --git a/back/methods/message/send.js b/back/methods/message/send.js deleted file mode 100644 index c8849774b..000000000 --- a/back/methods/message/send.js +++ /dev/null @@ -1,48 +0,0 @@ -module.exports = Self => { - Self.remoteMethodCtx('send', { - description: 'Send message to user', - accessType: 'WRITE', - accepts: [{ - arg: 'data', - type: 'object', - required: true, - description: 'recipientFk, message', - http: {source: 'body'} - }, { - arg: 'context', - type: 'object', - http: function(ctx) { - return ctx; - } - }], - returns: { - type: 'boolean', - root: true - }, - http: { - path: `/:recipient/send`, - verb: 'post' - } - }); - - Self.send = async(ctx, data, options) => { - const accessToken = ctx.options && ctx.options.accessToken || ctx.req && ctx.req.accessToken; - const userId = accessToken.userId; - const models = Self.app.models; - const sender = await models.Account.findById(userId, null, options); - const recipient = await models.Account.findById(data.recipientFk, null, options); - - await Self.create({ - sender: sender.name, - recipient: recipient.name, - message: data.message - }, options); - - return await models.MessageInbox.create({ - sender: sender.name, - recipient: recipient.name, - finalRecipient: recipient.name, - message: data.message - }, options); - }; -}; diff --git a/back/methods/message/specs/send.spec.js b/back/methods/message/specs/send.spec.js deleted file mode 100644 index 6749bd7cd..000000000 --- a/back/methods/message/specs/send.spec.js +++ /dev/null @@ -1,14 +0,0 @@ -const app = require('vn-loopback/server/server'); - -describe('message send()', () => { - it('should return a response containing the same message in params', async() => { - let ctx = {req: {accessToken: {userId: 1}}}; - let params = { - recipientFk: 1, - message: 'I changed something' - }; - let response = await app.models.Message.send(ctx, params, {transaction: 'You'}); - - expect(response.message).toEqual(params.message); - }); -}); diff --git a/back/model-config.json b/back/model-config.json index 057311992..50603be3f 100644 --- a/back/model-config.json +++ b/back/model-config.json @@ -23,12 +23,6 @@ "Delivery": { "dataSource": "vn" }, - "Message": { - "dataSource": "vn" - }, - "MessageInbox": { - "dataSource": "vn" - }, "Province": { "dataSource": "vn" }, diff --git a/back/models/message-inbox.json b/back/models/message-inbox.json deleted file mode 100644 index 238f3e590..000000000 --- a/back/models/message-inbox.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "name": "MessageInbox", - "base": "VnModel", - "options": { - "mysql": { - "table": "messageInbox" - } - }, - "properties": { - "id": { - "type": "Number", - "id": true, - "description": "Identifier" - }, - "sender": { - "type": "String", - "required": true - }, - "recipient": { - "type": "String", - "required": true - }, - "finalRecipient": { - "type": "String", - "required": true - }, - "message": { - "type": "String" - } - }, - "relations": { - "remitter": { - "type": "belongsTo", - "model": "User", - "foreignKey": "sender" - }, - "receptor": { - "type": "belongsTo", - "model": "User", - "foreignKey": "recipient" - } - } -} \ No newline at end of file diff --git a/back/models/message.js b/back/models/message.js deleted file mode 100644 index 6a53476f9..000000000 --- a/back/models/message.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = Self => { - require('../methods/message/send')(Self); -}; diff --git a/back/models/message.json b/back/models/message.json deleted file mode 100644 index 2a855c907..000000000 --- a/back/models/message.json +++ /dev/null @@ -1,39 +0,0 @@ -{ - "name": "Message", - "base": "VnModel", - "options": { - "mysql": { - "table": "message" - } - }, - "properties": { - "id": { - "type": "Number", - "id": true, - "description": "Identifier" - }, - "sender": { - "type": "String", - "required": true - }, - "recipient": { - "type": "String", - "required": true - }, - "message": { - "type": "String" - } - }, - "relations": { - "remitter": { - "type": "belongsTo", - "model": "User", - "foreignKey": "sender" - }, - "receptor": { - "type": "belongsTo", - "model": "User", - "foreignKey": "recipient" - } - } -} \ No newline at end of file diff --git a/loopback/locale/en.json b/loopback/locale/en.json index 91339d14e..70d06c9bd 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -57,5 +57,9 @@ "The postcode doesn't exists. Ensure you put the correct format": "The postcode doesn't exists. Ensure you put the correct format", "Can't create stowaway for this ticket": "Can't create stowaway for this ticket", "Has deleted the ticket id": "Has deleted the ticket id [#{{id}}]({{{url}}})", - "Swift / BIC can't be empty": "Swift / BIC can't be empty" + "Swift / BIC can't be empty": "Swift / BIC can't be empty", + "MESSAGE_BOUGHT_UNITS": "Bought {{quantity}} units of {{concept}} (#{{itemId}}) for the ticket id [#{{ticketId}}]({{{url}}})", + "MESSAGE_INSURANCE_CHANGE": "I have changed the insurence credit of client [{{clientName}} (#{{clientId}})]({{{url}}}) to *{{credit}} €*", + "MESSAGE_CHANGED_PAYMETHOD": "I have changed the pay method for client [{{clientName}} (#{{clientId}})]({{{url}}})", + "MESSAGE_CLAIM_ITEM_REGULARIZE": "I sent *{{quantity}}* units of [{{concept}} (#{{itemId}})]({{{itemUrl}}}) to {{nickname}} coming from ticket id [#{{ticketId}}]({{{ticketUrl}}})" } \ No newline at end of file diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 15fd89f5e..3de0b1626 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -118,5 +118,9 @@ "You should specify at least a start or end date": "Debes especificar al menos una fecha de inicio o de fín", "Start date should be lower than end date": "La fecha de inicio debe ser menor que la fecha de fín", "You should mark at least one week day": "Debes marcar al menos un día de la semana", - "Swift / BIC can't be empty": "Swift / BIC no puede estar vacío" + "Swift / BIC can't be empty": "Swift / BIC no puede estar vacío", + "MESSAGE_BOUGHT_UNITS": "Se ha comprado {{quantity}} unidades de {{concept}} (#{{itemId}}) para el ticket id [#{{ticketId}}]({{{url}}})", + "MESSAGE_INSURANCE_CHANGE": "He cambiado el crédito asegurado del cliente [{{clientName}} (#{{clientId}})]({{{url}}}) a *{{credit}} €*", + "MESSAGE_CHANGED_PAYMETHOD": "He cambiado la forma de pago del cliente [{{clientName}} (#{{clientId}})]({{{url}}})", + "MESSAGE_CLAIM_ITEM_REGULARIZE": "Envio *{{quantity}}* unidades de [{{concept}} (#{{itemId}})]({{{itemUrl}}}) a {{nickname}} provenientes del ticket id [#{{ticketId}}]({{{ticketUrl}}})" } \ No newline at end of file diff --git a/modules/claim/back/methods/claim/regularizeClaim.js b/modules/claim/back/methods/claim/regularizeClaim.js index e1e95728b..8a4f6dc95 100644 --- a/modules/claim/back/methods/claim/regularizeClaim.js +++ b/modules/claim/back/methods/claim/regularizeClaim.js @@ -19,6 +19,7 @@ module.exports = Self => { Self.regularizeClaim = async(ctx, params) => { const models = Self.app.models; + const $t = ctx.req.__; // $translate const resolvedState = 3; let tx = await Self.beginTransaction({}); @@ -38,8 +39,7 @@ module.exports = Self => { const destination = claimEnd.claimDestination(); const addressFk = destination && destination.addressFk; - if (!addressFk) - continue; + if (!addressFk) continue; let sale = await getSale(claimEnd.saleFk, options); let ticketFk = await getTicketId({ @@ -70,15 +70,19 @@ module.exports = Self => { discount: 100 }, options); - if (sale.ticket().client().salesPerson()) { - await sendMessage(ctx, { - itemFk: sale.itemFk, - ticketFk: sale.ticketFk, - recipientFk: sale.ticket().client().salesPerson().userFk, + const salesPerson = sale.ticket().client().salesPerson(); + if (salesPerson) { + const origin = ctx.req.headers.origin; + const message = $t('MESSAGE_CLAIM_ITEM_REGULARIZE', { quantity: sale.quantity, concept: sale.concept, - nickname: address.nickname - }, options); + itemId: sale.itemFk, + ticketId: sale.ticketFk, + nickname: address.nickname, + ticketUrl: `${origin}/#!/ticket/${sale.ticketFk}/summary`, + itemUrl: `${origin}/#!/item/${sale.itemFk}/summary` + }); + await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message); } } @@ -156,14 +160,4 @@ module.exports = Self => { return ticket.id; } - - async function sendMessage(ctx, params, options) { - const message = `Envio ${params.quantity} unidades de "${params.concept}" (#${params.itemFk}) a ` - + `"${params.nickname}" provenientes del ticket #${params.ticketFk}`; - - await Self.app.models.Message.send(ctx, { - recipientFk: params.recipientFk, - message: message - }, options); - } }; diff --git a/modules/claim/back/methods/claim/specs/regularizeClaim.spec.js b/modules/claim/back/methods/claim/specs/regularizeClaim.spec.js index d838bc907..fd3fb3c3d 100644 --- a/modules/claim/back/methods/claim/specs/regularizeClaim.spec.js +++ b/modules/claim/back/methods/claim/specs/regularizeClaim.spec.js @@ -22,7 +22,13 @@ describe('regularizeClaim()', () => { }); it('should change claim state to resolved', async() => { - let ctx = {req: {accessToken: {userId: 18}}}; + const ctx = {req: { + accessToken: {userId: 18}, + headers: {origin: 'http://localhost'}} + }; + ctx.req.__ = value => { + return value; + }; let params = {claimFk: claimFk}; claimEnds = await app.models.ClaimEnd.importTicketSales(ctx, { diff --git a/modules/client/back/methods/credit-classification/createWithInsurance.js b/modules/client/back/methods/credit-classification/createWithInsurance.js index 368e9a41a..0b74b70a2 100644 --- a/modules/client/back/methods/credit-classification/createWithInsurance.js +++ b/modules/client/back/methods/credit-classification/createWithInsurance.js @@ -23,23 +23,25 @@ module.exports = Self => { }); Self.createWithInsurance = async(data, ctx) => { - let tx = await Self.beginTransaction({}); + const tx = await Self.beginTransaction({}); + const models = Self.app.models; + const $t = ctx.req.__; // $translate try { let options = {transaction: tx}; - let classificationSchema = {client: data.clientFk, started: data.started}; - let newClassification = await Self.create(classificationSchema, options); - let CreditInsurance = Self.app.models.CreditInsurance; - let insuranceSchema = { + const newClassification = await Self.create({ + client: data.clientFk, + started: data.started + }, options); + + await models.CreditInsurance.create({ creditClassification: newClassification.id, credit: data.credit, grade: data.grade - }; + }, options); - let newCreditInsurance = await CreditInsurance.create(insuranceSchema, options); await tx.commit(); - await CreditInsurance.messageSend(newCreditInsurance, ctx.req.accessToken); return newClassification; } catch (e) { diff --git a/modules/client/back/methods/credit-classification/createWithInsurance.spec.js b/modules/client/back/methods/credit-classification/createWithInsurance.spec.js index 4e5b5127e..21abddb81 100644 --- a/modules/client/back/methods/credit-classification/createWithInsurance.spec.js +++ b/modules/client/back/methods/credit-classification/createWithInsurance.spec.js @@ -1,7 +1,20 @@ const app = require('vn-loopback/server/server'); +const LoopBackContext = require('loopback-context'); describe('Client createWithInsurance', () => { let classificationId; + const activeCtx = { + accessToken: {userId: 101}, + http: { + req: { + headers: {origin: 'http://localhost'} + } + } + }; + const ctx = {req: activeCtx}; + activeCtx.http.req.__ = value => { + return value; + }; afterAll(async done => { await app.models.CreditClassification.destroyById(classificationId); @@ -20,7 +33,9 @@ describe('Client createWithInsurance', () => { it('should not create the insurance if couldnt create the classification', async() => { let error; let data = {clientFk: null, started: Date.now(), credit: 999, grade: 255}; - let ctx = {req: {accessToken: {userId: 101}}}; + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx + }); await app.models.CreditClassification.createWithInsurance(data, ctx) .catch(e => { error = e; @@ -37,7 +52,9 @@ describe('Client createWithInsurance', () => { it('should create a new client credit classification with insurance', async() => { let data = {clientFk: 101, started: Date.now(), credit: 999, grade: 255}; - let ctx = {req: {accessToken: {userId: 101}}}; + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ + active: activeCtx + }); let result = await app.models.CreditClassification.createWithInsurance(data, ctx); classificationId = result.id; diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index 9b9db7161..1dba6c6db 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -225,34 +225,38 @@ module.exports = Self => { const newInstance = hookState.newInstance; const oldInstance = hookState.oldInstance; const instance = ctx.instance; + const models = Self.app.models; const payMethodChanged = oldInstance.payMethodFk != newInstance.payMethodFk; const ibanChanged = oldInstance.iban != newInstance.iban; const dueDayChanged = oldInstance.dueDay != newInstance.dueDay; if (payMethodChanged || ibanChanged || dueDayChanged) { - const message = `La forma de pago del cliente con id ${instance.id} ha cambiado`; - const salesPersonFk = instance.salesPersonFk; + const loopBackContext = LoopBackContext.getCurrentContext(); + const httpCtx = {req: loopBackContext.active}; + const httpRequest = httpCtx.req.http.req; + const $t = httpRequest.__; + const origin = httpRequest.headers.origin; - if (salesPersonFk) { - const salesPerson = await Self.app.models.Worker.findById(salesPersonFk); - await Self.app.models.Message.send(ctx, { - recipientFk: salesPerson.userFk, - message: message + const salesPersonId = instance.salesPersonFk; + + if (salesPersonId) { + const fullUrl = `${origin}/#!/client/${instance.id}/billing-data`; + const message = $t('MESSAGE_CHANGED_PAYMETHOD', { + clientId: instance.id, + clientName: instance.name, + url: fullUrl }); + await models.Chat.sendCheckingPresence(httpCtx, salesPersonId, message); } // Send email to client - if (!instance.email) return; - const loopBackContext = LoopBackContext.getCurrentContext(); - const headers = loopBackContext.active.http.req.headers; - const params = { + const serializedParams = httpParamSerializer({ clientId: instance.id, recipient: instance.email - }; - const serializedParams = httpParamSerializer(params); - const query = `${headers.origin}/api/email/payment-update?${serializedParams}`; + }); + const query = `${origin}/api/email/payment-update?${serializedParams}`; await request.get(query); } }); diff --git a/modules/client/back/models/credit-insurance.js b/modules/client/back/models/credit-insurance.js index 3310ffb11..25117ead3 100644 --- a/modules/client/back/models/credit-insurance.js +++ b/modules/client/back/models/credit-insurance.js @@ -1,3 +1,5 @@ +const LoopBackContext = require('loopback-context'); + module.exports = function(Self) { Self.validateCredit = function(credit) { return credit >= 0; @@ -38,54 +40,31 @@ module.exports = function(Self) { message: 'The grade must be similar to the last one' }); - Self.messageSend = async function(data, accessToken) { - let filter = { - include: { - relation: 'classification', - scope: { - fields: ['client'], - include: { - relation: 'customer', - scope: { - fields: ['name', 'salesPersonFk'], - include: { - relation: 'salesPerson', - scope: { - fields: 'userFk', - include: { - relation: 'user', - scope: { - fields: ['name'] - } - } - } - } - } - } - } - } - }; - - let ctx = {req: {accessToken: accessToken}}; - let insurance = await Self.findById(data.id, filter); - let customer = insurance.classification().customer(); - - if (!customer.salesPerson()) return; - let salesPersonId = customer.salesPerson().user().id; - let grade = data.grade ? `(Grado ${data.grade})` : '(Sin grado)'; - let params = { - recipientFk: salesPersonId, - message: `He cambiado el crédito asegurado del ` - + `cliente "${customer.name}" a ${data.credit} € ${grade}` - }; - - Self.app.models.Message.send(ctx, params); - }; - - // Update from transaction misses ctx accessToken. - // Fixed passing accessToken from method messageSend() Self.observe('after save', async function(ctx) { - if (ctx.options.accessToken) - await Self.messageSend(ctx.instance, ctx.options.accessToken); + const loopBackContext = LoopBackContext.getCurrentContext(); + const httpCtx = {req: loopBackContext.active}; + const options = ctx.options ? ctx.options : null; + const models = Self.app.models; + + if (!ctx.isNewInstance) return; + + const data = ctx.instance; + const insurance = await Self.findById(data.id, null, options); + const client = insurance.classification().customer(); + const salesPerson = client.salesPerson(); + + if (!salesPerson) return; + + const httpRequest = httpCtx.req.http.req; + const $t = httpRequest.__; + const origin = httpRequest.headers.origin; + const fullPath = `${origin}/#!/client/${client.id}`; + const message = $t('MESSAGE_INSURANCE_CHANGE', { + clientId: client.id, + clientName: client.name, + credit: data.credit, + url: fullPath + }); + await models.Chat.sendCheckingPresence(httpCtx, salesPerson.id, message); }); }; diff --git a/modules/client/back/models/credit-insurance.json b/modules/client/back/models/credit-insurance.json index 688c511fb..b62c5b4f5 100644 --- a/modules/client/back/models/credit-insurance.json +++ b/modules/client/back/models/credit-insurance.json @@ -32,5 +32,31 @@ "model": "CreditClassification", "foreignKey": "creditClassification" } + }, + "scope": { + "include": { + "relation": "classification", + "scope": { + "fields": ["client"], + "include": { + "relation": "customer", + "scope": { + "fields": ["name", "salesPersonFk"], + "include": { + "relation": "salesPerson", + "scope": { + "fields": "userFk", + "include": { + "relation": "user", + "scope": { + "fields": ["name"] + } + } + } + } + } + } + } + } } } \ No newline at end of file diff --git a/modules/ticket/back/methods/ticket-request/confirm.js b/modules/ticket/back/methods/ticket-request/confirm.js index 72c41737a..938efa7e5 100644 --- a/modules/ticket/back/methods/ticket-request/confirm.js +++ b/modules/ticket/back/methods/ticket-request/confirm.js @@ -32,8 +32,9 @@ module.exports = Self => { Self.confirm = async ctx => { const models = Self.app.models; + const tx = await Self.beginTransaction({}); + const $t = ctx.req.__; // $translate let sale; - let tx = await Self.beginTransaction({}); try { let options = {transaction: tx}; @@ -59,7 +60,7 @@ module.exports = Self => { if (request.saleFk) { sale = await models.Sale.findById(request.saleFk, null, options); - sale.updateAttributes({ + await sale.updateAttributes({ itemFk: ctx.args.itemFk, quantity: ctx.args.quantity, concept: item.name, @@ -71,7 +72,7 @@ module.exports = Self => { quantity: ctx.args.quantity, concept: item.name }, options); - request.updateAttributes({ + await request.updateAttributes({ saleFk: sale.id, itemFk: sale.itemFk, isOk: true @@ -81,13 +82,16 @@ module.exports = Self => { query = `CALL vn.ticketCalculateSale(?)`; await Self.rawSql(query, [sale.id], options); - const message = `Se ha comprado ${sale.quantity} unidades de "${sale.concept}" (#${sale.itemFk}) ` - + `para el ticket #${sale.ticketFk}`; - - await models.Message.send(ctx, { - recipientFk: request.requesterFk, - message: message - }, options); + const origin = ctx.req.headers.origin; + const requesterId = request.requesterFk; + const message = $t('MESSAGE_BOUGHT_UNITS', { + quantity: sale.quantity, + concept: sale.concept, + itemId: sale.itemFk, + ticketId: sale.ticketFk, + url: `${origin}/#!/ticket/${sale.ticketFk}/summary` + }); + await models.Chat.sendCheckingPresence(ctx, requesterId, message); await tx.commit(); diff --git a/modules/ticket/back/methods/ticket-request/specs/confirm.spec.js b/modules/ticket/back/methods/ticket-request/specs/confirm.spec.js index 2383fe560..6cce70b9c 100644 --- a/modules/ticket/back/methods/ticket-request/specs/confirm.spec.js +++ b/modules/ticket/back/methods/ticket-request/specs/confirm.spec.js @@ -4,6 +4,15 @@ describe('ticket-request confirm()', () => { let originalRequest; let originalSale; let createdSaleId; + let ctx = { + req: { + accessToken: {userId: 9}, + headers: {origin: 'http://localhost'} + } + }; + ctx.req.__ = value => { + return value; + }; afterAll(async done => { await originalRequest.updateAttributes(originalRequest); @@ -13,9 +22,9 @@ describe('ticket-request confirm()', () => { }); it(`should throw an error if the item doesn't exist`, async() => { - let ctx = {req: {accessToken: {userId: 9}}, args: {itemFk: 999}}; - let error; + ctx.args = {itemFk: 999}; + let error; try { await app.models.TicketRequest.confirm(ctx); } catch (err) { @@ -30,11 +39,12 @@ describe('ticket-request confirm()', () => { const itemId = 4; const quantity = 99999; - let ctx = {req: {accessToken: {userId: 9}}, args: { + ctx.args = { itemFk: itemId, id: requestId, quantity: quantity - }}; + }; + let error; try { @@ -52,18 +62,17 @@ describe('ticket-request confirm()', () => { const itemId = 1; const quantity = 10; + ctx.args = { + itemFk: itemId, + id: requestId, + quantity: quantity + }; + originalRequest = await app.models.TicketRequest.findById(requestId); originalSale = await app.models.Sale.findById(saleId); const request = await app.models.TicketRequest.findById(requestId); await request.updateAttributes({saleFk: saleId}); - - let ctx = {req: {accessToken: {userId: 9}}, args: { - itemFk: itemId, - id: requestId, - quantity: quantity - }}; - await app.models.TicketRequest.confirm(ctx); let updatedSale = await app.models.Sale.findById(saleId); @@ -77,16 +86,14 @@ describe('ticket-request confirm()', () => { const itemId = 1; const quantity = 10; - const request = await app.models.TicketRequest.findById(requestId); - await request.updateAttributes({saleFk: null}); - - let ctx = {req: {accessToken: {userId: 9}}, args: { + ctx.args = { itemFk: itemId, id: requestId, - quantity: quantity, - ticketFk: 1 - }}; + quantity: quantity + }; + const request = await app.models.TicketRequest.findById(requestId); + await request.updateAttributes({saleFk: null}); await app.models.TicketRequest.confirm(ctx); let updatedRequest = await app.models.TicketRequest.findById(requestId); diff --git a/print/methods/closure.js b/print/methods/closure.js index 4b25c5abc..05490609d 100644 --- a/print/methods/closure.js +++ b/print/methods/closure.js @@ -4,7 +4,7 @@ const smtp = require('../core/smtp'); const config = require('../core/config'); module.exports = app => { - app.get('/api/closure', async function(req, res) { + app.get('/api/closure', async function(request, response) { const failedtickets = []; const tickets = await db.rawSql(` SELECT @@ -59,7 +59,7 @@ module.exports = app => { }); } - res.status(200).json({ + response.status(200).json({ message: 'Closure executed successfully' }); });