module.exports = Self => { Self.remoteMethodCtx('importTicketSales', { description: 'Imports lines from claimBeginning to a new ticket with specific shipped, landed dates, agency and company', accessType: 'WRITE', accepts: [{ arg: 'params', type: 'object', http: {source: 'body'} }], returns: { type: ['Object'], root: true }, http: { path: `/importTicketSales`, verb: 'POST' } }); Self.importTicketSales = async(ctx, params, options) => { let models = Self.app.models; let userId = ctx.req.accessToken.userId; let tx; const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); if (!myOptions.transaction) { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } try { const worker = await models.Worker.findOne({where: {userFk: userId}}, myOptions); let ticketSales = await models.Sale.find({ where: {ticketFk: params.ticketFk} }, myOptions); let claimEnds = []; ticketSales.forEach(sale => { claimEnds.push({ saleFk: sale.id, claimFk: params.claimFk, workerFk: worker.id }); }); const createdClaimEnds = await Self.create(claimEnds, myOptions); if (tx) await tx.commit(); return createdClaimEnds; } catch (e) { if (tx) await tx.rollback(); throw e; } }; };