refactor & updated tests
gitea/salix/2018-deprecate_message_send This commit looks good
Details
gitea/salix/2018-deprecate_message_send This commit looks good
Details
This commit is contained in:
parent
de95127199
commit
57acca2f5e
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
};
|
|
@ -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);
|
||||
});
|
||||
});
|
|
@ -23,12 +23,6 @@
|
|||
"Delivery": {
|
||||
"dataSource": "vn"
|
||||
},
|
||||
"Message": {
|
||||
"dataSource": "vn"
|
||||
},
|
||||
"MessageInbox": {
|
||||
"dataSource": "vn"
|
||||
},
|
||||
"Province": {
|
||||
"dataSource": "vn"
|
||||
},
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
module.exports = Self => {
|
||||
require('../methods/message/send')(Self);
|
||||
};
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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}}})"
|
||||
}
|
|
@ -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}}})"
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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, {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
};
|
||||
|
|
|
@ -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"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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'
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue