Skip to content

Commit

Permalink
feat: 752 add proposal pdf download by key or id through token access
Browse files Browse the repository at this point in the history
  • Loading branch information
mutambaraf committed Nov 28, 2022
1 parent 056f7b4 commit d3f7971
Show file tree
Hide file tree
Showing 7 changed files with 523 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Roles } from '../models/Role';
import { UserWithRole } from '../models/User';
type Descriptor = {
value?: (agent: UserWithRole, ...args: any[]) => Promise<any>;
};

const FactoryServicesAuthorized = (roles: Roles[] = []) => {
return (target: any, name: string, descriptor: Descriptor) => {
const originalMethod = descriptor.value;

descriptor.value = async function (...args) {
const [agent] = args;

if (agent?.isApiAccessToken) {
if (agent?.accessPermissions?.[`${target.constructor.name}.${name}`]) {
return await originalMethod?.apply(this, args);
} else {
throw new Error('INSUFFICIENT_PERMISSIONS');
}
}

if (!agent) {
throw new Error('NOT_LOGGED_IN');
}

if (agent.externalToken && !agent.externalTokenValid) {
throw new Error('EXTERNAL_TOKEN_INVALID');
}

if (roles.length === 0) {
return await originalMethod?.apply(this, args);
}

const hasAccessRights = roles.some(
(role) => role === agent.currentRole?.shortCode
);

if (hasAccessRights) {
return await originalMethod?.apply(this, args);
} else {
throw new Error('INSUFFICIENT_PERMISSIONS');
}
};
};
};

export default FactoryServicesAuthorized;
27 changes: 17 additions & 10 deletions apps/user-office-backend/src/factory/pdf/genericTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getAllFields,
areDependenciesSatisfied,
} from '../../models/ProposalModelFunctions';
import { Answer } from '../../models/Questionary';
import { Answer, Questionary, QuestionaryStep } from '../../models/Questionary';
import { UserWithRole } from '../../models/User';
import { getFileAttachments, Attachment } from '../util';

Expand All @@ -17,13 +17,17 @@ export type GenericTemplatePDFData = {
export async function collectGenericTemplatePDFData(
genericTemplateId: number,
user: UserWithRole,
notify?: CallableFunction
notify?: CallableFunction,
newGenericTemplate?: GenericTemplate,
newQuestionary?: Questionary,
newQuestionarySteps?: QuestionaryStep[]
): Promise<GenericTemplatePDFData> {
const genericTemplate =
await baseContext.queries.genericTemplate.getGenericTemplate(
newGenericTemplate ||
(await baseContext.queries.genericTemplate.getGenericTemplate(
user,
genericTemplateId
);
));
if (!genericTemplate) {
throw new Error(
`GenericTemplate with ID '${genericTemplateId}' not found, or the user has insufficient rights`
Expand All @@ -32,10 +36,12 @@ export async function collectGenericTemplatePDFData(

notify?.(`genericTemplate_${genericTemplate.id}.pdf`);

const questionary = await baseContext.queries.questionary.getQuestionary(
user,
genericTemplate.questionaryId
);
const questionary =
newQuestionary ||
(await baseContext.queries.questionary.getQuestionary(
user,
genericTemplate.questionaryId
));

if (!questionary) {
throw new Error(
Expand All @@ -44,10 +50,11 @@ export async function collectGenericTemplatePDFData(
}

const questionarySteps =
await baseContext.queries.questionary.getQuestionarySteps(
newQuestionarySteps ||
(await baseContext.queries.questionary.getQuestionarySteps(
user,
genericTemplate.questionaryId
);
));

if (!questionarySteps) {
throw new Error(
Expand Down
6 changes: 3 additions & 3 deletions apps/user-office-backend/src/factory/pdf/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
GenericTemplatePDFData,
} from './genericTemplates';
import { collectSamplePDFData, SamplePDFData } from './sample';
type ProposalPDFData = {
export type ProposalPDFData = {
proposal: Proposal;
principalInvestigator: BasicUserDetails;
coProposers: BasicUserDetails[];
Expand All @@ -40,7 +40,7 @@ type ProposalPDFData = {
pdfTemplate: PdfTemplate | null;
};

const getTechnicalReviewHumanReadableStatus = (
export const getTechnicalReviewHumanReadableStatus = (
status: TechnicalReviewStatus | null
): string => {
switch (status) {
Expand All @@ -55,7 +55,7 @@ const getTechnicalReviewHumanReadableStatus = (
}
};

const getTopicActiveAnswers = (
export const getTopicActiveAnswers = (
questionarySteps: QuestionaryStep[],
topicId: number
) => {
Expand Down
Loading

0 comments on commit d3f7971

Please sign in to comment.