now we can create thermographs for travelThermograph
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2020-05-27 13:11:41 +02:00
parent 98157fc45a
commit 2345a922c8
18 changed files with 353 additions and 6 deletions

View File

@ -0,0 +1 @@
UPDATE `salix`.`ACL` SET `accessType`='WRITE' WHERE `id`='213';

View File

@ -0,0 +1,55 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
/**
* Returns a set of allowed values defined on table scheme
* @param {String} column - Model or Table column name
* @return {Array} - Array of set values
*/
Self.getEnumValues = async function(column) {
let model = this.app.models[this.modelName].definition;
let properties = model.properties;
let tableName = this.modelName;
let schema = null;
if (model.settings && model.settings.mysql) {
let tableSplit = model.settings.mysql.table.split('.');
tableName = tableSplit.pop();
schema = tableSplit.pop() || null;
}
let property = properties[column];
if (!property)
throw new UserError(`Column does not exist`);
let columnName = property.mysql
? property.mysql.columnName
: column;
let columnInfo = await this.rawSql(
`SELECT column_type columnType
FROM information_schema.columns
WHERE table_name = ?
AND table_schema = IFNULL(?, DATABASE())
AND column_name = ?`,
[tableName, schema, columnName]
);
if (!columnInfo || !columnInfo[0])
throw new UserError(`Cannot fetch column values`);
let setValues;
setValues = columnInfo[0].columnType
.replace(/^enum\((.*)\)$/i, '$1')
.replace(/'/g, '')
.match(new RegExp(/(\w+)+/, 'ig'));
let values = [];
setValues.forEach(setValue => {
values.push({value: setValue});
});
return values;
};
};

View File

@ -0,0 +1,18 @@
const app = require('vn-loopback/server/server');
describe('Model getEnumValues()', () => {
it('should extend getEnumValues properties to any model passed', () => {
let exampleModel = app.models.TravelThermograph;
expect(exampleModel.getEnumValues).toBeDefined();
});
it('should return an array of enum values from a given column', async() => {
let result = await app.models.TravelThermograph.getSetValues('temperature');
expect(result.length).toEqual(3);
expect(result[0].value).toEqual('enum');
expect(result[1].value).toEqual('COOL');
expect(result[2].value).toEqual('WARM');
});
});

View File

