2878 - Splitted method into activeWithRole and activeWithInheritedRole #600

Merged
carlosjr merged 4 commits from 2878-activeWorkers_refactor into dev 2021-04-14 08:17:26 +00:00
31 changed files with 161 additions and 94 deletions

View File

@ -31,7 +31,7 @@ describe('Client create path', () => {
await page.write(selectors.createClientView.taxNumber, '74451390E'); await page.write(selectors.createClientView.taxNumber, '74451390E');
await page.write(selectors.createClientView.userName, 'CaptainMarvel'); await page.write(selectors.createClientView.userName, 'CaptainMarvel');
await page.write(selectors.createClientView.email, 'CarolDanvers@verdnatura.es'); await page.write(selectors.createClientView.email, 'CarolDanvers@verdnatura.es');
await page.autocompleteSearch(selectors.createClientView.salesPerson, 'replenisher'); await page.autocompleteSearch(selectors.createClientView.salesPerson, 'salesPerson');
await page.waitToClick(selectors.createClientView.createButton); await page.waitToClick(selectors.createClientView.createButton);
const message = await page.waitForSnackbar(); const message = await page.waitForSnackbar();

View File

@ -96,7 +96,7 @@ describe('Client Edit basicData path', () => {
await page.write(selectors.clientBasicData.phone, '333333333'); await page.write(selectors.clientBasicData.phone, '333333333');
await page.clearInput(selectors.clientBasicData.mobile); await page.clearInput(selectors.clientBasicData.mobile);
await page.write(selectors.clientBasicData.mobile, '444444444'); await page.write(selectors.clientBasicData.mobile, '444444444');
await page.autocompleteSearch(selectors.clientBasicData.salesPerson, 'replenisherNick'); await page.autocompleteSearch(selectors.clientBasicData.salesPerson, 'salesPerson');
await page.autocompleteSearch(selectors.clientBasicData.channel, 'Metropolis newspaper'); await page.autocompleteSearch(selectors.clientBasicData.channel, 'Metropolis newspaper');
await page.waitToClick(selectors.clientBasicData.saveButton); await page.waitToClick(selectors.clientBasicData.saveButton);
const message = await page.waitForSnackbar(); const message = await page.waitForSnackbar();
@ -143,7 +143,7 @@ describe('Client Edit basicData path', () => {
const result = await page const result = await page
.waitToGetProperty(selectors.clientBasicData.salesPerson, 'value'); .waitToGetProperty(selectors.clientBasicData.salesPerson, 'value');
expect(result).toEqual('replenisherNick'); expect(result).toEqual('salesPersonNick');
}); });
it('should now confirm the channel have been selected', async() => { it('should now confirm the channel have been selected', async() => {

View File

@ -28,10 +28,10 @@
<vn-autocomplete <vn-autocomplete
disabled="false" disabled="false"
ng-model="$ctrl.claim.workerFk" ng-model="$ctrl.claim.workerFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithRole"
show-field="nickname" show-field="nickname"
search-function="{firstName: $search}" search-function="{firstName: $search}"
where="{role: 'employee'}" where="{role: 'salesPerson'}"
label="Attended by"> label="Attended by">
</vn-autocomplete> </vn-autocomplete>
<vn-autocomplete <vn-autocomplete

View File

@ -68,7 +68,7 @@
</vn-autocomplete> </vn-autocomplete>
<vn-autocomplete <vn-autocomplete
ng-model="claimDevelopment.workerFk" ng-model="claimDevelopment.workerFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithInheritedRole"
show-field="nickname" show-field="nickname"
search-function="{firstName: $search}" search-function="{firstName: $search}"
value-field="id" value-field="id"

View File

@ -25,20 +25,20 @@
<vn-autocomplete <vn-autocomplete
vn-one vn-one
ng-model="filter.salesPersonFk" ng-model="filter.salesPersonFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithRole"
search-function="{firstName: $search}" search-function="{firstName: $search}"
value-field="id" value-field="id"
where="{role: 'employee'}" where="{role: 'salesPerson'}"
label="Salesperson"> label="Salesperson">
<tpl-item>{{firstName}} {{name}}</tpl-item> <tpl-item>{{firstName}} {{name}}</tpl-item>
</vn-autocomplete> </vn-autocomplete>
<vn-autocomplete <vn-autocomplete
vn-one vn-one
ng-model="filter.attenderFk" ng-model="filter.attenderFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithRole"
search-function="{firstName: $search}" search-function="{firstName: $search}"
value-field="id" value-field="id"
where="{role: 'employee'}" where="{role: 'salesPerson'}"
label="Attended by"> label="Attended by">
<tpl-item>{{firstName}} {{name}}</tpl-item> <tpl-item>{{firstName}} {{name}}</tpl-item>
</vn-autocomplete> </vn-autocomplete>

View File

@ -1,23 +0,0 @@
const app = require('vn-loopback/server/server');
describe('Client activeWorkersWithRole', () => {
it('should return the sales people as result', async() => {
let filter = {where: {role: 'salesPerson'}};
let result = await app.models.Client.activeWorkersWithRole(filter);
let isSalesPerson = await app.models.Account.hasRole(result[0].id, 'salesPerson');
expect(result.length).toEqual(19);
expect(isSalesPerson).toBeTruthy();
});
it('should return the buyers as result', async() => {
let filter = {where: {role: 'buyer'}};
let result = await app.models.Client.activeWorkersWithRole(filter);
let isBuyer = await app.models.Account.hasRole(result[0].id, 'buyer');
expect(result.length).toEqual(17);
expect(isBuyer).toBeTruthy();
});
});

View File

@ -8,7 +8,6 @@ const LoopBackContext = require('loopback-context');
module.exports = Self => { module.exports = Self => {
// Methods // Methods
require('../methods/client/activeWorkersWithRole')(Self);
require('../methods/client/getCard')(Self); require('../methods/client/getCard')(Self);
require('../methods/client/createWithUser')(Self); require('../methods/client/createWithUser')(Self);
require('../methods/client/listWorkers')(Self); require('../methods/client/listWorkers')(Self);

View File

@ -54,11 +54,11 @@
<vn-autocomplete <vn-autocomplete
vn-one vn-one
ng-model="$ctrl.client.salesPersonFk" ng-model="$ctrl.client.salesPersonFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithInheritedRole"
show-field="nickname" show-field="nickname"
search-function="{firstName: $search}" search-function="{firstName: $search}"
value-field="id" value-field="id"
where="{role: 'employee'}" where="{role: 'salesPerson'}"
label="Salesperson" label="Salesperson"
vn-acl="salesAssistant"> vn-acl="salesAssistant">
</vn-autocomplete> </vn-autocomplete>

View File

@ -17,10 +17,10 @@
<vn-autocomplete <vn-autocomplete
vn-one vn-one
ng-model="filter.buyerId" ng-model="filter.buyerId"
url="Clients/activeWorkersWithRole" url="Workers/activeWithRole"
search-function="{firstName: $search}" search-function="{firstName: $search}"
value-field="id" value-field="id"
where="{role: 'employee'}" where="{role: 'buyer'}"
label="Buyer"> label="Buyer">
<tpl-item>{{nickname}}</tpl-item> <tpl-item>{{nickname}}</tpl-item>
</vn-autocomplete> </vn-autocomplete>

View File

@ -18,10 +18,10 @@
<vn-autocomplete <vn-autocomplete
label="Salesperson" label="Salesperson"
ng-model="$ctrl.client.salesPersonFk" ng-model="$ctrl.client.salesPersonFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithInheritedRole"
search-function="{firstName: $search}" search-function="{firstName: $search}"
show-field="firstName" show-field="firstName"
where="{role: 'employee'}"> where="{role: 'salesPerson'}">
<tpl-item>{{firstName}} {{lastName}}</tpl-item> <tpl-item>{{firstName}} {{lastName}}</tpl-item>
</vn-autocomplete> </vn-autocomplete>
</vn-horizontal> </vn-horizontal>

View File

@ -17,7 +17,7 @@
<vn-autocomplete <vn-autocomplete
vn-one vn-one
ng-model="filter.salesPersonFk" ng-model="filter.salesPersonFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithInheritedRole"
search-function="{firstName: $search}" search-function="{firstName: $search}"
show-field="firstName" show-field="firstName"
value-field="id" value-field="id"

View File

@ -39,11 +39,11 @@
<vn-autocomplete <vn-autocomplete
disabled="false" disabled="false"
ng-model="filter.salesPersonFk" ng-model="filter.salesPersonFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithRole"
show-field="nickname" show-field="nickname"
search-function="{firstName: $search}" search-function="{firstName: $search}"
value-field="id" value-field="id"
where="{role: 'employee'}" where="{role: 'buyer'}"
label="Buyer"> label="Buyer">
</vn-autocomplete> </vn-autocomplete>
</vn-horizontal> </vn-horizontal>

View File

@ -44,7 +44,7 @@
<vn-autocomplete <vn-autocomplete
vn-one vn-one
ng-model="filter.buyerFk" ng-model="filter.buyerFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithRolee"
show-field="nickname" show-field="nickname"
search-function="{firstName: $search}" search-function="{firstName: $search}"
value-field="id" value-field="id"

View File

@ -17,10 +17,10 @@
<vn-autocomplete <vn-autocomplete
vn-one vn-one
ng-model="filter.attenderFk" ng-model="filter.attenderFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithRole"
search-function="{firstName: $search}" search-function="{firstName: $search}"
value-field="id" value-field="id"
where="{role: 'employee'}" where="{role: 'buyer'}"
label="Atender"> label="Atender">
<tpl-item>{{nickname}}</tpl-item> <tpl-item>{{nickname}}</tpl-item>
</vn-autocomplete> </vn-autocomplete>

View File

@ -42,11 +42,11 @@
vn-one vn-one
disabled="false" disabled="false"
ng-model="filter.salesPersonFk" ng-model="filter.salesPersonFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithRole"
show-field="nickname" show-field="nickname"
search-function="{firstName: $search}" search-function="{firstName: $search}"
value-field="id" value-field="id"
where="{role: 'employee'}" where="{role: 'buyer'}"
label="Buyer"> label="Buyer">
</vn-autocomplete> </vn-autocomplete>
</vn-horizontal> </vn-horizontal>

View File

@ -28,11 +28,11 @@
<vn-autocomplete <vn-autocomplete
vn-one vn-one
ng-model="filter.workerFk" ng-model="filter.workerFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithInheritedRole"
search-function="{firstName: $search}" search-function="{firstName: $search}"
show-field="nickname" show-field="nickname"
value-field="id" value-field="id"
where="{role: 'employee'}" where="{role: 'salesPerson'}"
label="Sales person"> label="Sales person">
</vn-autocomplete> </vn-autocomplete>
</vn-horizontal> </vn-horizontal>

View File

@ -11,7 +11,7 @@
<vn-autocomplete <vn-autocomplete
vn-one vn-one
ng-model="$ctrl.route.workerFk" ng-model="$ctrl.route.workerFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithInheritedRole"
show-field="nickname" show-field="nickname"
search-function="{firstName: $search}" search-function="{firstName: $search}"
value-field="id" value-field="id"

View File

@ -11,7 +11,7 @@
<vn-autocomplete <vn-autocomplete
label="Worker" label="Worker"
ng-model="$ctrl.route.workerFk" ng-model="$ctrl.route.workerFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithInheritedRole"
show-field="nickname" show-field="nickname"
search-function="{firstName: $search}" search-function="{firstName: $search}"
where="{role: 'employee'}"> where="{role: 'employee'}">

View File

@ -13,7 +13,7 @@
<vn-autocomplete <vn-autocomplete
vn-one vn-one
ng-model="filter.workerFk" ng-model="filter.workerFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithInheritedRole"
show-field="nickname" show-field="nickname"
search-function="{firstName: $search}" search-function="{firstName: $search}"
value-field="id" value-field="id"

View File

@ -18,7 +18,7 @@
<vn-autocomplete <vn-autocomplete
vn-one vn-one
ng-model="$ctrl.supplier.workerFk" ng-model="$ctrl.supplier.workerFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithInheritedRole"
search-function="{firstName: $search}" search-function="{firstName: $search}"
show-field="nickname" show-field="nickname"
value-field="id" value-field="id"

View File

@ -17,10 +17,10 @@
<vn-autocomplete <vn-autocomplete
vn-one vn-one
ng-model="filter.buyerId" ng-model="filter.buyerId"
url="Clients/activeWorkersWithRole" url="Workers/activeWithRole"
search-function="{firstName: $search}" search-function="{firstName: $search}"
value-field="id" value-field="id"
where="{role: 'employee'}" where="{role: 'buyer'}"
label="Buyer"> label="Buyer">
<tpl-item>{{nickname}}</tpl-item> <tpl-item>{{nickname}}</tpl-item>
</vn-autocomplete> </vn-autocomplete>

View File

@ -18,7 +18,7 @@
<vn-autocomplete <vn-autocomplete
label="Buyer" label="Buyer"
ng-model="$ctrl.ticketRequest.attenderFk" ng-model="$ctrl.ticketRequest.attenderFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithRole"
show-field="nickname" show-field="nickname"
where="{role: 'buyer'}" where="{role: 'buyer'}"
search-function="{firstName: $search}"> search-function="{firstName: $search}">

View File

@ -60,7 +60,7 @@
<vn-autocomplete <vn-autocomplete
vn-one vn-one
ng-model="filter.salesPersonFk" ng-model="filter.salesPersonFk"
url="Clients/activeWorkersWithRole" url="Workers/activeWithInheritedRole"
search-function="{firstName: $search}" search-function="{firstName: $search}"
value-field="id" value-field="id"
where="{role: 'employee'}" where="{role: 'employee'}"

View File

@ -21,7 +21,7 @@
</vn-autocomplete> </vn-autocomplete>
<vn-autocomplete <vn-autocomplete
vn-one vn-one
url="Clients/activeWorkersWithRole" url="Workers/activeWithInheritedRole"
ng-if="$ctrl.isPickerDesignedState" ng-if="$ctrl.isPickerDesignedState"
ng-model="$ctrl.workerFk" ng-model="$ctrl.workerFk"
show-field="nickname" show-field="nickname"

View File

@ -3,26 +3,7 @@ const buildFilter = require('vn-loopback/util/filter').buildFilter;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters; const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('activeWorkersWithRole', { Self.activeWorkers = async(query, filter) => {
description: 'Returns actives workers with salesperson role',
accessType: 'READ',
accepts: [{
arg: 'filter',
type: 'Object',
description: 'Filter defining where and paginated data',
required: true
}],
returns: {
type: 'Worker',
root: true
},
http: {
path: `/activeWorkersWithRole`,
verb: 'get'
}
});
Self.activeWorkersWithRole = async filter => {
let conn = Self.dataSource.connector; let conn = Self.dataSource.connector;
if (filter.where && filter.where.and && Array.isArray(filter.where.and)) { if (filter.where && filter.where.and && Array.isArray(filter.where.and)) {
let where = {}; let where = {};
@ -54,14 +35,8 @@ module.exports = Self => {
myFilter = mergeFilters(myFilter, clientFilter); myFilter = mergeFilters(myFilter, clientFilter);
let stmt = new ParameterizedSQL( let stmt = new ParameterizedSQL(query);
`SELECT DISTINCT w.id, w.firstName, w.lastName, u.name, u.nickname
FROM worker w
JOIN account.user u ON u.id = w.userFk
JOIN account.roleRole i ON i.role = u.role
JOIN account.role r ON r.id = i.inheritsFrom`
);
stmt.merge(conn.makeSuffix(myFilter)); stmt.merge(conn.makeSuffix(myFilter));
return await conn.executeStmt(stmt); return conn.executeStmt(stmt);
}; };
}; };

View File

@ -0,0 +1,32 @@
module.exports = Self => {
Self.remoteMethod('activeWithInheritedRole', {
description: 'Returns active workers with a role',
accessType: 'READ',
accepts: [{
arg: 'filter',
type: 'Object',
description: 'Filter defining where and paginated data',
required: true
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/activeWithInheritedRole`,
verb: 'GET'
}
});
Self.activeWithInheritedRole = async filter => {
const query =
`SELECT DISTINCT w.id, w.firstName, w.lastName, u.name, u.nickname
FROM worker w
JOIN account.user u ON u.id = w.userFk
JOIN account.roleRole i ON i.role = u.role
JOIN account.role r ON r.id = i.inheritsFrom`;
return Self.activeWorkers(query, filter);
};
};

View File

@ -0,0 +1,31 @@
module.exports = Self => {
Self.remoteMethod('activeWithRole', {
description: 'Returns active workers with an inherited role',
accessType: 'READ',
accepts: [{
arg: 'filter',
type: 'Object',
description: 'Filter defining where and paginated data',
required: true
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/activeWithRole`,
verb: 'GET'
}
});
Self.activeWithRole = async filter => {
const query =
`SELECT DISTINCT w.id, w.firstName, w.lastName, u.name, u.nickname
FROM worker w
JOIN account.user u ON u.id = w.id
JOIN account.role r ON r.id = u.role`;
return Self.activeWorkers(query, filter);
};
};

View File

@ -0,0 +1,29 @@
const app = require('vn-loopback/server/server');
describe('Worker activeWithInheritedRole', () => {
it('should return the workers with an inherited role of salesPerson', async() => {
const filter = {where: {role: 'salesPerson'}};
const result = await app.models.Worker.activeWithInheritedRole(filter);
const randomIndex = Math.floor(Math.random() * result.length);
const worker = result[randomIndex];
const isSalesPerson = await app.models.Account.hasRole(worker.id, 'salesPerson');
expect(result.length).toEqual(19);
expect(isSalesPerson).toBe(true);
});
it('should return the workers with an inherited role of buyer', async() => {
const filter = {where: {role: 'buyer'}};
const result = await app.models.Worker.activeWithInheritedRole(filter);
const randomIndex = Math.floor(Math.random() * result.length);
const worker = result[randomIndex];
const isBuyer = await app.models.Account.hasRole(worker.id, 'buyer');
expect(result.length).toEqual(17);
expect(isBuyer).toBe(true);
});
});

View File

@ -0,0 +1,21 @@
const app = require('vn-loopback/server/server');
describe('Worker activeWithRole', () => {
it('should return the sales people as result', async() => {
const filter = {where: {role: 'salesPerson'}};
const result = await app.models.Worker.activeWithRole(filter);
const firstWorker = result[0];
expect(result.length).toEqual(1);
expect(firstWorker.nickname).toEqual('salesPersonNick');
});
it('should return the buyers as result', async() => {
const filter = {where: {role: 'buyer'}};
const result = await app.models.Worker.activeWithRole(filter);
const firstWorker = result[0];
expect(result.length).toEqual(1);
expect(firstWorker.nickname).toEqual('buyerNick');
});
});

View File

@ -7,4 +7,7 @@ module.exports = Self => {
require('../methods/worker/createAbsence')(Self); require('../methods/worker/createAbsence')(Self);
require('../methods/worker/deleteAbsence')(Self); require('../methods/worker/deleteAbsence')(Self);
require('../methods/worker/updateAbsence')(Self); require('../methods/worker/updateAbsence')(Self);
require('../methods/worker/active')(Self);
require('../methods/worker/activeWithRole')(Self);
require('../methods/worker/activeWithInheritedRole')(Self);
}; };

View File

@ -30,13 +30,13 @@
rule> rule>
</vn-textfield> </vn-textfield>
<vn-autocomplete <vn-autocomplete
disabled="false" disabled="false"
ng-model="$ctrl.worker.bossFk" ng-model="$ctrl.worker.bossFk"
url="Clients/activeWorkersWithRole" url="Clients/activeWorkersWithInheritRole"
show-field="nickname" show-field="nickname"
search-function="{firstName: $search}" search-function="{firstName: $search}"
where="{role: 'employee'}" where="{role: 'employee'}"
label="Boss"> label="Boss">
</vn-autocomplete> </vn-autocomplete>
</vn-horizontal> </vn-horizontal>
</vn-vertical> </vn-vertical>