Skip to content

Commit

Permalink
Content dynaic filters (#208)
Browse files Browse the repository at this point in the history
* create unit with topic

* fetch unit by topic id

* create topic with grade

* fetchTopicByGradeId

* create unit with grade

* fetch units by grade id
  • Loading branch information
brianmuks authored Jun 24, 2024
1 parent efd7677 commit bcd06f0
Show file tree
Hide file tree
Showing 8 changed files with 312 additions and 34 deletions.
3 changes: 2 additions & 1 deletion src/app/api/topic/[slug]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SPARKED_PROCESS_CODES from 'app/shared/processCodes';
import { Session } from 'next-auth';
import { getServerSession } from 'next-auth/next';
import fetchTopics_, { deleteTopics_, fetchTopicById_, fetchTopicsBySubjectId_, findTopicsByName_ } from '..';
import fetchTopics_, { deleteTopics_, fetchTopicByGradeId_, fetchTopicById_, findTopicsByName_ ,fetchTopicsBySubjectId_} from '..';
import { authOptions } from '../../auth/constants';
import createTopic_ from '../create';
import editTopic_ from '../edit';
Expand Down Expand Up @@ -50,6 +50,7 @@ export async function GET(
fetchTopics: fetchTopics_,
fetchTopicById: fetchTopicById_,
findTopicsByName: findTopicsByName_,
fetchTopicByGradeId: fetchTopicByGradeId_,
fetchTopicsBySubjectId: fetchTopicsBySubjectId_,
};

Expand Down
85 changes: 54 additions & 31 deletions src/app/api/topic/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ export default async function createTopic_(request: Request, session?: Session)
schoolId: zfd.text().optional(),
programId: zfd.text().optional(),
courseId: zfd.text().optional(),
subjectId: zfd.text().optional(),
subjectId: zfd.text().optional(),
gradeId: zfd.text().optional(),
});
const formBody = await request.json();

const { name, description, schoolId, programId, courseId, unitId, subjectId } = schema.parse(formBody);
const { name, description, schoolId, programId, courseId, unitId, gradeId, subjectId } = schema.parse(formBody);


try {
const db = await dbClient();
Expand Down Expand Up @@ -108,8 +110,27 @@ export default async function createTopic_(request: Request, session?: Session)
status: 200,
});
}
const grade = gradeId
? await db.collection(dbCollections.grades.name).findOne(
{
_id: new BSON.ObjectId(gradeId),
},
{ projection: { _id: 1 } },
)
: null;

const subject = subjectId
if (!grade && gradeId) {
const response = {
isError: true,
code: TOPIC_PROCESS_CODES.GRADE_NOT_FOUND,
};

return new Response(JSON.stringify(response), {
status: 200,
});
}

const subject = subjectId
? await db.collection(dbCollections.subjects.name).findOne(
{
_id: new BSON.ObjectId(subjectId),
Expand All @@ -129,37 +150,39 @@ export default async function createTopic_(request: Request, session?: Session)
});
}

const unit = await db.collection(dbCollections.units.name).findOne(
{
_id: new BSON.ObjectId(unitId),
},
{ projection: { _id: 1 } },
);
const unit = await db.collection(dbCollections.units.name).findOne(
{
_id: new BSON.ObjectId(unitId),
},
{ projection: { _id: 1 } },
);

if (!unit) {
const response = {
isError: true,
code: TOPIC_PROCESS_CODES.UNIT_NOT_FOUND,
};
if (!unit) {
const response = {
isError: true,
code: TOPIC_PROCESS_CODES.UNIT_NOT_FOUND,
};

return new Response(JSON.stringify(response), {
status: 200,
});
}
return new Response(JSON.stringify(response), {
status: 200,
});
}

await db.collection(dbCollections.topics.name).insertOne({
name,
description,
created_at: new Date(),
updated_at: new Date(),
//@ts-ignore
created_by_id: new BSON.ObjectId(session?.user?.id),
school_id: new BSON.ObjectId(schoolId),
program_id: new BSON.ObjectId(programId),
course_id: new BSON.ObjectId(courseId),
unit_id: new BSON.ObjectId(unitId),
grade_id: new BSON.ObjectId(gradeId),
subject_id: new BSON.ObjectId(subjectId),
});

await db.collection(dbCollections.topics.name).insertOne({
name,
description,
created_at: new Date(),
updated_at: new Date(),
//@ts-ignore
created_by_id: new BSON.ObjectId(session?.user?.id),
school_id: new BSON.ObjectId(schoolId),
program_id: new BSON.ObjectId(programId),
course_id: new BSON.ObjectId(courseId),
unit_id: new BSON.ObjectId(unitId),
subject_id: new BSON.ObjectId(subjectId),
});

