updated transactions

This commit is contained in:
Joan Sanchez 2019-09-18 15:11:09 +02:00
parent f56e4f4b7d
commit 8cb10759d3
2 changed files with 18 additions and 18 deletions

View File

@ -25,24 +25,24 @@ module.exports = Self => {
} }
}); });
Self.send = async(ctx, data, transaction) => { Self.send = async(ctx, data, options) => {
const accessToken = ctx.options && ctx.options.accessToken || ctx.req && ctx.req.accessToken; const accessToken = ctx.options && ctx.options.accessToken || ctx.req && ctx.req.accessToken;
const userId = accessToken.userId; const userId = accessToken.userId;
const models = Self.app.models; const models = Self.app.models;
const sender = await models.Account.findById(userId, transaction); const sender = await models.Account.findById(userId, options);
const recipient = await models.Account.findById(data.recipientFk, transaction); const recipient = await models.Account.findById(data.recipientFk, options);
await Self.create({ await Self.create({
sender: sender.name, sender: sender.name,
recipient: recipient.name, recipient: recipient.name,
message: data.message message: data.message
}, transaction); }, options);
return await models.MessageInbox.create({ return await models.MessageInbox.create({
sender: sender.name, sender: sender.name,
recipient: recipient.name, recipient: recipient.name,
finalRecipient: recipient.name, finalRecipient: recipient.name,
message: data.message message: data.message
}, transaction); }, options);
}; };
}; };

View File

@ -22,17 +22,17 @@ module.exports = Self => {
const models = Self.app.models; const models = Self.app.models;
const resolvedState = 3; const resolvedState = 3;
let tx = await Self.beginTransaction({});
try {
let options = {transaction: tx};
const claimEnds = await models.ClaimEnd.find({ const claimEnds = await models.ClaimEnd.find({
include: { include: {
relation: 'claimDestination', relation: 'claimDestination',
fields: ['addressFk'] fields: ['addressFk']
}, },
where: {claimFk: params.claimFk} where: {claimFk: params.claimFk}
}); }, options);
let tx = await Self.beginTransaction({});
try {
let options = {transaction: tx};
for (let i = 0; i < claimEnds.length; i++) { for (let i = 0; i < claimEnds.length; i++) {
const claimEnd = claimEnds[i]; const claimEnd = claimEnds[i];
@ -42,7 +42,7 @@ module.exports = Self => {
if (!addressFk) if (!addressFk)
continue; continue;
let sale = await getSale(claimEnd.saleFk); let sale = await getSale(claimEnd.saleFk, options);
let ticketFk = await getTicketId({ let ticketFk = await getTicketId({
addressFk: addressFk, addressFk: addressFk,
companyFk: sale.ticket().companyFk, companyFk: sale.ticket().companyFk,
@ -51,7 +51,7 @@ module.exports = Self => {
let address = await models.Address.findOne({ let address = await models.Address.findOne({
where: {id: addressFk} where: {id: addressFk}
}); }, options);
if (!ticketFk) { if (!ticketFk) {
ticketFk = await createTicket(ctx, { ticketFk = await createTicket(ctx, {
@ -84,7 +84,7 @@ module.exports = Self => {
} }
} }
let claim = await Self.findById(params.claimFk); let claim = await Self.findById(params.claimFk, null, options);
claim = await claim.updateAttributes({ claim = await claim.updateAttributes({
claimStateFk: resolvedState claimStateFk: resolvedState
}, options); }, options);
@ -98,7 +98,7 @@ module.exports = Self => {
} }
}; };
async function getSale(saleFk) { async function getSale(saleFk, options) {
return await Self.app.models.Sale.findOne({ return await Self.app.models.Sale.findOne({
include: [ include: [
{ {
@ -116,7 +116,7 @@ module.exports = Self => {
} }
}], }],
where: {id: saleFk} where: {id: saleFk}
}); }, options);
} }
async function getTicketId(params, options) { async function getTicketId(params, options) {