Skip to content

Commit

Permalink
update: update config for deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
khayss committed Jun 14, 2024
1 parent 673fd88 commit bb00177
Show file tree
Hide file tree
Showing 30 changed files with 76 additions and 28 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
node_modules
.env
.vscode
public/uploads/admin/*
public/uploads/users/*
public/uploads/food/*
9 changes: 5 additions & 4 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export const jwtSecret = process.env.JWT_SECRET;
export const jwtExpiration = 24 * 60 * 60;

// API prefixes
export const apiPrefix = "/api/v1/";
export const apiPrefixes = {
generalApi: "/api/v1",
adminApi: "/api/v2",
userApi: "/api/v3",
riderApi: "/api/v4",
generalApi: apiPrefix + "app",
adminApi: apiPrefix + "admin",
userApi: apiPrefix + "user",
riderApi: apiPrefix + "rider",
};
2 changes: 1 addition & 1 deletion controllers/generalControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const getFoods = catchErrorFunc(async (req, res) => {
});

export const getFoodById = catchErrorFunc(async (req, res) => {
const { id } = req.params;
const { id } = req.query;
const food = await FoodModel.findById(id, { createdBy: 0, lastUpdatedBy: 0 });
res.status(200).json({ success: true, data: { food } });
});
3 changes: 3 additions & 0 deletions controllers/riderController.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,13 @@ export const updateAvailability = catchErrorFunc(async (req, res) => {
switch (status) {
case 0:
availability = "UNAVAILABLE";
break;
case 1:
availability = "AVAILABLE";
break;
default:
availability = "UNAVAILABLE";
break;
}
const updatedAvailability = await RiderModel.findByIdAndUpdate(
id,
Expand Down
29 changes: 27 additions & 2 deletions controllers/userControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ export const getDeliveryStatus = catchErrorFunc(async (req, res) => {
});

export const getOrder = catchErrorFunc(async (req, res) => {
const { id: orderId } = req.params;
const { id: orderId } = req.query;

const order = await OrderModel.findById(orderId);

if (!delivery)
if (!order)
throw new AppError(
"1204",
404,
Expand All @@ -237,3 +237,28 @@ export const getOrder = catchErrorFunc(async (req, res) => {
},
});
});

export const getOrders = catchErrorFunc(async (req, res) => {
const { id } = req.verifiedUser;

console.log(id);

const order = await OrderModel.find(
{ orderBy: id },
{ rider: 0 },
{ limit: 10 }
).populate("food");
if (order.length < 1)
throw new AppError(
"1204",
404,
"This user hasn't made any other yet",
"No orders found"
);

res.status(200).json({
success: true,
message: "user orders successfully retrieved",
data: order,
});
});
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "dotenv/config";
import express from "express";
import morgan from "morgan";
import cors from "cors";
import { apiPrefixes, port } from "./config/config.js";
import { connectDB } from "./database/db.js";
import { adminRouter } from "./routes/adminRoutes.js";
Expand All @@ -14,15 +15,17 @@ const app = express();

/* Middlewares */
app
.use(cors({ origin: "http://localhost:3001", credentials: true }))
.use(morgan("dev")) // Logging requests and response
.use(express.json()) // Parse incoming requests with JSON payload
.use(express.urlencoded({ extended: true })) // Parses incoming requests with urlencoded payloads
.use("/images", express.static("public"))

/* Router for admin routes */
.use(apiPrefixes.generalApi, generalRouter)
.use(apiPrefixes.adminApi, adminRouter)
.use(apiPrefixes.riderApi, riderRouter)
.use(apiPrefixes.userApi, userRouter)
.use(apiPrefixes.riderApi, riderRouter)
.use(apiPrefixes.adminApi, adminRouter)
.use("*", (req, res) =>
res.status(404).json({
status: "error",
Expand Down
2 changes: 0 additions & 2 deletions middlewares/errorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export function errorHandler(error, req, res, next) {
res.status(error.responseCode).json({
success,
message: error.message,
type: error.type,
details: error.details,
});
} else if (error instanceof MulterError) {
res.status(400).json({ success, message: error.message });
Expand Down
8 changes: 8 additions & 0 deletions models/orderModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ const orderSchema = new mongoose.Schema(
type: String,
required: true,
},
rider: {
type: mongoose.Schema.Types.ObjectId,
ref: "Rider",
},
status: {
type: String,
enum: ["PENDING", "DISPATCHED", "DELIVERED", "FAILED"],
},
},
{ timestamps: true }
);
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"description": "",
"dependencies": {
"bcrypt": "^5.1.1",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"jsonwebtoken": "^9.0.2",
Expand Down
2 changes: 1 addition & 1 deletion routes/generalRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import { getFoodById, getFoods } from "../controllers/generalControllers.js";

export const generalRouter = Router();

generalRouter.get("/get-foods", getFoods).get("/food/:id", getFoodById);
generalRouter.get("/get-foods", getFoods).get("/food", getFoodById);
2 changes: 2 additions & 0 deletions routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createOrder,
getDeliveryStatus,
getOrder,
getOrders,
getUser,
userLoginController,
userSignupController,
Expand All @@ -19,4 +20,5 @@ userRouter
.get("/get-user", getUser)
.get("/delivery-status/:id", getDeliveryStatus)
.get("/get-order", getOrder)
.get("/get-orders", getOrders)
.post("/create-order", createOrder);
22 changes: 6 additions & 16 deletions utils/userValidations.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const userSignupSchema = z.object({
password: z
.string({ required_error: "password field is required" })
.trim()
.min(8, { message: "password must be 8 characters or more" })
.min(8, { message: "password too short. must be 8 characters or more" })
.max(100, { message: "password is limitted to 100 characters" }),
address: z
.string({ required_error: "'address' field is required for registration" })
Expand All @@ -65,7 +65,9 @@ export const userLoginSchema = z.object({
password: z
.string({ required_error: "password is required to sign in" })
.trim()
.min(8, { message: "password must be 8 characters or longer" }),
.min(8, {
message: "password is too short. must be 8 characters or longer",
}),
});

/* VALIDATORS */
Expand All @@ -77,13 +79,7 @@ export function validateUserSignupReqBody(data) {
return result.data;
} else {
const details = result.error.issues.map((issue) => issue.message);
throw new AppError(
"1000",
400,
"ValidationError",
"Incomplete or invalid sign up body",
details
);
throw new AppError("1000", 400, "ValidationError", details[0], details);
}
}

Expand All @@ -94,12 +90,6 @@ export function validateUserLoginReqBody(data) {
return result.data;
} else {
const details = result.error.issues.map((issue) => issue.message);
throw new AppError(
"1001",
400,
"ValidationError",
"Incomplete or invalid login body",
details
);
throw new AppError("1001", 400, "ValidationError", details[0], details);
}
}

0 comments on commit bb00177

Please sign in to comment.