fixes #5522 Loggable - Deadlock en inserción múltiple #1430

Merged
juan merged 3 commits from 5522-loggable-deadlock into dev 2023-04-12 15:44:45 +00:00
6 changed files with 31 additions and 19 deletions

View File

@ -0,0 +1 @@
DROP TRIGGER IF EXISTS `vn`.`claimBeginning_afterInsert`;

View File

@ -1774,12 +1774,12 @@ INSERT INTO `vn`.`claimState`(`id`, `code`, `description`, `roleFk`, `priority`,
( 6, 'mana', 'Mana', 72, 4, 0), ( 6, 'mana', 'Mana', 72, 4, 0),
( 7, 'lack', 'Faltas', 72, 2, 0); ( 7, 'lack', 'Faltas', 72, 2, 0);
INSERT INTO `vn`.`claim`(`id`, `ticketCreated`, `claimStateFk`, `clientFk`, `workerFk`, `responsibility`, `isChargedToMana`, `created`, `packages`, `rma`) INSERT INTO `vn`.`claim`(`id`, `ticketCreated`, `claimStateFk`, `clientFk`, `workerFk`, `responsibility`, `isChargedToMana`, `created`, `packages`, `rma`, `ticketFk`)
VALUES VALUES
(1, util.VN_CURDATE(), 1, 1101, 18, 3, 0, util.VN_CURDATE(), 0, '02676A049183'), (1, util.VN_CURDATE(), 1, 1101, 18, 3, 0, util.VN_CURDATE(), 0, '02676A049183', 11),
(2, util.VN_CURDATE(), 2, 1101, 18, 3, 0, util.VN_CURDATE(), 1, NULL), (2, util.VN_CURDATE(), 2, 1101, 18, 3, 0, util.VN_CURDATE(), 1, NULL, 16),
(3, util.VN_CURDATE(), 3, 1101, 18, 1, 1, util.VN_CURDATE(), 5, NULL), (3, util.VN_CURDATE(), 3, 1101, 18, 1, 1, util.VN_CURDATE(), 5, NULL, 7),
(4, util.VN_CURDATE(), 3, 1104, 18, 5, 0, util.VN_CURDATE(), 10, NULL); (4, util.VN_CURDATE(), 3, 1104, 18, 5, 0, util.VN_CURDATE(), 10, NULL, 8);
INSERT INTO `vn`.`claimObservation` (`claimFk`, `workerFk`, `text`, `created`) INSERT INTO `vn`.`claimObservation` (`claimFk`, `workerFk`, `text`, `created`)
VALUES VALUES

View File

@ -274,5 +274,6 @@
"This ticket cannot be signed because it has not been boxed": "Este ticket no puede firmarse porque no ha sido encajado", "This ticket cannot be signed because it has not been boxed": "Este ticket no puede firmarse porque no ha sido encajado",
"Insert a date range": "Inserte un rango de fechas", "Insert a date range": "Inserte un rango de fechas",
"Added observation": "{{user}} añadió esta observacion: {{text}}", "Added observation": "{{user}} añadió esta observacion: {{text}}",
"Comment added to client": "Observación añadida al cliente {{clientFk}}" "Comment added to client": "Observación añadida al cliente {{clientFk}}",
"Cannot create a new claimBeginning from a different ticket": "No se puede crear una línea de reclamación de un ticket diferente al origen"
} }

View File

@ -311,7 +311,7 @@ class VnMySQL extends MySQL {
return super[method].apply(this, args); return super[method].apply(this, args);
this.invokeMethodP(method, [...args], model, ctx, opts) this.invokeMethodP(method, [...args], model, ctx, opts)
.then(res => cb(...res), cb); .then(res => cb(...[null].concat(res)), cb);
} }
async invokeMethodP(method, args, model, ctx, opts) { async invokeMethodP(method, args, model, ctx, opts) {
@ -331,8 +331,7 @@ class VnMySQL extends MySQL {
const userId = opts.httpCtx && opts.httpCtx.active.accessToken.userId; const userId = opts.httpCtx && opts.httpCtx.active.accessToken.userId;
const user = await Model.app.models.Account.findById(userId, { fields: ['name'] }, opts); const user = await Model.app.models.Account.findById(userId, { fields: ['name'] }, opts);
await this.executeP(`CALL account.myUser_loginWithName(?)`, [user.name], opts); await this.executeP(`CALL account.myUser_loginWithName(?)`, [user.name], opts);
} } else {
else {
where = ctx.where; where = ctx.where;
id = ctx.id; id = ctx.id;
data = ctx.data; data = ctx.data;
@ -358,9 +357,12 @@ class VnMySQL extends MySQL {
} }
} }
const res = await new Promise(resolve => { const res = await new Promise((resolve, reject) => {
const fnArgs = args.slice(0, -2); const fnArgs = args.slice(0, -2);
fnArgs.push(opts, (...args) => resolve(args)); fnArgs.push(opts, (err, ...args) => {
if (err) return reject(err);
resolve(args);
});
super[method].apply(this, fnArgs); super[method].apply(this, fnArgs);
}); });
@ -375,11 +377,11 @@ class VnMySQL extends MySQL {
case 'update': { case 'update': {
switch (method) { switch (method) {
case 'createAll': case 'createAll':
for (const row of res[1]) for (const row of res[0])
ids.push(row[idName]); ids.push(row[idName]);
break; break;
case 'create': case 'create':
ids.push(res[1]); ids.push(res[0]);
break; break;
case 'update': case 'update':
if (data[idName] != null) if (data[idName] != null)
@ -387,7 +389,7 @@ class VnMySQL extends MySQL {
break; break;
} }
const newWhere = ids.length ? { [idName]: ids } : where; const newWhere = ids.length ? {[idName]: {inq: ids}} : where;
const stmt = this.buildSelectStmt(op, data, idName, model, newWhere, limit); const stmt = this.buildSelectStmt(op, data, idName, model, newWhere, limit);
newInstances = await this.executeStmt(stmt, opts); newInstances = await this.executeStmt(stmt, opts);

View File

@ -2,9 +2,9 @@ const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context'); const LoopBackContext = require('loopback-context');
describe('Claim createFromSales()', () => { describe('Claim createFromSales()', () => {
const ticketId = 16; const ticketId = 23;
const newSale = [{ const newSale = [{
id: 3, id: 31,
instance: 0, instance: 0,
quantity: 10 quantity: 10
}]; }];

View File

@ -10,7 +10,15 @@ module.exports = Self => {
}); });
Self.observe('before save', async ctx => { Self.observe('before save', async ctx => {
if (ctx.isNewInstance) return; if (ctx.isNewInstance) {
const models = Self.app.models;
const options = ctx.options;
const instance = ctx.instance;
const ticket = await models.Sale.findById(instance.saleFk, {fields: ['ticketFk']}, options);
const claim = await models.Claim.findById(instance.claimFk, {fields: ['ticketFk']}, options);
if (ticket.ticketFk != claim.ticketFk)
throw new UserError(`Cannot create a new claimBeginning from a different ticket`);
}
// await claimIsEditable(ctx); // await claimIsEditable(ctx);
}); });