8032-devToTest_2440 #3009

Merged
alexm merged 262 commits from 8032-devToTest_2440 into test 2024-09-24 09:34:49 +00:00
10 changed files with 81 additions and 222 deletions
Showing only changes of commit 2568ac4e92 - Show all commits

View File

@ -3024,9 +3024,6 @@ INSERT INTO `vn`.`workerTimeControlMail` (`id`, `workerFk`, `year`, `week`, `sta
(3, 9, 2000, 51, 'CONFIRMED', util.VN_NOW(), 1, NULL), (3, 9, 2000, 51, 'CONFIRMED', util.VN_NOW(), 1, NULL),
(4, 9, 2001, 1, 'SENDED', util.VN_NOW(), 1, NULL); (4, 9, 2001, 1, 'SENDED', util.VN_NOW(), 1, NULL);
INSERT INTO `vn`.`wagonConfig` (`id`, `width`, `height`, `maxWagonHeight`, `minHeightBetweenTrays`, `maxTrays`)
VALUES
(1, 1350, 1900, 200, 50, 6);
INSERT INTO `vn`.`wagonTypeColor` (`id`, `name`, `rgb`) INSERT INTO `vn`.`wagonTypeColor` (`id`, `name`, `rgb`)
VALUES VALUES
@ -3035,15 +3032,19 @@ INSERT INTO `vn`.`wagonTypeColor` (`id`, `name`, `rgb`)
(3, 'green', '#00ff00'), (3, 'green', '#00ff00'),
(4, 'blue', '#0000ff'); (4, 'blue', '#0000ff');
INSERT INTO `vn`.`wagonConfig` (`id`, `width`, `height`, `maxWagonHeight`, `minHeightBetweenTrays`, `maxTrays`, `defaultTrayColorFk`)
VALUES
(1, 1350, 1900, 200, 50, 6, 1);
INSERT INTO `vn`.`wagonType` (`id`, `name`, `divisible`) INSERT INTO `vn`.`wagonType` (`id`, `name`, `divisible`)
VALUES VALUES
(1, 'Wagon Type #1', 1); (1, 'Wagon Type #1', 1);
INSERT INTO `vn`.`wagonTypeTray` (`id`, `typeFk`, `height`, `colorFk`) INSERT INTO `vn`.`wagonTypeTray` (`id`, `wagonTypeFk`, `height`, `wagonTypeColorFk`)
VALUES VALUES
(1, 1, 100, 1), (1, 1, 0, 1),
(2, 1, 50, 2), (2, 1, 50, 2),
(3, 1, 0, 3); (3, 1, 100, 3);
INSERT INTO `salix`.`accessTokenConfig` (`id`, `renewPeriod`, `courtesyTime`, `renewInterval`) INSERT INTO `salix`.`accessTokenConfig` (`id`, `renewPeriod`, `courtesyTime`, `renewInterval`)
VALUES VALUES

View File

@ -0,0 +1,26 @@
-- Place your SQL code here
DROP TABLE IF EXISTS vn.wagonTypeTray;
CREATE TABLE vn.wagonTypeTray (
id INT UNSIGNED auto_increment NULL,
wagonTypeFk int(11) unsigned NULL,
height INT UNSIGNED NULL,
wagonTypeColorFk int(11) unsigned NULL,
CONSTRAINT wagonTypeTray_pk PRIMARY KEY (id),
CONSTRAINT wagonTypeTray_wagonType_FK FOREIGN KEY (wagonTypeFk) REFERENCES vn.wagonType(id),
CONSTRAINT wagonTypeTray_wagonTypeColor_FK FOREIGN KEY (wagonTypeColorFk) REFERENCES vn.wagonTypeColor(id)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8mb3
COLLATE=utf8mb3_unicode_ci;
ALTER TABLE vn.wagonConfig ADD IF NOT EXISTS defaultHeight INT UNSIGNED DEFAULT 0 NULL COMMENT 'Default height in cm for a base tray';
ALTER TABLE vn.wagonConfig ADD IF NOT EXISTS defaultTrayColorFk int(11) unsigned NULL COMMENT 'Default color for a base tray';
ALTER TABLE vn.wagonConfig ADD CONSTRAINT wagonConfig_wagonTypeColor_FK FOREIGN KEY (defaultTrayColorFk) REFERENCES vn.wagonTypeColor(id);
ALTER TABLE vn.wagonTypeTray DROP FOREIGN KEY wagonTypeTray_wagonType_FK;
ALTER TABLE vn.wagonTypeTray ADD CONSTRAINT wagonTypeTray_wagonType_FK FOREIGN KEY (wagonTypeFk) REFERENCES vn.wagonType(id) ON DELETE CASCADE ON UPDATE RESTRICT;
-- insertar datos por defecto

View File

@ -1,57 +0,0 @@
module.exports = Self => {
Self.remoteMethodCtx('createWagonType', {
description: 'Creates a new wagon type',
accessType: 'WRITE',
accepts: [
{
arg: 'name',
type: 'String',
required: true
},
{
arg: 'divisible',
type: 'boolean',
required: true
}, {
arg: 'trays',
type: 'any',
required: true
}
],
http: {
path: `/createWagonType`,
verb: 'PATCH'
}
});
Self.createWagonType = async(ctx, options) => {
const args = ctx.args;
const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const newWagonType = await models.WagonType.create({name: args.name, divisible: args.divisible}, myOptions);
args.trays.forEach(async tray => {
await models.WagonTypeTray.create({
typeFk: newWagonType.id,
height: tray.position,
colorFk: tray.color.id
}, myOptions);
});
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};

View File

@ -1,43 +0,0 @@
module.exports = Self => {
Self.remoteMethodCtx('deleteWagonType', {
description: 'Deletes a wagon type',
accessType: 'WRITE',
accepts: [
{
arg: 'id',
type: 'Number',
required: true
}
],
http: {
path: `/deleteWagonType`,
verb: 'DELETE'
}
});
Self.deleteWagonType = async(ctx, options) => {
const args = ctx.args;
const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
await models.Wagon.destroyAll({typeFk: args.id}, myOptions);
await models.WagonTypeTray.destroyAll({typeFk: args.id}, myOptions);
await models.WagonType.destroyAll({id: args.id}, myOptions);
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};

View File

@ -1,64 +0,0 @@
module.exports = Self => {
Self.remoteMethodCtx('editWagonType', {
description: 'Edits a new wagon type',
accessType: 'WRITE',
accepts: [
{
arg: 'id',
type: 'String',
required: true
},
{
arg: 'name',
type: 'String',
required: true
},
{
arg: 'divisible',
type: 'boolean',
required: true
}, {
arg: 'trays',
type: 'any',
required: true
}
],
http: {
path: `/editWagonType`,
verb: 'PATCH'
}
});
Self.editWagonType = async(ctx, options) => {
const args = ctx.args;
const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const wagonType = await models.WagonType.findById(args.id, null, myOptions);
wagonType.updateAttributes({name: args.name, divisible: args.divisible}, myOptions);
models.WagonTypeTray.destroyAll({typeFk: args.id}, myOptions);
args.trays.forEach(async tray => {
await models.WagonTypeTray.create({
typeFk: args.id,
height: tray.position,
colorFk: tray.color.id
}, myOptions);
});
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};

View File

@ -1,58 +1,16 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
describe('WagonType crudWagonType()', () => { describe('WagonType crudWagonType()', () => {
const ctx = {
args: {
name: 'Mock wagon type',
divisible: true,
trays: [{position: 0, color: {id: 1}},
{position: 50, color: {id: 2}},
{position: 100, color: {id: 3}}]
}
};
it(`should create, edit and delete a new wagon type and its trays`, async() => { it(`should create, edit and delete a new wagon type and its trays`, async() => {
const tx = await models.WagonType.beginTransaction({}); const tx = await models.WagonType.beginTransaction({});
try { try {
const options = {transaction: tx}; const options = {transaction: tx};
// create const wagonType = await models.WagonType.create({name: 'Mock wagon type'}, options);
await models.WagonType.createWagonType(ctx, options); const newWagonTrays = await models.WagonTypeTray.findOne({where: {typeFk: wagonType.id}}, options);
const newWagonType = await models.WagonType.findOne({where: {name: ctx.args.name}}, options); expect(newWagonTrays).toBeDefined();
const newWagonTrays = await models.WagonTypeTray.find({where: {typeFk: newWagonType.id}}, options);
expect(newWagonType).not.toEqual(null);
expect(newWagonType.name).toEqual(ctx.args.name);
expect(newWagonType.divisible).toEqual(ctx.args.divisible);
expect(newWagonTrays.length).toEqual(ctx.args.trays.length);
ctx.args = {
id: newWagonType.id,
name: 'Edited wagon type',
divisible: false,
trays: [{position: 0, color: {id: 1}}]
};
// edit
await models.WagonType.editWagonType(ctx, options);
const editedWagonType = await models.WagonType.findById(newWagonType.id, null, options);
const editedWagonTrays = await models.WagonTypeTray.find({where: {typeFk: newWagonType.id}}, options);
expect(editedWagonType.name).toEqual(ctx.args.name);
expect(editedWagonType.divisible).toEqual(ctx.args.divisible);
expect(editedWagonTrays.length).toEqual(ctx.args.trays.length);
// delete
await models.WagonType.deleteWagonType(ctx, options);
const deletedWagonType = await models.WagonType.findById(newWagonType.id, null, options);
const deletedWagonTrays = await models.WagonTypeTray.find({where: {typeFk: newWagonType.id}}, options);
expect(deletedWagonType).toEqual(null);
expect(deletedWagonTrays).toEqual([]);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {

View File

@ -25,6 +25,19 @@
}, },
"maxTrays": { "maxTrays": {
"type": "number" "type": "number"
},
"defaultHeight": {
"type": "number"
},
"defaultTrayColorFk": {
"type": "number"
}
},
"relations": {
"WagonTypeColor": {
"type": "belongsTo",
"model": "WagonTypeColor",
"foreignKey": "defaultTrayColorFk"
} }
} }
} }

View File

@ -0,0 +1,16 @@
// module.exports = Self => {
// Self.observe('before save', async ctx => {
// if (ctx.isNewInstance) {
// const models = Self.app.models;
// const config = await models.WagonConfig.findOne();
// await models.WagonTypeTray.create({
// wagonTypeFk: config.wagonTypeFk,
// height: config.height,
// wagonTypeColorFk: config.wagonTypeColorFk
// }, ctx.options);
// }
// if (ctx.instance < config.minHeightBetweenTrays)
// throw new Error('Height must be greater than ' + config.minHeightBetweenTrays);
// });
// };

View File

@ -11,13 +11,13 @@
"id": true, "id": true,
"type": "number" "type": "number"
}, },
"typeFk": { "wagonTypeFk": {
"type": "number" "type": "number"
}, },
"height": { "height": {
"type": "number" "type": "number"
}, },
"colorFk": { "wagonTypeColorFk": {
"type": "number" "type": "number"
} }
}, },
@ -25,12 +25,12 @@
"type": { "type": {
"type": "belongsTo", "type": "belongsTo",
"model": "WagonType", "model": "WagonType",
"foreignKey": "typeFk" "foreignKey": "wagonTypeFk"
}, },
"color": { "color": {
"type": "belongsTo", "type": "belongsTo",
"model": "WagonTypeColor", "model": "WagonTypeColor",
"foreignKey": "colorFk" "foreignKey": "wagonTypeColorFk"
} }
} }
} }

View File

@ -1,5 +1,14 @@
module.exports = Self => { module.exports = Self => {
require('../methods/wagonType/createWagonType')(Self); Self.observe('after save', async ctx => {
require('../methods/wagonType/editWagonType')(Self); if (ctx.isNewInstance) {
require('../methods/wagonType/deleteWagonType')(Self); const models = Self.app.models;
const config = await models.WagonConfig.findOne();
await models.WagonTypeTray.create({
wagonTypeFk: ctx.instance.id,
height: config.defaultHeight,
wagonTypeColorFk: config.defaultTrayColorFk
}, ctx.options);
}
});
}; };