Skip to content

Commit

Permalink
fix program creation parameters & some route parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mhd-hi committed Aug 14, 2024
1 parent 69466d6 commit 1d703d3
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 42 deletions.
2 changes: 1 addition & 1 deletion prisma/ERD.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
Warnings:
- The primary key for the `Course` table will be changed. If it partially fails, the table could be left without primary key constraint.
- You are about to drop the column `programId` on the `Course` table. All the data in the column will be lost.
- The primary key for the `CoursePrerequisite` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The primary key for the `Program` table will be changed. If it partially fails, the table could be left without primary key constraint.
- You are about to drop the column `programTypeIds` on the `Program` table. All the data in the column will be lost.
- The primary key for the `ProgramCourse` table will be changed. If it partially fails, the table could be left without primary key constraint.
- Changed the type of `id` on the `Course` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `courseId` on the `CourseInstance` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `courseId` on the `CoursePrerequisite` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `prerequisiteId` on the `CoursePrerequisite` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `id` on the `Program` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Added the required column `typicalSessionIndex` to the `ProgramCourse` table without a default value. This is not possible if the table is not empty.
- Changed the type of `courseId` on the `ProgramCourse` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `programId` on the `ProgramCourse` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
*/
-- DropForeignKey
ALTER TABLE "CourseInstance" DROP CONSTRAINT "CourseInstance_courseId_fkey";

-- DropForeignKey
ALTER TABLE "CoursePrerequisite" DROP CONSTRAINT "CoursePrerequisite_courseId_fkey";

-- DropForeignKey
ALTER TABLE "CoursePrerequisite" DROP CONSTRAINT "CoursePrerequisite_prerequisiteId_fkey";

-- DropForeignKey
ALTER TABLE "ProgramCourse" DROP CONSTRAINT "ProgramCourse_courseId_fkey";

-- DropForeignKey
ALTER TABLE "ProgramCourse" DROP CONSTRAINT "ProgramCourse_programId_fkey";

-- DropIndex
DROP INDEX "Course_code_programId_idx";

-- AlterTable
ALTER TABLE "Course" DROP CONSTRAINT "Course_pkey",
DROP COLUMN "programId",
DROP COLUMN "id",
ADD COLUMN "id" INTEGER NOT NULL,
ADD CONSTRAINT "Course_pkey" PRIMARY KEY ("id");

-- AlterTable
ALTER TABLE "CourseInstance" DROP COLUMN "courseId",
ADD COLUMN "courseId" INTEGER NOT NULL;

-- AlterTable
ALTER TABLE "CoursePrerequisite" DROP CONSTRAINT "CoursePrerequisite_pkey",
DROP COLUMN "courseId",
ADD COLUMN "courseId" INTEGER NOT NULL,
DROP COLUMN "prerequisiteId",
ADD COLUMN "prerequisiteId" INTEGER NOT NULL,
ADD CONSTRAINT "CoursePrerequisite_pkey" PRIMARY KEY ("courseId", "prerequisiteId");

-- AlterTable
ALTER TABLE "Program" DROP CONSTRAINT "Program_pkey",
DROP COLUMN "programTypeIds",
DROP COLUMN "id",
ADD COLUMN "id" INTEGER NOT NULL,
ADD CONSTRAINT "Program_pkey" PRIMARY KEY ("id");

-- AlterTable
ALTER TABLE "ProgramCourse" DROP CONSTRAINT "ProgramCourse_pkey",
ADD COLUMN "typicalSessionIndex" INTEGER NOT NULL,
DROP COLUMN "courseId",
ADD COLUMN "courseId" INTEGER NOT NULL,
DROP COLUMN "programId",
ADD COLUMN "programId" INTEGER NOT NULL,
ADD CONSTRAINT "ProgramCourse_pkey" PRIMARY KEY ("courseId", "programId");

