Skip to content

Commit

Permalink
Merge branch 'development' into 119-create-get-endpoint-for-accredita…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
Hitansh159 authored Jul 9, 2023
2 parents 3baa81c + 1681d5d commit cfb820d
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 15 deletions.
212 changes: 208 additions & 4 deletions _apidoc.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,223 @@
// ------------------------------------------------------------------------------------------
// General apiDoc documentation blocks and old history blocks.
// ------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------
// Current Success.
// ------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------
// Current Errors.
// ------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------
// Current Permissions.
// ------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------
// History.
// ------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------
// Index.
// ------------------------------------------------------------------------------------------

/**
* @api {get} / Retrieve Home Information
* @apiName GetIndex
* @apiGroup index
*
* @apiSuccess {String} res server working.
*/

// ------------------------------------------------------------------------------------------
// Auth.
// ------------------------------------------------------------------------------------------

/**
* @api {post} /auth Login User
* @apiName LoginUser
* @apiGroup Authentication
*
* @apiBody {String} id User ID.
* @apiBody {String} password User password.
*
* @apiSuccess {String} res Response message.
* @apiSuccess {Object} user User details.
* @apiSuccess {String} user.uid User ID.
* @apiSuccess {String} user.name User name.
* @apiSuccess {String} user.emailId User email ID.
* @apiSuccess {String} user.type User type.
* @apiSuccess {String} user.token User token.
*
* @apiSuccessExample Success Response:
* HTTP/1.1 200 OK
* {
* "res": "welcome",
* "user": {
* "uid": "123",
* "name": "Some User",
* "emailId": "[email protected]",
* "type": "user",
* "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
* }
* }
*
* @apiError (Error 403) UserDoesNotExist Incorrect ID or password.
* @apiError (Error 500) ServerError Something is wrong on our side. Try again.
*/

/**
* @api {post} /auth/validateUser Validate User
* @apiName ValidateUser
* @apiGroup Authentication
* @apiDescription Validates the user's authentication token.
*
* @apiHeader {String} Authorization User's authentication token.
*
* @apiSuccess {Object} res User object.
* @apiSuccess {Object} res.user User details.
* @apiSuccess {String} res.user.uid User ID.
* @apiSuccess {String} res.user.name User name.
* @apiSuccess {String} res.user.emailId User email ID.
* @apiSuccess {String} res.user.type User type.
*
* @apiSuccessExample Success Response:
* HTTP/1.1 200 OK
* {
* "res": {
* "user": {
* "uid": "123",
* "name": "Some User",
* "emailId": "[email protected]",
* "type": "user"
* },
* "msg": "user validated",
* "err": null
* }
* }
*/

/**
* @api {post} /auth/sendOTP Send OTP
* @apiName SendOTP
* @apiGroup Authentication
* @apiDescription Sends an OTP (One-Time Password) to the user's email ID.
*
* @apiBody {String} uid User ID.
* @apiBody {String} emailId User email ID.
*
* @apiSuccess {String} res Response message.
*
* @apiSuccessExample Success Response:
* HTTP/1.1 200 OK
* {
* "res": "otp sent to emailID"
* }
*
* @apiError (Error) IncorrectUidOrEmail Incorrect UID or emailId.
*/

/**
* @api {post} /auth/resetPassword Reset Password
* @apiName ResetPassword
* @apiGroup Authentication
* @apiDescription Resets the user's password using the provided OTP (One-Time Password).
*
* @apiBody {String} uid User ID.
* @apiBody {String} otp One-Time Password received by the user.
* @apiBody {String} password New password.
*
* @apiSuccess {String} res Response message.
*
* @apiSuccessExample Success Response:
* HTTP/1.1 200 OK
* {
* "res": "successfully updated password"
* }
*
* @apiError (Error) IncorrectOtp Incorrect OTP.
* @apiError (Error 500) UpdateError Something went wrong while updating password.
* @apiError (Error 500) ServerError Something went wrong.
*/

// ------------------------------------------------------------------------------------------
// Infrastructure.
// ------------------------------------------------------------------------------------------

