Merge pull request '3686-client_create' (#910) from 3686-client_create into dev
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:
Carlos Jimenez Ruiz 2022-04-07 08:28:50 +00:00
commit 4a59ef2bd5
6 changed files with 96 additions and 6 deletions

View File

@ -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);
}
};
};

View File

@ -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;
}
});
});

View File

@ -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 {

View File

@ -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

View File

@ -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`);
});
}
}

View File

@ -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() {