Skip to content

Commit

Permalink
Added subsection group delete sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
KartavyaSharma committed Sep 30, 2022
1 parent 297ab33 commit aed58c6
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion backend/config/development.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dotenv.config();
export default {
port: process.env.PORT,
database: {
uri: process.env.MONGO_URI,
uri: process.env.TEST_MONGO_URI,
},
token: {
secret: process.env.TOKEN_SECRET,
Expand Down
4 changes: 2 additions & 2 deletions backend/src/models/db/planner_models/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import Joi, { ObjectSchema } from "joi";
*/
export interface ICourse {
_id: string;
_courseGroupId: string;
_courseGroupId?: string;
name: string;
sections: ISection[];
}

export const JCourseSchema: ObjectSchema = Joi.object<ICourse>().keys(
{
_id: Joi.string().required(),
_courseGroupId: Joi.string().required(),
_courseGroupId: Joi.alternatives().try(Joi.string().required(), Joi.allow(null)),
name: Joi.string().required(),
sections: Joi.array().items(JSectionSchema).required(),
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/models/db/planner_models/sections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ export interface ISection {
_id: string;
_courseId: string;
name: string;
subsectionGroupId: string;
subsectionGroupId?: string;
}

export const JSectionSchema: ObjectSchema = Joi.object<ISection>(
{
_id: Joi.string().required(),
_courseId: Joi.string().required(),
name: Joi.string().required(),
subsectionGroupId: Joi.string().required(),
subsectionGroupId: Joi.alternatives().try(Joi.string().required(), Joi.allow(null)),
}
);

Expand Down
3 changes: 3 additions & 0 deletions backend/src/models/db/planner_models/subsection_group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import Joi, { ObjectSchema } from "joi";
export interface ISubsectionGroup {
_id: string;
_sectionId: string;
_userId?: string;
subsections: ISubSection[];
}

export const JSubsectionGroupSchema: ObjectSchema = Joi.object<ISubsectionGroup>().keys(
{
_id: Joi.string().required(),
_sectionId: Joi.string().required(),
_userId: Joi.alternatives().try(Joi.string().required(), Joi.allow(null)),
subsections: Joi.array().items(JSubsectionSchema).required(),
}
);
Expand All @@ -27,6 +29,7 @@ const ISubsectionGroup = new Schema<ISubsectionGroup>(
{
_id: { type: String, required: true },
_sectionId: { type: String, required: true },
_userId: { type: String, required: true, default: null },
subsections: { type: [ISubSectionSchema], required: false, default: null }
}
);
Expand Down
2 changes: 1 addition & 1 deletion backend/src/rmembr/planner/section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default class Section {
* @returns Section object wrapped in a ICreateSectionResponse object.
*/
public async register(userId: string): Promise<ISection> {
const newSubsectionGroup: SubsectionGroup = new SubsectionGroup(this.object);
const newSubsectionGroup: SubsectionGroup = new SubsectionGroup(this.object, null, userId);
const newSection: ISection = {
_id: this._id,
_courseId: this._courseId,
Expand Down
23 changes: 22 additions & 1 deletion backend/src/rmembr/planner/subsection/subsection_group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export default class SubsectionGroup {
/** Set contianing IDs of all contained subsections. */
private _subsections: ISubSection[];

/** User ID associated with subsection group */
private _userId: string;

/**
* @returns this subsection group's ID.
*/
Expand Down Expand Up @@ -67,15 +70,17 @@ export default class SubsectionGroup {
* Initializes new group with all relevent fields.
* @param sectionObj Section object associated with this group.
*/
constructor(sectionObj: ISection, subsectionGroupObj: ISubsectionGroup = null) {
constructor(sectionObj: ISection, subsectionGroupObj: ISubsectionGroup = null, userId: string = null) {
if (subsectionGroupObj) {
this._id = subsectionGroupObj._id;
this._sectionId = subsectionGroupObj._sectionId;
this._subsections = subsectionGroupObj.subsections;
this._userId = subsectionGroupObj._userId;
} else {
this._id = nanoid();
this._sectionId = sectionObj._id;
this._subsections = [];
this._userId = userId;
}
}

Expand All @@ -95,6 +100,7 @@ export default class SubsectionGroup {
const newSubsectionGroup: ISubsectionGroup = {
_id: this._id,
_sectionId: this._sectionId,
_userId: this._userId,
subsections: []
}
const created = await SubsectionGroupModel.create(newSubsectionGroup);
Expand Down Expand Up @@ -132,6 +138,21 @@ export default class SubsectionGroup {
return new SubsectionGroup(null, found);
}

/**
* Returns all subsection groups associated with a user.
* @param userId User ID.
* @returns Promise that resolves to a list of subsection groups.
*/
public static async getAll(userId: string): Promise<SubsectionGroup[]> {
const found: ISubsectionGroup[] = await SubsectionGroupModel.find({ _userId: userId });
if (!found) {
throw new Exception(ErrorCode.UnknownError, "No subsection groups associated with user.");
}
return found.map((obj) => {
return new SubsectionGroup(null, obj);
});
}

/**
* Deletes a subsection from the database.
* @returns Promise that resolves to an API response containing a deleted subsection object.
Expand Down
7 changes: 6 additions & 1 deletion backend/src/rmembr/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { Auth } from "../auth/auth_engine";
import { ICourseGroupResponse, IDeleteUserResponse, ILoginResponse } from "../../models/response/response_models";
import { Utils } from "../../utils/server_utils";
import { CourseGroupModel } from "../../models/db/planner_models/course_group";
import CourseGroup from "../planner/course_group";
import { defaultConf } from "../config_store/default_conf";
import { IUserClass, IUserClassStatic } from "../../models/server_models";
import { ISettings } from "../../models/db/user/settings";
import CourseGroup from "../planner/course_group";
import SubsectionGroup from "../planner/subsection/subsection_group";

/**
* Class representing a user.
Expand Down Expand Up @@ -163,6 +164,10 @@ export default class User implements IUserClass {
try {
await UserModel.deleteOne({ _id: user.id });
await CourseGroupModel.deleteOne({ _userId: user.id });
const groups:SubsectionGroup[] = await SubsectionGroup.getAll(user.id);
for (const group of groups) {
await SubsectionGroup.destroy(group.id);
}
} catch (err) {
return err;
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/routes/planner/course_routes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { NextFunction, Request, Response } from "express";
import { Routes } from "../routes";
import User from "../../rmembr/user/user";
import { ICourse } from "../../models/db/planner_models/course";
import { Utils } from "../../utils/server_utils";
import { IUpdateCourseRequest } from "../../models/request/request_models";
import { JUpdateCourseRequest } from "../../models/request/request_validators";
import {
ICourseGroupResponse,
ICreateCourseResponse,
Expand All @@ -12,7 +12,7 @@ import {
import SectionRoutes from "./section_routes";
import CourseGroup from "../../rmembr/planner/course_group";
import Course from "../../rmembr/planner/course";
import { JUpdateCourseRequest } from "../../models/request/request_validators";
import User from "../../rmembr/user/user";

export default class CourseRoutes extends Routes {

Expand Down
1 change: 0 additions & 1 deletion backend/src/routes/planner/section_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export default class SectionRoutes extends Routes {
try {
Utils.validateObject(req.params, "sectionId");
Utils.validateObject(req.body.payload, "section");
Utils.validateObject(req.body.payload.section, "subsectionGroupId");
const payload: { section: ISection } = req.body.payload;
await Utils.validateObjectDeep<IUpdateSectionRequest>(payload, JUpdateSectionRequest);
newUser = req.body.user;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/utils/errors/exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Exception extends Error {
]
console.log(errorOutput.join(""));
if (err instanceof Exception) {
console.log(`Error is known.\n Meta: ${err.meta}`)
console.log(`Error is known.\nMeta: ${err.meta}`)
res.status(err.status).send({ error: err });
} else {
/** For unhandled errors in system. */
Expand Down
3 changes: 2 additions & 1 deletion backend/src/utils/server_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ export class Utils {
try {
await validator.validateAsync(object);
} catch (err) {
const msg: string = `${err}\nON OBJECT: ${object}\nObject does not impelement proper interface.`;
throw new Exception(
ErrorCode.ValidationError,
`${err}\nON OBJECT:\n${object}\nObject does not impelement proper interface.`
msg
);
}
}
Expand Down

1 comment on commit aed58c6

@vercel
Copy link

@vercel vercel bot commented on aed58c6 Sep 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.