VnModel.crud code impruved and bugs/tests fixed
This commit is contained in:
parent
c276e50beb
commit
79bb359051
|
@ -0,0 +1,47 @@
|
|||
const app = require('../../../../../item/server/server');
|
||||
|
||||
fdescribe('Model crud()', () => {
|
||||
let insertId;
|
||||
let ItemBarcode = app.models.ItemBarcode;
|
||||
|
||||
it('should inherit crud method from VnModel', () => {
|
||||
expect(ItemBarcode.crud).toBeDefined();
|
||||
});
|
||||
|
||||
it('should create a new instance', async() => {
|
||||
let data = {code: '500', itemFk: '1'};
|
||||
|
||||
crudObject = {
|
||||
create: [data]
|
||||
};
|
||||
await ItemBarcode.crud(crudObject);
|
||||
let instance = await ItemBarcode.findOne({where: data});
|
||||
insertId = instance.id;
|
||||
|
||||
expect(instance).not.toEqual(null);
|
||||
expect(instance.code).toEqual('500');
|
||||
});
|
||||
|
||||
it('should update the instance', async() => {
|
||||
crudObject = {
|
||||
update: [{
|
||||
where: {id: insertId},
|
||||
data: {code: '501', itemFk: 1}
|
||||
}]
|
||||
};
|
||||
await ItemBarcode.crud(crudObject);
|
||||
let instance = await ItemBarcode.findById(insertId);
|
||||
|
||||
expect(instance.code).toEqual('501');
|
||||
});
|
||||
|
||||
it('should delete the created instance', async() => {
|
||||
crudObject = {
|
||||
delete: [insertId]
|
||||
};
|
||||
await ItemBarcode.crud(crudObject);
|
||||
let instance = await ItemBarcode.findById(insertId);
|
||||
|
||||
expect(instance).toEqual(null);
|
||||
});
|
||||
});
|
|
@ -1,60 +0,0 @@
|
|||
const app = require('../../../../../item/server/server');
|
||||
|
||||
describe('Model installCrudModel()', () => {
|
||||
it('should extend installCrudModel properties to any model passed', () => {
|
||||
let exampleModel = app.models.ItemBarcode;
|
||||
|
||||
expect(exampleModel.installCrudModel).toBeDefined();
|
||||
});
|
||||
|
||||
describe('installCrudModel()', () => {
|
||||
it('should create a new remothed method', () => {
|
||||
let exampleModel = app.models.ItemBarcode;
|
||||
exampleModel.installCrudModel('crudItemBarcodes');
|
||||
|
||||
expect(exampleModel.crudItemBarcodes).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('ItemBarcode crudMethod()', () => {
|
||||
let createdId;
|
||||
it('should create a new barcode', async() => {
|
||||
crudObject = {
|
||||
create: [{code: '500', itemFk: '1'}],
|
||||
update: [],
|
||||
delete: []
|
||||
};
|
||||
await app.models.ItemBarcode.crudItemBarcodes(crudObject);
|
||||
let result = await app.models.ItemBarcode.find({where: {itemFk: 1}});
|
||||
createdId = result[3].id;
|
||||
|
||||
expect(result[3].code).toEqual('500');
|
||||
expect(result.length).toEqual(4);
|
||||
});
|
||||
|
||||
it('should update a barcode', async() => {
|
||||
crudObject = {
|
||||
create: [],
|
||||
update: [{id: createdId, code: '501', itemFk: 1}],
|
||||
delete: []
|
||||
};
|
||||
await app.models.ItemBarcode.crudItemBarcodes(crudObject);
|
||||
let result = await app.models.ItemBarcode.find({where: {itemFk: 1}});
|
||||
|
||||
expect(result[3].code).toEqual('501');
|
||||
expect(result.length).toEqual(4);
|
||||
});
|
||||
|
||||
it('should delete a barcode', async() => {
|
||||
crudObject = {
|
||||
create: [],
|
||||
update: [],
|
||||
delete: [createdId]
|
||||
};
|
||||
await app.models.ItemBarcode.crudItemBarcodes(crudObject);
|
||||
let result = await app.models.ItemBarcode.find({where: {itemFk: 1}});
|
||||
|
||||
expect(result.length).toEqual(3);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -29,7 +29,24 @@ module.exports = function(Self) {
|
|||
// this.disableRemoteMethod(method, disableMethods[method]);
|
||||
}
|
||||
*/
|
||||
this.installCrudModel('crud');
|
||||
|
||||
this.remoteMethod('crud', {
|
||||
description: 'Create, update or/and delete instances from model with a single request',
|
||||
accessType: 'WRITE',
|
||||
accepts: [
|
||||
{
|
||||
arg: 'actions',
|
||||
type: 'Object',
|
||||
require: true,
|
||||
description: 'Instances to update, example: {create: [instances], update: [instances], delete: [ids]}',
|
||||
http: {source: 'body'}
|
||||
}
|
||||
],
|
||||
http: {
|
||||
path: `/crud`,
|
||||
verb: 'POST'
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Self.defineScope = function(serverFilter) {
|
||||
|
@ -123,56 +140,37 @@ module.exports = function(Self) {
|
|||
};
|
||||
};
|
||||
|
||||
Self.installCrudModel = function(methodName) {
|
||||
this.remoteMethod(methodName, {
|
||||
description: 'Create, update or/and delete instances from model in a single request',
|
||||
accessType: 'WRITE',
|
||||
accepts: [
|
||||
{
|
||||
arg: 'actions',
|
||||
type: 'Object',
|
||||
require: true,
|
||||
description: 'Instances to update, example: {create: [instances], update: [instances], delete: [ids]}',
|
||||
http: {source: 'body'}
|
||||
}
|
||||
],
|
||||
http: {
|
||||
path: `/${methodName}`,
|
||||
verb: 'POST'
|
||||
}
|
||||
});
|
||||
this[methodName] = async actions => {
|
||||
let transaction = await this.beginTransaction({});
|
||||
let options = {transaction: transaction};
|
||||
Self.crud = async function(actions) {
|
||||
let transaction = await this.beginTransaction({});
|
||||
let options = {transaction: transaction};
|
||||
|
||||
try {
|
||||
if (actions.delete && actions.delete.length) {
|
||||
await this.destroyAll({id: {inq: actions.delete}}, options);
|
||||
}
|
||||
if (actions.update) {
|
||||
try {
|
||||
let promises = [];
|
||||
actions.update.forEach(toUpdate => {
|
||||
promises.push(this.upsertWithWhere(toUpdate.where, toUpdate.data, options));
|
||||
});
|
||||
await Promise.all(promises);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
if (actions.create && actions.create.length) {
|
||||
try {
|
||||
await this.create(actions.create, options);
|
||||
} catch (error) {
|
||||
throw error[error.length - 1];
|
||||
}
|
||||
}
|
||||
await transaction.commit();
|
||||
} catch (error) {
|
||||
await transaction.rollback();
|
||||
throw error;
|
||||
try {
|
||||
if (actions.delete && actions.delete.length) {
|
||||
await this.destroyAll({id: {inq: actions.delete}}, options);
|
||||
}
|
||||
};
|
||||
if (actions.update) {
|
||||
try {
|
||||
let promises = [];
|
||||
actions.update.forEach(toUpdate => {
|
||||
promises.push(this.upsertWithWhere(toUpdate.where, toUpdate.data, options));
|
||||
});
|
||||
await Promise.all(promises);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
if (actions.create && actions.create.length) {
|
||||
try {
|
||||
await this.create(actions.create, options);
|
||||
} catch (error) {
|
||||
throw error[error.length - 1];
|
||||
}
|
||||
}
|
||||
await transaction.commit();
|
||||
} catch (error) {
|
||||
await transaction.rollback();
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue