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

Features/forms response #115

Merged
merged 21 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
1c07429
Refactor getAllForms and getFormById methods to include subQuestions
siasktv Mar 8, 2024
afbbd56
Merge branch 'dev' of https://github.com/chingu-x/chingu-dashboard-be…
siasktv Mar 8, 2024
3101c4a
Modified response to return in/forms and /forms/id
siasktv Mar 8, 2024
8148504
Updated CHANGELOG.md
siasktv Mar 8, 2024
b087581
Update response in /forms /forms/id
siasktv Mar 11, 2024
1168e2d
Updated /forms /formsId response
siasktv Mar 21, 2024
08cdba7
Updated /forms /formsId response to properly include subQuestions und…
siasktv Mar 21, 2024
c1ae4a0
Merge remote-tracking branch 'origin/dev' into features/forms-response
siasktv Mar 24, 2024
4c60c77
Refactor form response classes and update form select query
siasktv Mar 24, 2024
37dede6
Delete prisma/migrations/20240305172254_1222/migration.sql
siasktv Mar 24, 2024
fbc8bdd
Merge branch 'dev' into features/forms-response
cherylli Mar 25, 2024
a740da8
Merge remote-tracking branch 'origin/dev' into features/forms-response
siasktv Mar 26, 2024
68888e9
Merge branch 'features/forms-response' of https://github.com/chingu-x…
siasktv Mar 26, 2024
198b6e3
Update src/forms/forms.response.ts
siasktv Mar 26, 2024
590d904
Refactor code
siasktv Mar 26, 2024
738dc6c
Change data/form in forms/service
siasktv Mar 26, 2024
ff4808b
Change data/form in forms/service
siasktv Mar 26, 2024
abfe53d
Merge branch 'dev' into features/forms-response
siasktv Mar 28, 2024
7dc0ef1
Merge branch 'dev' into features/forms-response
siasktv Apr 3, 2024
cc17c13
Refactor code and fix bugs
siasktv Apr 3, 2024
3ccbe10
Merge branch 'dev' into features/forms-response
siasktv Apr 5, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Another example [here](https://co-pilot.dev/changelog)
- Update changelog ([#104](https://github.com/chingu-x/chingu-dashboard-be/pull/104))
- Update test.yml to run e2e tests on pull requests to the main branch [#105](https://github.com/chingu-x/chingu-dashboard-be/pull/105)
- Update email templates to use domain in environment variables [#110](https://github.com/chingu-x/chingu-dashboard-be/pull/110)
-Update /forms /forms/id response to include subQuestions [#115](https://github.com/chingu-x/chingu-dashboard-be/pull/115)
- Add role and permission guard to some existing routes (features, forms, ideations, teams) [#112](https://github.com/chingu-x/chingu-dashboard-be/pull/112)
- Refactor voyages endpoint paths to follow API naming conversion [#123](https://github.com/chingu-x/chingu-dashboard-be/pull/123)
- Refactor resources PATCH and DELETE URI [#127](https://github.com/chingu-x/chingu-dashboard-be/pull/127)
Expand Down
17 changes: 14 additions & 3 deletions src/forms/forms.response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Response {
responseGroupId: number;
}

class Question {
class BaseQuestion {
@ApiProperty({ example: 1 })
id: number;

Expand Down Expand Up @@ -97,8 +97,19 @@ class Question {
@ApiProperty({ example: null })
optionGroup: OptionGroup;

@ApiProperty({ isArray: true })
responses: Response;
@ApiProperty()
createdAt: Date;

@ApiProperty()
updatedAt: Date;
}

class SubQuestion extends BaseQuestion {}

class Question extends BaseQuestion {
@Optional()
@ApiProperty({ isArray: true, type: SubQuestion })
subQuestions: SubQuestion[];
}

export class FormResponse {
Expand Down
77 changes: 74 additions & 3 deletions src/forms/forms.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, NotFoundException } from "@nestjs/common";
import { PrismaService } from "../prisma/prisma.service";
import { Prisma } from "@prisma/client";
import { Prisma, Question } from "@prisma/client";

export const formSelect = {
id: true,
Expand Down Expand Up @@ -39,6 +39,36 @@ export const formSelect = {
},
},
},
subQuestions: {
select: {
id: true,
order: true,
inputType: {
select: {
id: true,
name: true,
},
},
text: true,
description: true,
answerRequired: true,
multipleAllowed: true,
optionGroup: {
select: {
optionChoices: {
select: {
id: true,
text: true,
},
},
},
},
createdAt: true,
updatedAt: true,
},
},
createdAt: true,
updatedAt: true,
},
},
} as Prisma.FormSelect;
Expand All @@ -47,10 +77,34 @@ export const formSelect = {
export class FormsService {
constructor(private prisma: PrismaService) {}

getAllForms() {
return this.prisma.form.findMany({
async getAllForms() {
const forms = await this.prisma.form.findMany({
select: formSelect,
});

const subQuestionsIds = [];

forms.forEach((form) => {
form.questions.forEach((question) => {
const currentQuestion = question as Question & {
subQuestions: Question[];
};
currentQuestion.subQuestions.forEach((subQuestion) => {
subQuestionsIds.push(subQuestion.id);
});
});
});

for (let i = 0; i < forms.length; i++) {
const form = forms[i];
const filteredQuestions = form.questions.filter(
(i) => !subQuestionsIds.includes(i.id),
);

form.questions = filteredQuestions;
}

return forms;
}

async getFormById(formId: number) {
Expand All @@ -64,6 +118,23 @@ export class FormsService {
throw new NotFoundException(
`Invalid formId: Form (id:${formId}) does not exist.`,
);
const subQuestionsIds = [];

form.questions.forEach((question) => {
const currentQuestion = question as Question & {
subQuestions: Question[];
};
currentQuestion.subQuestions.forEach((subQuestion) => {
subQuestionsIds.push(subQuestion.id);
});
});

const filteredQuestions = form.questions.filter(
(i) => !subQuestionsIds.includes(i.id),
);

form.questions = filteredQuestions;

return form;
}
}
7 changes: 4 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -868,9 +868,9 @@
integrity sha512-cq8o4cWH0ibXh9VGi5P20Tu9XF/0fFXl9EUinr9QfTM7a7p0oTA4iJRCQWppXR1Pg8dSM0UCItCkPwsk9qWWYA==

"@prisma/client@^5.3.1":
version "5.11.0"
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-5.11.0.tgz#d8e55fab85163415b2245fb408b9106f83c8106d"
integrity sha512-SWshvS5FDXvgJKM/a0y9nDC1rqd7KG0Q6ZVzd+U7ZXK5soe73DJxJJgbNBt2GNXOa+ysWB4suTpdK5zfFPhwiw==
version "5.12.0"
resolved "https://registry.yarnpkg.com/@prisma/client/-/client-5.12.0.tgz#4d1fb68074307bf9445d06ad387139301aa4bcc4"
integrity sha512-bk/+KPpRm0+IzqFCtAxrj+/TNiHzulspnO+OkysaYY/atc/eX0Gx8V3tTLxbHKVX0LKD4Hi8KKCcSbU1U72n7Q==

"@prisma/debug@5.11.0":
version "5.11.0"
Expand Down Expand Up @@ -5682,6 +5682,7 @@ wide-align@^1.1.2:
string-width "^1.0.2 || 2 || 3 || 4"

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
name wrap-ansi-cjs
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand Down
Loading