From 4002b51565055d7fe728f232bba616d42619a697 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Fri, 17 Jan 2020 08:43:10 +0100 Subject: [PATCH 1/6] added section --- db/dump/fixtures.sql | 12 +- .../methods/travel-thermograph/uploadFile.js | 76 ++++++++++++ .../travel/back/models/travel-thermograph.js | 3 + modules/travel/front/index.js | 3 +- modules/travel/front/locale/es.yml | 2 +- modules/travel/front/routes.json | 20 ++- .../front/thermograph/create/index.html | 80 ++++++++++++ .../travel/front/thermograph/create/index.js | 114 ++++++++++++++++++ .../front/thermograph/create/index.spec.js | 80 ++++++++++++ .../front/thermograph/{ => index}/index.html | 8 ++ .../front/thermograph/{ => index}/index.js | 4 +- .../front/thermograph/{ => index}/style.scss | 0 .../travel/front/thermograph/locale/es.yml | 10 +- 13 files changed, 399 insertions(+), 13 deletions(-) create mode 100644 modules/travel/back/methods/travel-thermograph/uploadFile.js create mode 100644 modules/travel/back/models/travel-thermograph.js create mode 100644 modules/travel/front/thermograph/create/index.html create mode 100644 modules/travel/front/thermograph/create/index.js create mode 100644 modules/travel/front/thermograph/create/index.spec.js rename modules/travel/front/thermograph/{ => index}/index.html (91%) rename modules/travel/front/thermograph/{ => index}/index.js (86%) rename modules/travel/front/thermograph/{ => index}/style.scss (100%) diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 068ced790..0cbbcad53 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2018,11 +2018,13 @@ INSERT INTO `vn`.`thermograph`(`id`, `model`) VALUES ('TMM190901395', 'TEMPMATE'), ('TL.BBA85422', 'TL30'), - ('TZ1905012010', 'DISPOSABLE'); + ('TZ1905012010', 'DISPOSABLE'), + ('138350-0', 'DISPOSABLE'); INSERT INTO `vn`.`travelThermograph`(`thermographFk`, `created`, `warehouseFk`, `travelFk`, `temperature`, `result`, `dmsFk`) VALUES - ('TMM190901395', CURDATE(), 1, 1, 'WARM', 'Ok', NULL), - ('TL.BBA85422', DATE_ADD(CURDATE(), INTERVAL -1 MONTH), 2, 2, 'COOL', 'Ok', NULL), - ('TL.BBA85422', CURDATE(), 2, 1, 'COOL', 'can not read the temperature', NULL), - ('TZ1905012010', CURDATE(), 1, 1, 'WARM', 'Temperature in range', 5); \ No newline at end of file + ('TMM190901395', CURDATE(), 1, 1, 'WARM', 'Ok', NULL), + ('TL.BBA85422', DATE_ADD(CURDATE(), INTERVAL -1 MONTH), 2, 2, 'COOL', 'Ok', NULL), + ('TL.BBA85422', CURDATE(), 2, 1, 'COOL', 'can not read the temperature', NULL), + ('TZ1905012010', CURDATE(), 1, 1, 'WARM', 'Temperature in range', 5), + ('138350-0', CURDATE(), 1, NULL, 'WARM', NULL, 5); \ No newline at end of file diff --git a/modules/travel/back/methods/travel-thermograph/uploadFile.js b/modules/travel/back/methods/travel-thermograph/uploadFile.js new file mode 100644 index 000000000..e5ea465cb --- /dev/null +++ b/modules/travel/back/methods/travel-thermograph/uploadFile.js @@ -0,0 +1,76 @@ +module.exports = Self => { + Self.remoteMethodCtx('uploadFile', { + description: 'Upload and attach a document', + accessType: 'WRITE', + accepts: [{ + arg: 'id', + type: 'Number', + description: 'The ticket id', + http: {source: 'path'} + }, { + arg: 'warehouseId', + type: 'Number', + description: 'The warehouse id', + required: true + }, { + arg: 'companyId', + type: 'Number', + description: 'The company id', + required: true + }, { + arg: 'dmsTypeId', + type: 'Number', + description: 'The dms type id', + required: true + }, { + arg: 'reference', + type: 'String', + required: true + }, { + arg: 'description', + type: 'String', + required: true + }, { + arg: 'hasFile', + type: 'Boolean', + description: 'True if has an attached file', + required: true + }], + returns: { + type: 'Object', + root: true + }, + http: { + path: `/:id/uploadFile`, + verb: 'POST' + } + }); + + Self.uploadFile = async(ctx, id) => { + const models = Self.app.models; + const promises = []; + const tx = await Self.beginTransaction({}); + + try { + const options = {transaction: tx}; + + const uploadedFiles = await models.Dms.uploadFile(ctx, options); + uploadedFiles.forEach(dms => { + const newTicketDms = models.TicketDms.create({ + ticketFk: id, + dmsFk: dms.id + }, options); + + promises.push(newTicketDms); + }); + const resolvedPromises = await Promise.all(promises); + + await tx.commit(); + + return resolvedPromises; + } catch (err) { + await tx.rollback(); + throw err; + } + }; +}; diff --git a/modules/travel/back/models/travel-thermograph.js b/modules/travel/back/models/travel-thermograph.js new file mode 100644 index 000000000..8eab0ab7b --- /dev/null +++ b/modules/travel/back/models/travel-thermograph.js @@ -0,0 +1,3 @@ +module.exports = Self => { + require('../methods/travel-thermograph/uploadFile')(Self); +}; diff --git a/modules/travel/front/index.js b/modules/travel/front/index.js index 4bf935a8c..1f5346e98 100644 --- a/modules/travel/front/index.js +++ b/modules/travel/front/index.js @@ -9,5 +9,6 @@ import './summary'; import './basic-data'; import './log'; import './create'; -import './thermograph'; +import './thermograph/index/'; +import './thermograph/create/'; diff --git a/modules/travel/front/locale/es.yml b/modules/travel/front/locale/es.yml index 31e4e452b..931f79ab8 100644 --- a/modules/travel/front/locale/es.yml +++ b/modules/travel/front/locale/es.yml @@ -16,4 +16,4 @@ New travel: Nuevo envío # Sections Travels: Envíos Log: Historial -Thermographs: Termómetros \ No newline at end of file +Thermographs: Termógrafos \ No newline at end of file diff --git a/modules/travel/front/routes.json b/modules/travel/front/routes.json index d7d5b52df..fcbe5b92f 100644 --- a/modules/travel/front/routes.json +++ b/modules/travel/front/routes.json @@ -11,7 +11,7 @@ "card": [ {"state": "travel.card.basicData", "icon": "settings"}, {"state": "travel.card.log", "icon": "history"}, - {"state": "travel.card.thermograph", "icon": "icon-thermometer"} + {"state": "travel.card.thermograph.index", "icon": "icon-thermometer"} ] }, "routes": [ @@ -59,14 +59,28 @@ "component": "vn-travel-create", "description": "New travel" }, { - "url" : "/thermograph", + "url": "/thermograph", "state": "travel.card.thermograph", - "component": "vn-travel-thermograph", + "abstract": true, + "component": "ui-view" + }, { + "url" : "/index", + "state": "travel.card.thermograph.index", + "component": "vn-travel-thermograph-index", "description": "Thermographs", "params": { "travel": "$ctrl.travel" }, "acl": ["buyer"] + }, { + "url" : "/create", + "state": "travel.card.thermograph.create", + "component": "vn-travel-thermograph-create", + "description": "Add thermograph", + "params": { + "travel": "$ctrl.travel" + }, + "acl": ["buyer"] } ] } \ No newline at end of file diff --git a/modules/travel/front/thermograph/create/index.html b/modules/travel/front/thermograph/create/index.html new file mode 100644 index 000000000..327c6e629 --- /dev/null +++ b/modules/travel/front/thermograph/create/index.html @@ -0,0 +1,80 @@ + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
diff --git a/modules/travel/front/thermograph/create/index.js b/modules/travel/front/thermograph/create/index.js new file mode 100644 index 000000000..2f3f3f807 --- /dev/null +++ b/modules/travel/front/thermograph/create/index.js @@ -0,0 +1,114 @@ +import ngModule from '../../module'; + +class Controller { + constructor($scope, $http, $state, $translate, vnApp, vnConfig) { + this.$ = $scope; + this.$http = $http; + this.$state = $state; + this.$translate = $translate; + this.vnApp = vnApp; + this.vnConfig = vnConfig; + this.dms = { + files: [], + hasFile: false, + hasFileAttached: false + }; + } + + get travel() { + return this._travel; + } + + set travel(value) { + this._travel = value; + + if (value) { + this.setDefaultParams(); + this.getAllowedContentTypes(); + } + } + + getAllowedContentTypes() { + // Replace with TravelThermographs + this.$http.get('ticketDms/allowedContentTypes').then(res => { + const contentTypes = res.data.join(', '); + this.allowedContentTypes = contentTypes; + }); + } + + get contentTypesInfo() { + return this.$translate.instant('ContentTypesInfo', { + allowedContentTypes: this.allowedContentTypes + }); + } + + setDefaultParams() { + const params = {filter: { + where: {code: 'miscellaneous'} + }}; + this.$http.get('DmsTypes/findOne', {params}).then(res => { + const dmsTypeId = res.data && res.data.id; + const companyId = this.vnConfig.companyFk; + const warehouseId = this.vnConfig.warehouseFk; + const defaultParams = { + reference: this.travel.id, + warehouseId: warehouseId, + companyId: companyId, + dmsTypeId: dmsTypeId, + description: this.$translate.instant('FileDescription', { + travelId: this.travel.id + }).toUpperCase() + }; + + this.dms = Object.assign(this.dms, defaultParams); + }); + } + + onSubmit() { + const query = `TravelThermographs/${this.dms.thermographId}/uploadFile`; + const options = { + method: 'POST', + url: query, + params: this.dms, + headers: { + 'Content-Type': undefined + }, + transformRequest: files => { + const formData = new FormData(); + + for (let i = 0; i < files.length; i++) + formData.append(files[i].name, files[i]); + + return formData; + }, + data: this.dms.files + }; + this.$http(options).then(res => { + if (res) { + this.vnApp.showSuccess(this.$translate.instant('Data saved!')); + this.$.watcher.updateOriginalData(); + this.$state.go('travel.card.thermograph.index'); + } + }); + } + + onFileChange(files) { + let hasFileAttached = false; + if (files.length > 0) + hasFileAttached = true; + + this.$.$applyAsync(() => { + this.dms.hasFileAttached = hasFileAttached; + }); + } +} + +Controller.$inject = ['$scope', '$http', '$state', '$translate', 'vnApp', 'vnConfig']; + +ngModule.component('vnTravelThermographCreate', { + template: require('./index.html'), + controller: Controller, + bindings: { + travel: '<' + } +}); diff --git a/modules/travel/front/thermograph/create/index.spec.js b/modules/travel/front/thermograph/create/index.spec.js new file mode 100644 index 000000000..c8b63358f --- /dev/null +++ b/modules/travel/front/thermograph/create/index.spec.js @@ -0,0 +1,80 @@ +import './index'; + +describe('Ticket', () => { + describe('Component vnTicketDmsCreate', () => { + let controller; + let $scope; + let $httpBackend; + let $httpParamSerializer; + + beforeEach(ngModule('ticket')); + + beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => { + $scope = $rootScope.$new(); + $httpBackend = _$httpBackend_; + $httpParamSerializer = _$httpParamSerializer_; + controller = $componentController('vnTicketDmsCreate', {$scope}); + controller._ticket = { + id: 15, + client: {id: 101, name: 'Bruce wayne'}, + warehouseFk: 1, + companyFk: 1 + }; + })); + + describe('client() setter', () => { + it('should set the ticket data and then call setDefaultParams() and getAllowedContentTypes()', () => { + spyOn(controller, 'setDefaultParams'); + spyOn(controller, 'getAllowedContentTypes'); + controller.ticket = { + id: 15, + name: 'Bruce wayne' + }; + + expect(controller.ticket).toBeDefined(); + expect(controller.setDefaultParams).toHaveBeenCalledWith(); + expect(controller.getAllowedContentTypes).toHaveBeenCalledWith(); + }); + }); + + describe('setDefaultParams()', () => { + it('should perform a GET query and define the dms property on controller', () => { + const params = {filter: { + where: {code: 'ticket'} + }}; + let serializedParams = $httpParamSerializer(params); + $httpBackend.when('GET', `DmsTypes/findOne?${serializedParams}`).respond({id: 14, code: 'ticket'}); + $httpBackend.expect('GET', `DmsTypes/findOne?${serializedParams}`); + controller.setDefaultParams(); + $httpBackend.flush(); + + expect(controller.dms).toBeDefined(); + expect(controller.dms.reference).toEqual(15); + expect(controller.dms.dmsTypeId).toEqual(14); + }); + }); + + describe('onFileChange()', () => { + it('should set dms hasFileAttached property to true if has any files', () => { + const files = [{id: 1, name: 'MyFile'}]; + controller.onFileChange(files); + $scope.$apply(); + + expect(controller.dms.hasFileAttached).toBeTruthy(); + }); + }); + + describe('getAllowedContentTypes()', () => { + it('should make an HTTP GET request to get the allowed content types', () => { + const expectedResponse = ['image/png', 'image/jpg']; + $httpBackend.when('GET', `ticketDms/allowedContentTypes`).respond(expectedResponse); + $httpBackend.expect('GET', `ticketDms/allowedContentTypes`); + controller.getAllowedContentTypes(); + $httpBackend.flush(); + + expect(controller.allowedContentTypes).toBeDefined(); + expect(controller.allowedContentTypes).toEqual('image/png, image/jpg'); + }); + }); + }); +}); diff --git a/modules/travel/front/thermograph/index.html b/modules/travel/front/thermograph/index/index.html similarity index 91% rename from modules/travel/front/thermograph/index.html rename to modules/travel/front/thermograph/index/index.html index 67d836d73..09814e5e9 100644 --- a/modules/travel/front/thermograph/index.html +++ b/modules/travel/front/thermograph/index/index.html @@ -48,3 +48,11 @@ question="Delete thermograph from travel?" on-response="$ctrl.removeThermographFromTravel($response)"> + + + + \ No newline at end of file diff --git a/modules/travel/front/thermograph/index.js b/modules/travel/front/thermograph/index/index.js similarity index 86% rename from modules/travel/front/thermograph/index.js rename to modules/travel/front/thermograph/index/index.js index 394bebdb8..5a4f13a7b 100644 --- a/modules/travel/front/thermograph/index.js +++ b/modules/travel/front/thermograph/index/index.js @@ -1,4 +1,4 @@ -import ngModule from '../module'; +import ngModule from '../../module'; import './style.scss'; import Component from 'core/lib/component'; @@ -16,7 +16,7 @@ class Controller extends Component { } } -ngModule.component('vnTravelThermograph', { +ngModule.component('vnTravelThermographIndex', { template: require('./index.html'), controller: Controller, require: { diff --git a/modules/travel/front/thermograph/style.scss b/modules/travel/front/thermograph/index/style.scss similarity index 100% rename from modules/travel/front/thermograph/style.scss rename to modules/travel/front/thermograph/index/style.scss diff --git a/modules/travel/front/thermograph/locale/es.yml b/modules/travel/front/thermograph/locale/es.yml index 9f5e04b72..d37be5102 100644 --- a/modules/travel/front/thermograph/locale/es.yml +++ b/modules/travel/front/thermograph/locale/es.yml @@ -3,4 +3,12 @@ Temperature: Temperatura State: Estado Destination: Destino Created: Creado -Remove thermograph: Eliminar termómetro \ No newline at end of file +Remove thermograph: Eliminar termómetro +Upload file: Subir fichero +Edit file: Editar fichero +Upload: Subir +File: Fichero +FileDescription: Travel id {{ticketId}} +ContentTypesInfo: 'Tipos de archivo permitidos: {{allowedContentTypes}}' +Are you sure you want to continue?: ¿Seguro que quieres continuar? +Add thermograph: Añadir termógrafo \ No newline at end of file From 65a227db9e6339bcf636fd17c719964f9b51b2ee Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Fri, 17 Jan 2020 09:23:32 +0100 Subject: [PATCH 2/6] uploadFile args --- .../methods/travel-thermograph/uploadFile.js | 38 ++++++++++--------- .../travel/front/thermograph/create/index.js | 6 +-- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/modules/travel/back/methods/travel-thermograph/uploadFile.js b/modules/travel/back/methods/travel-thermograph/uploadFile.js index e5ea465cb..0cff11204 100644 --- a/modules/travel/back/methods/travel-thermograph/uploadFile.js +++ b/modules/travel/back/methods/travel-thermograph/uploadFile.js @@ -4,9 +4,14 @@ module.exports = Self => { accessType: 'WRITE', accepts: [{ arg: 'id', - type: 'Number', - description: 'The ticket id', + type: 'String', + description: 'The thermograph id', http: {source: 'path'} + }, { + arg: 'travelId', + type: 'Number', + description: 'The travel id', + required: true }, { arg: 'warehouseId', type: 'Number', @@ -30,11 +35,6 @@ module.exports = Self => { arg: 'description', type: 'String', required: true - }, { - arg: 'hasFile', - type: 'Boolean', - description: 'True if has an attached file', - required: true }], returns: { type: 'Object', @@ -46,28 +46,30 @@ module.exports = Self => { } }); - Self.uploadFile = async(ctx, id) => { + Self.uploadFile = async(ctx, thermograpId, travelId) => { const models = Self.app.models; - const promises = []; const tx = await Self.beginTransaction({}); try { const options = {transaction: tx}; - const uploadedFiles = await models.Dms.uploadFile(ctx, options); - uploadedFiles.forEach(dms => { - const newTicketDms = models.TicketDms.create({ - ticketFk: id, - dmsFk: dms.id - }, options); + const firstDms = uploadedFiles[0]; - promises.push(newTicketDms); + const travelThermograph = await models.TravelThermograph.findOne({ + where: { + thermographFk: thermograpId, + travelFk: null + } + }, options); + + await travelThermograph.updateAttributes({ + dmsFk: firstDms.id, + travelFk: travelId }); - const resolvedPromises = await Promise.all(promises); await tx.commit(); - return resolvedPromises; + return travelThermograph; } catch (err) { await tx.rollback(); throw err; diff --git a/modules/travel/front/thermograph/create/index.js b/modules/travel/front/thermograph/create/index.js index 2f3f3f807..b12bf47ff 100644 --- a/modules/travel/front/thermograph/create/index.js +++ b/modules/travel/front/thermograph/create/index.js @@ -9,9 +9,9 @@ class Controller { this.vnApp = vnApp; this.vnConfig = vnConfig; this.dms = { - files: [], - hasFile: false, - hasFileAttached: false + travelId: $state.params.id, + hasFileAttached: false, + files: [] }; } From 5e4ab8b1283d81284b405956bc8124578e901a53 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Tue, 21 Jan 2020 09:00:42 +0100 Subject: [PATCH 3/6] Loggable errors --- back/models/dms.json | 3 + .../10140-kings/00-travelThermograph.sql | 7 +++ db/dump/fixtures.sql | 3 +- loopback/common/models/loggable.js | 2 +- .../back/methods/travel-thermograph/delete.js | 61 +++++++++++++++++++ .../methods/travel-thermograph/uploadFile.js | 6 +- modules/travel/back/models/travel-log.json | 2 +- .../back/models/travel-thermograph.json | 9 +-- modules/travel/back/models/travel.json | 3 +- .../travel/front/thermograph/locale/es.yml | 2 +- 10 files changed, 86 insertions(+), 12 deletions(-) create mode 100644 db/changes/10140-kings/00-travelThermograph.sql create mode 100644 modules/travel/back/methods/travel-thermograph/delete.js diff --git a/back/models/dms.json b/back/models/dms.json index bf6e44311..1d9e3ec21 100644 --- a/back/models/dms.json +++ b/back/models/dms.json @@ -3,6 +3,9 @@ "name": "Dms", "description": "Documental Managment system", "base": "VnModel", + "log": { + "showField": "reference" + }, "options": { "mysql": { "table": "dms" diff --git a/db/changes/10140-kings/00-travelThermograph.sql b/db/changes/10140-kings/00-travelThermograph.sql new file mode 100644 index 000000000..c19151a45 --- /dev/null +++ b/db/changes/10140-kings/00-travelThermograph.sql @@ -0,0 +1,7 @@ +ALTER TABLE `vn`.`travelThermograph` +ADD COLUMN `id` INT NOT NULL AUTO_INCREMENT FIRST, +DROP PRIMARY KEY, +ADD PRIMARY KEY (`id`); + +ALTER TABLE `vn`.`travelThermograph` +ADD UNIQUE INDEX `thermograph_created` (`thermographFk` ASC, `created` ASC) VISIBLE; \ No newline at end of file diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index c31510b2c..92580b36e 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -1959,4 +1959,5 @@ INSERT INTO `vn`.`travelThermograph`(`thermographFk`, `created`, `warehouseFk`, ('TL.BBA85422', DATE_ADD(CURDATE(), INTERVAL -1 MONTH), 2, 2, 'COOL', 'Ok', NULL), ('TL.BBA85422', CURDATE(), 2, 1, 'COOL', 'can not read the temperature', NULL), ('TZ1905012010', CURDATE(), 1, 1, 'WARM', 'Temperature in range', 5), - ('138350-0', CURDATE(), 1, NULL, 'WARM', NULL, 5); \ No newline at end of file + ('138350-0', DATE_ADD(CURDATE(), INTERVAL -1 MONTH), 1, NULL, 'WARM', NULL, 5), + ('138350-0', CURDATE(), 1, 1, 'WARM', 'Ok', 5); \ No newline at end of file diff --git a/loopback/common/models/loggable.js b/loopback/common/models/loggable.js index a6d0e8474..d9116a0de 100644 --- a/loopback/common/models/loggable.js +++ b/loopback/common/models/loggable.js @@ -219,7 +219,7 @@ module.exports = function(Self) { userFk: userFk, action: action, changedModel: ctx.Model.definition.name, - changedModelId: changedModelId, + changedModelId: changedModelId, // Model property with an different data type will throw a NaN error changedModelValue: where, oldInstance: oldInstance, newInstance: newInstance diff --git a/modules/travel/back/methods/travel-thermograph/delete.js b/modules/travel/back/methods/travel-thermograph/delete.js new file mode 100644 index 000000000..a8d6bc762 --- /dev/null +++ b/modules/travel/back/methods/travel-thermograph/delete.js @@ -0,0 +1,61 @@ + +module.exports = Self => { + Self.remoteMethod('delete', { + description: 'Delete a invoiceOut', + accessType: 'WRITE', + accepts: { + arg: 'id', + type: 'string', + required: true, + description: 'The invoiceOut id', + http: {source: 'path'} + }, + returns: { + type: 'object', + root: true + }, + http: { + path: '/:id/delete', + verb: 'POST' + } + }); + + Self.delete = async id => { + const models = Self.app.models; + + const targetClaimDms = await models.ClaimDms.findById(id); + const targetDms = await models.Dms.findById(targetClaimDms.dmsFk); + + + const trashDmsType = await models.DmsType.findOne({ + where: {code: 'trash'} + }); + + await models.Dms.removeFile(ctx, targetClaimDms.dmsFk); + await targetClaimDms.destroy(); + + return targetDms.updateAttribute('dmsTypeFk', trashDmsType.id); + + + const transaction = await Self.beginTransaction({}); + try { + let options = {transaction: transaction}; + + let invoiceOut = await Self.findById(id); + let tickets = await Self.app.models.Ticket.find({where: {refFk: invoiceOut.ref}}); + + const promises = []; + tickets.forEach(ticket => { + promises.push(ticket.updateAttribute('refFk', null, options)); + }); + + await Promise.all(promises); + await invoiceOut.destroy(options); + await transaction.commit(); + return tickets; + } catch (e) { + await transaction.rollback(); + throw e; + } + }; +}; diff --git a/modules/travel/back/methods/travel-thermograph/uploadFile.js b/modules/travel/back/methods/travel-thermograph/uploadFile.js index 0cff11204..6ac1b4c09 100644 --- a/modules/travel/back/methods/travel-thermograph/uploadFile.js +++ b/modules/travel/back/methods/travel-thermograph/uploadFile.js @@ -46,7 +46,7 @@ module.exports = Self => { } }); - Self.uploadFile = async(ctx, thermograpId, travelId) => { + Self.uploadFile = async(ctx, thermographId, travelId) => { const models = Self.app.models; const tx = await Self.beginTransaction({}); @@ -57,7 +57,7 @@ module.exports = Self => { const travelThermograph = await models.TravelThermograph.findOne({ where: { - thermographFk: thermograpId, + thermographFk: thermographId, travelFk: null } }, options); @@ -65,7 +65,7 @@ module.exports = Self => { await travelThermograph.updateAttributes({ dmsFk: firstDms.id, travelFk: travelId - }); + }, options); await tx.commit(); diff --git a/modules/travel/back/models/travel-log.json b/modules/travel/back/models/travel-log.json index d21821127..d53be88f1 100644 --- a/modules/travel/back/models/travel-log.json +++ b/modules/travel/back/models/travel-log.json @@ -36,7 +36,7 @@ "type": "Date" }, "changedModelId": { - "type": "Number" + "type": "String" }, "changedModelValue": { "type": "String" diff --git a/modules/travel/back/models/travel-thermograph.json b/modules/travel/back/models/travel-thermograph.json index 18e632d02..80a528655 100644 --- a/modules/travel/back/models/travel-thermograph.json +++ b/modules/travel/back/models/travel-thermograph.json @@ -2,7 +2,9 @@ "name": "TravelThermograph", "base": "Loggable", "log": { - "model":"TravelLog" + "model":"TravelLog", + "relation": "travel", + "showField": "ref" }, "options": { "mysql": { @@ -12,12 +14,11 @@ "properties": { "thermographFk": { "type": "String", - "id": 1, - "description": "Identifier" + "description": "Identifier", + "id": true }, "created": { "type": "Date", - "id": 2, "description": "Identifier" }, "temperature": { diff --git a/modules/travel/back/models/travel.json b/modules/travel/back/models/travel.json index b4f154525..0eafe4010 100644 --- a/modules/travel/back/models/travel.json +++ b/modules/travel/back/models/travel.json @@ -2,7 +2,8 @@ "name": "Travel", "base": "Loggable", "log": { - "model":"TravelLog" + "model":"TravelLog", + "showField": "ref" }, "options": { "mysql": { diff --git a/modules/travel/front/thermograph/locale/es.yml b/modules/travel/front/thermograph/locale/es.yml index d37be5102..d6e634ce3 100644 --- a/modules/travel/front/thermograph/locale/es.yml +++ b/modules/travel/front/thermograph/locale/es.yml @@ -8,7 +8,7 @@ Upload file: Subir fichero Edit file: Editar fichero Upload: Subir File: Fichero -FileDescription: Travel id {{ticketId}} +FileDescription: Travel id {{travelId}} ContentTypesInfo: 'Tipos de archivo permitidos: {{allowedContentTypes}}' Are you sure you want to continue?: ¿Seguro que quieres continuar? Add thermograph: Añadir termógrafo \ No newline at end of file From f0b4c26aeac80bfec5bc59ba9584382f497b3e78 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Tue, 21 Jan 2020 12:21:53 +0100 Subject: [PATCH 4/6] added travel thermograph create & remove methods --- back/methods/dms/removeFile.js | 4 +- db/dump/fixtures.sql | 4 +- .../back/methods/travel-thermograph/delete.js | 61 ------------------- .../createThermograph.js} | 18 +++--- .../back/methods/travel/deleteThermograph.js | 53 ++++++++++++++++ .../travel/back/models/travel-thermograph.js | 3 - .../back/models/travel-thermograph.json | 7 +-- modules/travel/back/models/travel.js | 2 + .../travel/front/thermograph/create/index.js | 3 +- .../travel/front/thermograph/index/index.html | 13 +++- .../travel/front/thermograph/index/index.js | 21 ++++++- .../travel/front/thermograph/locale/es.yml | 7 ++- 12 files changed, 109 insertions(+), 87 deletions(-) delete mode 100644 modules/travel/back/methods/travel-thermograph/delete.js rename modules/travel/back/methods/{travel-thermograph/uploadFile.js => travel/createThermograph.js} (89%) create mode 100644 modules/travel/back/methods/travel/deleteThermograph.js delete mode 100644 modules/travel/back/models/travel-thermograph.js diff --git a/back/methods/dms/removeFile.js b/back/methods/dms/removeFile.js index 350bea6bc..93fae9728 100644 --- a/back/methods/dms/removeFile.js +++ b/back/methods/dms/removeFile.js @@ -22,8 +22,10 @@ module.exports = Self => { Self.removeFile = async(ctx, id) => { const models = Self.app.models; - const trashDmsType = await models.DmsType.findOne({where: {code: 'trash'}}); const dms = await models.Dms.findById(id); + const trashDmsType = await models.DmsType.findOne({ + where: {code: 'trash'} + }); const hasWriteRole = await models.DmsType.hasWriteRole(ctx, dms.dmsTypeFk); if (!hasWriteRole) diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 92580b36e..95ea7b20f 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -1959,5 +1959,5 @@ INSERT INTO `vn`.`travelThermograph`(`thermographFk`, `created`, `warehouseFk`, ('TL.BBA85422', DATE_ADD(CURDATE(), INTERVAL -1 MONTH), 2, 2, 'COOL', 'Ok', NULL), ('TL.BBA85422', CURDATE(), 2, 1, 'COOL', 'can not read the temperature', NULL), ('TZ1905012010', CURDATE(), 1, 1, 'WARM', 'Temperature in range', 5), - ('138350-0', DATE_ADD(CURDATE(), INTERVAL -1 MONTH), 1, NULL, 'WARM', NULL, 5), - ('138350-0', CURDATE(), 1, 1, 'WARM', 'Ok', 5); \ No newline at end of file + ('138350-0', DATE_ADD(CURDATE(), INTERVAL -1 MONTH), 1, 1, 'WARM', NULL, 5), + ('138350-0', CURDATE(), 1, NULL, 'COOL', NULL, NULL); \ No newline at end of file diff --git a/modules/travel/back/methods/travel-thermograph/delete.js b/modules/travel/back/methods/travel-thermograph/delete.js deleted file mode 100644 index a8d6bc762..000000000 --- a/modules/travel/back/methods/travel-thermograph/delete.js +++ /dev/null @@ -1,61 +0,0 @@ - -module.exports = Self => { - Self.remoteMethod('delete', { - description: 'Delete a invoiceOut', - accessType: 'WRITE', - accepts: { - arg: 'id', - type: 'string', - required: true, - description: 'The invoiceOut id', - http: {source: 'path'} - }, - returns: { - type: 'object', - root: true - }, - http: { - path: '/:id/delete', - verb: 'POST' - } - }); - - Self.delete = async id => { - const models = Self.app.models; - - const targetClaimDms = await models.ClaimDms.findById(id); - const targetDms = await models.Dms.findById(targetClaimDms.dmsFk); - - - const trashDmsType = await models.DmsType.findOne({ - where: {code: 'trash'} - }); - - await models.Dms.removeFile(ctx, targetClaimDms.dmsFk); - await targetClaimDms.destroy(); - - return targetDms.updateAttribute('dmsTypeFk', trashDmsType.id); - - - const transaction = await Self.beginTransaction({}); - try { - let options = {transaction: transaction}; - - let invoiceOut = await Self.findById(id); - let tickets = await Self.app.models.Ticket.find({where: {refFk: invoiceOut.ref}}); - - const promises = []; - tickets.forEach(ticket => { - promises.push(ticket.updateAttribute('refFk', null, options)); - }); - - await Promise.all(promises); - await invoiceOut.destroy(options); - await transaction.commit(); - return tickets; - } catch (e) { - await transaction.rollback(); - throw e; - } - }; -}; diff --git a/modules/travel/back/methods/travel-thermograph/uploadFile.js b/modules/travel/back/methods/travel/createThermograph.js similarity index 89% rename from modules/travel/back/methods/travel-thermograph/uploadFile.js rename to modules/travel/back/methods/travel/createThermograph.js index 6ac1b4c09..56a27ddb3 100644 --- a/modules/travel/back/methods/travel-thermograph/uploadFile.js +++ b/modules/travel/back/methods/travel/createThermograph.js @@ -1,16 +1,16 @@ module.exports = Self => { - Self.remoteMethodCtx('uploadFile', { + Self.remoteMethodCtx('createThermograph', { description: 'Upload and attach a document', accessType: 'WRITE', accepts: [{ arg: 'id', - type: 'String', - description: 'The thermograph id', - http: {source: 'path'} - }, { - arg: 'travelId', type: 'Number', description: 'The travel id', + http: {source: 'path'} + }, { + arg: 'thermographId', + type: 'String', + description: 'The thermograph id', required: true }, { arg: 'warehouseId', @@ -41,12 +41,12 @@ module.exports = Self => { root: true }, http: { - path: `/:id/uploadFile`, + path: `/:id/createThermograph`, verb: 'POST' } }); - Self.uploadFile = async(ctx, thermographId, travelId) => { + Self.createThermograph = async(ctx, id, thermographId) => { const models = Self.app.models; const tx = await Self.beginTransaction({}); @@ -64,7 +64,7 @@ module.exports = Self => { await travelThermograph.updateAttributes({ dmsFk: firstDms.id, - travelFk: travelId + travelFk: id }, options); await tx.commit(); diff --git a/modules/travel/back/methods/travel/deleteThermograph.js b/modules/travel/back/methods/travel/deleteThermograph.js new file mode 100644 index 000000000..ba541c560 --- /dev/null +++ b/modules/travel/back/methods/travel/deleteThermograph.js @@ -0,0 +1,53 @@ + +module.exports = Self => { + Self.remoteMethodCtx('deleteThermograph', { + description: 'Deletes a travel thermograph', + accessType: 'WRITE', + accepts: { + arg: 'id', + type: 'Number', + description: 'The thermograph id', + required: true + }, + returns: { + type: 'object', + root: true + }, + http: { + path: '/deleteThermograph', + verb: 'DELETE' + } + }); + + Self.deleteThermograph = async(ctx, id) => { + const models = Self.app.models; + const userId = ctx.req.accessToken.userId; + const travelThermograph = await models.TravelThermograph.findById(id); + + await models.Dms.removeFile(ctx, travelThermograph.dmsFk); + await Self.rawSql(` + UPDATE travelThermograph + SET travelFk = NULL, dmsFk = NULL + WHERE id = ?`, [id]); + + const oldInstance = { + travelFk: travelThermograph.travelFk, + dmsFk: travelThermograph.dmsFk + }; + + await models.TravelLog.create({ + originFk: travelThermograph.travelFk, + userFk: userId, + action: 'delete', + changedModel: 'TravelThermograph', + changedModelId: id, + oldInstance: oldInstance, + newInstance: {} + }); + + travelThermograph.travelFk = null; + travelThermograph.dmsFk = null; + + return travelThermograph; + }; +}; diff --git a/modules/travel/back/models/travel-thermograph.js b/modules/travel/back/models/travel-thermograph.js deleted file mode 100644 index 8eab0ab7b..000000000 --- a/modules/travel/back/models/travel-thermograph.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = Self => { - require('../methods/travel-thermograph/uploadFile')(Self); -}; diff --git a/modules/travel/back/models/travel-thermograph.json b/modules/travel/back/models/travel-thermograph.json index 80a528655..b8f7fa41a 100644 --- a/modules/travel/back/models/travel-thermograph.json +++ b/modules/travel/back/models/travel-thermograph.json @@ -12,14 +12,13 @@ } }, "properties": { - "thermographFk": { - "type": "String", + "id": { + "type": "Number", "description": "Identifier", "id": true }, "created": { - "type": "Date", - "description": "Identifier" + "type": "Date" }, "temperature": { "type": "String" diff --git a/modules/travel/back/models/travel.js b/modules/travel/back/models/travel.js index 5fa55a366..895de7af1 100644 --- a/modules/travel/back/models/travel.js +++ b/modules/travel/back/models/travel.js @@ -2,4 +2,6 @@ module.exports = Self => { require('../methods/travel/getTravel')(Self); require('../methods/travel/getEntries')(Self); require('../methods/travel/filter')(Self); + require('../methods/travel/createThermograph')(Self); + require('../methods/travel/deleteThermograph')(Self); }; diff --git a/modules/travel/front/thermograph/create/index.js b/modules/travel/front/thermograph/create/index.js index b12bf47ff..40a0fafd0 100644 --- a/modules/travel/front/thermograph/create/index.js +++ b/modules/travel/front/thermograph/create/index.js @@ -9,7 +9,6 @@ class Controller { this.vnApp = vnApp; this.vnConfig = vnConfig; this.dms = { - travelId: $state.params.id, hasFileAttached: false, files: [] }; @@ -65,7 +64,7 @@ class Controller { } onSubmit() { - const query = `TravelThermographs/${this.dms.thermographId}/uploadFile`; + const query = `Travels/${this.travel.id}/createThermograph`; const options = { method: 'POST', url: query, diff --git a/modules/travel/front/thermograph/index/index.html b/modules/travel/front/thermograph/index/index.html index 09814e5e9..ca9ebcaea 100644 --- a/modules/travel/front/thermograph/index/index.html +++ b/modules/travel/front/thermograph/index/index.html @@ -28,6 +28,15 @@ {{thermograph.result}} {{thermograph.warehouse.name}} {{thermograph.created | date: 'dd/MM/yyyy'}} + + + + + + + question="Are you sure you want to remove the thermograph?" + on-accept="$ctrl.deleteThermograph()"> { + this.vnApp.showSuccess(this.$translate.instant('Thermograph deleted')); + this.$.model.remove(this.thermographIndex); + this.thermographIndex = null; + }); + } } +Controller.$inject = ['$element', '$scope', 'vnToken']; + ngModule.component('vnTravelThermographIndex', { template: require('./index.html'), controller: Controller, diff --git a/modules/travel/front/thermograph/locale/es.yml b/modules/travel/front/thermograph/locale/es.yml index d6e634ce3..184e95e73 100644 --- a/modules/travel/front/thermograph/locale/es.yml +++ b/modules/travel/front/thermograph/locale/es.yml @@ -3,7 +3,7 @@ Temperature: Temperatura State: Estado Destination: Destino Created: Creado -Remove thermograph: Eliminar termómetro +Remove thermograph: Eliminar termógrafo Upload file: Subir fichero Edit file: Editar fichero Upload: Subir @@ -11,4 +11,7 @@ File: Fichero FileDescription: Travel id {{travelId}} ContentTypesInfo: 'Tipos de archivo permitidos: {{allowedContentTypes}}' Are you sure you want to continue?: ¿Seguro que quieres continuar? -Add thermograph: Añadir termógrafo \ No newline at end of file +Add thermograph: Añadir termógrafo +Thermograph deleted: Termógrafo eliminado +Thermograph: Termógrafo +Are you sure you want to remove the thermograph?: ¿Seguro que quieres quitar el termógrafo? \ No newline at end of file From 2339d347f71da37678d5aa20f94a7700236167c2 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Wed, 22 Jan 2020 08:26:54 +0100 Subject: [PATCH 5/6] Added back unit tests --- .../back/methods/travel/createThermograph.js | 10 +++- .../travel/specs/createThermograph.spec.js | 50 +++++++++++++++++ .../travel/specs/deleteThermograph.spec.js | 56 +++++++++++++++++++ 3 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 modules/travel/back/methods/travel/specs/createThermograph.spec.js create mode 100644 modules/travel/back/methods/travel/specs/deleteThermograph.spec.js diff --git a/modules/travel/back/methods/travel/createThermograph.js b/modules/travel/back/methods/travel/createThermograph.js index 56a27ddb3..cbf0678d1 100644 --- a/modules/travel/back/methods/travel/createThermograph.js +++ b/modules/travel/back/methods/travel/createThermograph.js @@ -1,3 +1,5 @@ +const UserError = require('vn-loopback/util/user-error'); + module.exports = Self => { Self.remoteMethodCtx('createThermograph', { description: 'Upload and attach a document', @@ -52,8 +54,6 @@ module.exports = Self => { try { const options = {transaction: tx}; - const uploadedFiles = await models.Dms.uploadFile(ctx, options); - const firstDms = uploadedFiles[0]; const travelThermograph = await models.TravelThermograph.findOne({ where: { @@ -62,6 +62,12 @@ module.exports = Self => { } }, options); + if (!travelThermograph) + throw new UserError('No valid travel thermograph found'); + + const uploadedFiles = await models.Dms.uploadFile(ctx, options); + const firstDms = uploadedFiles[0]; + await travelThermograph.updateAttributes({ dmsFk: firstDms.id, travelFk: id diff --git a/modules/travel/back/methods/travel/specs/createThermograph.spec.js b/modules/travel/back/methods/travel/specs/createThermograph.spec.js new file mode 100644 index 000000000..b85dcca04 --- /dev/null +++ b/modules/travel/back/methods/travel/specs/createThermograph.spec.js @@ -0,0 +1,50 @@ +const app = require('vn-loopback/server/server'); + +describe('Travel createThermograph()', () => { + const models = app.models; + const travelId = 3; + const currentUserId = 102; + const thermographId = '138350-0'; + const ctx = {req: {accessToken: {userId: currentUserId}}, args: {dmsTypeId: 1}}; + let travelThermographBefore; + + afterAll(async done => { + await app.models.TravelThermograph.rawSql(` + UPDATE travelThermograph + SET travelFk = NULL, dmsFk = NULL + WHERE id = ?`, [travelThermographBefore.id]); + + done(); + }); + + it(`should set the travelFk and dmsFk properties to the travel thermograph`, async() => { + spyOn(app.models.Dms, 'uploadFile').and.returnValue([{id: 5}]); + + travelThermographBefore = await models.TravelThermograph.findOne({ + where: { + thermographFk: thermographId, + travelFk: null + } + }); + + await models.Travel.createThermograph(ctx, travelId, thermographId); + + const travelThermographAfter = await models.TravelThermograph.findOne({ + where: { + thermographFk: thermographId, + travelFk: travelId + } + }); + + expect(app.models.Dms.uploadFile).toHaveBeenCalledWith(ctx, jasmine.any(Object)); + + expect(travelThermographBefore).toBeDefined(); + expect(travelThermographBefore.thermographFk).toEqual(thermographId); + expect(travelThermographBefore.travelFk).toBeNull(); + expect(travelThermographAfter).toBeDefined(); + + expect(travelThermographAfter.thermographFk).toEqual(thermographId); + expect(travelThermographAfter.travelFk).toEqual(travelId); + expect(travelThermographAfter.dmsFk).toEqual(5); + }); +}); diff --git a/modules/travel/back/methods/travel/specs/deleteThermograph.spec.js b/modules/travel/back/methods/travel/specs/deleteThermograph.spec.js new file mode 100644 index 000000000..843fa046c --- /dev/null +++ b/modules/travel/back/methods/travel/specs/deleteThermograph.spec.js @@ -0,0 +1,56 @@ +const app = require('vn-loopback/server/server'); + +describe('Travel deleteThermograph()', () => { + const models = app.models; + const travelId = 1; + const currentUserId = 102; + const thermographId = 'TZ1905012010'; + const travelThermographId = 4; + const dmsId = 5; + const ctx = {req: {accessToken: {userId: currentUserId}}}; + let travelThermographBefore; + + afterAll(async done => { + await app.models.TravelThermograph.rawSql(` + UPDATE travelThermograph + SET travelFk = ?, dmsFk = ? + WHERE id = ?`, [ + travelThermographBefore.travelFk, + travelThermographBefore.dmsFk, + travelThermographBefore.id + ]); + + done(); + }); + + it(`should set the travelFk and dmsFk properties to null for travel thermograph removal`, async() => { + spyOn(app.models.Dms, 'removeFile').and.returnValue([{id: 5}]); + + travelThermographBefore = await models.TravelThermograph.findOne({ + where: { + thermographFk: thermographId, + travelFk: travelId + } + }); + + await models.Travel.deleteThermograph(ctx, travelThermographId); + + const travelThermographAfter = await models.TravelThermograph.findOne({ + where: { + thermographFk: thermographId, + travelFk: null + } + }); + + expect(app.models.Dms.removeFile).toHaveBeenCalledWith(ctx, dmsId); + + expect(travelThermographBefore).toBeDefined(); + expect(travelThermographBefore.thermographFk).toEqual(thermographId); + expect(travelThermographBefore.travelFk).toEqual(travelId); + expect(travelThermographBefore.dmsFk).toEqual(5); + + expect(travelThermographAfter).toBeDefined(); + expect(travelThermographAfter.thermographFk).toEqual(thermographId); + expect(travelThermographAfter.travelFk).toBeNull(); + }); +}); From c36e62a97b2cc59683414aed5c44fb20463c8e58 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Wed, 22 Jan 2020 09:04:26 +0100 Subject: [PATCH 6/6] added front unit tests --- modules/claim/front/action/index.spec.js | 4 +- .../travel-thermograph/allowedContentTypes.js | 23 ++++++++ .../travel/back/models/travel-thermograph.js | 4 ++ .../front/thermograph/create/index.html | 1 - .../travel/front/thermograph/create/index.js | 26 ++-------- .../front/thermograph/create/index.spec.js | 52 +++++++------------ 6 files changed, 54 insertions(+), 56 deletions(-) create mode 100644 modules/travel/back/methods/travel-thermograph/allowedContentTypes.js create mode 100644 modules/travel/back/models/travel-thermograph.js diff --git a/modules/claim/front/action/index.spec.js b/modules/claim/front/action/index.spec.js index 0d10957e7..539d2e8ef 100644 --- a/modules/claim/front/action/index.spec.js +++ b/modules/claim/front/action/index.spec.js @@ -206,9 +206,9 @@ describe('claim', () => { return resolve({id: freightPickUpPrice}); })); controller.onUpdateGreugeResponse('accept').then(res => { - console.log('asdas'); + }).catch(error => { - console.log('errorrrr!!'); + }); $httpBackend.flush(); diff --git a/modules/travel/back/methods/travel-thermograph/allowedContentTypes.js b/modules/travel/back/methods/travel-thermograph/allowedContentTypes.js new file mode 100644 index 000000000..2f5183f92 --- /dev/null +++ b/modules/travel/back/methods/travel-thermograph/allowedContentTypes.js @@ -0,0 +1,23 @@ +module.exports = Self => { + Self.remoteMethodCtx('allowedContentTypes', { + description: 'Returns a list of allowed contentTypes', + accessType: 'READ', + returns: { + type: ['Object'], + root: true + }, + http: { + path: `/allowedContentTypes`, + verb: 'GET' + } + }); + + Self.allowedContentTypes = async() => { + const storageConnector = Self.app.dataSources.storage.connector; + const allowedContentTypes = storageConnector.allowedContentTypes; + const modelAllowedContentTypes = Self.definition.settings.allowedContentTypes; + + return modelAllowedContentTypes || allowedContentTypes; + }; +}; + diff --git a/modules/travel/back/models/travel-thermograph.js b/modules/travel/back/models/travel-thermograph.js new file mode 100644 index 000000000..0d70edd7e --- /dev/null +++ b/modules/travel/back/models/travel-thermograph.js @@ -0,0 +1,4 @@ +module.exports = Self => { + require('../methods/travel-thermograph/allowedContentTypes')(Self); +}; + diff --git a/modules/travel/front/thermograph/create/index.html b/modules/travel/front/thermograph/create/index.html index 327c6e629..02ef54264 100644 --- a/modules/travel/front/thermograph/create/index.html +++ b/modules/travel/front/thermograph/create/index.html @@ -59,7 +59,6 @@ vn-one label="File" ng-model="$ctrl.dms.files" - on-change="$ctrl.onFileChange($files)" accept="{{$ctrl.allowedContentTypes}}" multiple="true"> diff --git a/modules/travel/front/thermograph/create/index.js b/modules/travel/front/thermograph/create/index.js index 40a0fafd0..8e10aca0f 100644 --- a/modules/travel/front/thermograph/create/index.js +++ b/modules/travel/front/thermograph/create/index.js @@ -8,10 +8,7 @@ class Controller { this.$translate = $translate; this.vnApp = vnApp; this.vnConfig = vnConfig; - this.dms = { - hasFileAttached: false, - files: [] - }; + this.dms = {files: []}; } get travel() { @@ -28,8 +25,7 @@ class Controller { } getAllowedContentTypes() { - // Replace with TravelThermographs - this.$http.get('ticketDms/allowedContentTypes').then(res => { + this.$http.get('TravelThermographs/allowedContentTypes').then(res => { const contentTypes = res.data.join(', '); this.allowedContentTypes = contentTypes; }); @@ -83,21 +79,9 @@ class Controller { data: this.dms.files }; this.$http(options).then(res => { - if (res) { - this.vnApp.showSuccess(this.$translate.instant('Data saved!')); - this.$.watcher.updateOriginalData(); - this.$state.go('travel.card.thermograph.index'); - } - }); - } - - onFileChange(files) { - let hasFileAttached = false; - if (files.length > 0) - hasFileAttached = true; - - this.$.$applyAsync(() => { - this.dms.hasFileAttached = hasFileAttached; + this.vnApp.showSuccess(this.$translate.instant('Data saved!')); + this.$.watcher.updateOriginalData(); + this.$state.go('travel.card.thermograph.index'); }); } } diff --git a/modules/travel/front/thermograph/create/index.spec.js b/modules/travel/front/thermograph/create/index.spec.js index c8b63358f..bf5b8bec5 100644 --- a/modules/travel/front/thermograph/create/index.spec.js +++ b/modules/travel/front/thermograph/create/index.spec.js @@ -1,37 +1,35 @@ import './index'; describe('Ticket', () => { - describe('Component vnTicketDmsCreate', () => { + describe('Component vnTravelThermographCreate', () => { let controller; let $scope; let $httpBackend; let $httpParamSerializer; + const travelId = 3; + const dmsTypeId = 5; - beforeEach(ngModule('ticket')); + beforeEach(ngModule('travel')); beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => { $scope = $rootScope.$new(); $httpBackend = _$httpBackend_; $httpParamSerializer = _$httpParamSerializer_; - controller = $componentController('vnTicketDmsCreate', {$scope}); - controller._ticket = { - id: 15, - client: {id: 101, name: 'Bruce wayne'}, - warehouseFk: 1, - companyFk: 1 + controller = $componentController('vnTravelThermographCreate', {$scope}); + controller._travel = { + id: travelId }; })); - describe('client() setter', () => { - it('should set the ticket data and then call setDefaultParams() and getAllowedContentTypes()', () => { + describe('travel() setter', () => { + it('should set the travel data and then call setDefaultParams() and getAllowedContentTypes()', () => { spyOn(controller, 'setDefaultParams'); spyOn(controller, 'getAllowedContentTypes'); - controller.ticket = { - id: 15, - name: 'Bruce wayne' + controller.travel = { + id: travelId }; - expect(controller.ticket).toBeDefined(); + expect(controller.travel).toBeDefined(); expect(controller.setDefaultParams).toHaveBeenCalledWith(); expect(controller.getAllowedContentTypes).toHaveBeenCalledWith(); }); @@ -40,40 +38,30 @@ describe('Ticket', () => { describe('setDefaultParams()', () => { it('should perform a GET query and define the dms property on controller', () => { const params = {filter: { - where: {code: 'ticket'} + where: {code: 'miscellaneous'} }}; let serializedParams = $httpParamSerializer(params); - $httpBackend.when('GET', `DmsTypes/findOne?${serializedParams}`).respond({id: 14, code: 'ticket'}); + $httpBackend.when('GET', `DmsTypes/findOne?${serializedParams}`).respond({id: dmsTypeId, code: 'miscellaneous'}); $httpBackend.expect('GET', `DmsTypes/findOne?${serializedParams}`); controller.setDefaultParams(); $httpBackend.flush(); expect(controller.dms).toBeDefined(); - expect(controller.dms.reference).toEqual(15); - expect(controller.dms.dmsTypeId).toEqual(14); - }); - }); - - describe('onFileChange()', () => { - it('should set dms hasFileAttached property to true if has any files', () => { - const files = [{id: 1, name: 'MyFile'}]; - controller.onFileChange(files); - $scope.$apply(); - - expect(controller.dms.hasFileAttached).toBeTruthy(); + expect(controller.dms.reference).toEqual(travelId); + expect(controller.dms.dmsTypeId).toEqual(dmsTypeId); }); }); describe('getAllowedContentTypes()', () => { it('should make an HTTP GET request to get the allowed content types', () => { - const expectedResponse = ['image/png', 'image/jpg']; - $httpBackend.when('GET', `ticketDms/allowedContentTypes`).respond(expectedResponse); - $httpBackend.expect('GET', `ticketDms/allowedContentTypes`); + const expectedResponse = ['application/pdf', 'image/png', 'image/jpg']; + $httpBackend.when('GET', `TravelThermographs/allowedContentTypes`).respond(expectedResponse); + $httpBackend.expect('GET', `TravelThermographs/allowedContentTypes`); controller.getAllowedContentTypes(); $httpBackend.flush(); expect(controller.allowedContentTypes).toBeDefined(); - expect(controller.allowedContentTypes).toEqual('image/png, image/jpg'); + expect(controller.allowedContentTypes).toEqual('application/pdf, image/png, image/jpg'); }); }); });