Skip to content

Commit 9588cc5

Browse files
committed
orders update
1 parent dcab1d1 commit 9588cc5

File tree

7 files changed

+175
-23
lines changed

7 files changed

+175
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import request from 'supertest';
2+
import { app } from '../app';
3+
// Mongo Memory Server - Users collection always starts out empty**
4+
it('success if there is itemid and units', async () => {
5+
const itemId = '6581bbc692686e6e68d25d7d';
6+
const sellerID = '123123';
7+
await global.createItem(itemId);
8+
const response = await request(app)
9+
.get('/api/orders/getMyOrder')
10+
.send({
11+
itemId: itemId,
12+
})
13+
.expect(200);
14+
// console.log(response);
15+
expect(response.body.units).toEqual(50);
16+
});
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,51 @@
11
import { Request, Response } from 'express';
22
import axios from 'axios';
3+
import { Order } from '../models/Order';
4+
import { BadRequestError, CurrentUserRequest, Events } from '@chronosrx/common';
5+
import { Inventory } from '../models/Inventory';
36

4-
export const getOrder =async (req:Request,res:Response) => {
5-
6-
}
7+
export const createOrder = async (req: CurrentUserRequest, res: Response) => {
8+
//deconstruct req.body
9+
const buyerId = req.currentUser;
10+
const { itemId, amount, totalPrice, sellerId } = req.body;
11+
//create Order document in the databse
12+
const newOrder = Inventory.build({});
13+
await newOrder.save();
14+
//send created order to event bus
15+
await axios.post('http://localhost:3005/', {
16+
event: {
17+
type: Events.ORDER_CREATED,
18+
payload: newOrder,
19+
},
20+
});
21+
res.status(201).send(newOrder);
22+
};
723

8-
export const getSale =async (req:Request,res:Response) => {
9-
10-
}
24+
export const getOrder = async (req: Request, res: Response) => {
25+
// check if order already exists
26+
const { buyerId } = req.body;
27+
const exsitingOrder = await Order.findOne({ buyerId });
28+
if (!exsitingOrder) {
29+
throw new BadRequestError('Order with that id does not exists');
30+
}
31+
//check inventory itemId, amount
32+
res.status(200).send(exsitingOrder);
33+
};
1134

12-
export const createOrder =async (req:Request,res:Response) => {
13-
14-
}
35+
export const getSale = async (req: Request, res: Response) => {
36+
const { sellerId } = req.body;
37+
const sales = await Order.find({ sellerId });
38+
if (!sales) {
39+
throw new BadRequestError('Sales with that sellerId does not exist');
40+
}
41+
res.status(200).send(sales);
42+
};
1543

