#1279 loggable rawSql
gitea/salix/dev This commit looks good Details

This commit is contained in:
Gerard 2019-04-02 14:36:49 +02:00
parent 287e4048e2
commit a23c636c16
11 changed files with 262 additions and 97 deletions

View File

@ -171,7 +171,7 @@ module.exports = function(Self) {
if (ctx.where && ctx.where[primaryKey]) if (ctx.where && ctx.where[primaryKey])
originId = ctx.where[primaryKey]; originId = ctx.where[primaryKey];
else { else if (ctx.instance) {
originId = ctx.instance[primaryKey]; originId = ctx.instance[primaryKey];
changedModelId = ctx.instance.id; changedModelId = ctx.instance.id;
} }
@ -186,10 +186,11 @@ module.exports = function(Self) {
if (showField && (!ctx.instance || !ctx.instance[showField]) && ctx.where) { if (showField && (!ctx.instance || !ctx.instance[showField]) && ctx.where) {
changedModelId = []; changedModelId = [];
where = []; where = [];
let changedInstances = await ctx.Model.app.models[definition.name].find({where: ctx.where, fields: ['id', showField]}, options); let changedInstances = await ctx.Model.app.models[definition.name].find({where: ctx.where, fields: ['id', showField, primaryKey]}, options);
changedInstances.forEach(element => { changedInstances.forEach(element => {
where.push(element[showField]); where.push(element[showField]);
changedModelId.push(element.id); changedModelId.push(element.id);
originId = element[primaryKey];
}); });
} else if (ctx.hookState.oldInstance) } else if (ctx.hookState.oldInstance)
where = ctx.instance[showField]; where = ctx.instance[showField];

View File

@ -1,5 +1,5 @@
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('confirmTransaction', { Self.remoteMethodCtx('confirmTransaction', {
description: 'Confirm a web payment', description: 'Confirm a web payment',
accessType: 'WRITE', accessType: 'WRITE',
accepts: [{ accepts: [{
@ -18,7 +18,37 @@ module.exports = Self => {
} }
}); });
Self.confirmTransaction = async id => { Self.confirmTransaction = async(ctx, id) => {
return await Self.rawSql('CALL hedera.tpvTransactionConfirmById(?)', [id]); let transaction = await Self.beginTransaction({});
let userId = ctx.req.accessToken.userId;
try {
let oldTpvTransaction = await Self.app.models.TpvTransaction.findById(id, {options: transaction});
let confirm = await Self.rawSql('CALL hedera.tpvTransactionConfirmById(?)', [id], {options: transaction});
let tpvTransaction = await Self.app.models.TpvTransaction.findById(id, {options: transaction});
let oldInstance = {status: oldTpvTransaction.status};
let newInstance = {status: tpvTransaction.status};
let logRecord = {
originFk: tpvTransaction.clientFk,
userFk: userId,
action: 'update',
changedModel: 'TpvTransaction',
changedModelId: id,
oldInstance: oldInstance,
newInstance: newInstance
};
await Self.app.models.ClientLog.create(logRecord, {options: transaction});
await transaction.commit();
return confirm;
} catch (e) {
await transaction.rollback();
throw e;
}
}; };
}; };

View File

@ -11,7 +11,8 @@ describe('Client confirmTransaction', () => {
}); });
it('should call confirmTransaction() method to mark transaction as confirmed', async() => { it('should call confirmTransaction() method to mark transaction as confirmed', async() => {
await app.models.Client.confirmTransaction(transactionId); let ctx = {req: {accessToken: {userId: 1}}};
await app.models.Client.confirmTransaction(ctx, transactionId);
let [receipt] = await app.models.Client.rawSql( let [receipt] = await app.models.Client.rawSql(
`SELECT receiptFk `SELECT receiptFk

View File

@ -88,5 +88,8 @@
}, },
"TpvResponse": { "TpvResponse": {
"dataSource": "vn" "dataSource": "vn"
},
"TpvTransaction": {
"dataSource": "vn"
} }
} }

View File

@ -0,0 +1,48 @@
{
"name": "TpvTransaction",
"base": "VnModel",
"options": {
"mysql": {
"table": "hedera.tpvTransaction"
}
},
"properties": {
"id": {
"type": "Number",
"id": true,
"description": "Identifier"
},
"merchantFk": {
"type": "Number"
},
"clientFk": {
"type": "Number"
},
"receiptFk": {
"type": "Number"
},
"amount": {
"type": "Number"
},
"response": {
"type": "Number"
},
"errorCode": {
"type": "String"
},
"status": {
"type": "String"
},
"created": {
"type": "Date"
}
},
"relations": {
"client": {
"type": "belongsTo",
"model": "Client",
"foreignKey": "clientFk"
}
}
}

View File

@ -1,5 +1,3 @@
let UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('clone', { Self.remoteMethod('clone', {
description: 'clone item', description: 'clone item',
@ -22,6 +20,8 @@ module.exports = Self => {
}); });
Self.clone = async itemId => { Self.clone = async itemId => {
let $ = Self.app.models;
let transaction = await Self.beginTransaction({});
let filter = { let filter = {
where: { where: {
id: itemId id: itemId
@ -42,28 +42,54 @@ module.exports = Self => {
delete copy.comment; delete copy.comment;
let newItem = await Self.create(copy); let newItem = await Self.create(copy);
let promises = [];
let createBotanical = `INSERT INTO vn.itemBotanical (itemFk, botanical, genusFk, specieFk) let botanical = await $.ItemBotanical.findOne({
SELECT ?, botanical, genusFk, specieFk where: {itemFk: origin.id},
FROM vn.itemBotanical ib WHERE itemFk = ?`; fields: ['botanical', 'genusFk', 'specieFk']
promises.push(Self.rawSql(createBotanical, [newItem.id, origin.id])); }, {options: transaction});
let createTags = `INSERT INTO vn.itemTag(itemFk, tagFk, value, priority) if (botanical) {
SELECT ?, i.tagFk, i.value,i.priority botanical.itemFk = newItem.id;
FROM vn.itemTag i WHERE i.itemFk = ? await $.ItemBotanical.create(botanical);
ON DUPLICATE KEY UPDATE value = i.value, priority = i.priority`; }
promises.push(Self.rawSql(createTags, [newItem.id, origin.id]));
let createTax = `REPLACE INTO vn.itemTaxCountry (itemFk, countryFk, taxClassFk)
SELECT ?, countryFk, taxClassFk
FROM vn.itemTaxCountry WHERE itemFk = ?`;
promises.push(Self.rawSql(createTax, [newItem.id, origin.id]));
await Promise.all(promises); let copyTags = await $.ItemTag.find({
where: {
itemFk: origin.id
},
fields: ['tagFk', 'value', 'priority']
}, {options: transaction});
for (let i = 0; i < copyTags.length; i++) {
copyTags[i].itemFk = newItem.id;
await $.ItemTag.upsert(copyTags[i], {options: transaction});
}
let copyTaxes = await Self.app.models.ItemTaxCountry.find({
where: {itemFk: origin.id},
fields: ['botanical', 'countryFk', 'taxClassFk']
}, {options: transaction});
for (let i = 0; i < copyTaxes.length; i++) {
let tax = copyTaxes[i];
tax.itemFk = newItem.id;
await Self.app.models.ItemTaxCountry.upsertWithWhere(
{
itemFk: tax.itemFk,
countryFk: tax.countryFk,
taxClassFk: tax.taxClassFk,
},
tax,
{options: transaction}
);
}
await transaction.commit();
return newItem.id; return newItem.id;
} catch (err) { } catch (e) {
throw new UserError(err); await transaction.rollback();
throw e;
} }
}; };
}; };

View File

@ -53,7 +53,7 @@ module.exports = Self => {
}, transaction); }, transaction);
if (!ticketFk) { if (!ticketFk) {
ticketFk = await createTicket({ ticketFk = await createTicket(ctx, {
clientFk: itemDestination.address().clientFk, clientFk: itemDestination.address().clientFk,
addressFk: itemDestination.addressFk, addressFk: itemDestination.addressFk,
warehouseFk: warehouseFk, warehouseFk: warehouseFk,
@ -85,7 +85,7 @@ module.exports = Self => {
throw e; throw e;
} }
async function createTicket(params, transaction) { async function createTicket(ctx, params, transaction) {
let ticket = await Self.app.models.Ticket.new( let ticket = await Self.app.models.Ticket.new(
ctx, ctx,
{ {

View File

@ -20,6 +20,14 @@
"value": { "value": {
"type": "String" "type": "String"
}, },
"itemFk": {
"type": "Number",
"required": true
},
"tagFk": {
"type": "Number",
"required": true
},
"priority": { "priority": {
"type": "Number", "type": "Number",
"required": true "required": true

View File

@ -1,6 +1,11 @@
{ {
"name": "ItemTaxCountry", "name": "ItemTaxCountry",
"base": "VnModel", "base": "Loggable",
"log": {
"model": "ItemLog",
"relation": "item",
"showField": "countryFk"
},
"options": { "options": {
"mysql": { "mysql": {
"table": "itemTaxCountry" "table": "itemTaxCountry"
@ -14,6 +19,15 @@
}, },
"effectived": { "effectived": {
"type": "Boolean" "type": "Boolean"
},
"itemFk": {
"type": "Number"
},
"countryFk": {
"type": "Number"
},
"taxClassFk": {
"type": "Number"
} }
}, },
"relations": { "relations": {

View File

@ -1,7 +1,7 @@
let UserError = require('vn-loopback/util/user-error'); let UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('updatePrice', { Self.remoteMethodCtx('updatePrice', {
description: 'Changes the discount of a sale', description: 'Changes the discount of a sale',
accessType: '', accessType: '',
accepts: [{ accepts: [{
@ -62,10 +62,22 @@ module.exports = Self => {
let value = (params.price - currentLine.price); let value = (params.price - currentLine.price);
let query = `INSERT INTO vn.saleComponent(saleFk, componentFk, value) let saleComponent = await Self.app.models.SaleComponent.findOne({
VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE value = value + ?`; where: {
componentFk: componentToUse,
saleFk: params.id
}
});
await Self.rawSql(query, [params.id, componentToUse, value, value]); if (saleComponent)
saleComponent.updateAttributes({value: saleComponent.value + value});
else {
await Self.app.models.SaleComponent.create({
saleFk: params.id,
componentFk: componentToUse,
value: value
});
}
await currentLine.updateAttributes({price: params.price}); await currentLine.updateAttributes({price: params.price});

View File

@ -58,6 +58,10 @@ module.exports = Self => {
throw new UserError(`You can't create a ticket for a client that has a debt`); throw new UserError(`You can't create a ticket for a client that has a debt`);
} }
if (!transaction || !transaction.commit)
transaction = await Self.beginTransaction({});
try {
if (!params.shipped && params.landed) { if (!params.shipped && params.landed) {
params.shipped = await Self.app.models.Agency.getShipped({ params.shipped = await Self.app.models.Agency.getShipped({
landed: params.landed, landed: params.landed,
@ -93,8 +97,26 @@ module.exports = Self => {
params.userId params.userId
], {options: transaction}); ], {options: transaction});
return await Self.findOne({ let ticket = await Self.app.models.Ticket.findById(result[1][0].newTicketId, {options: transaction});
where: {id: result[1][0].newTicketId} let cleanInstance = JSON.parse(JSON.stringify(ticket));
}, {options: transaction});
let logRecord = {
originFk: cleanInstance.id,
userFk: params.userId,
action: 'create',
changedModel: 'Ticket',
changedModelId: cleanInstance.id,
oldInstance: {},
newInstance: cleanInstance
};
await Self.app.models.TicketLog.create(logRecord, {options: transaction});
await transaction.commit();
return await ticket;
} catch (e) {
await transaction.rollback();
throw e;
}
}; };
}; };