This commit is contained in:
parent
c02d576bb9
commit
82ab9e5ac5
|
@ -0,0 +1,53 @@
|
||||||
|
|
||||||
|
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
||||||
|
|
||||||
|
module.exports = Self => {
|
||||||
|
Self.remoteMethod('filter', {
|
||||||
|
description: 'Find all instances of the model matched by filter from the data source.',
|
||||||
|
accessType: 'READ',
|
||||||
|
accepts: [
|
||||||
|
{
|
||||||
|
arg: 'filter',
|
||||||
|
type: 'Object',
|
||||||
|
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string',
|
||||||
|
http: {source: 'query'}
|
||||||
|
}
|
||||||
|
|
||||||
|
],
|
||||||
|
returns: {
|
||||||
|
type: ['Object'],
|
||||||
|
root: true
|
||||||
|
},
|
||||||
|
http: {
|
||||||
|
path: `/filter`,
|
||||||
|
verb: 'GET'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Self.filter = async filter => {
|
||||||
|
let conn = Self.dataSource.connector;
|
||||||
|
|
||||||
|
let stmts = [];
|
||||||
|
let stmt;
|
||||||
|
|
||||||
|
filter.order = [
|
||||||
|
'c.defaultAddressFk DESC',
|
||||||
|
'a.isActive DESC',
|
||||||
|
'a.nickname ASC'
|
||||||
|
];
|
||||||
|
|
||||||
|
stmt = new ParameterizedSQL(
|
||||||
|
`SELECT a.*
|
||||||
|
FROM vn.address a
|
||||||
|
LEFT JOIN vn.client c ON c.defaultAddressFk = a.id`
|
||||||
|
);
|
||||||
|
|
||||||
|
stmt.merge(conn.makeSuffix(filter));
|
||||||
|
let itemsIndex = stmts.push(stmt) - 1;
|
||||||
|
|
||||||
|
let sql = ParameterizedSQL.join(stmts, ';');
|
||||||
|
let result = await conn.executeStmt(sql);
|
||||||
|
return itemsIndex === 0 ? result : result[itemsIndex];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
|
@ -5,6 +5,7 @@ let isMultiple = require('vn-loopback/util/hook').isMultiple;
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
// Methods
|
// Methods
|
||||||
require('../methods/address/createDefaultAddress')(Self);
|
require('../methods/address/createDefaultAddress')(Self);
|
||||||
|
require('../methods/address/filter')(Self);
|
||||||
|
|
||||||
Self.validateAsync('isEqualizated', cannotHaveET, {
|
Self.validateAsync('isEqualizated', cannotHaveET, {
|
||||||
message: 'Cannot check Equalization Tax in this NIF/CIF'
|
message: 'Cannot check Equalization Tax in this NIF/CIF'
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
<vn-crud-model
|
<vn-crud-model
|
||||||
vn-id="model"
|
vn-id="model"
|
||||||
url="/client/api/Addresses"
|
url="/client/api/Addresses/filter"
|
||||||
filter="::$ctrl.filter"
|
limit="10"
|
||||||
link="{clientFk: $ctrl.$stateParams.id}"
|
link="{clientFk: $ctrl.$stateParams.id}"
|
||||||
data="$ctrl.addresses"
|
data="$ctrl.addresses"
|
||||||
auto-load="true">
|
auto-load="false">
|
||||||
</vn-crud-model>
|
</vn-crud-model>
|
||||||
<div compact>
|
<div compact>
|
||||||
|
<vn-table model="model">
|
||||||
<vn-card pad-large>
|
<vn-card pad-large>
|
||||||
<vn-horizontal
|
<vn-horizontal
|
||||||
ng-repeat="address in $ctrl.addresses"
|
ng-repeat="address in $ctrl.addresses"
|
||||||
|
@ -67,6 +68,7 @@
|
||||||
</vn-horizontal>
|
</vn-horizontal>
|
||||||
</vn-one>
|
</vn-one>
|
||||||
</vn-horizontal>
|
</vn-horizontal>
|
||||||
|
</vn-table>
|
||||||
</vn-card>
|
</vn-card>
|
||||||
<vn-float-button
|
<vn-float-button
|
||||||
vn-bind="+"
|
vn-bind="+"
|
||||||
|
@ -76,4 +78,5 @@
|
||||||
icon="add"
|
icon="add"
|
||||||
label="Add">
|
label="Add">
|
||||||
</vn-float-button>
|
</vn-float-button>
|
||||||
|
<vn-pagination model="model"></vn-pagination>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -5,30 +5,6 @@ class Controller {
|
||||||
this.$http = $http;
|
this.$http = $http;
|
||||||
this.$scope = $scope;
|
this.$scope = $scope;
|
||||||
this.$stateParams = $stateParams;
|
this.$stateParams = $stateParams;
|
||||||
this.filter = {
|
|
||||||
include: {
|
|
||||||
observations: 'observationType'
|
|
||||||
},
|
|
||||||
order: ['isActive DESC', 'nickname ASC']
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
get client() {
|
|
||||||
return this._client;
|
|
||||||
}
|
|
||||||
|
|
||||||
set client(value) {
|
|
||||||
this._client = value;
|
|
||||||
this.sortAddresses();
|
|
||||||
}
|
|
||||||
|
|
||||||
get addresses() {
|
|
||||||
return this._addresses;
|
|
||||||
}
|
|
||||||
|
|
||||||
set addresses(value) {
|
|
||||||
this._addresses = value;
|
|
||||||
this.sortAddresses();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setDefault(address) {
|
setDefault(address) {
|
||||||
|
@ -43,7 +19,7 @@ class Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
isDefaultAddress(address) {
|
isDefaultAddress(address) {
|
||||||
if (!this.client) return;
|
if (this.client)
|
||||||
return this.client.defaultAddressFk === address.id;
|
return this.client.defaultAddressFk === address.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +28,7 @@ class Controller {
|
||||||
*/
|
*/
|
||||||
sortAddresses() {
|
sortAddresses() {
|
||||||
if (!this.client || !this.addresses) return;
|
if (!this.client || !this.addresses) return;
|
||||||
this.$scope.model.data = this.addresses.sort((a, b) => {
|
this.addresses = this.addresses.sort((a, b) => {
|
||||||
return this.isDefaultAddress(b) - this.isDefaultAddress(a);
|
return this.isDefaultAddress(b) - this.isDefaultAddress(a);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue