transactions for entry module endpoints
This commit is contained in:
parent
e1c1bb8425
commit
b38605442e
|
@ -89,7 +89,7 @@ module.exports = Self => {
|
|||
|
||||
const newBuy = await models.Buy.create(ctx.args, myOptions);
|
||||
|
||||
let filter = {
|
||||
const filter = {
|
||||
fields: [
|
||||
'id',
|
||||
'itemFk',
|
||||
|
@ -136,7 +136,7 @@ module.exports = Self => {
|
|||
}
|
||||
};
|
||||
|
||||
let stmts = [];
|
||||
const stmts = [];
|
||||
let stmt;
|
||||
|
||||
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.buyRecalc');
|
||||
|
|
|
@ -32,7 +32,7 @@ module.exports = Self => {
|
|||
}
|
||||
|
||||
try {
|
||||
let promises = [];
|
||||
const promises = [];
|
||||
for (let buy of ctx.args.buys) {
|
||||
const buysToDelete = models.Buy.destroyById(buy.id, myOptions);
|
||||
promises.push(buysToDelete);
|
||||
|
|
|
@ -30,7 +30,18 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.editLatestBuys = async(field, newValue, lines) => {
|
||||
Self.editLatestBuys = async(field, newValue, lines, options) => {
|
||||
let tx;
|
||||
let myOptions = {};
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
let modelName;
|
||||
let identifier;
|
||||
|
||||
|
@ -60,28 +71,27 @@ module.exports = Self => {
|
|||
const models = Self.app.models;
|
||||
const model = models[modelName];
|
||||
|
||||
let tx = await model.beginTransaction({});
|
||||
|
||||
try {
|
||||
let promises = [];
|
||||
let options = {transaction: tx};
|
||||
|
||||
let targets = lines.map(line => {
|
||||
const targets = lines.map(line => {
|
||||
return line[identifier];
|
||||
});
|
||||
|
||||
let value = {};
|
||||
const value = {};
|
||||
value[field] = newValue;
|
||||
|
||||
// intentarlo con updateAll
|
||||
for (let target of targets)
|
||||
promises.push(model.upsertWithWhere({id: target}, value, options));
|
||||
promises.push(model.upsertWithWhere({id: target}, value, myOptions));
|
||||
|
||||
await Promise.all(promises);
|
||||
await tx.commit();
|
||||
} catch (error) {
|
||||
await tx.rollback();
|
||||
throw error;
|
||||
const result = await Promise.all(promises, myOptions);
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return result;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
@ -13,71 +13,85 @@ module.exports = Self => {
|
|||
type: 'object',
|
||||
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string',
|
||||
http: {source: 'query'}
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'search',
|
||||
type: 'string',
|
||||
description: 'Searchs the entry by id',
|
||||
http: {source: 'query'}
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'id',
|
||||
type: 'integer',
|
||||
description: 'The entry id',
|
||||
http: {source: 'query'}
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'created',
|
||||
type: 'date',
|
||||
description: 'The created date to filter',
|
||||
http: {source: 'query'}
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'travelFk',
|
||||
type: 'number',
|
||||
description: 'The travel id to filter',
|
||||
http: {source: 'query'}
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'companyFk',
|
||||
type: 'number',
|
||||
description: 'The company to filter',
|
||||
http: {source: 'query'}
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'isBooked',
|
||||
type: 'boolean',
|
||||
description: 'The isBokked filter',
|
||||
http: {source: 'query'}
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'isConfirmed',
|
||||
type: 'boolean',
|
||||
description: 'The isConfirmed filter',
|
||||
http: {source: 'query'}
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'isOrdered',
|
||||
type: 'boolean',
|
||||
description: 'The isOrdered filter',
|
||||
http: {source: 'query'}
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'ref',
|
||||
type: 'string',
|
||||
description: 'The ref filter',
|
||||
http: {source: 'query'}
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'supplierFk',
|
||||
type: 'number',
|
||||
description: 'The supplier id to filter',
|
||||
http: {source: 'query'}
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'invoiceInFk',
|
||||
type: 'number',
|
||||
description: 'The invoiceIn id to filter',
|
||||
http: {source: 'query'}
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'currencyFk',
|
||||
type: 'number',
|
||||
description: 'The currency id to filter',
|
||||
http: {source: 'query'}
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'from',
|
||||
type: 'date',
|
||||
description: `The from date filter`
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'to',
|
||||
type: 'date',
|
||||
description: `The to date filter`
|
||||
|
@ -93,9 +107,14 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.filter = async(ctx, filter) => {
|
||||
let conn = Self.dataSource.connector;
|
||||
let where = buildFilter(ctx.args, (param, value) => {
|
||||
Self.filter = async(ctx, filter, options) => {
|
||||
let myOptions = {};
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
const conn = Self.dataSource.connector;
|
||||
const where = buildFilter(ctx.args, (param, value) => {
|
||||
switch (param) {
|
||||
case 'search':
|
||||
return /^\d+$/.test(value)
|
||||
|
@ -128,7 +147,7 @@ module.exports = Self => {
|
|||
});
|
||||
filter = mergeFilters(ctx.args.filter, {where});
|
||||
|
||||
let stmts = [];
|
||||
const stmts = [];
|
||||
let stmt;
|
||||
stmt = new ParameterizedSQL(
|
||||
`SELECT
|
||||
|
@ -163,10 +182,10 @@ module.exports = Self => {
|
|||
);
|
||||
|
||||
stmt.merge(conn.makeSuffix(filter));
|
||||
let itemsIndex = stmts.push(stmt) - 1;
|
||||
const itemsIndex = stmts.push(stmt) - 1;
|
||||
|
||||
let sql = ParameterizedSQL.join(stmts, ';');
|
||||
let result = await conn.executeStmt(sql);
|
||||
const sql = ParameterizedSQL.join(stmts, ';');
|
||||
const result = await conn.executeStmt(sql, myOptions);
|
||||
return itemsIndex === 0 ? result : result[itemsIndex];
|
||||
};
|
||||
};
|
||||
|
|
|
@ -19,8 +19,14 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.getBuys = async id => {
|
||||
let filter = {
|
||||
Self.getBuys = async(id, options) => {
|
||||
const models = Self.app.models;
|
||||
let myOptions = {};
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
const filter = {
|
||||
where: {entryFk: id},
|
||||
fields: [
|
||||
'id',
|
||||
|
@ -68,7 +74,6 @@ module.exports = Self => {
|
|||
}
|
||||
};
|
||||
|
||||
let buys = await Self.app.models.Buy.find(filter);
|
||||
return buys;
|
||||
return models.Buy.find(filter, myOptions);
|
||||
};
|
||||
};
|
||||
|
|
|
@ -19,8 +19,14 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.getEntry = async id => {
|
||||
let filter = {
|
||||
Self.getEntry = async(id, options) => {
|
||||
const models = Self.app.models;
|
||||
let myOptions = {};
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
const filter = {
|
||||
where: {id: id},
|
||||
include: [
|
||||
{
|
||||
|
@ -70,7 +76,6 @@ module.exports = Self => {
|
|||
],
|
||||
};
|
||||
|
||||
let entry = await Self.app.models.Entry.findOne(filter);
|
||||
return entry;
|
||||
return models.Entry.findOne(filter, myOptions);
|
||||
};
|
||||
};
|
||||
|
|
|
@ -45,8 +45,8 @@ module.exports = Self => {
|
|||
const conn = Self.dataSource.connector;
|
||||
const args = ctx.args;
|
||||
const models = Self.app.models;
|
||||
|
||||
let tx;
|
||||
|
||||
if (!options.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
options.transaction = tx;
|
||||
|
@ -76,7 +76,7 @@ module.exports = Self => {
|
|||
const createdBuys = await models.Buy.create(buys, options);
|
||||
const buyIds = createdBuys.map(buy => buy.id);
|
||||
|
||||
let stmts = [];
|
||||
const stmts = [];
|
||||
let stmt;
|
||||
|
||||
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.buyRecalc');
|
||||
|
|
|
@ -24,14 +24,19 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.importBuysPreview = async(id, buys) => {
|
||||
Self.importBuysPreview = async(id, buys, options) => {
|
||||
const models = Self.app.models;
|
||||
let myOptions = {};
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
for (let buy of buys) {
|
||||
const packaging = await models.Packaging.findOne({
|
||||
fields: ['id'],
|
||||
where: {volume: {gte: buy.volume}},
|
||||
order: 'volume ASC'
|
||||
});
|
||||
}, myOptions);
|
||||
buy.packageFk = packaging.id;
|
||||
}
|
||||
|
||||
|
|
|
@ -17,36 +17,44 @@ module.exports = Self => {
|
|||
arg: 'search',
|
||||
type: 'String',
|
||||
description: `If it's and integer searchs by id, otherwise it searchs by name`,
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'id',
|
||||
type: 'Integer',
|
||||
description: 'Item id',
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'tags',
|
||||
type: ['Object'],
|
||||
description: 'List of tags to filter with',
|
||||
http: {source: 'query'}
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'description',
|
||||
type: 'String',
|
||||
description: 'The item description',
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'salesPersonFk',
|
||||
type: 'Integer',
|
||||
description: 'The buyer of the item',
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'active',
|
||||
type: 'Boolean',
|
||||
description: 'Whether the item is or not active',
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'visible',
|
||||
type: 'Boolean',
|
||||
description: 'Whether the item is or not visible',
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'typeFk',
|
||||
type: 'Integer',
|
||||
description: 'Type id',
|
||||
}, {
|
||||
},
|
||||
{
|
||||
arg: 'categoryFk',
|
||||
type: 'Integer',
|
||||
description: 'Category id',
|
||||
|
@ -62,9 +70,14 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.latestBuysFilter = async(ctx, filter) => {
|
||||
let conn = Self.dataSource.connector;
|
||||
let where = buildFilter(ctx.args, (param, value) => {
|
||||
Self.latestBuysFilter = async(ctx, filter, options) => {
|
||||
let myOptions = {};
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
const conn = Self.dataSource.connector;
|
||||
const where = buildFilter(ctx.args, (param, value) => {
|
||||
switch (param) {
|
||||
case 'search':
|
||||
return /^\d+$/.test(value)
|
||||
|
@ -93,7 +106,7 @@ module.exports = Self => {
|
|||
});
|
||||
filter = mergeFilters(ctx.args.filter, {where});
|
||||
|
||||
let stmts = [];
|
||||
const stmts = [];
|
||||
let stmt;
|
||||
|
||||
stmts.push('CALL cache.last_buy_refresh(FALSE)');
|
||||
|
@ -179,10 +192,10 @@ module.exports = Self => {
|
|||
}
|
||||
|
||||
stmt.merge(conn.makeSuffix(filter));
|
||||
let buysIndex = stmts.push(stmt) - 1;
|
||||
const buysIndex = stmts.push(stmt) - 1;
|
||||
|
||||
let sql = ParameterizedSQL.join(stmts, ';');
|
||||
let result = await conn.executeStmt(sql);
|
||||
const sql = ParameterizedSQL.join(stmts, ';');
|
||||
const result = await conn.executeStmt(sql, myOptions);
|
||||
return buysIndex === 0 ? result : result[buysIndex];
|
||||
};
|
||||
};
|
||||
|
|
|
@ -3,29 +3,32 @@ const model = app.models.Buy;
|
|||
|
||||
describe('Buy editLatestsBuys()', () => {
|
||||
it('should change the value of a given column for the selected buys', async() => {
|
||||
const tx = await app.models.Entry.beginTransaction({});
|
||||
const options = {transaction: tx};
|
||||
|
||||
try {
|
||||
let ctx = {
|
||||
args: {
|
||||
search: 'Ranged weapon longbow 2m'
|
||||
}
|
||||
};
|
||||
|
||||
let [original] = await model.latestBuysFilter(ctx);
|
||||
const [original] = await model.latestBuysFilter(ctx, null, options);
|
||||
|
||||
const field = 'size';
|
||||
let newValue = 99;
|
||||
const newValue = 99;
|
||||
const lines = [{itemFk: original.itemFk, id: original.id}];
|
||||
|
||||
await model.editLatestBuys(field, newValue, lines);
|
||||
await model.editLatestBuys(field, newValue, lines, options);
|
||||
|
||||
let [result] = await model.latestBuysFilter(ctx);
|
||||
const [result] = await model.latestBuysFilter(ctx, null, options);
|
||||
|
||||
expect(result.size).toEqual(99);
|
||||
expect(result[field]).toEqual(newValue);
|
||||
|
||||
newValue = original.size;
|
||||
await model.editLatestBuys(field, newValue, lines);
|
||||
|
||||
let [restoredFixture] = await model.latestBuysFilter(ctx);
|
||||
|
||||
expect(restoredFixture.size).toEqual(original.size);
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue