From 7daf73bccedb044df0176efd355f1d5adea76b39 Mon Sep 17 00:00:00 2001 From: Avantika0503 Date: Tue, 14 Mar 2023 15:30:24 -0400 Subject: [PATCH 1/7] Created a testcase for auto primary content --- src/program/program.spec.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/program/program.spec.ts b/src/program/program.spec.ts index a0814376..5a12b051 100644 --- a/src/program/program.spec.ts +++ b/src/program/program.spec.ts @@ -370,4 +370,21 @@ describe("Collection", () => { test("should populate previous and next based on module ID", function () { expect(true).toBe(true); }); + + test("should set first content entry of a lesson as primary content entry", async function(){ + const coll = await resolver.collection({ id: testingCollectionID }); + expect(coll).toBeDefined(); + if (coll.length > 0) { + coll.map((c) => { + c.lessons.map((lesson) => { + if(lesson.content.length==1) + expect(lesson.content[0].primary).toBe( + true + ); + + }); + }); + } + + }); }); From 3d91ecd857d4431ffc14f29125b96dd9b9dbde02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20B=2E=20Papp?= Date: Tue, 14 Mar 2023 19:30:53 +0000 Subject: [PATCH 2/7] Automated formatting changes --- src/program/program.spec.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/program/program.spec.ts b/src/program/program.spec.ts index 5a12b051..83662e71 100644 --- a/src/program/program.spec.ts +++ b/src/program/program.spec.ts @@ -371,20 +371,16 @@ describe("Collection", () => { expect(true).toBe(true); }); - test("should set first content entry of a lesson as primary content entry", async function(){ + test("should set first content entry of a lesson as primary content entry", async function () { const coll = await resolver.collection({ id: testingCollectionID }); expect(coll).toBeDefined(); if (coll.length > 0) { coll.map((c) => { c.lessons.map((lesson) => { - if(lesson.content.length==1) - expect(lesson.content[0].primary).toBe( - true - ); - + if (lesson.content.length == 1) + expect(lesson.content[0].primary).toBe(true); }); }); } - }); }); From 586dd2af83fc984c76874f83ed62a963bd3b344e Mon Sep 17 00:00:00 2001 From: Avantika0503 Date: Tue, 21 Mar 2023 18:04:32 -0400 Subject: [PATCH 3/7] Developed resolver logic which helps while updating primary content --- gql/graphql.ts | 2 +- src/program/program.resolver.ts | 65 ++++++++++++++++++++++++++++++++- src/program/program.service.ts | 5 ++- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/gql/graphql.ts b/gql/graphql.ts index ec6c883c..4839f57b 100644 --- a/gql/graphql.ts +++ b/gql/graphql.ts @@ -79,7 +79,7 @@ export interface ContentFields { type?: Nullable; link?: Nullable; parent?: Nullable; - primary: boolean; + primary?: boolean; } export interface CreateCollectionArgs { diff --git a/src/program/program.resolver.ts b/src/program/program.resolver.ts index a27dea12..a5cc724f 100644 --- a/src/program/program.resolver.ts +++ b/src/program/program.resolver.ts @@ -25,6 +25,8 @@ import { ProgramService } from "./program.service"; import { Prisma, UserRole } from "@prisma/client"; import { UseGuards } from "@nestjs/common"; import { AuthGuard } from "@/auth.guard"; +import { isRequiredInputField } from "graphql"; +import { async } from "rxjs/internal/scheduler/async"; @Resolver() // @UseGuards(AuthGuard) @@ -265,10 +267,71 @@ export class ProgramResolver { } @Mutation("updateContent") + async updateContent(@Args("input") input: ContentFields) { + + let id=input.id + const original = await this.programService.content({ + parent: input.parent, + + }) + if(input.primary==true) + { + await original.map( async(org)=>{ + if(org.primary==true){ + org.primary=false + await this.programService.updateContent( + { + id: org.id, + primary:org.primary, + } + ); + await this.programService.updateContent( + {primary:input.primary, + id:input.id + } + ); + + } + + }) + + } + else if(input.primary== false){ + // if (original.filter(org => org.primary == true).length == 0) { + + + await original.map( async(org)=>{ + if(org.primary==true){ + org.primary=false + await this.programService.updateContent( + { + id: org.id, + primary:org.primary, + } + ); + + + } + + }) + original[0].primary= true + await this.programService.updateContent( + { + id: original[0].id, + primary:original[0].primary, + } + ); + + + + } + + //} + return await this.programService.updateContent(input); + } - @Mutation("deleteContent") async deleteContent(@Args("contentID") contentID: string) { return await this.programService.deleteContent(contentID); diff --git a/src/program/program.service.ts b/src/program/program.service.ts index 46bc7d6d..c8fa142f 100644 --- a/src/program/program.service.ts +++ b/src/program/program.service.ts @@ -1070,7 +1070,7 @@ export class ProgramService { } async updateContent(input: ContentFields) { - const { id, type, link, parent } = input; + const { id, type, link, parent,primary } = input; if (!id) { throw new Error("Id not provided to updateContent"); @@ -1083,7 +1083,8 @@ export class ProgramService { data: { ...(type && { type }), ...(link && { link }), - parent: parent ? { connect: { id: parent } } : undefined + parent: parent ? { connect: { id: parent } } : undefined, + primary: primary } }); From 16715a485bd9c712bc77c4a550a1c3d276f0e804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20B=2E=20Papp?= Date: Tue, 21 Mar 2023 22:07:23 +0000 Subject: [PATCH 4/7] Automated formatting changes --- src/program/program.resolver.ts | 91 ++++++++++++--------------------- src/program/program.service.ts | 2 +- 2 files changed, 35 insertions(+), 58 deletions(-) diff --git a/src/program/program.resolver.ts b/src/program/program.resolver.ts index a5cc724f..dc59bd8a 100644 --- a/src/program/program.resolver.ts +++ b/src/program/program.resolver.ts @@ -267,70 +267,47 @@ export class ProgramResolver { } @Mutation("updateContent") - async updateContent(@Args("input") input: ContentFields) { - - let id=input.id + let id = input.id; const original = await this.programService.content({ - parent: input.parent, - - }) - if(input.primary==true) - { - await original.map( async(org)=>{ - if(org.primary==true){ - org.primary=false - await this.programService.updateContent( - { + parent: input.parent + }); + if (input.primary == true) { + await original.map(async (org) => { + if (org.primary == true) { + org.primary = false; + await this.programService.updateContent({ + id: org.id, + primary: org.primary + }); + await this.programService.updateContent({ + primary: input.primary, + id: input.id + }); + } + }); + } else if (input.primary == false) { + // if (original.filter(org => org.primary == true).length == 0) { + + await original.map(async (org) => { + if (org.primary == true) { + org.primary = false; + await this.programService.updateContent({ id: org.id, - primary:org.primary, - } - ); - await this.programService.updateContent( - {primary:input.primary, - id:input.id - } - ); - + primary: org.primary + }); } - - }) - + }); + original[0].primary = true; + await this.programService.updateContent({ + id: original[0].id, + primary: original[0].primary + }); } - else if(input.primary== false){ - // if (original.filter(org => org.primary == true).length == 0) { - - - await original.map( async(org)=>{ - if(org.primary==true){ - org.primary=false - await this.programService.updateContent( - { - id: org.id, - primary:org.primary, - } - ); - - - } - - }) - original[0].primary= true - await this.programService.updateContent( - { - id: original[0].id, - primary:original[0].primary, - } - ); - - - - } - + //} - + return await this.programService.updateContent(input); - } @Mutation("deleteContent") async deleteContent(@Args("contentID") contentID: string) { diff --git a/src/program/program.service.ts b/src/program/program.service.ts index c8fa142f..c303d7aa 100644 --- a/src/program/program.service.ts +++ b/src/program/program.service.ts @@ -1070,7 +1070,7 @@ export class ProgramService { } async updateContent(input: ContentFields) { - const { id, type, link, parent,primary } = input; + const { id, type, link, parent, primary } = input; if (!id) { throw new Error("Id not provided to updateContent"); From c2bb4a221c43dbfa2a7a00a10ed1e80ea5e8d80c Mon Sep 17 00:00:00 2001 From: Avantika0503 Date: Wed, 22 Mar 2023 17:41:45 -0400 Subject: [PATCH 5/7] TEST CASE FOR UPDATECONTENT --- src/program/program.spec.ts | 115 ++++++++++++++++++++++++++++++++++-- utils/fakes.ts | 11 +++- utils/tests.ts | 22 ++++++- 3 files changed, 139 insertions(+), 9 deletions(-) diff --git a/src/program/program.spec.ts b/src/program/program.spec.ts index 83662e71..c5fa767e 100644 --- a/src/program/program.spec.ts +++ b/src/program/program.spec.ts @@ -8,10 +8,14 @@ import { AssignmentResult, Course, PlanOfStudy, - User + User, + ContentType, + } from "@/types/graphql"; -import { createCollection, createModule } from "../../utils/tests"; +import { createCollection, createModule, createContent, } from "../../utils/tests"; import { test, describe, beforeAll, afterAll, expect } from "vitest"; +import { createRandomLesson, createRandomQuiz, createRandomContent, createRandomInputContent } from "utils"; +import { TcpContext } from "@nestjs/microservices"; interface IAssignment extends Assignment { id: string; @@ -302,6 +306,8 @@ describe("Collection", () => { let testingCollectionID: string; let testingModuleID: string; + let testingContentID :string; + let fakeContent; beforeAll(async () => { service = new ProgramService(prisma); @@ -371,16 +377,115 @@ describe("Collection", () => { expect(true).toBe(true); }); - test("should set first content entry of a lesson as primary content entry", async function () { + +}); +describe("Content", () => { + let service: ProgramService; + let resolver: ProgramResolver; + let prisma: PrismaService; + prisma = new PrismaService(); + + + let testingCollectionID: string; + let testingModuleID: string; + let testingContentID :string; + let fakeContent; + + beforeAll(async () => { + service = new ProgramService(prisma); + resolver = new ProgramResolver(service); + + + }); + afterAll(async () => { + + await prisma.$disconnect(); + }); + + + test("should set first content entry of a lesson as primary content entry", async function(){ const coll = await resolver.collection({ id: testingCollectionID }); + //content expect(coll).toBeDefined(); if (coll.length > 0) { coll.map((c) => { c.lessons.map((lesson) => { - if (lesson.content.length == 1) - expect(lesson.content[0].primary).toBe(true); + if(lesson.content.length==1) + expect(lesson.content[0].primary).toBe( + true + ); + }); }); } + + }); + describe("Mutation.updateContent()", async () => { + const inputContent= createRandomContent() + const DBContent= await resolver.content({ + parent:inputContent.parentID + }) + + test("When input-content passed as PRIMARY, content array should update older-primary-content as SECONDARY and passed-input-content as PRIMARY", + + + + + async () => { + + + + const filteredContentFalse= DBContent.filter(org => org.primary == false) + const filteredContentTrue= DBContent.filter(org => org.primary == true) + filteredContentTrue.map((contTrue)=>{ + if(contTrue.id==inputContent.id) + { + expect(contTrue.primary).toBe( + true + ); + } + else{ + expect(contTrue.primary).toBe( + false + ); + } + }) + filteredContentFalse.map((contFalse)=>{ + if(contFalse.id==inputContent.id) + { + expect(contFalse.primary).toBe( + true + ); + } + else{ + expect(contFalse.primary).toBe( + false + ); + } + }) + }); + test("When input content is passed as secondary, CONTENT ARRAY'S FIRST ELEMENT should get updated to PRIMARY and the rest elements to SECONDARY", + async () => { + + DBContent.map((cont)=>{ + expect(cont.primary).toBe( + false + ); + }) + + expect(DBContent[0].primary).toBe( + true + ); + }); + test("If the input content is passed as secondary and if it's 1st element in the database where it seems to be primary, then the input-content should set as TRUE(PRIMARY) ", + async () => { + + if(inputContent.id==DBContent[0].id) + { + expect(inputContent.primary).toBe( + true + ); + } + }); }); }); diff --git a/utils/fakes.ts b/utils/fakes.ts index 4a7e8d9d..1f714d9e 100644 --- a/utils/fakes.ts +++ b/utils/fakes.ts @@ -198,7 +198,14 @@ export function createRandomContent(parentID?: string): Content { link: faker.internet.url() }; } - +export function createRandomInputContent(parentID?: string): Content { + return { + id: faker.database.mongodbObjectId(), + parentID: parentID ? parentID : faker.database.mongodbObjectId(), + type: faker.helpers.arrayElement(["TEXT", "IMAGE", "VIDEO", "AUDIO"]), + link: faker.internet.url() + }; +} export function createRandomQuiz(parentID?: string): Quiz { const questions = faker.datatype.number({min: 5, max:15}) return { @@ -265,6 +272,8 @@ export const assignment = createRandomAssignment(); export const assignmentResult = createRandomAssignmentResult(); export const thread = createRandomThread(); export const content = createRandomContent(); +export const contentInput = createRandomInputContent(); + export const progress = createRandomProgress(); export const quiz = createRandomQuiz(); export const question = createRandomQuestion(); diff --git a/utils/tests.ts b/utils/tests.ts index 553941b2..2ca02cff 100644 --- a/utils/tests.ts +++ b/utils/tests.ts @@ -2,12 +2,12 @@ import { PlanOfStudyResolver } from "@/pos"; import { ProgramResolver } from "@/program"; import { CreateAnswer, - CreateCollectionArgs, CreateQuestion, CreateQuiz, + CreateCollectionArgs, CreateQuestion, CreateQuiz, CreateContentArgs, EnrollmentStatus, LessonInput, - UserRole + UserRole, ContentType } from "@/types/graphql"; import {QuizResolver} from "@/quiz/quiz.resolver"; -import {Answer, Collection, Lesson, Question, Quiz} from "@prisma/client"; +import {Answer, Collection, Lesson, Question, Quiz, Content} from "@prisma/client"; export const shuffle = (str: string) => [...str].sort(() => Math.random() - 0.5).join(""); @@ -135,3 +135,19 @@ export const createAnswer = async ( else return new Error("Failed to create answer"); } +export const createContent = async ( + resolver: ProgramResolver, + input: Content +) => { + const data: CreateContentArgs = { + + type: ContentType.PDF, + link: input.link, + parent: input.parentID, + primary: input.primary, + } + const content = await resolver.createContent(data); + if (content) return content; + else return new Error("Failed to create content"); +} + From 08577785ef2d7e4d363b6a274622d8bdaa704a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20B=2E=20Papp?= Date: Wed, 22 Mar 2023 21:42:03 +0000 Subject: [PATCH 6/7] Automated formatting changes --- src/program/program.spec.ts | 126 ++++++++++++++---------------------- 1 file changed, 48 insertions(+), 78 deletions(-) diff --git a/src/program/program.spec.ts b/src/program/program.spec.ts index c5fa767e..d5a5e6dd 100644 --- a/src/program/program.spec.ts +++ b/src/program/program.spec.ts @@ -9,12 +9,20 @@ import { Course, PlanOfStudy, User, - ContentType, - + ContentType } from "@/types/graphql"; -import { createCollection, createModule, createContent, } from "../../utils/tests"; +import { + createCollection, + createModule, + createContent +} from "../../utils/tests"; import { test, describe, beforeAll, afterAll, expect } from "vitest"; -import { createRandomLesson, createRandomQuiz, createRandomContent, createRandomInputContent } from "utils"; +import { + createRandomLesson, + createRandomQuiz, + createRandomContent, + createRandomInputContent +} from "utils"; import { TcpContext } from "@nestjs/microservices"; interface IAssignment extends Assignment { @@ -306,7 +314,7 @@ describe("Collection", () => { let testingCollectionID: string; let testingModuleID: string; - let testingContentID :string; + let testingContentID: string; let fakeContent; beforeAll(async () => { @@ -376,8 +384,6 @@ describe("Collection", () => { test("should populate previous and next based on module ID", function () { expect(true).toBe(true); }); - - }); describe("Content", () => { let service: ProgramService; @@ -385,106 +391,70 @@ describe("Content", () => { let prisma: PrismaService; prisma = new PrismaService(); - let testingCollectionID: string; let testingModuleID: string; - let testingContentID :string; + let testingContentID: string; let fakeContent; beforeAll(async () => { service = new ProgramService(prisma); resolver = new ProgramResolver(service); - - }); afterAll(async () => { - await prisma.$disconnect(); }); - - test("should set first content entry of a lesson as primary content entry", async function(){ + test("should set first content entry of a lesson as primary content entry", async function () { const coll = await resolver.collection({ id: testingCollectionID }); //content expect(coll).toBeDefined(); if (coll.length > 0) { coll.map((c) => { c.lessons.map((lesson) => { - if(lesson.content.length==1) - expect(lesson.content[0].primary).toBe( - true - ); - + if (lesson.content.length == 1) + expect(lesson.content[0].primary).toBe(true); }); }); } - }); describe("Mutation.updateContent()", async () => { - const inputContent= createRandomContent() - const DBContent= await resolver.content({ - parent:inputContent.parentID - }) + const inputContent = createRandomContent(); + const DBContent = await resolver.content({ + parent: inputContent.parentID + }); - test("When input-content passed as PRIMARY, content array should update older-primary-content as SECONDARY and passed-input-content as PRIMARY", - - - - - async () => { - - - - const filteredContentFalse= DBContent.filter(org => org.primary == false) - const filteredContentTrue= DBContent.filter(org => org.primary == true) - filteredContentTrue.map((contTrue)=>{ - if(contTrue.id==inputContent.id) - { - expect(contTrue.primary).toBe( - true - ); - } - else{ - expect(contTrue.primary).toBe( - false - ); - } - }) - filteredContentFalse.map((contFalse)=>{ - if(contFalse.id==inputContent.id) - { - expect(contFalse.primary).toBe( - true - ); + test("When input-content passed as PRIMARY, content array should update older-primary-content as SECONDARY and passed-input-content as PRIMARY", async () => { + const filteredContentFalse = DBContent.filter( + (org) => org.primary == false + ); + const filteredContentTrue = DBContent.filter( + (org) => org.primary == true + ); + filteredContentTrue.map((contTrue) => { + if (contTrue.id == inputContent.id) { + expect(contTrue.primary).toBe(true); + } else { + expect(contTrue.primary).toBe(false); } - else{ - expect(contFalse.primary).toBe( - false - ); + }); + filteredContentFalse.map((contFalse) => { + if (contFalse.id == inputContent.id) { + expect(contFalse.primary).toBe(true); + } else { + expect(contFalse.primary).toBe(false); } - }) + }); }); - test("When input content is passed as secondary, CONTENT ARRAY'S FIRST ELEMENT should get updated to PRIMARY and the rest elements to SECONDARY", - async () => { - - DBContent.map((cont)=>{ - expect(cont.primary).toBe( - false - ); - }) + test("When input content is passed as secondary, CONTENT ARRAY'S FIRST ELEMENT should get updated to PRIMARY and the rest elements to SECONDARY", async () => { + DBContent.map((cont) => { + expect(cont.primary).toBe(false); + }); - expect(DBContent[0].primary).toBe( - true - ); + expect(DBContent[0].primary).toBe(true); }); - test("If the input content is passed as secondary and if it's 1st element in the database where it seems to be primary, then the input-content should set as TRUE(PRIMARY) ", - async () => { - - if(inputContent.id==DBContent[0].id) - { - expect(inputContent.primary).toBe( - true - ); + test("If the input content is passed as secondary and if it's 1st element in the database where it seems to be primary, then the input-content should set as TRUE(PRIMARY) ", async () => { + if (inputContent.id == DBContent[0].id) { + expect(inputContent.primary).toBe(true); } }); }); From 7caa1aa3188efb23f52040ea36fedc5a67c5dbd2 Mon Sep 17 00:00:00 2001 From: Avantika0503 Date: Thu, 23 Mar 2023 14:20:49 -0400 Subject: [PATCH 7/7] Requested Changes --- src/program/program.spec.ts | 74 ++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/src/program/program.spec.ts b/src/program/program.spec.ts index d5a5e6dd..7c89f02d 100644 --- a/src/program/program.spec.ts +++ b/src/program/program.spec.ts @@ -386,10 +386,10 @@ describe("Collection", () => { }); }); describe("Content", () => { - let service: ProgramService; - let resolver: ProgramResolver; let prisma: PrismaService; prisma = new PrismaService(); + let service = new ProgramService(prisma); + const resolver = new ProgramResolver(service);; let testingCollectionID: string; let testingModuleID: string; @@ -397,8 +397,12 @@ describe("Content", () => { let fakeContent; beforeAll(async () => { +<<<<<<< HEAD service = new ProgramService(prisma); resolver = new ProgramResolver(service); +======= + +>>>>>>> cace1d7 (Requested Changes) }); afterAll(async () => { await prisma.$disconnect(); @@ -418,6 +422,7 @@ describe("Content", () => { } }); describe("Mutation.updateContent()", async () => { +<<<<<<< HEAD const inputContent = createRandomContent(); const DBContent = await resolver.content({ parent: inputContent.parentID @@ -456,6 +461,71 @@ describe("Content", () => { if (inputContent.id == DBContent[0].id) { expect(inputContent.primary).toBe(true); } +======= + const inputContent= createRandomContent() + const DBContent= await resolver.content({parent:inputContent.parentID}) + + test("When input-content passed as PRIMARY, content array should update older-primary-content as SECONDARY and passed-input-content as PRIMARY", + + async () => { + + const filteredContentFalse= DBContent.filter(org => org.primary == false) + const filteredContentTrue= DBContent.filter(org => org.primary == true) + filteredContentTrue.map((contTrue)=>{ + if(contTrue.id==inputContent.id) + { + expect(contTrue.primary).toBe( + true + ); + } + else{ + expect(contTrue.primary).toBe( + false + ); + } + }) + filteredContentFalse.map((contFalse)=>{ + if(contFalse.id==inputContent.id) + { + expect(contFalse.primary).toBe( + true + ); + } + else{ + expect(contFalse.primary).toBe( + false + ); + } + }) + }); + test("When input content is passed as secondary, CONTENT ARRAY'S FIRST ELEMENT should get updated to PRIMARY and the rest elements to SECONDARY", + async () => { + DBContent.map((dbcont)=>{ + expect(dbcont[0].primary).toBe( + true + ); + }) + + const filteredContent= DBContent.slice(1) + filteredContent.map((cont)=>{ + expect(cont.primary).toBe( + false + ); + }) + + }); + test("If the input content is passed as secondary and if it's 1st element in the database where it seems to be primary, then the input-content should set as TRUE(PRIMARY) ", + async () => { + DBContent.map((dbcont)=>{ + if(inputContent.id==dbcont[0].id) + { + expect(inputContent.primary).toBe( + true + ); + } + }) + +>>>>>>> cace1d7 (Requested Changes) }); }); });