const response = {
isError: false,
Expand Down
70 changes: 70 additions & 0 deletions src/app/api/topic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,15 @@ export async function findTopicsByName_(request: any) {
}



export async function fetchTopicsBySubjectId_(request: any) {
const schema = zfd.formData({
subjectId: zfd.text(),
withMetaData: zfd.text().optional(),
});
const params = request.nextUrl.searchParams;


const { subjectId, withMetaData } = schema.parse(params);
const isWithMetaData = Boolean(withMetaData);

Expand All @@ -289,6 +291,7 @@ export async function fetchTopicsBySubjectId_(request: any) {
p_fetchTopicsWithMetaData({
project,
query: {

subject_id: new BSON.ObjectId(subjectId),
},
}),
Expand All @@ -297,6 +300,7 @@ export async function fetchTopicsBySubjectId_(request: any) {

topic = topics.length ? topics[0] : {};
} else {

topic = await db.collection(dbCollections.topics.name).findOne({ subject_id: new BSON.ObjectId(subjectId) });
}

Expand All @@ -314,6 +318,72 @@ export async function fetchTopicsBySubjectId_(request: any) {
code: SPARKED_PROCESS_CODES.UNKNOWN_ERROR,
};

return new Response(JSON.stringify(resp), {
status: 200,
});
}
}



export async function fetchTopicByGradeId_(request: any) {
const schema = zfd.formData({
gradeId: zfd.text(),
withMetaData: zfd.text().optional(),
});
const params = request.nextUrl.searchParams;

const { gradeId, withMetaData } = schema.parse(params);
const isWithMetaData = Boolean(withMetaData);

try {
const db = await dbClient();

if (!db) {
const response = {
isError: true,
code: SPARKED_PROCESS_CODES.DB_CONNECTION_FAILED,
};
return new Response(JSON.stringify(response), {
status: 200,
});
}
const project = await getDbFieldNamesConfigStatus({ dbConfigData });

let topic: { [key: string]: string } | null;

if (isWithMetaData) {
const topics = await db
.collection(dbCollections.topics.name)
.aggregate(
p_fetchTopicsWithMetaData({
project,
query: {
grade_id: new BSON.ObjectId(gradeId),
},
}),
)
.toArray();

topic = topics.length ? topics[0] : {};
} else {
topic = await db.collection(dbCollections.topics.name).findOne({ grade_id: new BSON.ObjectId(gradeId) });
}

const response = {
isError: false,
topic,
};

return new Response(JSON.stringify(response), {
status: 200,
});
} catch (error) {
const resp = {
isError: true,
code: SPARKED_PROCESS_CODES.UNKNOWN_ERROR,
};

return new Response(JSON.stringify(resp), {
status: 200,
});
Expand Down
1 change: 1 addition & 0 deletions src/app/api/topic/processCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const TOPIC_PROCESS_CODES = {
PROGRAM_NOT_FOUND: 1605,
TOPIC_NOT_FOUND: 1606,
UNIT_NOT_FOUND: 1606,
GRADE_NOT_FOUND: 1608,
SUBJECT_NOT_FOUND: 1607,
} satisfies TProcessCode;

Expand Down
11 changes: 10 additions & 1 deletion src/app/api/unit/[slug]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import SPARKED_PROCESS_CODES from 'app/shared/processCodes';
import { Session } from 'next-auth';
import { getServerSession } from 'next-auth/next';
import fetchUnits_, { deleteUnits_, fetchUnitById_, fetchUnitBySubjectId_, findUnitsByName_ } from '..';
import fetchUnits_, {
deleteUnits_,
fetchUnitByGradeId_,
fetchUnitById_,
fetchUnitBySubjectId_,
fetchUnitByTopicId_,
findUnitsByName_,
} from '..';
import { authOptions } from '../../auth/constants';
import createUnit_ from '../create';
import editUnit_ from '../edit';
Expand Down Expand Up @@ -50,6 +57,8 @@ export async function GET(
fetchUnitById: fetchUnitById_,
findUnitsByName: findUnitsByName_,
fetchUnitBySubjectId: fetchUnitBySubjectId_,
fetchUnitByTopicId: fetchUnitByTopicId_,
fetchUnitByGradeId: fetchUnitByGradeId_,
};

if (unitFunctions[slug]) {
Expand Down
45 changes: 44 additions & 1 deletion src/app/api/unit/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ export default async function createUnit_(request: Request, session?: Session) {
programId: zfd.text().optional(),
courseId: zfd.text().optional(),
subjectId: zfd.text().optional(),
topicId: zfd.text().optional(),
gradeId: zfd.text().optional(),
});
const formBody = await request.json();

const { name, description, schoolId, programId, courseId, subjectId } = schema.parse(formBody);
const { name, description, schoolId, programId, courseId, subjectId, topicId, gradeId } = schema.parse(formBody);

try {
const db = await dbClient();
Expand Down Expand Up @@ -108,6 +110,45 @@ export default async function createUnit_(request: Request, session?: Session) {
});
}

const topic = topicId
? await db.collection(dbCollections.topics.name).findOne(
{
_id: new BSON.ObjectId(topicId),
},
{ projection: { _id: 1 } },
)
: null;

if (!topic && topicId) {
const response = {
isError: true,
code: UNIT_PROCESS_CODES.TOPIC_NOT_FOUND,
};

return new Response(JSON.stringify(response), {
status: 200,
});
}
const grade = gradeId
? await db.collection(dbCollections.grades.name).findOne(
{
_id: new BSON.ObjectId(gradeId),
},
{ projection: { _id: 1 } },
)
: null;

if (!grade && gradeId) {
const response = {
isError: true,
code: UNIT_PROCESS_CODES.GRADE_NOT_FOUND,
};

return new Response(JSON.stringify(response), {
status: 200,
});
}

await db.collection(dbCollections.units.name).insertOne({
name,
description,
Expand All @@ -119,6 +160,8 @@ export default async function createUnit_(request: Request, session?: Session) {
program_id: new BSON.ObjectId(programId),
course_id: new BSON.ObjectId(courseId),
subject_id: new BSON.ObjectId(subjectId),
topic_id: new BSON.ObjectId(topicId),
grade_id: new BSON.ObjectId(gradeId),
});

const response = {
Expand Down
Loading

0 comments on commit bcd06f0

Please sign in to comment.