Skip to content

Commit f5cbdf2

Browse files
fix: commit history
1 parent edc1d50 commit f5cbdf2

File tree

20 files changed

+538
-153
lines changed

20 files changed

+538
-153
lines changed

.DS_Store

-6 KB
Binary file not shown.

backend/Bruno/Create an Order.bru

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
meta {
2+
name: Create an Order
3+
type: http
4+
seq: 11
5+
}
6+
7+
post {
8+
url: http://localhost:3030/api/v1/order/01F8MECHZX3TBDSZ7XRADM79XE/newitem
9+
body: json
10+
auth: none
11+
}
12+
13+
body:json {
14+
{
15+
"itemID": "df",
16+
"itemName": "Pizza",
17+
"quantity": 2,
18+
"pricePerUnit": 10
19+
}
20+
}

backend/Bruno/Get all Order.bru

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
meta {
2+
name: Get all Order
3+
type: http
4+
seq: 10
5+
}
6+
7+
get {
8+
url: http://localhost:3030/api/v1/order
9+
body: json
10+
auth: none
11+
}
12+
13+
body:json {
14+
{
15+
"id": "01F8MECHZX3TBDSZ7XRADM79XJ",
16+
"name": "Pav Bhaji",
17+
"description": "This is awesome.",
18+
"imageUrl": "https://example.com/image.jpg",
19+
"price": 80,
20+
"isDeleted": true
21+
}
22+
}

backend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
"mongodb": "^6.13.1",
1212
"node-cache": "^5.1.2",
1313
"ulid": "^2.3.0",
14+
"ws": "^8.18.1",
1415
"zod": "^3.24.2"
1516
},
1617
"devDependencies": {
1718
"@types/node": "^20.11.17",
19+
"@types/ws": "^8.5.14",
1820
"tsx": "^4.7.1"
1921
},
2022
"packageManager": "pnpm@9.15.4+sha512.b2dc20e2fc72b3e18848459b37359a32064663e5627a51e4c74b2c29dd8e8e0491483c3abb40789cfd578bf362fb6ba8261b05f0387d76792ed6e23ea3b1b6a0"

backend/pnpm-lock.yaml

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/src/controller/auth/user.controller.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { Context } from "hono";
22
import { userSchema, type UserType } from "./user.schema";
33
import db from "../../db";
44
import { BackendError } from "../../utils/errors";
5-
import { ObjectId } from "mongodb";
65

