WIP: 7151-dms_refactor_methods #3164
|
@ -16,13 +16,12 @@ module.exports = Self => {
|
|||
return await models.DmsType.hasReadRole(ctx, dms.dmsTypeFk);
|
||||
};
|
||||
|
||||
Self.getFile = async function(id) {
|
||||
const models = Self.app.models;
|
||||
const DmsContainer = models.DmsContainer;
|
||||
Self.getFile = async function(id, container = 'DmsContainer') {
|
||||
const Container = Self.app.models.models[container];
|
||||
const dms = await Self.findById(id);
|
||||
const pathHash = DmsContainer.getHash(dms.id);
|
||||
const pathHash = Container.getHash(dms.id);
|
||||
try {
|
||||
await DmsContainer.getFile(pathHash, dms.file);
|
||||
await Container.getFile(pathHash, dms.file);
|
||||
} catch (e) {
|
||||
if (e.code != 'ENOENT')
|
||||
throw e;
|
||||
|
@ -33,7 +32,7 @@ module.exports = Self => {
|
|||
throw error;
|
||||
}
|
||||
|
||||
const stream = DmsContainer.downloadStream(pathHash, dms.file);
|
||||
const stream = Container.downloadStream(pathHash, dms.file);
|
||||
|
||||
return [stream, dms.contentType, `filename="${dms.file}"`];
|
||||
};
|
||||
|
@ -65,4 +64,67 @@ module.exports = Self => {
|
|||
await stream.pipe(writeStream);
|
||||
return dms;
|
||||
};
|
||||
|
||||
Self.uploadFileTemplate = async(ctx, id, model, foreignKey, options) => {
|
||||
const {Dms, [model]: modelDms} = 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 =>
|
||||
modelDms.create({
|
||||
[foreignKey]: 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.removeFileTemplate = async(ctx, id, currentSelf, options) => {
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await currentSelf.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
try {
|
||||
const targetModelDms = await currentSelf.findById(id, null, myOptions);
|
||||
const targetDms = await Self.removeFile(ctx, targetModelDms.dmsFk, myOptions);
|
||||
|
||||
if (!targetDms || !targetModelDms)
|
||||
throw new UserError('Try again');
|
||||
|
||||
const modelDmsDestroyed = await targetModelDms.destroy(myOptions);
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return modelDmsDestroyed;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
const UserError = require('vn-loopback/util/user-error');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('removeFile', {
|
||||
description: 'Removes a claim document',
|
||||
|
@ -20,35 +18,7 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.removeFile = async(ctx, id, options) => {
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
try {
|
||||
const claimDms = await Self.findById(id, null, myOptions);
|
||||
|
||||
const targetDms = await Self.app.models.Dms.removeFile(ctx, claimDms.dmsFk, myOptions);
|
||||
|
||||
if (!targetDms || ! claimDms)
|
||||
throw new UserError('Try again');
|
||||
|
||||
const claimDmsDestroyed = await claimDms.destroy(myOptions);
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return claimDmsDestroyed;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
Self.removeFile = async(ctx, id, options) =>
|
||||
Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options);
|
||||
};
|
||||
|
||||
|
|
|
@ -18,35 +18,7 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.removeFile = async(ctx, id, options) => {
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
try {
|
||||
const clientDms = await Self.findById(id, null, myOptions);
|
||||
|
||||
const targetDms = await Self.app.models.Dms.removeFile(ctx, clientDms.dmsFk, myOptions);
|
||||
|
||||
if (!targetDms || !clientDms)
|
||||
throw new UserError('Try again');
|
||||
|
||||
const clientDmsDestroyed = await clientDms.destroy(myOptions);
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return clientDmsDestroyed;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
Self.removeFile = async(ctx, id, options) =>
|
||||
Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options);
|
||||
};
|
||||
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
const UserError = require('vn-loopback/util/user-error');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('removeFile', {
|
||||
description: 'Removes a entry document',
|
||||
|
@ -20,34 +18,7 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.removeFile = async(ctx, id, options) => {
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
try {
|
||||
const targetEntryDms = await Self.findById(id, null, myOptions);
|
||||
const targetDms = await Self.app.models.Dms.removeFile(ctx, targetEntryDms.dmsFk, myOptions);
|
||||
|
||||
if (!targetDms)
|
||||
throw new UserError('Try again');
|
||||
|
||||
const entryDmsDestroyed = await targetEntryDms.destroy(myOptions);
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return entryDmsDestroyed;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
Self.removeFile = async(ctx, id, options) =>
|
||||
Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options);
|
||||
};
|
||||
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
const UserError = require('vn-loopback/util/user-error');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('removeFile', {
|
||||
description: 'Removes a entry document',
|
||||
|
@ -20,34 +18,7 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.removeFile = async(ctx, id, options) => {
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
try {
|
||||
const targetSupplierDms = await Self.findById(id, null, myOptions);
|
||||
const targetDms = await Self.app.models.Dms.removeFile(ctx, targetSupplierDms.dmsFk, myOptions);
|
||||
|
||||
if (!targetDms)
|
||||
throw new UserError('Try again');
|
||||
|
||||
const supplierDmsDestroyed = await targetSupplierDms.destroy(myOptions);
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return supplierDmsDestroyed;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
Self.removeFile = async(ctx, id, options) =>
|
||||
Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options);
|
||||
};
|
||||
|
||||
|
|
|
@ -18,35 +18,7 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.removeFile = async(ctx, id, options) => {
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
try {
|
||||
const ticketDms = await Self.findById(id, null, myOptions);
|
||||
|
||||
const targetDms = await Self.app.models.Dms.removeFile(ctx, ticketDms.dmsFk, myOptions);
|
||||
|
||||
if (!targetDms || !ticketDms)
|
||||
throw new UserError('Try again');
|
||||
|
||||
const ticketDmsDestroyed = await ticketDms.destroy(myOptions);
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return ticketDmsDestroyed;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
Self.removeFile = async(ctx, id, options) =>
|
||||
Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options);
|
||||
};
|
||||
|
||||
|
|
|
@ -18,35 +18,7 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.removeFile = async(ctx, dmsFk, options) => {
|
||||
const myOptions = {};
|
||||
let tx;
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
if (!myOptions.transaction) {
|
||||
tx = await Self.beginTransaction({});
|
||||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
try {
|
||||
const WorkerDms = await Self.findOne({
|
||||
where: {document: dmsFk}
|
||||
}, myOptions);
|
||||
|
||||
const targetDms = await Self.app.models.Dms.removeFile(ctx, dmsFk, myOptions);
|
||||
|
||||
if (!targetDms || !WorkerDms)
|
||||
throw new UserError('Try again');
|
||||
|
||||
const workerDmsDestroyed = await WorkerDms.destroy(myOptions);
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return workerDmsDestroyed;
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
Self.removeFile = async(ctx, id, options) =>
|
||||
Self.app.models.Dms.removeFileTemplate(ctx, id, Self, options);
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue