Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: optimise generic template copying #888

Merged
merged 13 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions apps/backend/db_patches/0165_AddViewForGenericTemplates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
DO
$$
BEGIN
IF register_patch('AddViewForGenericTemplates.sql', 'ellenwright', 'Add new view for generic templates', '2024-11-13') THEN
BEGIN

CREATE VIEW generic_templates_view as select
g.generic_template_id as generic_template_id, g.title, g.creator_id, g.proposal_pk, g.questionary_id, g.question_id, g.created_at,
fr.user_id as fap_reviewer,
fc.user_id as fap_chair,
fs.user_id as fap_secretary,
i.manager_user_id as instrument_manager,
ihs.user_id as scientist_on_proposal,
vu.user_id as visitor

from generic_templates g
left join visits v on v.proposal_pk = g.proposal_pk
left join visits_has_users vu on vu.visit_id = v.visit_id
left join fap_reviews fr on fr.proposal_pk = g.proposal_pk
left join fap_chairs fc on fc.fap_id = fr.fap_id
left join fap_secretaries fs on fs.fap_id = fr.fap_id
left join instrument_has_proposals ihp on ihp.proposal_pk = g.proposal_pk
left join instrument_has_scientists ihs on ihs.instrument_id = ihp.instrument_id
left join instruments i on i.instrument_id = ihp.instrument_id;

END;
END IF;
END;
$$
LANGUAGE plpgsql;
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ export class GenericTemplateQuestionaryAuthorizer
}

const queryResult =
await this.genericTemplateDataSource.getGenericTemplates({
filter: { questionaryIds: [questionaryId] },
});
await this.genericTemplateDataSource.getGenericTemplates(
{
filter: { questionaryIds: [questionaryId] },
},
agent
);

