module.exports = Self => { Self.remoteMethodCtx('saveSign', { description: 'Save sign', accessType: 'WRITE', accepts: [ { arg: 'tickets', type: ['number'], required: true, description: 'The tickets' }, { arg: 'location', type: 'object', description: 'The employee location the moment the sign is saved' }, { arg: 'signedTime', type: 'date', description: 'The signed time' } ], returns: { type: 'string', root: true }, http: { path: `/saveSign`, verb: 'POST' } }); Self.saveSign = async(ctx, options) => { const args = Object.assign({}, ctx.args); const models = Self.app.models; const myOptions = {}; let tx; if (typeof options == 'object') Object.assign(myOptions, options); if (!myOptions.transaction) { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } async function setLocation(ticketId) { if (args.signedTime) { await models.Delivery.create({ ticketFk: ticketId, longitude: args.location.Longitude, latitude: args.location.Latitude, dated: args.signedTime }, myOptions); } } async function gestDocExists(ticketId) { const ticketDms = await models.TicketDms.findOne({ where: { ticketFk: ticketId }, fields: ['dmsFk'] }, myOptions); if (!ticketDms) return false; const ticket = await models.Ticket.findById(ticketId, {fields: ['isSigned']}, myOptions); if (ticket.isSigned == true) return true; else await models.Dms.destroyAll({where: {reference: ticketId}}, myOptions); return false; } async function createGestDoc(id) { const ticket = await models.Ticket.findById(id, {include: [ { relation: 'warehouse', scope: { fields: ['id'] } }, { relation: 'client', scope: { fields: ['name'] } }, { relation: 'route', scope: { fields: ['id'] } } ] }, myOptions); const dmsType = await models.DmsType.findOne({where: {code: 'Ticket'}, fields: ['id']}, myOptions); const ctxUploadFile = Object.assign({}, ctx); ctxUploadFile.args = { warehouseId: ticket.warehouseFk, companyId: ticket.companyFk, dmsTypeId: dmsType.id, reference: id, description: `Ticket ${id} Cliente ${ticket.client().name} Ruta ${ticket.route().id}`, hasFile: true }; await models.Ticket.uploadFile(ctxUploadFile, id, myOptions); } // async function dmsRecover(ticketFk, sign) { // await models.DmsRecover.create({ticketFk, sign}, myOptions); // } try { for (let i = 0; i < args.tickets.length; i++) { const ticketState = await models.TicketState.findOne( {where: {ticketFk: args.tickets[i]}, fields: ['alertLevel'] }, myOptions); if (ticketState.alertLevel >= 2 && !await gestDocExists(args.tickets[i])) { if (!args.signedTime) args.signedTime = Date.vnNew(); if (args.location) setLocation(args.tickets[i]); await createGestDoc(args.tickets[i]); // if (image) { // if (!fs.existsSync(dir)) // dmsRecover(args.tickets[i], args.signContent); // else { // fs.writeFile(image, args.signContent, 'base64', async function(err) { // if (err) { // await tx.rollback(); // throw err.message; // } // }); // } // } else // dmsRecover(args.tickets[i], args.signContent); // if (id && fileName) { // await models.TicketDms.replaceOrCreate({ticketFk: args.tickets[i], dmsFk: id}, myOptions); await Self.rawSql(`CALL vn.ticket_setState(?, ?)`, [args.tickets[i], 'DELIVERED'], myOptions); // await models.Dms.updateAll({id}, {file: fileName, contentType: 'image/png'}, myOptions); // } } } if (tx) await tx.commit(); return 'Sign uploaded correctly'; } catch (e) { if (tx) await tx.rollback(); throw e; } }; };