diff --git a/modules/client/back/methods/address/filter.js b/modules/client/back/methods/address/filter.js new file mode 100644 index 000000000..f84499695 --- /dev/null +++ b/modules/client/back/methods/address/filter.js @@ -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]; + }; +}; + diff --git a/modules/client/back/models/address.js b/modules/client/back/models/address.js index d0cfd0900..f0f1efa35 100644 --- a/modules/client/back/models/address.js +++ b/modules/client/back/models/address.js @@ -5,6 +5,7 @@ let isMultiple = require('vn-loopback/util/hook').isMultiple; module.exports = Self => { // Methods require('../methods/address/createDefaultAddress')(Self); + require('../methods/address/filter')(Self); Self.validateAsync('isEqualizated', cannotHaveET, { message: 'Cannot check Equalization Tax in this NIF/CIF' diff --git a/modules/client/front/address/index/index.html b/modules/client/front/address/index/index.html index d8971cd35..cdfd4d5a7 100644 --- a/modules/client/front/address/index/index.html +++ b/modules/client/front/address/index/index.html @@ -1,72 +1,74 @@ + auto-load="false">
- - - - - - - - - - - - - - - -
{{::address.nickname}}
-
{{::address.street}}
-
{{::address.city}}, {{::address.province}}
-
{{::address.phone}}, {{::address.mobile}}
-
- - - - -
-
- - - {{::observation.observationType.description}}: - {{::observation.description}} + + + + + + + + + + + + + + + + +
{{::address.nickname}}
+
{{::address.street}}
+
{{::address.city}}, {{::address.province}}
+
{{::address.phone}}, {{::address.mobile}}
+
+ + + + +
-
- - - -
-
-
+ + + {{::observation.observationType.description}}: + {{::observation.description}} + + + + + + + + +
+
diff --git a/modules/client/front/address/index/index.js b/modules/client/front/address/index/index.js index eeabaf5d8..3bc77ec62 100644 --- a/modules/client/front/address/index/index.js +++ b/modules/client/front/address/index/index.js @@ -5,30 +5,6 @@ class Controller { this.$http = $http; this.$scope = $scope; 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) { @@ -43,8 +19,8 @@ class Controller { } isDefaultAddress(address) { - if (!this.client) return; - return this.client.defaultAddressFk === address.id; + if (this.client) + return this.client.defaultAddressFk === address.id; } /** @@ -52,7 +28,7 @@ class Controller { */ sortAddresses() { 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); }); }