refactor(order): catalogFilter, confirm and filter transactions

This commit is contained in:
Carlos Jimenez Ruiz 2021-10-25 13:13:05 +02:00
parent b055618577
commit f62814e81e
4 changed files with 136 additions and 81 deletions

View File

@ -7,28 +7,28 @@ module.exports = Self => {
accepts: [
{
arg: 'orderFk',
type: 'Number',
type: 'number',
required: true
},
{
arg: 'orderBy',
type: 'Object',
type: 'object',
description: 'Items order',
required: true
},
{
arg: 'filter',
type: 'Object',
type: 'object',
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string'
},
{
arg: 'tagGroups',
type: ['Object'],
type: ['object'],
description: 'Filter by tag'
},
],
returns: {
type: ['Object'],
type: ['object'],
root: true,
},
http: {
@ -37,8 +37,13 @@ module.exports = Self => {
},
});
Self.catalogFilter = async(orderFk, orderBy, filter, tagGroups) => {
let conn = Self.dataSource.connector;
Self.catalogFilter = async(orderFk, orderBy, filter, tagGroups, options) => {
const conn = Self.dataSource.connector;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const stmts = [];
let stmt;
@ -85,7 +90,7 @@ module.exports = Self => {
stmts.push(stmt);
// Calculate items
const order = await Self.findById(orderFk);
const order = await Self.findById(orderFk, null, myOptions);
stmts.push(new ParameterizedSQL(
'CALL vn.catalog_calculate(?, ?, ?)', [
order.landed,
@ -131,9 +136,9 @@ module.exports = Self => {
params: [orderBy.field],
});
let way = orderBy.way == 'DESC' ? 'DESC' : 'ASC';
let tag = await Self.app.models.Tag.findById(orderBy.field);
let orderSql = `
const way = orderBy.way == 'DESC' ? 'DESC' : 'ASC';
const tag = await Self.app.models.Tag.findById(orderBy.field, null, myOptions);
const orderSql = `
ORDER BY
itg.value IS NULL,
${tag.isQuantitative ? 'CAST(itg.value AS SIGNED)' : 'itg.value'}
@ -181,7 +186,7 @@ module.exports = Self => {
stmts.push('CALL vn.ticketCalculatePurge()');
const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql);
const result = await conn.executeStmt(sql, myOptions);
// Add prices to items
result[itemsIndex].forEach(item => {

View File

@ -19,9 +19,29 @@ module.exports = Self => {
}
});
Self.confirm = async(ctx, orderFk) => {
Self.confirm = async(ctx, orderFk, option) => {
const userId = ctx.req.accessToken.userId;
let query = `CALL hedera.order_confirmWithUser(?, ?)`;
return await Self.rawSql(query, [orderFk, userId]);
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const query = `CALL hedera.order_confirmWithUser(?, ?)`;
const response = await Self.rawSql(query, [orderFk, userId], myOptions);
if (tx) await tx.commit();
return response;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};

View File

@ -9,60 +9,60 @@ module.exports = Self => {
accepts: [
{
arg: 'ctx',
type: 'Object',
type: 'object',
http: {source: 'context'}
}, {
arg: 'filter',
type: 'Object',
description: `Filter defining where, order, offset, and limit - must be a JSON-encoded string`
type: 'object',
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string'
}, {
arg: 'search',
type: 'String',
type: 'string',
description: `If it's and integer searchs by id, otherwise it searchs by nickname`
}, {
arg: 'from',
type: 'Date',
description: `The from date`
type: 'date',
description: 'The from date'
}, {
arg: 'to',
type: 'Date',
description: `The to date`
type: 'date',
description: 'The to date'
}, {
arg: 'id',
type: 'Integer',
description: `The ticket id`
type: 'integer',
description: 'The ticket id'
}, {
arg: 'clientFk',
type: 'Integer',
description: `The client id`
type: 'integer',
description: 'The client id'
}, {
arg: 'ticketFk',
type: 'Integer',
description: `The ticket id`
type: 'integer',
description: 'The ticket id'
}, {
arg: 'agencyModeFk',
type: 'Integer',
description: `The agency mode id`
type: 'integer',
description: 'The agency mode id'
}, {
arg: 'workerFk',
type: 'Integer',
description: `The salesperson id`
type: 'integer',
description: 'The salesperson id'
}, {
arg: 'myTeam',
type: 'Boolean',
description: `Whether to show only tickets for the current logged user team (For now it shows only the current user tickets)`
type: 'boolean',
description: 'Whether to show only tickets for the current logged user team (currently user tickets)'
}, {
arg: 'isConfirmed',
type: 'Boolean',
description: `Order is confirmed`
type: 'boolean',
description: 'Order is confirmed'
}, {
arg: 'showEmpty',
type: 'boolean',
description: `Show empty orders`
description: 'Show empty orders'
}
],
returns: {
type: ['Object'],
type: ['object'],
root: true
},
http: {
@ -71,14 +71,20 @@ module.exports = Self => {
}
});
Self.filter = async(ctx, filter) => {
let conn = Self.dataSource.connector;
Self.filter = async(ctx, filter, options) => {
const conn = Self.dataSource.connector;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
let worker = await Self.app.models.Worker.findOne({
where: {userFk: ctx.req.accessToken.userId},
include: [
{relation: 'collegues'}
]
});
}, myOptions);
const args = ctx.args;
let teamIds = [];
@ -92,13 +98,15 @@ module.exports = Self => {
worker = await Self.app.models.Worker.findOne({
fields: ['id'],
where: {userFk: ctx.req.accessToken.userId}
});
}, myOptions);
teamIds = [worker && worker.id];
}
if (args && args.myTeam)
args.teamIds = teamIds;
let where = buildFilter(args, (param, value) => {
const where = buildFilter(args, (param, value) => {
switch (param) {
case 'search':
return /^\d+$/.test(value)
@ -133,7 +141,7 @@ module.exports = Self => {
});
filter = mergeFilters(filter, {where});
let stmts = [];
const stmts = [];
let stmt;
stmt = new ParameterizedSQL(
@ -183,7 +191,7 @@ module.exports = Self => {
stmts.push(`DROP TEMPORARY TABLE tmp.filter`);
const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql);
const result = await conn.executeStmt(sql, myOptions);
return result[ordersIndex];
};

View File

@ -1,51 +1,73 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
describe('order catalogFilter()', () => {
const colorTagId = 1;
const categoryTagId = 67;
it('should return an array of items', async() => {
let filter = {
where: {
categoryFk: 1,
typeFk: 2
}
};
let tags = [];
let orderFk = 11;
let orderBy = {field: 'relevancy DESC, name', way: 'DESC'};
let result = await app.models.Order.catalogFilter(orderFk, orderBy, filter, tags);
let firstItemId = result[0].id;
const tx = await models.Order.beginTransaction({});
expect(result.length).toEqual(4);
expect(firstItemId).toEqual(1);
try {
const options = {transaction: tx};
const filter = {
where: {
categoryFk: 1,
typeFk: 2
}
};
const tags = [];
const orderFk = 11;
const orderBy = {field: 'relevancy DESC, name', way: 'DESC'};
const result = await models.Order.catalogFilter(orderFk, orderBy, filter, tags, options);
const firstItemId = result[0].id;
expect(result.length).toEqual(4);
expect(firstItemId).toEqual(1);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should now return an array of items based on tag filter', async() => {
const filter = {
where: {
categoryFk: 1,
typeFk: 2
}
};
const tx = await models.Order.beginTransaction({});
const tagGroups = [
{tagFk: colorTagId, values: [{value: 'Silver'}, {value: 'Brown'}]},
{tagFk: categoryTagId, values: [{value: 'Concussion'}]}
];
const orderFk = 11;
const orderBy = {field: 'relevancy DESC, name', way: 'DESC'};
const result = await app.models.Order.catalogFilter(orderFk, orderBy, filter, tagGroups);
try {
const options = {transaction: tx};
const randomIndex = Math.round(Math.random());
const item = result[randomIndex];
const itemTags = item.tags;
const filter = {
where: {
categoryFk: 1,
typeFk: 2
}
};
const colorTag = itemTags.find(tag => tag.tagFk == colorTagId);
const categoryTag = itemTags.find(tag => tag.tagFk == categoryTagId);
const tagGroups = [
{tagFk: colorTagId, values: [{value: 'Silver'}, {value: 'Brown'}]},
{tagFk: categoryTagId, values: [{value: 'Concussion'}]}
];
const orderFk = 11;
const orderBy = {field: 'relevancy DESC, name', way: 'DESC'};
const result = await models.Order.catalogFilter(orderFk, orderBy, filter, tagGroups, options);
expect(result.length).toEqual(2);
expect(colorTag.value).toEqual('Silver');
expect(categoryTag.value).toEqual('Concussion');
const randomIndex = Math.round(Math.random());
const item = result[randomIndex];
const itemTags = item.tags;
const colorTag = itemTags.find(tag => tag.tagFk == colorTagId);
const categoryTag = itemTags.find(tag => tag.tagFk == categoryTagId);
expect(result.length).toEqual(2);
expect(colorTag.value).toEqual('Silver');
expect(categoryTag.value).toEqual('Concussion');
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});