@ -6,6 +6,7 @@ module.exports = function(Self) {
Self.ParameterizedSQL = ParameterizedSQL;
require('../methods/vn-model/getSetValues')(Self);
require('../methods/vn-model/getEnumValues')(Self);
Object.assign(Self, {
setup() {

View File

@ -132,5 +132,5 @@
"Distance must be lesser than 1000": "La distancia debe ser inferior a 1000",
"This ticket is deleted": "Este ticket está eliminado",
"A travel with this data already exists": "Ya existe un travel con estos datos",
"AMOUNT_NOT_MATCH_GROUPING": "AMOUNT_NOT_MATCH_GROUPING"
"This thermograph id already exists": "La id del termógrafo ya existe"
}

View File

@ -0,0 +1,60 @@
module.exports = Self => {
Self.remoteMethod('createThermograph', {
description: 'Creates a new thermograph',
accessType: 'WRITE',
accepts: [{
arg: 'thermographId',
type: 'String',
description: 'The thermograph id',
required: true
}, {
arg: 'model',
type: 'String',
description: 'The thermograph model',
required: true
}, {
arg: 'temperature',
type: 'String',
description: 'The thermograph temperature',
required: true
}, {
arg: 'warehouseId',
type: 'Number',
description: 'The warehouse id',
required: true
}],
returns: {
type: 'Object',
root: true
},
http: {
path: `/createThermograph`,
verb: 'POST'
}
});
Self.createThermograph = async(thermographId, model, temperature, warehouseId) => {
const models = Self.app.models;
const tx = await Self.beginTransaction({});
try {
const options = {transaction: tx};
const thermograph = await models.Thermograph.create({
id: thermographId,
model: model
}, options);
await Self.rawSql(`
INSERT INTO travelThermograph(thermographFk, warehouseFk, temperature, created)
VALUES (?, ?,?, NOW())
`, [thermograph.id, warehouseId, temperature], options);
await tx.commit();
return thermograph;
} catch (err) {
await tx.rollback();
throw err;
}
};
};

View File

@ -0,0 +1,18 @@
module.exports = Self => {
Self.remoteMethod('getThermographModels', {
description: 'Gets the thermograph models',
accessType: 'READ',
returns: {
type: ['String'],
root: true
},
http: {
path: `/getThermographModels`,
verb: 'GET'
}
});
Self.getThermographModels = async() => {
return Self.getEnumValues('model');
};
};

View File

@ -0,0 +1,49 @@
const app = require('vn-loopback/server/server');
describe('Termograph createThermograph()', () => {
const models = app.models;
const thermographId = '99999-1';
const model = 'DISPOSABLE';
const temperature = 'COOL';
const warehouseId = 1;
let createdThermograph;
afterAll(async done => {
let travelThermograpToDelete = await models.TravelThermograph.findOne({where: {thermographFk: createdThermograph.id}});
let thermograpToDelete = await models.Thermograph.findById(createdThermograph.id);
await travelThermograpToDelete.destroy();
await thermograpToDelete.destroy();
done();
});
it(`should create a thermograph which is saved in both thermograph and travelThermograph`, async() => {
let createdTravelThermograpth = await models.TravelThermograph.findOne({where: {thermographFk: thermographId}});
expect(createdTravelThermograpth).toBeNull();
createdThermograph = await models.Thermograph.createThermograph(thermographId, model, temperature, warehouseId);
expect(createdThermograph.id).toEqual(thermographId);
expect(createdThermograph.model).toEqual(model);
createdTravelThermograpth = await models.TravelThermograph.findOne({where: {thermographFk: thermographId}});
expect(createdTravelThermograpth.warehouseFk).toEqual(warehouseId);
expect(createdTravelThermograpth.temperature).toEqual(temperature);
});
it(`should not be able to created duplicated entries`, async() => {
let error;
try {
await models.Thermograph.createThermograph(thermographId, model, temperature, warehouseId);
} catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error.message).toBe('This thermograph id already exists');
});
});

View File

@ -0,0 +1,18 @@
module.exports = Self => {
Self.remoteMethod('getThermographTemperatures', {
description: 'Gets the thermograph temperatures',
accessType: 'READ',
returns: {
type: ['String'],
root: true
},
http: {
path: `/getThermographTemperatures`,
verb: 'GET'
}
});
Self.getThermographTemperatures = async() => {
return Self.getEnumValues('temperature');
};
};

View File

@ -0,0 +1,12 @@
let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
require('../methods/thermograph/createThermograph')(Self);
require('../methods/thermograph/getThermographModels')(Self);
Self.rewriteDbError(function(err) {
if (err.code === 'ER_DUP_ENTRY')
return new UserError(`This thermograph id already exists`);
return err;
});
};

View File

@ -10,10 +10,12 @@
"id": {
"type": "String",
"id": true,
"description": "Identifier"
"description": "Identifier",
"required": true
},
"model": {
"type": "String"
"type": "String",
"required": true
}
}
}

View File

@ -1,4 +1,5 @@
module.exports = Self => {
require('../methods/travel-thermograph/allowedContentTypes')(Self);
require('../methods/travel-thermograph/getThermographTemperatures')(Self);
};

View File

