Merge pull request '3686-client_create' (#910) from 3686-client_create into dev
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
Reviewed-on: #910 Reviewed-by: Carlos Jimenez Ruiz <carlosjr@verdnatura.es>
This commit is contained in:
commit
4a59ef2bd5
|
@ -0,0 +1,64 @@
|
||||||
|
module.exports = Self => {
|
||||||
|
Self.remoteMethod('checkDuplicatedData', {
|
||||||
|
description: 'Checks if a client has same email, mobile or phone than other client and send an email',
|
||||||
|
accepts: [{
|
||||||
|
arg: 'id',
|
||||||
|
type: 'number',
|
||||||
|
required: true,
|
||||||
|
description: 'The client id'
|
||||||
|
}],
|
||||||
|
returns: {
|
||||||
|
type: 'object',
|
||||||
|
root: true
|
||||||
|
},
|
||||||
|
http: {
|
||||||
|
verb: 'GET',
|
||||||
|
path: '/:id/checkDuplicatedData'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Self.checkDuplicatedData = async function(id, options) {
|
||||||
|
const myOptions = {};
|
||||||
|
|
||||||
|
if (typeof options == 'object')
|
||||||
|
Object.assign(myOptions, options);
|
||||||
|
|
||||||
|
const client = await Self.app.models.Client.findById(id, myOptions);
|
||||||
|
|
||||||
|
const emails = client.email ? client.email.split(',') : null;
|
||||||
|
|
||||||
|
const findParams = [];
|
||||||
|
if (emails.length) {
|
||||||
|
for (let email of emails)
|
||||||
|
findParams.push({email: email});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (client.phone)
|
||||||
|
findParams.push({phone: client.phone});
|
||||||
|
|
||||||
|
if (client.mobile)
|
||||||
|
findParams.push({mobile: client.mobile});
|
||||||
|
|
||||||
|
const filterObj = {
|
||||||
|
where: {
|
||||||
|
and: [
|
||||||
|
{or: findParams},
|
||||||
|
{id: {neq: client.id}}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const clientSameData = await Self.findOne(filterObj, myOptions);
|
||||||
|
|
||||||
|
if (clientSameData) {
|
||||||
|
await Self.app.models.Mail.create({
|
||||||
|
receiver: 'direccioncomercial@verdnatura.es',
|
||||||
|
subject: `Cliente con email/teléfono/móvil duplicados`,
|
||||||
|
body: 'El cliente ' + client.id + ' comparte alguno de estos datos con el cliente ' + clientSameData.id +
|
||||||
|
'\n- Email: ' + client.email +
|
||||||
|
'\n- Teléfono: ' + client.phone +
|
||||||
|
'\n- Móvil: ' + client.mobile
|
||||||
|
}, myOptions);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
|
@ -0,0 +1,24 @@
|
||||||
|
const models = require('vn-loopback/server/server').models;
|
||||||
|
|
||||||
|
describe('client checkDuplicated()', () => {
|
||||||
|
it('should send an mail if mobile/phone/email is duplicated', async() => {
|
||||||
|
const tx = await models.Client.beginTransaction({});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const options = {transaction: tx};
|
||||||
|
|
||||||
|
const id = 1110;
|
||||||
|
const mailModel = models.Mail;
|
||||||
|
spyOn(mailModel, 'create');
|
||||||
|
|
||||||
|
await models.Client.checkDuplicatedData(id, options);
|
||||||
|
|
||||||
|
expect(mailModel.create).toHaveBeenCalled();
|
||||||
|
|
||||||
|
await tx.rollback();
|
||||||
|
} catch (e) {
|
||||||
|
await tx.rollback();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
|
@ -26,10 +26,9 @@ describe('Client updatePortfolio', () => {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// task 3817
|
||||||
it('should keep the same portfolioWeight when a salesperson is unassigned of a client', async() => {
|
xit('should keep the same portfolioWeight when a salesperson is unassigned of a client', async() => {
|
||||||
const salesPersonId = 19;
|
const salesPersonId = 19;
|
||||||
|
|
||||||
const tx = await models.Client.beginTransaction({});
|
const tx = await models.Client.beginTransaction({});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -30,6 +30,7 @@ module.exports = Self => {
|
||||||
require('../methods/client/consumption')(Self);
|
require('../methods/client/consumption')(Self);
|
||||||
require('../methods/client/createReceipt')(Self);
|
require('../methods/client/createReceipt')(Self);
|
||||||
require('../methods/client/updatePortfolio')(Self);
|
require('../methods/client/updatePortfolio')(Self);
|
||||||
|
require('../methods/client/checkDuplicated')(Self);
|
||||||
|
|
||||||
// Validations
|
// Validations
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ export default class Controller extends Section {
|
||||||
return this.$.watcher.submit().then(() => {
|
return this.$.watcher.submit().then(() => {
|
||||||
const query = `Clients/updatePortfolio`;
|
const query = `Clients/updatePortfolio`;
|
||||||
this.$http.get(query);
|
this.$http.get(query);
|
||||||
|
this.$http.get(`Clients/${this.$params.id}/checkDuplicatedData`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,10 @@ export default class Controller extends Section {
|
||||||
}
|
}
|
||||||
|
|
||||||
onSubmit() {
|
onSubmit() {
|
||||||
return this.$.watcher.submit().then(
|
return this.$.watcher.submit().then(json => {
|
||||||
json => this.$state.go('client.card.basicData', {id: json.data.id})
|
this.$state.go('client.card.basicData', {id: json.data.id});
|
||||||
);
|
this.$http.get(`Clients/${this.client.id}/checkDuplicatedData`);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
get province() {
|
get province() {
|
||||||
|
|
Loading…
Reference in New Issue