if (queryResult.length !== 1) {
logger.logError(
Expand Down
11 changes: 10 additions & 1 deletion apps/backend/src/datasources/GenericTemplateDataSource.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { GenericTemplate } from '../models/GenericTemplate';
import { Role } from '../models/Role';
import { UserWithRole } from '../models/User';
import { UpdateGenericTemplateArgs } from '../resolvers/mutations/UpdateGenericTemplateMutation';
import { GenericTemplatesArgs } from '../resolvers/queries/GenericTemplatesQuery';

Expand All @@ -21,7 +23,14 @@ export interface GenericTemplateDataSource {
getGenericTemplate(
genericTemplateId: number
): Promise<GenericTemplate | null>;
getGenericTemplates(args: GenericTemplatesArgs): Promise<GenericTemplate[]>;
getGenericTemplates(
args: GenericTemplatesArgs,
agent: UserWithRole | null
): Promise<GenericTemplate[]>;
getGenericTemplatesForCopy(
userId?: number,
role?: Role
): Promise<GenericTemplate[]>;
createGenericTemplateWithCopiedAnswers(
title: string,
creatorId: number,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { GenericTemplate } from '../../models/GenericTemplate';
import { Role } from '../../models/Role';
import { UserWithRole } from '../../models/User';
import { UpdateGenericTemplateArgs } from '../../resolvers/mutations/UpdateGenericTemplateMutation';
import { GenericTemplatesArgs } from '../../resolvers/queries/GenericTemplatesQuery';
import { GenericTemplateDataSource } from '../GenericTemplateDataSource';
Expand Down Expand Up @@ -35,7 +37,15 @@ export class GenericTemplateDataSourceMock
}

async getGenericTemplates(
_args: GenericTemplatesArgs
_args: GenericTemplatesArgs,
agent: UserWithRole | null
): Promise<GenericTemplate[]> {
return this.genericTemplates;
}

async getGenericTemplatesForCopy(
userId?: number,
role?: Role
): Promise<GenericTemplate[]> {
return this.genericTemplates;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { inject, injectable } from 'tsyringe';

import { Tokens } from '../../config/Tokens';
import { GenericTemplate } from '../../models/GenericTemplate';
import { Role, Roles } from '../../models/Role';
import { UpdateGenericTemplateArgs } from '../../resolvers/mutations/UpdateGenericTemplateMutation';
import { GenericTemplatesArgs } from '../../resolvers/queries/GenericTemplatesQuery';
import { SubTemplateConfig } from '../../resolvers/types/FieldConfig';
Expand Down Expand Up @@ -247,4 +248,39 @@ export default class PostgresGenericTemplateDataSource
records.map((record) => createGenericTemplateObject(record))
);
}

async getGenericTemplatesForCopy(
userId?: number | null,
role?: Role
): Promise<GenericTemplate[]> {
if (
!(
role?.shortCode == Roles.INSTRUMENT_SCIENTIST ||
role?.shortCode == Roles.USER_OFFICER
)
) {
return database
.select('*')
.from('generic_templates_view')
.modify((query) => {
// add filter for user role
query.where('fap_reviewer', userId);
query.orWhere('creator_id', userId);
query.orWhere('scientist_on_proposal', userId);
query.orWhere('fap_chair', userId);
query.orWhere('fap_secretary', userId);
query.orWhere('instrument_manager', userId);
query.orWhere('visitor', userId);
query.distinctOn('generic_template_id');
})
.orderBy('generic_template_id', 'asc')
.then((records: GenericTemplateRecord[]) =>
records.map((record) => createGenericTemplateObject(record))
);
} else {
const args: GenericTemplatesArgs = {};

return this.getGenericTemplates(args);
}
}
}
9 changes: 6 additions & 3 deletions apps/backend/src/factory/pdf/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,12 @@ export const collectProposalPDFDataTokenAccess = async (
Tokens.GenericTemplateDataSource
);

const genericTemplates = await genericTemplateDataSource.getGenericTemplates({
filter: { proposalPk: proposal.primaryKey },
});
const genericTemplates = await genericTemplateDataSource.getGenericTemplates(
{
filter: { proposalPk: proposal.primaryKey },
},
user
);

const genericTemplatePDFData = (
await Promise.all(
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/factory/zip/attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ const init = (user: UserWithRole) => {
.resolve<GenericTemplateDataSource>(
Tokens.GenericTemplateDataSource
)
.getGenericTemplates(args);
.getGenericTemplates(args, user);
}

return baseContext.queries.genericTemplate.getGenericTemplates(
Expand Down
9 changes: 6 additions & 3 deletions apps/backend/src/mutations/ProposalMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,9 +875,12 @@ export default class ProposalMutations {
}

const proposalGenericTemplates =
await this.genericTemplateDataSource.getGenericTemplates({
filter: { proposalPk: sourceProposal.primaryKey },
});
await this.genericTemplateDataSource.getGenericTemplates(
{
filter: { proposalPk: sourceProposal.primaryKey },
},
agent
);

for await (const genericTemplate of proposalGenericTemplates) {
const clonedGenericTemplate =
Expand Down
21 changes: 20 additions & 1 deletion apps/backend/src/queries/GenericTemplateQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,26 @@ export default class GenericTemplateQueries {
agent: UserWithRole | null,
args: GenericTemplatesArgs
) {
let genericTemplates = await this.dataSource.getGenericTemplates(args);
let genericTemplates = await this.dataSource.getGenericTemplates(
args,
agent
);

genericTemplates = await Promise.all(
genericTemplates.map((genericTemplate) =>
this.genericTemplateAuth.hasReadRights(agent, genericTemplate.id)
)
).then((results) => genericTemplates.filter((_v, index) => results[index]));

return genericTemplates;
}

@Authorized()
jekabs-karklins marked this conversation as resolved.
Show resolved Hide resolved
async genericTemplatesOnCopy(agent: UserWithRole | null) {
let genericTemplates = await this.dataSource.getGenericTemplatesForCopy(
agent?.id,
agent?.currentRole
);

genericTemplates = await Promise.all(
genericTemplates.map((genericTemplate) =>
Expand Down
10 changes: 10 additions & 0 deletions apps/backend/src/resolvers/queries/GenericTemplatesQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@ export class GenericTemplatesQuery {

return response;
}

@Query(() => [GenericTemplate], { nullable: true })
async genericTemplatesOnCopy(@Ctx() context: ResolverContext) {
const response =
await context.queries.genericTemplate.genericTemplatesOnCopy(
context.user
);

return response;
}
}
Loading
Loading