16-
export const deleteOrder =async (req:Request,res:Response) => {
17-
18-
}
44+
export const deleteOrder = async (req: Request, res: Response) => {
45+
const { id } = req.body;
46+
const deletedOrder = await Order.findOneAndDelete({ id });
47+
if (!deletedOrder) {
48+
throw new BadRequestError(`Could not find that order by id to delete`);
49+
}
50+
res.status(200).send(deletedOrder);
51+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import mongoose from 'mongoose';
2+
interface InventoryAttrs {
3+
itemName: string;
4+
sellerId: string; // some user's id)
5+
unitPrice: number;
6+
}
7+
// define item attributes
8+
interface InventoryModel extends mongoose.Model<InventoryDoc> {
9+
build(attrs: InventoryAttrs): InventoryDoc;
10+
}
11+
//create item data in the database with these types;
12+
interface InventoryDoc extends mongoose.Document {
13+
itemName: string;
14+
sellerId: string;
15+
unitPrice: number;
16+
itemId: string;
17+
units: number;
18+
}
19+
// create the Schema in mongoose with defines requirements
20+
const InventorySchema = new mongoose.Schema(
21+
{
22+
itemName: {
23+
type: String,
24+
required: true,
25+
// unique: true,
26+
},
27+
sellerId: {
28+
type: mongoose.Types.ObjectId,
29+
required: true,
30+
ref: 'User',
31+
},
32+
unitPrice: {
33+
type: Number,
34+
required: true,
35+
},
36+
itemId: {
37+
type: mongoose.Types.ObjectId,
38+
required: true,
39+
unique: true,
40+
},
41+
units: {
42+
type: Number,
43+
required: true,
44+
},
45+
},
46+
{
47+
//anytime we create JSON formatted data, transform item using following rules
48+
toJSON: {
49+
transform(doc, ret) {
50+
ret.id = ret._id;
51+
delete ret._id;
52+
delete ret.__v;
53+
},
54+
},
55+
}
56+
);
57+
InventorySchema.statics.build = (attrs: InventoryAttrs) => {
58+
//returning item document with (attrs) passed in
59+
return new Inventory(attrs);
60+
};
61+
const Inventory = mongoose.model<InventoryDoc, InventoryModel>('Inventory', InventorySchema);
62+
export { Inventory };

examples_new/microservices/orders/src/models/Order.ts

-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ interface OrderAttrs {
44
itemId: string;
55
amount: number;
66
totalPrice: number;
7-
buyerId: string;
87
sellerId: string;
98
}
109

@@ -16,7 +15,6 @@ interface OrderDoc extends mongoose.Document {
1615
itemId: string;
1716
amount: number;
1817
totalPrice: number;
19-
buyerId: string;
2018
sellerId: string;
2119
}
2220

@@ -36,10 +34,6 @@ const OrderSchema = new mongoose.Schema(
3634
type: Number,
3735
required: true,
3836
},
39-
buyerId: {
40-
type: String,
41-
require: true,
42-
},
4337
sellerId: {
4438
type: String,
4539
require: true,
@@ -62,7 +56,6 @@ OrderSchema.statics.build = (attrs: OrderAttrs) => {
6256
itemId: attrs.itemId,
6357
amount: attrs.amount,
6458
totalPrice: attrs.totalPrice,
65-
buyerId: attrs.buyerId,
6659
sellerId: attrs.sellerId,
6760
});
6861
};

examples_new/microservices/orders/src/routes/event-router.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import express from 'express';
22
import { Events } from '@chronosrx/common';
33
import { Order } from '../models/Order';
4+
import { Inventory } from '../models/Inventory';
45

56
const router = express.Router();
67

@@ -9,7 +10,7 @@ router.post('/', async (req, res) => {
910
console.log(event);
1011
switch (event.type) {
1112
case Events.ITEM_CREATED:
12-
const newOrder = Order.build(event.payload);
13+
const newOrder = Inventory.build(event.payload);
1314
await newOrder.save();
1415
break;
1516
default:

examples_new/microservices/orders/src/routes/order-router.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import express from 'express';
2-
import {getOrder,getSale,createOrder,deleteOrder} from '../controllers/order-controller';
3-
import { currentUser } from '@chronosrx/common';
2+
import { getOrder, getSale, createOrder, deleteOrder } from '../controllers/order-controller';
3+
import { currentUser, requireAuth } from '@chronosrx/common';
44

55
const router = express.Router();
6-
6+
router.use(currentUser);
7+
router.use(requireAuth);
78
router.get('/getMyOrders', getOrder);
89
router.get('/getMySales', getSale);
910
router.post('/createOrder', createOrder);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { MongoMemoryServer } from 'mongodb-memory-server';
2+
import mongoose from 'mongoose';
3+
import { app } from '../app';
4+
import request from 'supertest';
5+
import { Order } from '../models/Order';
6+
7+
declare global {
8+
function createOrder(itemId: string, sellerId: string): void;
9+
}
10+
11+
let mongo: any;
12+
13+
beforeAll(async () => {
14+
process.env.JWT_KEY = 'erreoivcxspasfgj';
15+
16+
mongo = await MongoMemoryServer.create();
17+
const mongoUri = mongo.getUri();
18+
19+
await mongoose.connect(mongoUri, {});
20+
});
21+
22+
beforeEach(async () => {
23+
const collections = await mongoose.connection.db.collections();
24+
25+
for (let collection of collections) {
26+
await collection.deleteMany({});
27+
}
28+
});
29+
30+
afterAll(async () => {
31+
if (mongo) {
32+
await mongo.stop();
33+
}
34+
35+
await mongoose.connection.close();
36+
});
37+
38+
global.createOrder = async (itemId, sellerId) => {
39+
const data = Order.build({
40+
itemId: itemId,
41+
amount: 50,
42+
totalPrice: 5000,
43+
sellerId: sellerId,
44+
});
45+
await data.save();
46+
};

0 commit comments

Comments
 (0)