Skip to content

Commit

Permalink
changes in routes and schema files and add verify (validate file)
Browse files Browse the repository at this point in the history
  • Loading branch information
muskan170604 committed Oct 30, 2024
1 parent 1250412 commit de3a84d
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 10,815 deletions.
10,759 changes: 0 additions & 10,759 deletions package-lock.json

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"prepare": "husky"
},
"dependencies": {
"ajv": "^8.17.1",
"eslint-config-airbnb": "^19.0.4",
"express": "^4.21.1",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.7.2",
"next": "^14.1.0",
"next-auth": "^4.24.8",
"next-pwa": "^5.6.0",
"nodemon": "^3.1.7",
"pnpm": "^9.12.2",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
58 changes: 41 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 30 additions & 12 deletions src/route.js → src/app/(pages)/about/api/apiRoutes.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
/* eslint-disable consistent-return */
const express = require("express");
const mongoose = require("mongoose");
const adminAuth = require("./middleware"); // The adminAuth middleware
const TeamMember = require("./app/schema/teamMemberSchema"); // TeamMember model

import express, { json } from "express";
import { connect } from "mongoose";
import adminAuth from "./app/middlewares/middleware"; // The adminAuth middleware
import TeamMember, {
find,
findOne,
findById,
findByIdAndDelete,
} from "./app/schema/teamMemberSchema"; // TeamMember model
const app = express();

app.use(express.json());
app.use(json());

mongoose.connect(process.env.MONGODB_URI || " ", {});
connect(process.env.MONGODB_URI || " ", {});

// List all the team member

app.get("/api/v1/getAllTeamMembers", adminAuth, async (req, res) => {
try {
// Find all team members in the database
const teamMembers = await TeamMember.find();
const teamMembers = await find();

// Return the list of team members
res.status(200).json({
Expand Down Expand Up @@ -45,7 +51,7 @@ app.post("/api/v1/addTeamMember", adminAuth, async (req, res) => {

try {
// Check if the email already exists
const existingMember = await TeamMember.findOne({ email });
const existingMember = await findOne({ email });
if (existingMember) {
return res.status(400).json({
status: "error",
Expand Down Expand Up @@ -92,7 +98,7 @@ app.put("/api/v1/editTeamMember/:memberId", adminAuth, async (req, res) => {

try {
// Find the team member by ID
const teamMember = await TeamMember.findById(req.params.memberId);
const teamMember = await findById(req.params.memberId);
if (!teamMember) {
return res.status(404).json({
status: "error",
Expand Down Expand Up @@ -129,9 +135,7 @@ app.delete(
async (req, res) => {
try {
// Find the team member by ID and delete
const teamMember = await TeamMember.findByIdAndDelete(
req.params.memberId,
);
const teamMember = await findByIdAndDelete(req.params.memberId);
if (!teamMember) {
return res.status(404).json({
status: "error",
Expand All @@ -153,6 +157,20 @@ app.delete(
},
);

const response = await fetch("/api/validateTeamMember", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name: "Alice",
email: "alice@example.com",
role: "admin",
}),
});
const result = await response.json();
console.log(result);

// Start the server

const port = process.env.PORT || 3000;
Expand Down
File renamed without changes.
29 changes: 29 additions & 0 deletions src/app/schema/teamMemberSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const mongoose = require("mongoose");

const teamMemberSchema = new mongoose.Schema({
type: "object",
properties: {
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
lowercase: true,
},
role: {
type: String,
required: true,
enum: ["admin", "developer", "designer", "manager", "other"],
default: "admin",
},
required: ["id", "name", "email", "role"],
additionalProperties: false,
},
});

const TeamMember = mongoose.model("TeamMember", teamMemberSchema);

module.exports = TeamMember;
24 changes: 0 additions & 24 deletions src/app/schema/teamMemberSchema.ts

This file was deleted.

20 changes: 20 additions & 0 deletions src/app/verify/validateTeamMember.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// eslint-disable-next-line import/no-extraneous-dependencies
const Ajv = require("ajv");
const ajv = new Ajv({ allErrors: true });

const schema = require("../schema/teamMemberSchema");

const validate = ajv.compile(schema);

// eslint-disable-next-line consistent-return
export default async function handler(req, res) {
if (req.method !== "POST") {
return res.status(405).json({ error: "Method Not Allowed" });
}
const valid = validate(req.body);
if (!valid) {
return res.status(400).json({ errors: validate.errors });
}

res.status(200).json({ message: "TeamMember data is valid!" });
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
".next/types/**/*.ts",
"**/*.ts",
"**/*.tsx",
"src/middleware.ts",
"src/app/schema/teamMemberSchema.ts",
"src/app/middlewares/middleware.js",
"src/app/schema/teamMemberSchema.js",
"src/route.js"
],
"exclude": ["node_modules"]
Expand Down

0 comments on commit de3a84d

Please sign in to comment.