WIP: 7151-dms_refactor_methods #3164

Draft
alexm wants to merge 4 commits from 7151-dms_refactor_methods into dev
5 changed files with 37 additions and 59 deletions
Showing only changes of commit 379e7437f6 - Show all commits

View File

@ -16,12 +16,13 @@ module.exports = Self => {
return await models.DmsType.hasReadRole(ctx, dms.dmsTypeFk); return await models.DmsType.hasReadRole(ctx, dms.dmsTypeFk);
}; };
Self.getFile = async function(id, container = 'DmsContainer') { Self.getFile = async function(id) {
const Container = Self.app.models.models[container]; const models = Self.app.models;
const DmsContainer = models.DmsContainer;
const dms = await Self.findById(id); const dms = await Self.findById(id);
const pathHash = Container.getHash(dms.id); const pathHash = DmsContainer.getHash(dms.id);
try { try {
await Container.getFile(pathHash, dms.file); await DmsContainer.getFile(pathHash, dms.file);
} catch (e) { } catch (e) {
if (e.code != 'ENOENT') if (e.code != 'ENOENT')
throw e; throw e;
@ -32,7 +33,7 @@ module.exports = Self => {
throw error; throw error;
} }
const stream = Container.downloadStream(pathHash, dms.file); const stream = DmsContainer.downloadStream(pathHash, dms.file);
return [stream, dms.contentType, `filename="${dms.file}"`]; return [stream, dms.contentType, `filename="${dms.file}"`];
}; };
@ -127,4 +128,26 @@ module.exports = Self => {
throw e; 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}"`];
};
}; };

View File

@ -1,7 +1,5 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('downloadFile', { Self.remoteMethod('downloadFile', {
description: 'Get the claim file', description: 'Get the claim file',
accessType: 'READ', accessType: 'READ',
accepts: [ accepts: [
@ -36,25 +34,6 @@ module.exports = Self => {
accessScopes: ['DEFAULT', 'read:multimedia'] accessScopes: ['DEFAULT', 'read:multimedia']
}); });
Self.downloadFile = async function(ctx, id) { Self.downloadFile = async id =>
const models = Self.app.models; Self.app.models.Dms.downloadFileTemplate(id, 'ClaimContainer');
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}"`];
};
}; };

View File

@ -1,12 +1,9 @@
const app = require('vn-loopback/server/server'); const app = require('vn-loopback/server/server');
describe('claim downloadFile()', () => { describe('claim downloadFile()', () => {
const dmsId = 7;
it('should return a response for an employee with image content-type', async() => { it('should return a response for an employee with image content-type', async() => {
const workerId = 1107; const dmsId = 7;
const ctx = {req: {accessToken: {userId: workerId}}}; const result = await app.models.Claim.downloadFile(dmsId);
const result = await app.models.Claim.downloadFile(ctx, dmsId);
expect(result[1]).toEqual('image/jpeg'); expect(result[1]).toEqual('image/jpeg');
}); });

View File

@ -1,7 +1,5 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('downloadFile', { Self.remoteMethod('downloadFile', {
description: 'Get the entry file', description: 'Get the entry file',
accessType: 'READ', accessType: 'READ',
accepts: [ accepts: [
@ -35,25 +33,6 @@ module.exports = Self => {
} }
}); });
Self.downloadFile = async function(ctx, id) { Self.downloadFile = async id =>
const models = Self.app.models; Self.app.models.Dms.downloadFileTemplate(id, 'EntryContainer');
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}"`];
};
}; };

View File

@ -46,6 +46,6 @@ module.exports = Self => {
} }
}); });
Self.uploadFile = async(ctx, id) => Self.uploadFile = async(ctx, id, options) =>
Self.app.models.Dms.uploadFileTemplate(ctx, id, 'WorkerDms', 'workerFk', options); Self.app.models.Dms.uploadFileTemplate(ctx, id, 'WorkerDms', 'workerFk', options);
}; };