-- CreateTable
CREATE TABLE "_ProgramToProgramType" (
"A" INTEGER NOT NULL,
"B" INTEGER NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "_ProgramToProgramType_AB_unique" ON "_ProgramToProgramType"("A", "B");

-- CreateIndex
CREATE INDEX "_ProgramToProgramType_B_index" ON "_ProgramToProgramType"("B");

-- CreateIndex
CREATE INDEX "Course_code_idx" ON "Course"("code");

-- CreateIndex
CREATE INDEX "CourseInstance_courseId_sessionId_idx" ON "CourseInstance"("courseId", "sessionId");

-- CreateIndex
CREATE UNIQUE INDEX "CourseInstance_courseId_sessionId_key" ON "CourseInstance"("courseId", "sessionId");

-- AddForeignKey
ALTER TABLE "CourseInstance" ADD CONSTRAINT "CourseInstance_courseId_fkey" FOREIGN KEY ("courseId") REFERENCES "Course"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CoursePrerequisite" ADD CONSTRAINT "CoursePrerequisite_courseId_fkey" FOREIGN KEY ("courseId") REFERENCES "Course"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "CoursePrerequisite" ADD CONSTRAINT "CoursePrerequisite_prerequisiteId_fkey" FOREIGN KEY ("prerequisiteId") REFERENCES "Course"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ProgramCourse" ADD CONSTRAINT "ProgramCourse_courseId_fkey" FOREIGN KEY ("courseId") REFERENCES "Course"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ProgramCourse" ADD CONSTRAINT "ProgramCourse_programId_fkey" FOREIGN KEY ("programId") REFERENCES "Program"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_ProgramToProgramType" ADD CONSTRAINT "_ProgramToProgramType_A_fkey" FOREIGN KEY ("A") REFERENCES "Program"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_ProgramToProgramType" ADD CONSTRAINT "_ProgramToProgramType_B_fkey" FOREIGN KEY ("B") REFERENCES "ProgramType"("id") ON DELETE CASCADE ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Program" ALTER COLUMN "code" SET DATA TYPE TEXT;
33 changes: 17 additions & 16 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ model Session {

model CourseInstance {
id String @id @default(uuid())
courseId String
courseId Int
sessionId String
course Course @relation(fields: [courseId], references: [id])
Expand All @@ -47,8 +47,7 @@ model CourseInstance {
}

model Course {
id String @id
programId String
id Int @id
code String @unique
title String
description String
Expand All @@ -62,12 +61,12 @@ model Course {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([code, programId])
@@index([code])
}

model CoursePrerequisite {
courseId String
prerequisiteId String
courseId Int
prerequisiteId Int
course Course @relation("CourseToPrerequisite", fields: [courseId], references: [id])
prerequisite Course @relation("PrerequisiteToCourse", fields: [prerequisiteId], references: [id])
Expand All @@ -79,9 +78,9 @@ model CoursePrerequisite {
}

model ProgramCourse {
courseId String
programId String
typicalSession Int
courseId Int
programId Int
typicalSessionIndex Int
course Course @relation(fields: [courseId], references: [id])
program Program @relation(fields: [programId], references: [id])
Expand All @@ -93,13 +92,14 @@ model ProgramCourse {
}

model Program {
id String @id
code Int @unique
title String
credits String
cycle Int
url String
programTypeIds Json
id Int @id
code String @unique
title String
credits String
cycle Int
url String
programTypes ProgramType[]
horaireCoursPdfJson Json?
planificationPdfJson Json?
courses ProgramCourse[]
Expand All @@ -113,4 +113,5 @@ model Program {
model ProgramType {
id Int @id
title String
programs Program[]
}
16 changes: 13 additions & 3 deletions src/common/api-helper/ets/program/ets-program.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ import { firstValueFrom } from 'rxjs';

import { ETS_API_GET_ALL_PROGRAMS } from '../../../constants/url';

type Program = {
interface ProgramETSAPI {
id: number;
title: string;
cycle: string;
code: string;
credits: string;
types: number[];
url: string;
}

export type Program = {
id: number;
title: string;
cycle: string;
code: string;
credits: string;
programTypes: { id: number }[];
url: string;
};

export interface ProgramType {
Expand All @@ -33,13 +43,13 @@ export class EtsProgramService {

const types: ProgramType[] = response.data.types;
const programs: Program[] = response.data.results.map(
(program: Program) => ({
(program: ProgramETSAPI) => ({
id: program.id,
title: program.title,
cycle: program.cycle,
code: program.code,
credits: program.credits,
types: program.types,
programTypes: program.types.map((typeId) => ({ id: typeId })),
url: program.url,
}),
);
Expand Down
4 changes: 2 additions & 2 deletions src/common/exceptions/dtos/id.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';
import { IsInt } from 'class-validator';

export class IdDto {
@ApiProperty()
@IsString()
@IsInt()
public id!: number;
}
6 changes: 3 additions & 3 deletions src/course-prerequisite/course-prerequisite.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Controller, Get, Param } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';

import { IdDto } from '../common/exceptions/dtos/id.dto';
import { CoursePrerequisiteService } from './course-prerequisite.service';

@ApiTags('Course prerequisites')
Expand All @@ -11,9 +12,8 @@ export class CoursePrerequisiteController {
) {}

@Get(':courseId')
public async getCoursePrerequisites(@Param('courseId') courseId: string) {
//TODO: Change to int IdDto? (Prisma update uuid to int)
return this.coursePrerequisiteService.getPrerequisites(courseId);
public async getCoursePrerequisites(@Param('courseId') { id }: IdDto) {
return this.coursePrerequisiteService.getPrerequisites({ courseId: id });
}

@Get()
Expand Down
6 changes: 3 additions & 3 deletions src/course-prerequisite/course-prerequisite.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export class CoursePrerequisiteService {

private logger = new Logger(CoursePrerequisiteService.name);

public async getPrerequisites(courseId: string) {
this.logger.log('coursePrerequisiteById');
public async getPrerequisites(data: Prisma.CoursePrerequisiteWhereInput) {
this.logger.log('coursePrerequisiteById', data);

return this.prisma.coursePrerequisite.findMany({
where: { courseId },
where: data,
include: { prerequisite: true },
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/course/course.controller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Controller, Get, Param } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';

import { IdDto } from '../common/exceptions/dtos/id.dto';
import { CourseService } from './course.service';
@ApiTags('Courses')
@Controller('courses')
export class CourseController {
constructor(private readonly courseService: CourseService) {}

@Get(':id')
public getCourse(@Param('id') id: string) {
//TODO: Change this for int IdDto? (Prisma update uuid to int)
public getCourse(@Param('id') { id }: IdDto) {
return this.courseService.getCourse({ id });
}

Expand Down
4 changes: 2 additions & 2 deletions src/course/course.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class CourseService {
return this.prisma.course.findMany();
}

public async getCoursesByProgram(programId: string): Promise<Course[]> {
public async getCoursesByProgram(programId: number): Promise<Course[]> {
this.logger.log('getCoursesByProgram', programId);

return this.prisma.course.findMany({
Expand All @@ -42,7 +42,7 @@ export class CourseService {
}

public async getCourseAvailability(
courseId: string,
courseId: number,
): Promise<{ session: Session; available: boolean }[]> {
this.logger.log('getCourseAvailability', courseId);

Expand Down
5 changes: 2 additions & 3 deletions src/program/program.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { ApiTags } from '@nestjs/swagger';
import { Program } from '@prisma/client';

import { UuidDto } from '../common/exceptions/dtos/uuid.dto';
import { IdDto } from '../common/exceptions/dtos/id.dto';
import { ProgramService } from './program.service';

@ApiTags('Programs')
Expand All @@ -18,8 +18,7 @@ export class ProgramController {

@Get(':id')
@UsePipes(new ValidationPipe({ transform: true }))
//TODO: Change this to use IdDto when Prisma is updated (uuid to id)
public async getProgram(@Param() { id }: UuidDto): Promise<Program | null> {
public async getProgram(@Param() { id }: IdDto): Promise<Program | null> {
return this.programService.getProgram({ id });
}

Expand Down
14 changes: 7 additions & 7 deletions src/program/program.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ export class ProgramService {
this.logger.log('createProgram', data);

return this.prisma.program.create({
data: {
...data,
programTypeIds: data.programTypeIds ?? '[]',
},
data,
});
}

Expand All @@ -48,21 +45,24 @@ export class ProgramService {
update: {
...data,
updatedAt: new Date(),
programTypeIds: data.programTypeIds ?? '[]',
programTypes: {
set: data.programTypes?.connect || [], //clears all relations and set new ones
},
},
create: {
...data,
createdAt: new Date(),
updatedAt: new Date(),
programTypeIds: data.programTypeIds ?? '[]',
programTypes: {
connect: data.programTypes?.connect || [],
},
},
});
}

public async upsertPrograms(
data: Prisma.ProgramCreateInput[],
): Promise<Program[]> {
this.logger.log('upsertPrograms', data);
return Promise.all(
data.map((programData) => this.upsertProgram(programData)),
);
Expand Down

0 comments on commit 1d703d3

Please sign in to comment.