/**
* @api {post} /infrastructure/add Add Infrastructure
* @apiName AddInfrastructure
* @apiGroup Infrastructure
*
* @apiBody {String} name The name of the infrastructure.
* @apiBody {String} type The type of the infrastructure.
* @apiBody {String} wing The wing where the infrastructure is located.
* @apiBody {Number} floor The floor where the infrastructure is located.
* @apiBody {Number} capacity The capacity of the infrastructure.
*
* @apiSuccess {String} res Success message with the ID of the added infrastructure.
*
* @apiError (Error 500) DatabaseError Error while inserting in the database.
*
* @apiDescription Adds a new infrastructure to the system.
*/

/**
* @api {get} infrastructure/list Request Infrastructure List
* @apiName GetInfrastructure
* @apiGroup Infrastructure
*
* @apiQuery {String} name Name of Infrastructure .
* @apiQuery {String} type Type of Infrastructure. One of possible Lab, Classroom.
* @apiQuery {String} wing Wing of Infrastructure. One of possible A,B,C.
* @apiQuery {Number} floor Floor of Infrastructure.
* @apiQuery {Number} capacity Capacity of Infrastructure.
*
* @apiSuccess {Infrastructure[]} res Array of Filtered Infrastructure Doc .
* @apiSuccess {String} infrastructure.name Name of Infrastructure
* @apiSuccess {String} infrastructure.type Type of Infrastructure. One of possible Lab, Classroom.
* @apiSuccess {String} infrastructure.wing Wing of Infrastructure. One of possible A,B,C.
* @apiSuccess {Number} infrastructure.floor Floor of Infrastructure.
* @apiSuccess {Number} infrastructure.capacity Capacity of Infrastructure.
*/

/**
* @api {delete} /infrastructure/delete/:infrastructureId Delete Infrastructure
* @apiName DeleteInfrastructure
* @apiGroup Infrastructure
*
* @apiParam {String} infrastructureId The ID of the infrastructure document to delete.
*
* @apiSuccess {String} res Success message indicating the deletion.
*
* @apiError (Error 500) err Error message if there was an error during the deletion.
*
* */

// ------------------------------------------------------------------------------------------
// Accreditation.
// ------------------------------------------------------------------------------------------

/**
* @api {post} /accreditation/add Add Accreditation
* @apiName AddAccreditation
* @apiGroup Accreditation
* @apiDescription Add a new accreditation.
*
* @apiBody {String} name Accreditation name.
* @apiBody {String} agencyName Agency name.
* @apiBody {Date} dateofAccreditation Date of accreditation.
* @apiBody {Date} dateofExpiry Date of expiry.
*
* @apiSuccess {String} res Response message.
* @apiError (Error 500) UserNotFound The of the User was not found
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "res": "added accreditation Example Accreditation"
* }
*
* @apiErrorExample Error-Response:
* HTTP/1.1 500 Internal Server Error
* {
* "err": "Error while inserting in DB"
* }
*/
30 changes: 28 additions & 2 deletions controller/accreditation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addNewAccreditation, getAccreditations } from "#services/accreditation";
import { addNewAccreditation, deleteAccreditationById, updateAccreditationById, getAccreditations } from "#services/accreditation";
import { logger } from "#util";

async function addAccreditation(req, res) {
Expand All @@ -15,6 +15,32 @@ async function addAccreditation(req, res) {
res.json({ err: "Error while inserting in DB" });
}
}
async function deleteAccreditation(req, res) {
const { accredationId } = req.params;
try {
await deleteAccreditationById(accredationId);
res.json({ res: "Accreditation deleted successfully" });
} catch (error) {
logger.error("Error while deleting", error);
res.status(500);
res.json({ err: "Error while deleting from DB" });
}
}

async function updateAccreditation(req, res) {
const {
id, ...data
} = req.body;

try {
await updateAccreditationById(id, data);
res.json({ res: "accreditation updated" });
} catch (error) {
logger.error("Error while inserting", error);
res.status(500);
res.json({ err: "Error while inserting in DB" });
}
}

