const md5 = require('md5'); const fs = require('fs-extra'); module.exports = Self => { Self.remoteMethodCtx('saveSign', { description: 'Save sign', accessType: 'WRITE', accepts: [ { arg: 'signContent', type: 'string', required: true, description: 'The sign content' }, { arg: 'tickets', type: ['number'], required: true, description: 'The tickets' }, { arg: 'signedTime', type: 'date', description: 'The signed time' }, { arg: 'addressFk', type: 'number', required: true, description: 'The address fk' } ], returns: { type: 'Object', root: true }, http: { path: `/saveSign`, verb: 'POST' } }); async function createGestDoc(ticketId, userFk) { const models = Self.app.models; if (!await gestDocExists(ticketId)) { const result = await models.Ticket.findOne({ where: { id: ticketId }, include: [ { relation: 'warehouse', scope: { fields: ['id'] } }, { relation: 'client', scope: { fields: ['name'] } }, { relation: 'route', scope: { fields: ['id'] } } ] }); const warehouseFk = result.warehouseFk; const companyFk = result.companyFk; const client = result.client.name; const route = result.route.id; const resultDmsType = await models.DmsType.findOne({ where: { code: 'Ticket' } }); const resultDms = await models.Dms.create({ dmsTypeFk: resultDmsType.id, reference: ticketId, description: `Ticket ${ticketId} Cliente ${client} Ruta ${route}`, companyFk: companyFk, warehouseFk: warehouseFk, workerFk: userFk }); return resultDms.insertId; } } async function gestDocExists(ticket) { const models = Self.app.models; const result = await models.TicketDms.findOne({ where: { ticketFk: ticket }, fields: ['dmsFk'] }); if (result == null) return false; const isSigned = await models.Ticket.findOne({ where: { id: ticket }, fields: ['isSigned'] }); if (isSigned) return true; else await models.Dms.destroyById(ticket); } async function dmsRecover(ticket, signContent) { const models = Self.app.models; await models.DmsRecover.create({ ticketFk: ticket, sign: signContent }); } async function ticketGestdoc(ticket, dmsFk) { const models = Self.app.models; models.TicketDms.replaceOrCreate({ ticketFk: ticket, dmsFk: dmsFk }); const queryVnTicketSetState = `CALL vn.ticket_setState(?, ?)`; await Self.rawSql(queryVnTicketSetState, [ticket, 'DELIVERED']); } async function updateGestdoc(file, ticket) { const models = Self.app.models; models.Dms.updateOne({ where: { id: ticket }, file: file, contentType: 'image/png' }); } Self.saveSign = async(ctx, signContent, tickets, signedTime) => { const models = Self.app.models; let tx = await Self.beginTransaction({}); try { const userId = ctx.req.accessToken.userId; const dmsDir = `storage/dms`; let image = null; for (let i = 0; i < tickets.length; i++) { const alertLevel = await models.TicketState.findOne({ where: { ticketFk: tickets[i] }, fields: ['alertLevel'] }); signedTime ? signedTime != undefined : signedTime = new Date(); if (alertLevel >= 2) { let dir; let id = null; let fileName = null; if (!await gestDocExists(tickets[i])) { id = await createGestDoc(tickets[i], userId); const hashDir = md5(id).substring(0, 3); dir = `${dmsDir}/${hashDir}`; if (!fs.existsSync(dir)) fs.mkdirSync(dir); fileName = `${id}.png`; image = `${dir}/${fileName}`; } else if (image != null) { if (!fs.existsSync(dir)) dmsRecover(tickets[i], signContent); else { fs.writeFile(image, signContent, 'base64', async function(err) { if (err) { await tx.rollback(); return err.message; } }); } } else dmsRecover(tickets[i], signContent); if (id != null && fileName.length > 0) { ticketGestdoc(tickets[i], id); updateGestdoc(id, fileName); } } } if (tx) await tx.commit(); return 'OK'; } catch (err) { await tx.rollback(); throw err.message; } }; };