@ -21,10 +21,15 @@
"type": "Date"
},
"temperature": {
"type": "String"
"type": "String",
"required": true
},
"result": {
"type": "String"
},
"warehouseFk": {
"type": "Number",
"required": true
}
},
"relations": {

View File

@ -17,6 +17,18 @@
where="{travelFk: null}"
show-field="thermographFk"
value-field="thermographFk">
<tpl-item>
{{thermographFk}}
</tpl-item>
<append>
<vn-icon-button
icon="add_circle"
vn-tooltip="New thermograph"
ng-click="$ctrl.onAddThermographClick($event)"
vn-acl="buyer"
vn-acl-action="remove">
</vn-icon-button>
</append>
</vn-autocomplete>
<vn-textfield vn-one
label="State"
@ -84,3 +96,53 @@
</vn-button-bar>
</div>
</form>
<!-- Create thermograph dialog -->
<vn-dialog class="edit"
vn-id="newThermographDialog"
on-accept="$ctrl.onNewThermographAccept()"
message="New thermograph">
<tpl-body>
<vn-horizontal>
<vn-textfield
vn-one
required="true"
label="Identifier"
ng-model="$ctrl.newThermograph.thermographId"
vn-focus>
</vn-textfield>
<vn-autocomplete
vn-one
required="true"
label="Model"
ng-model="$ctrl.newThermograph.model"
url="Thermographs/getThermographModels"
show-field="value"
value-field="value">
</vn-autocomplete>
</vn-horizontal>
<vn-horizontal>
<vn-autocomplete
vn-one
required="true"
label="Warehouse"
ng-model="$ctrl.newThermograph.warehouseId"
url="Warehouses"
show-field="name"
value-field="id">
</vn-autocomplete>
<vn-autocomplete
vn-one
required="true"
label="Temperature"
ng-model="$ctrl.newThermograph.temperature"
url="TravelThermographs/getThermographTemperatures"
show-field="value"
value-field="value">
</vn-autocomplete>
</vn-horizontal>
</tpl-body>
<tpl-buttons>
<input type="button" response="cancel" translate-attr="{value: 'Cancel'}"/>
<button response="accept" translate>Create</button>
</tpl-buttons>
</vn-dialog>

View File

@ -55,6 +55,24 @@ class Controller extends Section {
});
}
onAddThermographClick(event) {
this.thermographModel = 'DISPOSABLE';
this.temperature = 'COOL';
event.preventDefault();
this.newThermograph = {
thermographId: this.thermographId,
warehouseId: this.warehouseId,
temperature: this.temperature,
model: this.thermographModel
};
this.$.newThermographDialog.show();
}
onNewThermographAccept() {
return this.$http.post(`Thermographs/createThermograph`, this.newThermograph)
.then(res => this.dms.thermographId = res.data.id);
}
onSubmit() {
const query = `Travels/${this.travel.id}/createThermograph`;
const options = {

View File

@ -63,5 +63,30 @@ describe('Ticket', () => {
expect(controller.allowedContentTypes).toEqual('application/pdf, image/png, image/jpg');
});
});
describe('onAddThermographClick()', () => {
it('should call the show() function of the create thermograph dialog', () => {
controller.$.newThermographDialog = {show: () => {}};
jest.spyOn(controller.$.newThermographDialog, 'show');
const event = new Event('click');
jest.spyOn(event, 'preventDefault');
controller.onAddThermographClick(event);
expect(event.preventDefault).toHaveBeenCalledTimes(1);
expect(controller.$.newThermographDialog.show).toHaveBeenCalledTimes(1);
});
});
describe('onNewThermographAccept()', () => {
it('should set the created thermograph id on to the controller for the autocomplete to use it', () => {
const response = {id: 'the created id'};
$httpBackend.when('POST', `Thermographs/createThermograph`).respond(response);
controller.onNewThermographAccept();
$httpBackend.flush();
expect(controller.dms.thermographId).toEqual(response.id);
});
});
});
});

View File

@ -15,4 +15,6 @@ Add thermograph: Añadir termógrafo
Edit thermograph: Editar termógrafo
Thermograph deleted: Termógrafo eliminado
Thermograph: Termógrafo
New thermograph: Nuevo termógrafo
Are you sure you want to remove the thermograph?: ¿Seguro que quieres quitar el termógrafo?
Identifier: Identificador