-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
016c82c
commit c67dc21
Showing
14 changed files
with
265 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AnswerController } from './answer.controller'; | ||
|
||
describe('AnswerController', () => { | ||
let controller: AnswerController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AnswerController], | ||
}).compile(); | ||
|
||
controller = module.get<AnswerController>(AnswerController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Body, Controller, Get, Post } from '@nestjs/common'; | ||
import { Answer } from '@prisma/client'; | ||
import { AnswerService } from './answer.service'; | ||
import { AnsDto, AnswerDto, CreateAnswerDto } from './dtos/create-answer.dto'; | ||
import { QuestionService } from '../question/question.service'; | ||
import { AnswerTypeService } from '../answer-type/answer-type.service'; | ||
import { EncountersService } from '../encounters/encounters.service'; | ||
|
||
@Controller('answer') | ||
export class AnswerController { | ||
constructor( | ||
private answerService: AnswerService, | ||
private qstnService: QuestionService, | ||
private ansTypeService: AnswerTypeService, | ||
private encounterService: EncountersService | ||
) {} | ||
@Get() | ||
findAll(): Promise<Answer[]> { | ||
return this.answerService.findAll(); | ||
} | ||
@Post() | ||
async create(@Body() body: CreateAnswerDto) { | ||
const { encounterUuid, answers } = body; | ||
const encounter = await this.encounterService.findEncounterIdFromUuid( | ||
encounterUuid | ||
); | ||
const encounterId = encounter.id; | ||
for (let i = 0; i < answers.length; i++) { | ||
const qstn = answers[i]; | ||
await this.createOne(qstn, encounterId); | ||
} | ||
return this.answerService.findByEncounterId(encounterId); | ||
} | ||
async createOne(ans: AnsDto, encounterId: number) { | ||
const { answer, questionUuid, answerType } = ans; | ||
const qstn = await this.qstnService.findIdFromUuid(questionUuid); | ||
const questionId = qstn.id; | ||
const ansType = await this.ansTypeService.findIdByName(answerType); | ||
const answerTypeId = ansType.id; | ||
const payload: AnswerDto = { | ||
questionId: questionId, | ||
answerTypeId: answerTypeId, | ||
encounterId: encounterId, | ||
}; | ||
if (answerType === 'Text') { | ||
payload['valueText'] = answer as string; | ||
} else if (answerType === 'Number') { | ||
payload['valueNumber'] = parseInt(answer as string); | ||
} else if (answerType === 'Date' || answerType === 'Datetime') { | ||
payload['valueDateTime'] = new Date(answer as unknown as Date); | ||
} | ||
return this.answerService.createOne(payload); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AnswerService } from './answer.service'; | ||
import { AnswerController } from './answer.controller'; | ||
import { PrismaService } from '../app/prisma/prisma.service'; | ||
import { QuestionService } from '../question/question.service'; | ||
import { AnswerTypeService } from '../answer-type/answer-type.service'; | ||
import { EncountersService } from '../encounters/encounters.service'; | ||
|
||
@Module({ | ||
providers: [ | ||
AnswerService, | ||
PrismaService, | ||
QuestionService, | ||
AnswerTypeService, | ||
EncountersService, | ||
], | ||
controllers: [AnswerController], | ||
}) | ||
export class AnswerModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AnswerService } from './answer.service'; | ||
|
||
describe('AnswerService', () => { | ||
let service: AnswerService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [AnswerService], | ||
}).compile(); | ||
|
||
service = module.get<AnswerService>(AnswerService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PrismaService } from '../app/prisma/prisma.service'; | ||
|
||
@Injectable() | ||
export class AnswerService { | ||
constructor(private prismaService: PrismaService) {} | ||
findAll() { | ||
return this.prismaService.answer.findMany(); | ||
} | ||
createOne(answer: any) { | ||
return this.prismaService.answer.create({ | ||
data: { | ||
...answer, | ||
}, | ||
}); | ||
} | ||
findByEncounterId(encounterId: number) { | ||
return this.prismaService.answer.findMany({ | ||
where: { | ||
encounterId: encounterId, | ||
}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export interface AnsDto { | ||
questionUuid: string; | ||
answerType: 'Text' | 'Number' | 'Date' | 'Datetime'; | ||
answer: string | number; | ||
} | ||
export interface CreateAnswerDto { | ||
encounterUuid: string; | ||
answers: AnsDto[]; | ||
} | ||
|
||
export interface AnswerDto { | ||
encounterId: number; | ||
questionId: number; | ||
answerTypeId: number; | ||
valueText?: string; | ||
valueNumber?: number; | ||
valueDateTime?: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
prisma/migrations/20230710173442_added_datetime_column_in_answer_table/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
Warnings: | ||
- Added the required column `valueDateTime` to the `Answer` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "Answer" ADD COLUMN "valueDateTime" TIMESTAMP(3) NOT NULL; |
4 changes: 4 additions & 0 deletions
4
...ma/migrations/20230710181908_value_text_number_and_datetime_fields_optional/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-- AlterTable | ||
ALTER TABLE "Answer" ALTER COLUMN "valueText" DROP NOT NULL, | ||
ALTER COLUMN "valueNumber" DROP NOT NULL, | ||
ALTER COLUMN "valueDateTime" DROP NOT NULL; |
11 changes: 11 additions & 0 deletions
11
prisma/migrations/20230710183706_added_encounter_id_to_answer/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
Warnings: | ||
- Added the required column `encounterId` to the `Answer` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "Answer" ADD COLUMN "encounterId" INTEGER NOT NULL; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Answer" ADD CONSTRAINT "Answer_encounterId_fkey" FOREIGN KEY ("encounterId") REFERENCES "Encounter"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters