From 96d755cd2120f3bb855acfc3958e69cb14ae053e Mon Sep 17 00:00:00 2001 From: Juan Date: Fri, 6 Jul 2018 16:32:23 +0200 Subject: [PATCH] Code linting, code clean --- .../src/components/rest-model/crud-model.js | 17 ++++-- .../src/components/searchbar/searchbar.js | 36 ++++++++++--- client/item/src/index/index.html | 2 +- client/item/src/index/index.js | 6 ++- client/item/src/index/product.js | 6 +-- client/item/src/search-panel/index.html | 2 - client/ticket/src/index/index.html | 20 +++---- .../client-module/04_edit_pay_method.spec.js | 2 +- .../06_add_address_notes.spec.js | 2 +- .../common/methods/client-contact/crud.js | 32 ------------ .../methods/client-contact/specs/crud.spec.js | 52 ------------------- .../client/common/models/client-contact.js | 2 - .../loopback/common/methods/item/filter.js | 32 ++++++++++++ services/loopback/common/models/item.js | 1 + 14 files changed, 94 insertions(+), 118 deletions(-) delete mode 100644 services/client/common/methods/client-contact/crud.js delete mode 100644 services/client/common/methods/client-contact/specs/crud.spec.js create mode 100644 services/loopback/common/methods/item/filter.js diff --git a/client/core/src/components/rest-model/crud-model.js b/client/core/src/components/rest-model/crud-model.js index ab2c93956..614d15aee 100644 --- a/client/core/src/components/rest-model/crud-model.js +++ b/client/core/src/components/rest-model/crud-model.js @@ -19,7 +19,7 @@ export default class CrudModel extends ModelProxy { this.refresh(); } - refresh(usFilter, usData) { + refresh(usFilter) { if (!this.url) return; let myFilter = { @@ -47,9 +47,16 @@ export default class CrudModel extends ModelProxy { sendRequest(filter, append) { this.cancelRequest(); this.canceler = this.$q.defer(); - let options = {timeout: this.canceler.promise}; - let json = encodeURIComponent(JSON.stringify(filter)); - return this.$http.get(`${this.url}?filter=${json}`, options).then( + + let options = { + timeout: this.canceler.promise, + params: {filter: filter} + }; + + if (this.userParams instanceof Object) + Object.assign(options.params, this.userParams); + + return this.$http.get(this.url, options).then( json => this.onRemoteDone(json, filter, append), json => this.onRemoteError(json) ); @@ -118,7 +125,7 @@ export default class CrudModel extends ModelProxy { return changes; } - save(ignoreChanges) { + save() { let changes = this.getChanges(); if (!changes) diff --git a/client/core/src/components/searchbar/searchbar.js b/client/core/src/components/searchbar/searchbar.js index 8db621933..ff4af190d 100644 --- a/client/core/src/components/searchbar/searchbar.js +++ b/client/core/src/components/searchbar/searchbar.js @@ -105,14 +105,34 @@ export default class Controller extends Component { } pushFilterToState(filter) { - let history = window.history || {pushState: () => { - console.error('Error in history.pushState(): Browser incompatibility error'); - }}; - let aux = window.location.hash.split('?q='); - if (Object.keys(filter).length) - history.pushState({}, null, `${aux[0]}?q=${encodeURIComponent(JSON.stringify(filter))}`); - else - history.pushState({}, null, aux[0]); + let state = window.location.hash.split('?')[0]; + let keys = Object.keys(filter); + + if (keys.length) { + let hashFilter = {}; + + keys.forEach(key => { + let value = filter[key]; + + if (value instanceof Date) + hashFilter[key] = value; + else + switch (typeof value) { + case 'number': + case 'string': + case 'boolean': + hashFilter[key] = value; + } + }); + + let search = encodeURIComponent(JSON.stringify(hashFilter)); + state += `?q=${search}`; + } + + if (!window.history) + throw new Error('Browser incompatibility: window.history not found'); + + window.history.pushState({}, null, state); } /** diff --git a/client/item/src/index/index.html b/client/item/src/index/index.html index a0b0f5101..d998416a4 100644 --- a/client/item/src/index/index.html +++ b/client/item/src/index/index.html @@ -1,6 +1,6 @@ - diff --git a/client/ticket/src/index/index.html b/client/ticket/src/index/index.html index e04c85854..08311d625 100644 --- a/client/ticket/src/index/index.html +++ b/client/ticket/src/index/index.html @@ -39,16 +39,16 @@ class="{{::$ctrl.compareDate(ticket.shipped)}} clickable" ui-sref="ticket.card.summary({id: {{::ticket.id}}})"> {{::ticket.id}} - {{::ticket.client.salesPerson.name | dashIfEmpty}} - {{::ticket.shipped | date:'dd/MM/yyyy'}} - {{::ticket.shipped | date:'HH:MM'}} - {{::ticket.nickname}} - {{::ticket.address.province.name}} - {{::ticket.tracking.state.name}} - {{::ticket.agencyMode.name}} - {{::ticket.warehouse.name}} - {{::ticket.refFk | dashIfEmpty}} - {{::ticket.routeFk | dashIfEmpty}} + {{::ticket.client.salesPerson.name | dashIfEmpty}} + {{::ticket.shipped | date:'dd/MM/yyyy'}} + {{::ticket.shipped | date:'HH:MM'}} + {{::ticket.nickname}} + {{::ticket.address.province.name}} + {{::ticket.tracking.state.name}} + {{::ticket.agencyMode.name}} + {{::ticket.warehouse.name}} + {{::ticket.refFk | dashIfEmpty}} + {{::ticket.routeFk | dashIfEmpty}} { .waitToClick(selectors.clientPayMethod.saveButton) .waitForSnackbar() .then(result => { - expect(result).toEqual(jasmine.arrayContaining(['requires an IBAN'])); + expect(result).toEqual(jasmine.arrayContaining(['That payment method requires an IBAN'])); }); }); diff --git a/e2e/paths/client-module/06_add_address_notes.spec.js b/e2e/paths/client-module/06_add_address_notes.spec.js index e8f0df0d4..ac8bca29f 100644 --- a/e2e/paths/client-module/06_add_address_notes.spec.js +++ b/e2e/paths/client-module/06_add_address_notes.spec.js @@ -65,7 +65,7 @@ describe('Client', () => { .waitToClick(selectors.clientAddresses.saveButton) .waitForSnackbar() .then(result => { - expect(result).toEqual(jasmine.arrayContaining(['type cannot be blank'])); + expect(result).toEqual(jasmine.arrayContaining(['Observation type cannot be blank'])); }); }); diff --git a/services/client/common/methods/client-contact/crud.js b/services/client/common/methods/client-contact/crud.js deleted file mode 100644 index d3a1075d5..000000000 --- a/services/client/common/methods/client-contact/crud.js +++ /dev/null @@ -1,32 +0,0 @@ -module.exports = Self => { - Self.remoteMethod('crud', { - description: 'Client contact crud', - accepts: [{ - arg: 'data', - type: 'object', - http: {source: 'body'} - }], - returns: { - root: true, - type: 'boolean' - }, - http: { - verb: 'post', - path: '/crud' - } - }); - - Self.crud = async data => { - let models = Self.app.models; - - await Promise.all(data.delete.map(contactId => { - return models.ClientContact.destroyById(contactId); - })); - - let upsert = data.update.concat(data.create); - - await Promise.all(upsert.map(contact => { - return models.ClientContact.upsert(contact); - })); - }; -}; diff --git a/services/client/common/methods/client-contact/specs/crud.spec.js b/services/client/common/methods/client-contact/specs/crud.spec.js deleted file mode 100644 index f58752b3a..000000000 --- a/services/client/common/methods/client-contact/specs/crud.spec.js +++ /dev/null @@ -1,52 +0,0 @@ -const app = require(`${servicesDir}/client/server/server`); - -describe('Client crud', () => { - afterAll(async() => { - await app.models.ClientContact.destroyById(4113); - }); - - it('should perfom a query to create new contacts', async() => { - let data = { - delete: [], - create: [ - {id: 4113, clientFk: 101, name: 'My contact', phone: '111111111'} - ], - update: [] - }; - - await app.models.ClientContact.crud(data); - let contacts = await app.models.ClientContact.find(); - - expect(contacts.length).toEqual(5); - }); - - it('should perfom a query to update contacts', async() => { - let data = { - delete: [], - create: [], - update: [ - {id: 4113, name: 'My contact 2 updated', phone: '222222222'} - ] - }; - - await app.models.ClientContact.crud(data); - let contacts = await app.models.ClientContact.findById(4113); - - expect(contacts.name).toEqual('My contact 2 updated'); - expect(contacts.phone).toEqual('222222222'); - }); - - it('should perfom a query to delete contacts', async() => { - let data = { - delete: [4113], - create: [], - update: [] - }; - - await app.models.ClientContact.crud(data); - - let contacts = await app.models.ClientContact.find(); - - expect(contacts.length).toEqual(4); - }); -}); diff --git a/services/client/common/models/client-contact.js b/services/client/common/models/client-contact.js index c731ffc15..db9d72ef6 100644 --- a/services/client/common/models/client-contact.js +++ b/services/client/common/models/client-contact.js @@ -1,6 +1,4 @@ module.exports = Self => { - require('../methods/client-contact/crud')(Self); - Self.validatesPresenceOf('name', { message: 'Name cannot be blank' }); diff --git a/services/loopback/common/methods/item/filter.js b/services/loopback/common/methods/item/filter.js new file mode 100644 index 000000000..1decb9c10 --- /dev/null +++ b/services/loopback/common/methods/item/filter.js @@ -0,0 +1,32 @@ +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 fields, where, include, order, offset, and limit - must be a JSON-encoded string ({"something":"value"})', + http: {source: 'query'} + }, { + arg: 'tags', + type: ['Object'], + description: 'List of tags to filter with', + http: {source: 'query'} + } + ], + returns: { + type: ['Object'], + root: true + }, + http: { + path: `/filter`, + verb: 'GET' + } + }); + + Self.filter = async(filter, tags) => { + console.log(tags); + return await Self.find(filter); + }; +}; diff --git a/services/loopback/common/models/item.js b/services/loopback/common/models/item.js index 2f54244bf..08ea8828a 100644 --- a/services/loopback/common/models/item.js +++ b/services/loopback/common/models/item.js @@ -1,6 +1,7 @@ let UserError = require('../helpers').UserError; module.exports = Self => { + require('../methods/item/filter')(Self); require('../methods/item/clone')(Self); require('../methods/item/updateTaxes')(Self); require('../methods/item/getDiary')(Self);