feat(salix): refs #5926 Decouple docuwareUpload for Tickets
gitea/salix/pipeline/pr-dev This commit looks good
Details
gitea/salix/pipeline/pr-dev This commit looks good
Details
This commit is contained in:
parent
44ce998604
commit
6b66fd6b3c
|
@ -23,6 +23,16 @@ module.exports = Self => {
|
|||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the docuware id
|
||||
*
|
||||
* @param {object} filter - The filter to use in findOne method
|
||||
* @return {object} - The doware record
|
||||
*/
|
||||
Self.getDocuware = async filter => {
|
||||
return await Self.app.models.Docuware.findOne(filter);
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the dialog id
|
||||
*
|
||||
|
@ -35,7 +45,7 @@ module.exports = Self => {
|
|||
if (!process.env.NODE_ENV)
|
||||
return Math.floor(Math.random() + 100);
|
||||
|
||||
const docuwareInfo = await Self.app.models.Docuware.findOne({
|
||||
const docuwareInfo = await Self.getDocuware({
|
||||
where: {
|
||||
code,
|
||||
action
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
const UserError = require('vn-loopback/util/user-error');
|
||||
const axios = require('axios');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('upload', {
|
||||
|
@ -7,9 +6,9 @@ module.exports = Self => {
|
|||
accessType: 'WRITE',
|
||||
accepts: [
|
||||
{
|
||||
arg: 'ticketIds',
|
||||
arg: 'ids',
|
||||
type: ['number'],
|
||||
description: 'The ticket ids',
|
||||
description: 'The ids',
|
||||
required: true
|
||||
},
|
||||
{
|
||||
|
@ -29,8 +28,7 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.upload = async function(ctx, ticketIds, fileCabinet, options) {
|
||||
delete ctx.args.ticketIds;
|
||||
Self.upload = async function(ctx, ids, fileCabinet, options) {
|
||||
const models = Self.app.models;
|
||||
const action = 'store';
|
||||
|
||||
|
@ -39,125 +37,29 @@ module.exports = Self => {
|
|||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
const userConfig = await models.UserConfig.findById(ctx.req.accessToken.userId, {
|
||||
const {tabletFk} = await models.UserConfig.findById(ctx.req.accessToken.userId, {
|
||||
fields: ['tabletFk']
|
||||
}, myOptions);
|
||||
|
||||
if (!userConfig?.tabletFk)
|
||||
if (!tabletFk)
|
||||
throw new UserError('This user does not have an assigned tablet');
|
||||
|
||||
const docuwareOptions = await Self.getOptions();
|
||||
const {modelFk} = await Self.getDocuware({code: fileCabinet, action});
|
||||
const modelIsValid = await Self.app.models.Module.findOne({where: {code: modelFk}});
|
||||
|
||||
if (!modelIsValid)
|
||||
throw new UserError('This fileCabinet does not have an assigned module');
|
||||
|
||||
const model = modelFk.replace(/^.{1}/g, modelFk[0].toUpperCase());
|
||||
|
||||
const fileCabinetId = await Self.getFileCabinet(fileCabinet);
|
||||
const dialogId = await Self.getDialog(fileCabinet, action, fileCabinetId);
|
||||
|
||||
const uploaded = [];
|
||||
for (id of ticketIds) {
|
||||
// get delivery note
|
||||
ctx.args.id = id;
|
||||
const deliveryNote = await models.Ticket.deliveryNotePdf(ctx, {
|
||||
id,
|
||||
type: 'deliveryNote'
|
||||
}, myOptions);
|
||||
// get ticket data
|
||||
const ticket = await models.Ticket.findById(id, {
|
||||
include: [{
|
||||
relation: 'client',
|
||||
scope: {
|
||||
fields: ['id', 'name', 'fi']
|
||||
}
|
||||
}]
|
||||
}, myOptions);
|
||||
const uri = `${docuwareOptions.url}/FileCabinets/${fileCabinetId}/Documents`;
|
||||
|
||||
// upload file
|
||||
const templateJson = {
|
||||
'Fields': [
|
||||
{
|
||||
'FieldName': 'N__ALBAR_N',
|
||||
'ItemElementName': 'string',
|
||||
'Item': id,
|
||||
},
|
||||
{
|
||||
'FieldName': 'CIF_PROVEEDOR',
|
||||
'ItemElementName': 'string',
|
||||
'Item': ticket.client().fi,
|
||||
},
|
||||
{
|
||||
'FieldName': 'CODIGO_PROVEEDOR',
|
||||
'ItemElementName': 'string',
|
||||
'Item': ticket.client().id,
|
||||
},
|
||||
{
|
||||
'FieldName': 'NOMBRE_PROVEEDOR',
|
||||
'ItemElementName': 'string',
|
||||
'Item': ticket.client().name + ' - ' + id,
|
||||
},
|
||||
{
|
||||
'FieldName': 'FECHA_FACTURA',
|
||||
'ItemElementName': 'date',
|
||||
'Item': ticket.shipped,
|
||||
},
|
||||
{
|
||||
'FieldName': 'TOTAL_FACTURA',
|
||||
'ItemElementName': 'Decimal',
|
||||
'Item': ticket.totalWithVat,
|
||||
},
|
||||
{
|
||||
'FieldName': 'ESTADO',
|
||||
'ItemElementName': 'string',
|
||||
'Item': 'Pendiente procesar',
|
||||
},
|
||||
{
|
||||
'FieldName': 'FIRMA_',
|
||||
'ItemElementName': 'string',
|
||||
'Item': 'Si',
|
||||
},
|
||||
{
|
||||
'FieldName': 'FILTRO_TABLET',
|
||||
'ItemElementName': 'string',
|
||||
'Item': userConfig.tabletFk,
|
||||
}
|
||||
]
|
||||
};
|
||||
await Self.app.models[model].docuwareUpload({ctx, tabletFk, ids, myOptions, uri, docuwareOptions, dialogId});
|
||||
|
||||
if (process.env.NODE_ENV != 'production')
|
||||
throw new UserError('Action not allowed on the test environment');
|
||||
|
||||
// delete old
|
||||
const docuwareFile = await models.Docuware.checkFile(id, fileCabinet, false);
|
||||
if (docuwareFile) {
|
||||
const deleteJson = {
|
||||
'Field': [{'FieldName': 'ESTADO', 'Item': 'Pendiente eliminar', 'ItemElementName': 'String'}]
|
||||
};
|
||||
const deleteUri = `${docuwareOptions.url}/FileCabinets/${fileCabinetId}/Documents/${docuwareFile.id}/Fields`;
|
||||
await axios.put(deleteUri, deleteJson, docuwareOptions.headers);
|
||||
}
|
||||
|
||||
const uploadUri = `${docuwareOptions.url}/FileCabinets/${fileCabinetId}/Documents?StoreDialogId=${dialogId}`;
|
||||
const FormData = require('form-data');
|
||||
const data = new FormData();
|
||||
|
||||
data.append('document', JSON.stringify(templateJson), 'schema.json');
|
||||
data.append('file[]', deliveryNote[0], 'file.pdf');
|
||||
const uploadOptions = {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
'X-File-ModifiedDate': Date.vnNew(),
|
||||
'Cookie': docuwareOptions.headers.headers.Cookie,
|
||||
...data.getHeaders()
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
await axios.post(uploadUri, data, uploadOptions);
|
||||
} catch (err) {
|
||||
const $t = ctx.req.__;
|
||||
const message = $t('Failed to upload delivery note', {id});
|
||||
if (uploaded.length)
|
||||
await models.TicketTracking.setDelivered(ctx, uploaded, myOptions);
|
||||
throw new UserError(message);
|
||||
}
|
||||
uploaded.push(id);
|
||||
}
|
||||
return models.TicketTracking.setDelivered(ctx, ticketIds, myOptions);
|
||||
return models.TicketTracking.setDelivered(ctx, ids, myOptions);
|
||||
};
|
||||
};
|
||||
|
|
|
@ -27,6 +27,9 @@
|
|||
},
|
||||
"findById": {
|
||||
"type": "string"
|
||||
},
|
||||
"modelFk": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"relations": {
|
||||
|
|
|
@ -3788,7 +3788,26 @@ INSERT INTO vn.workerTeam(id, team, workerFk)
|
|||
VALUES
|
||||
(8, 1, 19);
|
||||
|
||||
INSERT INTO vn.workCenter (id, name, payrollCenterFk, counter, warehouseFk, street, geoFk, deliveryManAdjustment)
|
||||
INSERT INTO vn.workCenter (id, name, payrollCenterFk, counter, warehouseFk, street, geoFk, deliveryManAdjustment)
|
||||
VALUES(100, 'workCenterOne', 1, NULL, 1, 'gotham', NULL, NULL);
|
||||
|
||||
UPDATE vn.locker SET workerFk = 1110 WHERE id = 147;
|
||||
UPDATE vn.locker SET workerFk = 1110 WHERE id = 147;
|
||||
|
||||
ALTER TABLE vn.docuware ADD modelFk VARCHAR(45) DEFAULT NULL NULL;
|
||||
ALTER TABLE vn.docuware ADD CONSTRAINT docuware_module_FK FOREIGN KEY (modelFk) REFERENCES salix.module(code);
|
||||
-- Auto-generated SQL script #202405201318
|
||||
UPDATE vn.userConfig
|
||||
SET tabletFk='Tablet1'
|
||||
WHERE userFk=5;
|
||||
|
||||
|
||||
-- Auto-generated SQL script #202405201207
|
||||
UPDATE vn.docuware
|
||||
SET modelFk='ticket'
|
||||
WHERE id=1;
|
||||
UPDATE vn.docuware
|
||||
SET modelFk='ticket'
|
||||
WHERE id=2;
|
||||
UPDATE vn.docuware
|
||||
SET modelFk='worker'
|
||||
WHERE id=3;
|
||||
|
|
|
@ -0,0 +1,116 @@
|
|||
const axios = require('axios');
|
||||
const {models} = require('vn-loopback/server/server');
|
||||
const UserError = require('vn-loopback/util/user-error');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.docuwareUpload = async({ctx, tabletFk, ids: ticketIds, myOptions, uri, docuwareOptions, dialogId}) => {
|
||||
for (id of ticketIds) {
|
||||
// get delivery note
|
||||
ctx.args.id = id;
|
||||
const deliveryNote = await models.Ticket.deliveryNotePdf(ctx, {
|
||||
id,
|
||||
type: 'deliveryNote'
|
||||
}, myOptions);
|
||||
// get ticket data
|
||||
const ticket = await models.Ticket.findById(id, {
|
||||
include: [{
|
||||
relation: 'client',
|
||||
scope: {
|
||||
fields: ['id', 'name', 'fi']
|
||||
}
|
||||
}]
|
||||
}, myOptions);
|
||||
|
||||
// upload file
|
||||
const templateJson = {
|
||||
'Fields': [
|
||||
{
|
||||
'FieldName': 'N__ALBAR_N',
|
||||
'ItemElementName': 'string',
|
||||
'Item': id,
|
||||
},
|
||||
{
|
||||
'FieldName': 'CIF_PROVEEDOR',
|
||||
'ItemElementName': 'string',
|
||||
'Item': ticket.client().fi,
|
||||
},
|
||||
{
|
||||
'FieldName': 'CODIGO_PROVEEDOR',
|
||||
'ItemElementName': 'string',
|
||||
'Item': ticket.client().id,
|
||||
},
|
||||
{
|
||||
'FieldName': 'NOMBRE_PROVEEDOR',
|
||||
'ItemElementName': 'string',
|
||||
'Item': ticket.client().name + ' - ' + id,
|
||||
},
|
||||
{
|
||||
'FieldName': 'FECHA_FACTURA',
|
||||
'ItemElementName': 'date',
|
||||
'Item': ticket.shipped,
|
||||
},
|
||||
{
|
||||
'FieldName': 'TOTAL_FACTURA',
|
||||
'ItemElementName': 'Decimal',
|
||||
'Item': ticket.totalWithVat,
|
||||
},
|
||||
{
|
||||
'FieldName': 'ESTADO',
|
||||
'ItemElementName': 'string',
|
||||
'Item': 'Pendiente procesar',
|
||||
},
|
||||
{
|
||||
'FieldName': 'FIRMA_',
|
||||
'ItemElementName': 'string',
|
||||
'Item': 'Si',
|
||||
},
|
||||
{
|
||||
'FieldName': 'FILTRO_TABLET',
|
||||
'ItemElementName': 'string',
|
||||
'Item': tabletFk,
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
if (process.env.NODE_ENV != 'production')
|
||||
throw new UserError('Action not allowed on the test environment');
|
||||
|
||||
// delete old
|
||||
const docuwareFile = await models.Docuware.checkFile(id, fileCabinet, false);
|
||||
if (docuwareFile) {
|
||||
const deleteJson = {
|
||||
'Field': [{'FieldName': 'ESTADO', 'Item': 'Pendiente eliminar', 'ItemElementName': 'String'}]
|
||||
};
|
||||
const deleteUri = `${uri}/${docuwareFile.id}/Fields`;
|
||||
await axios.put(deleteUri, deleteJson, docuwareOptions.headers);
|
||||
}
|
||||
|
||||
const uploadUri = `${uri}?StoreDialogId=${dialogId}`;
|
||||
const FormData = require('form-data');
|
||||
const data = new FormData();
|
||||
|
||||
data.append('document', JSON.stringify(templateJson), 'schema.json');
|
||||
data.append('file[]', deliveryNote[0], 'file.pdf');
|
||||
const uploadOptions = {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
'X-File-ModifiedDate': Date.vnNew(),
|
||||
'Cookie': docuwareOptions.headers.headers.Cookie,
|
||||
...data.getHeaders()
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
await axios.post(uploadUri, data, uploadOptions);
|
||||
} catch (err) {
|
||||
const $t = ctx.req.__;
|
||||
const message = $t('Failed to upload delivery note', {id});
|
||||
if (uploaded.length)
|
||||
await models.TicketTracking.setDelivered(ctx, uploaded, myOptions);
|
||||
throw new UserError(message);
|
||||
}
|
||||
uploaded.push(id);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
@ -45,6 +45,7 @@ module.exports = function(Self) {
|
|||
require('../methods/ticket/invoiceTickets')(Self);
|
||||
require('../methods/ticket/invoiceTicketsAndPdf')(Self);
|
||||
require('../methods/ticket/docuwareDownload')(Self);
|
||||
require('../methods/ticket/docuwareUpload')(Self);
|
||||
require('../methods/ticket/myLastModified')(Self);
|
||||
require('../methods/ticket/addSaleByCode')(Self);
|
||||
require('../methods/ticket/clone')(Self);
|
||||
|
|
Loading…
Reference in New Issue