Skip to content

Commit

Permalink
Added answers endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
maikofelix47 committed Jul 10, 2023
1 parent 016c82c commit c67dc21
Show file tree
Hide file tree
Showing 14 changed files with 265 additions and 57 deletions.
10 changes: 10 additions & 0 deletions apps/back-end/src/answer-type/answer-type.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ export class AnswerTypeService {
},
});
}
findIdByName(answerType: string) {
return this.prismaService.answerType.findFirstOrThrow({
where: {
name: answerType,
},
select: {
id: true,
},
});
}
}
18 changes: 18 additions & 0 deletions apps/back-end/src/answer/answer.controller.spec.ts
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();
});
});
54 changes: 54 additions & 0 deletions apps/back-end/src/answer/answer.controller.ts
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);
}
}
19 changes: 19 additions & 0 deletions apps/back-end/src/answer/answer.module.ts
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 {}
18 changes: 18 additions & 0 deletions apps/back-end/src/answer/answer.service.spec.ts
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();
});
});
24 changes: 24 additions & 0 deletions apps/back-end/src/answer/answer.service.ts
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,
},
});
}
}
18 changes: 18 additions & 0 deletions apps/back-end/src/answer/dtos/create-answer.dto.ts
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;
}
2 changes: 2 additions & 0 deletions apps/back-end/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { UsersModule } from '../users/users.module';
import { AuthModule } from '../auth/auth.module';
import { QuestionModule } from '../question/question.module';
import { AnswerTypeModule } from '../answer-type/answer-type.module';
import { AnswerModule } from '../answer/answer.module';

@Module({
imports: [
Expand All @@ -32,6 +33,7 @@ import { AnswerTypeModule } from '../answer-type/answer-type.module';
AuthModule,
QuestionModule,
AnswerTypeModule,
AnswerModule,
],
controllers: [AppController],
providers: [PrismaService, AppService],
Expand Down
100 changes: 54 additions & 46 deletions apps/back-end/src/encounters/encounters.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,58 @@ import { CreateEncountePayloadDto } from './dtos/encounter.dto';

@Injectable()
export class EncountersService {
constructor(private prismaService: PrismaService){

}
findAll(){
return this.prismaService.encounter.findMany({
select:{
uuid: true,
encounterDate: true,
location:{
select:{
uuid: true,
name: true
}
},
visit: {
select: {
uuid: true
}
},
encounterType:{
select:{
uuid: true,
name: true
}
}
}
});
}
create(body: CreateEncountePayloadDto){
return this.prismaService.encounter.create({
data:{
encounterDate: body.encounterDate,
locationId: body.locationId,
visitId: body.visitId,
patientId: body.patientId,
encounterTypeId: body.encounterTypeId
}
});
}
findPatientEncountersCount(patientId: number){
return this.prismaService.encounter.count({
where:{
patientId: patientId
}
});
}
constructor(private prismaService: PrismaService) {}
findAll() {
return this.prismaService.encounter.findMany({
select: {
uuid: true,
encounterDate: true,
location: {
select: {
uuid: true,
name: true,
},
},
visit: {
select: {
uuid: true,
},
},
encounterType: {
select: {
uuid: true,
name: true,
},
},
},
});
}
create(body: CreateEncountePayloadDto) {
return this.prismaService.encounter.create({
data: {
encounterDate: body.encounterDate,
locationId: body.locationId,
visitId: body.visitId,
patientId: body.patientId,
encounterTypeId: body.encounterTypeId,
},
});
}
findPatientEncountersCount(patientId: number) {
return this.prismaService.encounter.count({
where: {
patientId: patientId,
},
});
}
findEncounterIdFromUuid(uuid: string) {
return this.prismaService.encounter.findFirstOrThrow({
where: {
uuid: uuid,
},
select: {
id: true,
},
});
}
}
10 changes: 10 additions & 0 deletions apps/back-end/src/question/question.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ export class QuestionService {
},
});
}
findIdFromUuid(uuid: string) {
return this.prismaService.question.findFirstOrThrow({
where: {
uuid: uuid,
},
select: {
id: true,
},
});
}
}
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;
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;
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;
26 changes: 15 additions & 11 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ model Encounter {
updatedAt DateTime @updatedAt
visit Visit @relation(fields: [visitId], references: [id])
visitId Int
Answer Answer[]
}

model ProgramEnrollment {
Expand Down Expand Up @@ -187,15 +188,18 @@ model AnswerType {
}

model Answer {
id Int @id @default(autoincrement())
question Question @relation(fields: [questionId], references: [id])
questionId Int
answerType AnswerType @relation(fields: [answerTypeId], references: [id])
answerTypeId Int
valueText String
valueNumber Int
uuid String @default(uuid())
voided Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
id Int @id @default(autoincrement())
encounter Encounter @relation(fields: [encounterId], references: [id])
encounterId Int
question Question @relation(fields: [questionId], references: [id])
questionId Int
answerType AnswerType @relation(fields: [answerTypeId], references: [id])
answerTypeId Int
valueText String?
valueNumber Int?
valueDateTime DateTime?
uuid String @default(uuid())
voided Boolean @default(false)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

0 comments on commit c67dc21

Please sign in to comment.