WIP: 7151-dms_refactor_methods #3164
|
@ -65,4 +65,89 @@ 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;
|
||||
}
|
||||
};
|
||||
|
||||
Self.downloadFileTemplate = async(id, container) => {
|
||||
const models = Self.app.models;
|
||||
const Container = models[container];
|
||||
const dms = await models.Dms.findById(id);
|
||||
const pathHash = Container.getHash(dms.id);
|
||||
try {
|
||||
await Container.getFile(pathHash, dms.file);
|
||||
} catch (e) {
|
||||
if (e.code != 'ENOENT')
|
||||
throw e;
|
||||
|
||||
const error = new UserError(`File doesn't exists`);
|
||||
error.statusCode = 404;
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
const stream = Container.downloadStream(pathHash, dms.file);
|
||||
|
||||
return [stream, dms.contentType, `filename="${dms.file}"`];
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
const UserError = require('vn-loopback/util/user-error');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('downloadFile', {
|
||||
Self.remoteMethod('downloadFile', {
|
||||
description: 'Get the claim file',
|
||||
accessType: 'READ',
|
||||
accepts: [
|
||||
|
@ -36,25 +34,6 @@ module.exports = Self => {
|
|||
accessScopes: ['DEFAULT', 'read:multimedia']
|
||||
});
|
||||
|
||||
Self.downloadFile = async function(ctx, id) {
|
||||
const models = Self.app.models;
|
||||
const ClaimContainer = models.ClaimContainer;
|
||||
const dms = await models.Dms.findById(id);
|
||||
const pathHash = ClaimContainer.getHash(dms.id);
|
||||
try {
|
||||
await ClaimContainer.getFile(pathHash, dms.file);
|
||||
} catch (e) {
|
||||
if (e.code != 'ENOENT')
|
||||
throw e;
|
||||
|
||||
const error = new UserError(`File doesn't exists`);
|
||||
error.statusCode = 404;
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
const stream = ClaimContainer.downloadStream(pathHash, dms.file);
|
||||
|
||||
return [stream, dms.contentType, `filename="${dms.file}"`];
|
||||
};
|
||||
Self.downloadFile = async id =>
|
||||
Self.app.models.Dms.downloadFileTemplate(id, 'ClaimContainer');
|
||||
};
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
const app = require('vn-loopback/server/server');
|
||||
|
||||
describe('claim downloadFile()', () => {
|
||||
const dmsId = 7;
|
||||
|
||||
it('should return a response for an employee with image content-type', async() => {
|
||||
const workerId = 1107;
|
||||
const ctx = {req: {accessToken: {userId: workerId}}};
|
||||
const result = await app.models.Claim.downloadFile(ctx, dmsId);
|
||||
const dmsId = 7;
|
||||
const result = await app.models.Claim.downloadFile(dmsId);
|
||||
|
||||
expect(result[1]).toEqual('image/jpeg');
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
const UserError = require('vn-loopback/util/user-error');
|
||||
|
||||
module.exports = Self => {
|
||||
Self.remoteMethodCtx('downloadFile', {
|
||||
Self.remoteMethod('downloadFile', {
|
||||
description: 'Get the entry file',
|
||||
accessType: 'READ',
|
||||
accepts: [
|
||||
|
@ -35,25 +33,6 @@ module.exports = Self => {
|
|||
}
|
||||
});
|
||||
|
||||
Self.downloadFile = async function(ctx, id) {
|
||||
const models = Self.app.models;
|
||||
const EntryContainer = models.EntryContainer;
|
||||
const dms = await models.Dms.findById(id);
|
||||
const pathHash = EntryContainer.getHash(dms.id);
|
||||
try {
|
||||
await EntryContainer.getFile(pathHash, dms.file);
|
||||
} catch (e) {
|
||||
if (e.code != 'ENOENT')
|
||||
throw e;
|
||||
|
||||
const error = new UserError(`File doesn't exists`);
|
||||
error.statusCode = 404;
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
const stream = EntryContainer.downloadStream(pathHash, dms.file);
|
||||
|
||||
return [stream, dms.contentType, `filename="${dms.file}"`];
|
||||
};
|
||||
Self.downloadFile = async id =>
|
||||
Self.app.models.Dms.downloadFileTemplate(id, 'EntryContainer');
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
||||
|
|
|
@ -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, options) =>
|
||||
Self.app.models.Dms.uploadFileTemplate(ctx, id, 'WorkerDms', 'workerFk', options);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue