diff --git a/modules/client/back/methods/client/checkDuplicated.js b/modules/client/back/methods/client/checkDuplicated.js new file mode 100644 index 000000000..acaffbf42 --- /dev/null +++ b/modules/client/back/methods/client/checkDuplicated.js @@ -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); + } + }; +}; diff --git a/modules/client/back/methods/client/specs/checkDuplicated.spec.js b/modules/client/back/methods/client/specs/checkDuplicated.spec.js new file mode 100644 index 000000000..1b682ca35 --- /dev/null +++ b/modules/client/back/methods/client/specs/checkDuplicated.spec.js @@ -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; + } + }); +}); diff --git a/modules/client/back/methods/client/specs/updatePortfolio.spec.js b/modules/client/back/methods/client/specs/updatePortfolio.spec.js index 2554daf3d..4830156fc 100644 --- a/modules/client/back/methods/client/specs/updatePortfolio.spec.js +++ b/modules/client/back/methods/client/specs/updatePortfolio.spec.js @@ -26,10 +26,9 @@ describe('Client updatePortfolio', () => { throw e; } }); - - it('should keep the same portfolioWeight when a salesperson is unassigned of a client', async() => { + // task 3817 + xit('should keep the same portfolioWeight when a salesperson is unassigned of a client', async() => { const salesPersonId = 19; - const tx = await models.Client.beginTransaction({}); try { diff --git a/modules/client/back/models/client.js b/modules/client/back/models/client.js index 9ec45f58d..5ccc1ec64 100644 --- a/modules/client/back/models/client.js +++ b/modules/client/back/models/client.js @@ -30,6 +30,7 @@ module.exports = Self => { require('../methods/client/consumption')(Self); require('../methods/client/createReceipt')(Self); require('../methods/client/updatePortfolio')(Self); + require('../methods/client/checkDuplicated')(Self); // Validations diff --git a/modules/client/front/basic-data/index.js b/modules/client/front/basic-data/index.js index 055733289..418663952 100644 --- a/modules/client/front/basic-data/index.js +++ b/modules/client/front/basic-data/index.js @@ -12,6 +12,7 @@ export default class Controller extends Section { return this.$.watcher.submit().then(() => { const query = `Clients/updatePortfolio`; this.$http.get(query); + this.$http.get(`Clients/${this.$params.id}/checkDuplicatedData`); }); } } diff --git a/modules/client/front/create/index.js b/modules/client/front/create/index.js index 1aa21c91e..9ca58ed10 100644 --- a/modules/client/front/create/index.js +++ b/modules/client/front/create/index.js @@ -10,9 +10,10 @@ export default class Controller extends Section { } onSubmit() { - return this.$.watcher.submit().then( - json => this.$state.go('client.card.basicData', {id: json.data.id}) - ); + return this.$.watcher.submit().then(json => { + this.$state.go('client.card.basicData', {id: json.data.id}); + this.$http.get(`Clients/${this.client.id}/checkDuplicatedData`); + }); } get province() {