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

fix: handle the deletion of subtemplate answers #918

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
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
127 changes: 123 additions & 4 deletions apps/backend/src/mutations/QuestionaryMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { container, inject, injectable } from 'tsyringe';

import { QuestionaryAuthorization } from '../auth/QuestionaryAuthorization';
import { Tokens } from '../config/Tokens';
import { GenericTemplateDataSource } from '../datasources/GenericTemplateDataSource';
import { ProposalDataSource } from '../datasources/ProposalDataSource';
import { QuestionaryDataSource } from '../datasources/QuestionaryDataSource';
import { TemplateDataSource } from '../datasources/TemplateDataSource';
import { Authorized, EventBus } from '../decorators';
Expand All @@ -15,10 +17,15 @@ import {
import { AnswerBasic } from '../models/Questionary';
import { getQuestionDefinition } from '../models/questionTypes/QuestionRegistry';
import { rejection } from '../models/Rejection';
import { DataType } from '../models/Template';
import { UserJWT, UserWithRole } from '../models/User';
import { AnswerTopicArgs } from '../resolvers/mutations/AnswerTopicMutation';
import {
AnswerInput,
AnswerTopicArgs,
} from '../resolvers/mutations/AnswerTopicMutation';
import { CreateQuestionaryArgs } from '../resolvers/mutations/CreateQuestionaryMutation';
import { UpdateAnswerArgs } from '../resolvers/mutations/UpdateAnswerMutation';
import { SubTemplateConfig } from '../resolvers/types/FieldConfig';

@injectable()
export default class QuestionaryMutations {
Expand All @@ -28,13 +35,19 @@ export default class QuestionaryMutations {
@inject(Tokens.QuestionaryDataSource)
private dataSource: QuestionaryDataSource,
@inject(Tokens.TemplateDataSource)
private templateDataSource: TemplateDataSource
private templateDataSource: TemplateDataSource,
@inject(Tokens.GenericTemplateDataSource)
private genericTemplateDataSource: GenericTemplateDataSource,
@inject(Tokens.ProposalDataSource)
private proposalDataSource: ProposalDataSource
) {}

async deleteOldAnswers(
templateId: number,
questionaryId: number,
topicId: number
topicId: number,
answers: AnswerInput[],
agent: UserWithRole | null
) {
const templateSteps =
await this.templateDataSource.getTemplateSteps(templateId);
Expand All @@ -53,9 +66,109 @@ export default class QuestionaryMutations {
const questionIds: string[] = stepQuestions.map(
(question) => question.question.id
);

const genericTemplateQuestions = stepQuestions.filter(
(step) => step.question.dataType == DataType.GENERIC_TEMPLATE
);

if (genericTemplateQuestions) {
deepaksftc marked this conversation as resolved.
Show resolved Hide resolved
// Confirm dependency with dependency condition
const unsatisfiedDependencyQues = genericTemplateQuestions.filter(
(genericTemplateQues) => {
const notSatisfiedQuestions = genericTemplateQues.dependencies.filter(
(dependency) => {
const answer = answers.find(
(answer) => answer.questionId === dependency.dependencyId
);

if (answer) {
const { value } = JSON.parse(answer.value);
if (value[0] !== dependency.condition.params) {
return true;
}
}
}
);

if (notSatisfiedQuestions.length != 0) {
deepaksftc marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
}
);

unsatisfiedDependencyQues.forEach((subTemplateQues) => {
const config = subTemplateQues.config as SubTemplateConfig;
this.deleteSubTemplatesAnswers(
subTemplateQues.question.id,
config.templateId,
questionaryId,
agent
);
deepaksftc marked this conversation as resolved.
Show resolved Hide resolved
});
}

await this.dataSource.deleteAnswers(questionaryId, questionIds);
}

async deleteSubTemplatesAnswers(
questionId: string,
genericTemplateId: number | null,
questionaryId: number,
agent: UserWithRole | null
): Promise<void> {
const proposal = (
await this.proposalDataSource.getProposals({
questionaryIds: [questionaryId],
})
).proposals[0];

const genericTemplates =
await this.genericTemplateDataSource.getGenericTemplates(
{
filter: {
questionId: questionId,
proposalPk: proposal?.primaryKey,
},
},
agent
);

if (!genericTemplates || !genericTemplateId) {
return;
}
deepaksftc marked this conversation as resolved.
Show resolved Hide resolved

const templateSteps =
await this.templateDataSource.getTemplateSteps(genericTemplateId);
const genericTemplateStepQuestions = templateSteps.flatMap(
(step) => step.fields
);

if (genericTemplateStepQuestions === undefined) {
logger.logError('Expected to find sub template step, but was not found', {
genericTemplateId,
questionaryId,
});
throw new GraphQLError(
'Expected to find sub template step, but was not found'
);
}
deepaksftc marked this conversation as resolved.
Show resolved Hide resolved

const genericTemplateQuestionIds: string[] | undefined =
genericTemplateStepQuestions?.map((question) => question.question.id);
deepaksftc marked this conversation as resolved.
Show resolved Hide resolved

if (!genericTemplateQuestionIds) {
return;
}

genericTemplates.forEach(async (genericTemplate) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same issue with the await is here

await this.dataSource.deleteAnswers(
genericTemplate.questionaryId,
genericTemplateQuestionIds
);
await this.genericTemplateDataSource.delete(genericTemplate.id);
});
}

@Authorized()
@EventBus(Event.TOPIC_ANSWERED)
async answerTopic(agent: UserWithRole | null, args: AnswerTopicArgs) {
Expand Down Expand Up @@ -89,7 +202,13 @@ export default class QuestionaryMutations {
);
}

await this.deleteOldAnswers(template.templateId, questionaryId, topicId);
await this.deleteOldAnswers(
template.templateId,
questionaryId,
topicId,
answers,
agent
);

const updatedAnswers: AnswerBasic[] = [];
for (const answer of answers) {
Expand Down
Loading