From 382309752f89aae7abbb2fcb764a4c960513d910 Mon Sep 17 00:00:00 2001 From: alexm Date: Tue, 5 Nov 2024 16:56:33 +0100 Subject: [PATCH] refactor(dms): refs #7151 uploadFile --- .../claim/back/methods/claim/uploadFile.js | 32 ++--------------- .../client/back/methods/client/uploadFile.js | 34 ++----------------- .../back/methods/entry-dms/uploadFile.js | 32 ++--------------- .../supplier/supplier-dms/uploadFile.js | 32 ++--------------- .../ticket/back/methods/ticket/uploadFile.js | 33 ++---------------- .../worker/back/methods/worker/uploadFile.js | 32 ++--------------- 6 files changed, 12 insertions(+), 183 deletions(-) diff --git a/modules/claim/back/methods/claim/uploadFile.js b/modules/claim/back/methods/claim/uploadFile.js index 4fd041456..a5fd9b405 100644 --- a/modules/claim/back/methods/claim/uploadFile.js +++ b/modules/claim/back/methods/claim/uploadFile.js @@ -53,34 +53,6 @@ module.exports = Self => { } }); - Self.uploadFile = async(ctx, id, options) => { - const {Dms, ClaimDms} = 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; - } - - try { - const uploadedFiles = await Dms.uploadFile(ctx, myOptions); - - const promises = uploadedFiles.map(dms => ClaimDms.create({ - claimFk: id, - dmsFk: dms.id - }, myOptions)); - await Promise.all(promises); - - if (tx) await tx.commit(); - - return uploadedFiles; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.uploadFile = async(ctx, id, options) => + Self.app.models.Dms.uploadFileTemplate(ctx, id, 'ClaimDms', 'claimFk', options); }; diff --git a/modules/client/back/methods/client/uploadFile.js b/modules/client/back/methods/client/uploadFile.js index 1a5965955..5f7bb7f9a 100644 --- a/modules/client/back/methods/client/uploadFile.js +++ b/modules/client/back/methods/client/uploadFile.js @@ -54,36 +54,6 @@ module.exports = Self => { } }); - Self.uploadFile = async(ctx, id, options) => { - const {Dms, ClientDms} = 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; - } - - try { - const uploadedFiles = await Dms.uploadFile(ctx, myOptions); - const promises = uploadedFiles.map(dms => - ClientDms.create({ - clientFk: id, - dmsFk: dms.id - }, myOptions) - - ); - await Promise.all(promises); - - if (tx) await tx.commit(); - - return uploadedFiles; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.uploadFile = async(ctx, id, options) => + Self.app.models.Dms.uploadFileTemplate(ctx, id, 'ClientDms', 'clientFk', options); }; diff --git a/modules/entry/back/methods/entry-dms/uploadFile.js b/modules/entry/back/methods/entry-dms/uploadFile.js index 54b56fed4..f29506aa7 100644 --- a/modules/entry/back/methods/entry-dms/uploadFile.js +++ b/modules/entry/back/methods/entry-dms/uploadFile.js @@ -53,34 +53,6 @@ module.exports = Self => { } }); - Self.uploadFile = async(ctx, id, options) => { - const {Dms, EntryDms} = 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; - } - - try { - const uploadedFiles = await Dms.uploadFile(ctx, myOptions); - - const promises = uploadedFiles.map(dms => EntryDms.create({ - entryFk: id, - dmsFk: dms.id - }, myOptions)); - await Promise.all(promises); - - if (tx) await tx.commit(); - - return uploadedFiles; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.uploadFile = async(ctx, id, options) => + Self.app.models.Dms.uploadFileTemplate(ctx, id, 'EntryDms', 'entryFk', options); }; diff --git a/modules/supplier/back/methods/supplier/supplier-dms/uploadFile.js b/modules/supplier/back/methods/supplier/supplier-dms/uploadFile.js index a9ff2d125..9b6ec2d14 100644 --- a/modules/supplier/back/methods/supplier/supplier-dms/uploadFile.js +++ b/modules/supplier/back/methods/supplier/supplier-dms/uploadFile.js @@ -53,34 +53,6 @@ module.exports = Self => { } }); - Self.uploadFile = async(ctx, id, options) => { - const {Dms, SupplierDms} = 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; - } - - try { - const uploadedFiles = await Dms.uploadFile(ctx, myOptions); - - const promises = uploadedFiles.map(dms => SupplierDms.create({ - supplierFk: id, - dmsFk: dms.id - }, myOptions)); - await Promise.all(promises); - - if (tx) await tx.commit(); - - return uploadedFiles; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.uploadFile = async(ctx, id, options) => + Self.app.models.Dms.uploadFileTemplate(ctx, id, 'SupplierDms', 'supplierFk', options); }; diff --git a/modules/ticket/back/methods/ticket/uploadFile.js b/modules/ticket/back/methods/ticket/uploadFile.js index 5b79aa973..07d4c8971 100644 --- a/modules/ticket/back/methods/ticket/uploadFile.js +++ b/modules/ticket/back/methods/ticket/uploadFile.js @@ -46,35 +46,6 @@ module.exports = Self => { } }); - Self.uploadFile = async(ctx, id, options) => { - const {Dms, TicketDms} = 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; - } - - try { - const uploadedFiles = await Dms.uploadFile(ctx, myOptions); - - const promises = uploadedFiles.map(dms => TicketDms.create({ - ticketFk: id, - dmsFk: dms.id - }, myOptions)); - - await Promise.all(promises); - - if (tx) await tx.commit(); - - return uploadedFiles; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.uploadFile = async(ctx, id, options) => + Self.app.models.Dms.uploadFileTemplate(ctx, id, 'TicketDms', 'ticketFk', options); }; diff --git a/modules/worker/back/methods/worker/uploadFile.js b/modules/worker/back/methods/worker/uploadFile.js index c3526cbb1..b853806d8 100644 --- a/modules/worker/back/methods/worker/uploadFile.js +++ b/modules/worker/back/methods/worker/uploadFile.js @@ -46,34 +46,6 @@ module.exports = Self => { } }); - Self.uploadFile = async(ctx, id) => { - const {Dms, WorkerDms} = 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; - } - - try { - const uploadedFiles = await Dms.uploadFile(ctx, myOptions); - const promises = uploadedFiles.map(dms => - WorkerDms.create({ - workerFk: id, - dmsFk: dms.id - }, myOptions)); - await Promise.all(promises); - - if (tx) await tx.commit(); - - return uploadedFiles; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; + Self.uploadFile = async(ctx, id) => + Self.app.models.Dms.uploadFileTemplate(ctx, id, 'WorkerDms', 'workerFk', options); };