76
export async function handleCreateUser(c: Context) {
87
try {
@@ -34,7 +33,7 @@ export async function handleGetUserDetails(c: Context) {
3433
const userId = c.req.param("user");
3534
const user = await (await db())
3635
.collection<UserType>("users")
37-
.findOne({ _id: new ObjectId(userId) });
36+
.findOne({ userID: userId });
3837
if (!user) {
3938
throw new BackendError("NOT_FOUND", { message: "User not found" });
4039
}
@@ -51,7 +50,7 @@ export async function handleUpdateUser(c: Context) {
5150
const user = await userSchema.parseAsync(body);
5251
const result = await (await db())
5352
.collection<UserType>("users")
54-
.updateOne({ _id: new ObjectId(userId) }, { $set: user });
53+
.updateOne({ userID: userId }, { $set: user });
5554
if (result.matchedCount === 0) {
5655
throw new BackendError("NOT_FOUND", { message: "User not found" });
5756
}
@@ -66,7 +65,7 @@ export async function handleDeleteUser(c: Context) {
6665
const userId = c.req.param("user");
6766
const result = await (await db())
6867
.collection<UserType>("users")
69-
.deleteOne({ _id: new ObjectId(userId) });
68+
.deleteOne({ userID: userId });
7069
if (result.deletedCount === 0) {
7170
throw new BackendError("NOT_FOUND", { message: "User not found" });
7271
}

backend/src/controller/auth/user.schema.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
import { z } from "zod";
22

3-
import { ObjectId } from "mongodb";
4-
5-
const objectIdSchema = z.custom<ObjectId>(
6-
(val) => {
7-
return ObjectId.isValid(val);
8-
},
9-
{
10-
message: "Invalid ObjectId",
11-
},
12-
);
13-
143
export const userSchema = z.object({
15-
id: objectIdSchema.optional(),
4+
id: z.string().ulid().optional(),
165
userID: z.string().uuid(),
176
userEmail: z.string().email(),
18-
197
userOrder: z.array(z.string()),
208
});
219

backend/src/controller/orders/order.controller.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Context } from "hono";
2-
import { orderSchema, type OrderType } from "./order.schema";
2+
import { itemSchema, orderSchema, type OrderType } from "./order.schema";
33
import db from "../../db";
44
import { BackendError } from "../../utils/errors";
5-
import { ObjectId } from "mongodb";
5+
import { notifyNewOrder } from "../vendor/websocket";
66

77
export async function handleCreateOrder(c: Context) {
88
try {
@@ -11,12 +11,54 @@ export async function handleCreateOrder(c: Context) {
1111
const data = await (await db())
1212
.collection<OrderType>("orders")
1313
.insertOne(order);
14+
15+
await notifyNewOrder(order);
16+
1417
return c.json({ success: true, data });
1518
} catch (error) {
1619
throw new BackendError("VALIDATION_ERROR", { details: error });
1720
}
1821
}
1922

23+
export async function handleNewItem(c: Context) {
24+
const orderId = c.req.param("order");
25+
const body = await c.req.json();
26+
const item = await itemSchema.parseAsync(body);
27+
28+
const order = await (await db())
29+
.collection<OrderType>("orders")
30+
.findOne({ orderID: orderId });
31+
32+
if (!order) {
33+
throw new BackendError("NOT_FOUND", { message: "Order not found" });
34+
}
35+
36+
if (!order.orderItems) {
37+
order.orderItems = [];
38+
}
39+
40+
const existingItem = order.orderItems.find(
41+
(i) => i.itemName === item.itemName,
42+
);
43+
44+
if (existingItem) {
45+
existingItem.quantity += item.quantity;
46+
} else {
47+
order.orderItems.push(item);
48+
}
49+
50+
await (await db())
51+
.collection<OrderType>("orders")
52+
.updateOne(
53+
{ orderID: orderId },
54+
{ $set: { orderItems: order.orderItems } },
55+
);
56+
57+
await notifyNewOrder(order);
58+
59+
return c.json({ success: true, data: order });
60+
}
61+
2062
export async function handleGetAllOrders(c: Context) {
2163
try {
2264
const orders = await (await db())
@@ -34,7 +76,7 @@ export async function handleGetOrderDetails(c: Context) {
3476
const orderId = c.req.param("order");
3577
const order = await (await db())
3678
.collection<OrderType>("orders")
37-
.findOne({ _id: new ObjectId(orderId) });
79+
.findOne({ orderID: orderId });
3880
if (!order) {
3981
throw new BackendError("NOT_FOUND", { message: "Order not found" });
4082
}
@@ -51,7 +93,7 @@ export async function handleUpdateOrder(c: Context) {
5193
const order = await orderSchema.parseAsync(body);
5294
const result = await (await db())
5395
.collection<OrderType>("orders")
54-
.updateOne({ _id: new ObjectId(orderId) }, { $set: order });
96+
.updateOne({ orderID: orderId }, { $set: order });
5597
if (result.matchedCount === 0) {
5698
throw new BackendError("NOT_FOUND", { message: "Order not found" });
5799
}
@@ -66,7 +108,7 @@ export async function handleDeleteOrder(c: Context) {
66108
const orderId = c.req.param("order");
67109
const result = await (await db())
68110
.collection<OrderType>("orders")
69-
.deleteOne({ _id: new ObjectId(orderId) });
111+
.deleteOne({ orderID: orderId });
70112
if (result.deletedCount === 0) {
71113
throw new BackendError("NOT_FOUND", { message: "Order not found" });
72114
}

backend/src/controller/orders/order.schema.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
1-
import { ObjectId } from "mongodb";
21
import { z } from "zod";
32

43
export const itemSchema = z.object({
5-
itemID: z.string().ulid(),
4+
itemID: z.string(),
65
itemName: z.string().min(2).max(100),
76
quantity: z.number().min(1),
87
pricePerUnit: z.number().min(0),
8+
note: z.string().optional().default(""),
99
});
1010

11-
const objectIdSchema = z.custom<ObjectId>(
12-
(val) => {
13-
return ObjectId.isValid(val);
14-
},
15-
{
16-
message: "Invalid ObjectId",
17-
},
18-
);
19-
2011
export const orderSchema = z.object({
21-
id: objectIdSchema.optional(),
12+
id: z.string().ulid().optional(),
13+
vendorID: z.string().ulid().optional(),
14+
tableID: z.string().uuid(),
2215
orderID: z.string().ulid(),
2316
orderItems: z.array(itemSchema).min(1),
24-
2517
isPriceCalculated: z.boolean().default(false),
26-
orderPrice: z.number().min(0),
27-
18+
orderPrice: z.number().min(0).optional().default(0),
2819
isPaid: z.boolean().default(false),
29-
3020
createdAt: z.date().default(new Date()),
3121
updatedAt: z.date().default(new Date()),
3222
deletedAt: z.date().optional().nullable().default(null),

backend/src/controller/vendor/vendor.controller.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import {
66
} from "./vendor.schema";
77
import db from "../../db";
88
import { BackendError } from "../../utils/errors";
9-
import { ObjectId } from "mongodb";
109
import { ZodError } from "zod";
11-
import type { itemType } from "../orders/order.schema";
1210
import { ulid } from "ulid";
1311

1412
export async function handleCreateVendor(c: Context) {
@@ -47,7 +45,7 @@ export async function handleGetVendorDetails(c: Context) {
4745
const vendorId = c.req.param("vendor");
4846
const vendor = await (await db())
4947
.collection<VendorType>("vendors")
50-
.findOne({ _id: new ObjectId(vendorId), isDeleted: false });
48+
.findOne({ id: vendorId, isDeleted: false });
5149
if (!vendor) {
5250
throw new BackendError("NOT_FOUND", { message: "Vendor not found" });
5351
}
@@ -67,7 +65,7 @@ export async function handleUpdateVendor(c: Context) {
6765
const vendor = await vendorSchema.parseAsync(body);
6866
const result = await (await db())
6967
.collection<VendorType>("vendors")
70-
.updateOne({ _id: new ObjectId(vendorId) }, { $set: vendor });
68+
.updateOne({ id: vendorId }, { $set: vendor });
7169
if (result.matchedCount === 0) {
7270
throw new BackendError("NOT_FOUND", { message: "Vendor not found" });
7371
}
@@ -86,7 +84,7 @@ export async function handleDeleteVendor(c: Context) {
8684
const result = await (await db())
8785
.collection<VendorType>("vendors")
8886
.updateOne(
89-
{ _id: new ObjectId(vendorId) },
87+
{ id: vendorId },
9088
{ $set: { isDeleted: true, deletedAt: new Date() } },
9189
);
9290
if (result.matchedCount === 0) {
@@ -110,7 +108,7 @@ export async function handleCreateVendorItem(c: Context) {
110108

111109
const vendor = await (await db())
112110
.collection("vendors")
113-
.findOne({ _id: new ObjectId(vendorId), isDeleted: false });
111+
.findOne({ id: vendorId, isDeleted: false });
114112
if (!vendor) {
115113
throw new BackendError("NOT_FOUND", { message: "Vendor not found" });
116114
}
@@ -120,10 +118,7 @@ export async function handleCreateVendorItem(c: Context) {
120118

121119
await (await db())
122120
.collection("vendors")
123-
.updateOne(
124-
{ _id: new ObjectId(vendorId) },
125-
{ $set: { items: vendorItems } },
126-
);
121+
.updateOne({ id: vendorId }, { $set: { items: vendorItems } });
127122

128123
return c.json({ success: true, data: item });
129124
} catch (error) {
@@ -139,7 +134,7 @@ export async function handleGetVendorItems(c: Context) {
139134
const vendorId = c.req.param("vendor");
140135
const vendor = await (await db())
141136
.collection("vendors")
142-
.findOne({ _id: new ObjectId(vendorId), isDeleted: false });
137+
.findOne({ id: vendorId, isDeleted: false });
143138
if (!vendor) {
144139
throw new BackendError("NOT_FOUND", { message: "Vendor not found" });
145140
}
@@ -157,7 +152,7 @@ export async function handleGetAllVendorItems(c: Context) {
157152
const vendorId = c.req.param("vendor");
158153
const vendor = await (await db())
159154
.collection("vendors")
160-
.findOne({ _id: new ObjectId(vendorId), isDeleted: false });
155+
.findOne({ id: vendorId, isDeleted: false });
161156
if (!vendor) {
162157
throw new BackendError("NOT_FOUND", { message: "Vendor not found" });
163158
}
@@ -179,7 +174,7 @@ export async function handleUpdateVendorItem(c: Context) {
179174
const result = await (await db())
180175
.collection("vendors")
181176
.updateOne(
182-
{ _id: new ObjectId(vendorId), "items.id": itemId },
177+
{ id: vendorId, "items.id": itemId },
183178
{ $set: { "items.$": item } },
184179
);
185180
if (result.matchedCount === 0) {

0 commit comments

Comments
 (0)