async function showAccreditation(req, res) {
try {
Expand All @@ -27,4 +53,4 @@ async function showAccreditation(req, res) {
}
}

export default { addAccreditation, showAccreditation };
export default { addAccreditation, updateAccreditation, deleteAccreditation, showAccreditation };
45 changes: 40 additions & 5 deletions controller/infrastructure.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
import { createInfrastructure } from "#services/infrastructure";
import {
createInfrastructure, deleteInfrastructureById, infrastructureList, updateInfrastructureById,
} from "#services/infrastructure";
import { logger } from "#util";

async function addinfrastructure(req, res) {
async function addInfrastructure(req, res) {
const {
name, type, wing, floor, capacity,
} = req.body;
try {
const newinfrastructure = await createInfrastructure(name, type, wing, floor, capacity);
res.json({ res: `added user ${newinfrastructure.id}` });
const newInfrastructure = await createInfrastructure(name, type, wing, floor, capacity);
res.json({ res: `added user ${newInfrastructure.id}` });
} catch (error) {
logger.error("Error while inserting", error);
res.status(500);
res.json({ err: "Error while inserting in DB" });
}
}

export default { addinfrastructure };
async function updateInfrastructure(req, res) {
const {
id, ...data
} = req.body;
try {
await updateInfrastructureById(id, data);
res.json({ res: `updated infrastructure with id ${id}` });
} catch (error) {
logger.error("Error while updating", error);
res.status(500);
res.json({ err: "Error while updaing in DB" });
}
}

async function getInfrastructure(req, res) {
const filter = req.query;
const infralist = await infrastructureList(filter);
res.json({ res: infralist });
}

async function deleteInfrastructure(req, res) {
const { infrastructureId } = req.params;
try {
await deleteInfrastructureById(infrastructureId);

res.json({ res: `Deleted infrastructure with ID ${infrastructureId}` });
} catch (error) {
logger.error("Error while deleting", error);
res.status(500).json({ error: "Error while deleting from DB" });
}
}
export default {
addInfrastructure, deleteInfrastructure, getInfrastructure, updateInfrastructure,
};
6 changes: 6 additions & 0 deletions error/DataDeleteError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class DataDeleteError extends Error {
constructor(modelName) {
super(`Error while deleting document in ${modelName}`);
this.name = "DataDeleteError";
}
}
3 changes: 2 additions & 1 deletion error/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { DataEntryError } from "#error/DataEntryError";
import { DataNotFoundError } from "#error/DataNotFoundError";
import { UpdateError } from "#error/UpdateError";
import { UserDoesNotExistError } from "#error/UserDoesNotExist";
import { DataDeleteError } from "#error/DataDeleteError";

export default {
DataEntryError, DataNotFoundError, UpdateError, UserDoesNotExistError,
DataEntryError, DataNotFoundError, UpdateError, UserDoesNotExistError, DataDeleteError,
};
7 changes: 7 additions & 0 deletions models/topic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import connector from "#models/databaseUtil";

const topicSchema = {
title: { type: String, required: true },
};
// eslint-disable-next-line no-unused-vars
const Topic = connector.model("topic", topicSchema);
3 changes: 3 additions & 0 deletions routes/accreditation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ import accreditationController from "#controller/accreditation";
const router = express.Router();
router.get("/list", accreditationController.showAccreditation);
router.post("/add", accreditationController.addAccreditation);
router.delete("/delete/:accredationId", accreditationController.deleteAccreditation);
router.post("/update", accreditationController.updateAccreditation);

export default router;
5 changes: 4 additions & 1 deletion routes/infrastructure.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import express from "express";
import infrastructureController from "#controller/infrastructure";

const router = express.Router();
router.post("/add", infrastructureController.addinfrastructure);
router.post("/add", infrastructureController.addInfrastructure);
router.get("/list", infrastructureController.getInfrastructure);
router.post("/update", infrastructureController.updateInfrastructure);
router.post("/delete/:infrastructureId", infrastructureController.deleteInfrastructure);

export default router;
20 changes: 20 additions & 0 deletions services/accreditation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,23 @@ export async function getAccreditations(filter) {
}
throw new databaseError.DataNotFoundError("Accreditation");
}

export async function deleteAccreditationById(accredationId) {
const deleted = await accreditation.remove({ _id: accredationId });
if (deleted) {
return deleted;
}
throw new databaseError.DataDeleteError("Accreditation");
}

export async function updateAccreditationById(id, data) {
const updated = await accreditation.update({ _id: id }, data);
if (updated) {
return updated;
}
throw new databaseError.DataEntryError("Accrediation");
}

export default {
deleteAccreditationById, addNewAccreditation, updateAccreditationById,
};
Loading

0 comments on commit cfb820d

Please sign in to comment.