refs #5056 wagonType backs and test
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alexandre Riera 2023-03-21 10:44:00 +01:00
parent 1cd768f7b8
commit 1198ad3b01
8 changed files with 246 additions and 6 deletions

View File

@ -15,7 +15,7 @@ CREATE TABLE `vn`.`wagonTypeColor` (
CREATE TABLE `vn`.`wagonTypeTray` ( CREATE TABLE `vn`.`wagonTypeTray` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`typeFk` int(11) unsigned, `typeFk` int(11) unsigned,
`height` int(11) unsigned, `height` int(11) unsigned NOT NULL,
`colorFk` int(11) unsigned, `colorFk` int(11) unsigned,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
UNIQUE KEY (`typeFk`,`height`), UNIQUE KEY (`typeFk`,`height`),
@ -54,7 +54,8 @@ CREATE TABLE `vn`.`collectionWagonTicket` (
CONSTRAINT `collectionWagonTicket_tray` FOREIGN KEY (`trayFk`) REFERENCES `wagonTypeTray` (`id`) ON UPDATE CASCADE CONSTRAINT `collectionWagonTicket_tray` FOREIGN KEY (`trayFk`) REFERENCES `wagonTypeTray` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
ALTER TABLE `vn`.`wagon` ADD `typeFk` int(11) unsigned DEFAULT NULL; ALTER TABLE `vn`.`wagon` ADD `typeFk` int(11) unsigned NOT NULL;
ALTER TABLE `vn`.`wagon` ADD `label` int(11) unsigned NOT NULL;
ALTER TABLE `vn`.`wagon` ADD CONSTRAINT `wagon_type` FOREIGN KEY (`typeFk`) REFERENCES `wagonType` (`id`) ON UPDATE CASCADE; ALTER TABLE `vn`.`wagon` ADD CONSTRAINT `wagon_type` FOREIGN KEY (`typeFk`) REFERENCES `wagonType` (`id`) ON UPDATE CASCADE;
INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`)
@ -65,4 +66,7 @@ INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `pri
('WagonConfig', '*', '*', 'ALLOW', 'ROLE', 'employee'), ('WagonConfig', '*', '*', 'ALLOW', 'ROLE', 'employee'),
('CollectionWagon', '*', '*', 'ALLOW', 'ROLE', 'employee'), ('CollectionWagon', '*', '*', 'ALLOW', 'ROLE', 'employee'),
('CollectionWagonTicket', '*', '*', 'ALLOW', 'ROLE', 'employee'), ('CollectionWagonTicket', '*', '*', 'ALLOW', 'ROLE', 'employee'),
('Wagon', '*', '*', 'ALLOW', 'ROLE', 'employee'); ('Wagon', '*', '*', 'ALLOW', 'ROLE', 'employee'),
('WagonType', 'createWagonType', '*', 'ALLOW', 'ROLE', 'employee'),
('WagonType', 'deleteWagonType', '*', 'ALLOW', 'ROLE', 'employee'),
('WagonType', 'editWagonType', '*', 'ALLOW', 'ROLE', 'employee');

View File

@ -2838,8 +2838,9 @@ INSERT INTO `vn`.`wagonConfig` (`id`, `width`, `height`, `maxWagonHeight`, `minH
INSERT INTO `vn`.`wagonTypeColor` (`id`, `name`, `rgb`) INSERT INTO `vn`.`wagonTypeColor` (`id`, `name`, `rgb`)
VALUES VALUES
(1, 'red', '#ff0000'), (1, 'white', '#ffffff'),
(2, 'green', '#00ff00'), (2, 'red', '#ff0000'),
(3, 'blue', '#0000ff'); (3, 'green', '#00ff00'),
(4, 'blue', '#0000ff');

View File

@ -0,0 +1,57 @@
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

@ -0,0 +1,43 @@
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

@ -0,0 +1,64 @@
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

@ -0,0 +1,63 @@
const models = require('vn-loopback/server/server').models;
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() => {
const tx = await models.WagonType.beginTransaction({});
try {
const options = {transaction: tx};
// create
await models.WagonType.createWagonType(ctx, options);
const newWagonType = await models.WagonType.findOne({where: {name: ctx.args.name}}, options);
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();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -0,0 +1,5 @@
module.exports = Self => {
require('../methods/wagonType/createWagonType')(Self);
require('../methods/wagonType/editWagonType')(Self);
require('../methods/wagonType/deleteWagonType')(Self);
};

View File

@ -11,6 +11,9 @@
"id": true, "id": true,
"type": "number" "type": "number"
}, },
"label": {
"type": "number"
},
"volume": { "volume": {
"type": "number" "type": "number"
}, },