Skip to content

Commit 2bca0d4

Browse files
authored
Merge pull request #20 from oslabs-beta/inventory
inventory simplified
2 parents bce28f0 + 0f0e285 commit 2bca0d4

6 files changed

Lines changed: 93 additions & 96 deletions

File tree

examples_new/microservices/inventory/src/__test__/inventory.test.ts

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,50 @@ import { app } from '../app';
44
// Mongo Memory Server - Users collection always starts out empty**
55

66
//fails where there is no unit or no item id, it should return item
7-
it('fails if item id or units are missing', async () => {
8-
await request(app)
9-
.get('/api/inventory/getItemInventory')
10-
.send({
11-
itemId: '6581bbc692686e6e68d25d7d',
12-
})
13-
.expect(400);
14-
await request(app)
15-
.get('/api/inventory/getItemInventory')
16-
.send({
17-
units: 50,
18-
})
19-
.expect(400);
20-
});
21-
it('success if there is itemid and units', async () => {
22-
const itemId = '6581bbc692686e6e68d25d7d';
23-
const sellerId = '6581bbc692686e6e68d25xxx';
24-
await global.createItem(itemId, sellerId);
25-
const response = await request(app)
26-
.get('/api/inventory/getItemInventory')
27-
.send({
28-
itemId: itemId,
29-
})
30-
.expect(200);
31-
console.log(response);
32-
expect(response.body.units).toEqual(50);
33-
});
7+
// it('fails if item id or units are missing', async () => {
8+
// await request(app)
9+
// .get('/api/inventory/getItemInventory')
10+
// .send({
11+
// itemId: '6581bbc692686e6e68d25d7d',
12+
// })
13+
// .expect(400);
14+
// await request(app)
15+
// .get('/api/inventory/getItemInventory')
16+
// .send({
17+
// units: 50,
18+
// })
19+
// .expect(400);
20+
// });
21+
// it('success if there is itemid and units', async () => {
22+
// const itemId = '6581bbc692686e6e68d25d7d';
23+
// await global.createItem(itemId);
24+
// const response = await request(app)
25+
// .get('/api/inventory/getItemInventory')
26+
// .send({
27+
// itemId: itemId,
28+
// })
29+
// .expect(200);
30+
// console.log(response);
31+
// expect(response.body.units).toEqual(50);
32+
// });
3433
//testing updateItemInventory route, fail if id or units are missing
35-
it('fails if item id or units are missing when trying to update', async () => {
36-
await request(app)
37-
.patch('/api/inventory/updateItemInventory')
38-
.send({
39-
itemId: '6581bbc692686e6e68d25d7d',
40-
})
41-
.expect(400);
42-
await request(app)
43-
.patch('/api/inventory/updateItemInventory')
44-
.send({
45-
units: 50,
46-
})
47-
.expect(400);
48-
});
34+
// it('fails if item id or units are missing when trying to update', async () => {
35+
// await request(app)
36+
// .patch('/api/inventory/updateItemInventory')
37+
// .send({
38+
// itemId: '6581bbc692686e6e68d25d7d',
39+
// })
40+
// .expect(400);
41+
// await request(app)
42+
// .patch('/api/inventory/updateItemInventory')
43+
// .send({
44+
// units: 50,
45+
// })
46+
// .expect(400);
47+
// });
4948
it('successful updated the item with updated units', async () => {
5049
const itemId = '6581bbc692686e6e68d25d7d';
51-
const sellerId = '6581bbc692686e6e68d25xxx';
52-
await global.createItem(itemId, sellerId);
50+
await global.createItem(itemId);
5351

5452
const response = await request(app)
5553
.patch('/api/inventory/updateItemInventory')
@@ -58,5 +56,6 @@ it('successful updated the item with updated units', async () => {
5856
units: 40,
5957
})
6058
.expect(200);
59+
console.log(response);
6160
expect(response.body.units).toEqual(40);
6261
});
Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,53 @@
11
import { Request, Response } from 'express';
22
import axios from 'axios';
33
import { Inventory } from '../models/Inventory';
4-
import { BadRequestError, CurrentUserRequest, Events } from '@chronosrx/common';
4+
import { BadRequestError, CurrentUserRequest, EventTypes, Events } from '@chronosrx/common';
55

66
export const getAllItems = async (req: Request, res: Response) => {
77
console.log(req.body);
8-
const { itemId, units } = req.body;
9-
const exsitingItem = await Inventory.find({ itemId });
10-
if (!exsitingItem) {
11-
throw new BadRequestError('Item with that ID does not exist');
12-
}
138

14-
// await axios.post('http://localhost:3005/', {
15-
// event: {
16-
// type: Events.ITEM_CREATED,
17-
// payload: newInventory,
18-
// },
19-
// });
9+
const exsitingItem = await Inventory.find({});
2010
res.status(200).send(exsitingItem);
2111
};
12+
// // await axios.post('http://localhost:3005/', {
13+
// // event: {
14+
// // type: Events.ITEM_CREATED,
15+
// // payload: newInventory,
16+
// // },
17+
// // });
18+
// res.status(200).send(exsitingItem);
19+
// };
2220

23-
export const getMyItems = async (req: Request, res: Response) => {
24-
console.log(req.body);
25-
const { sellerId } = req.body;
26-
const exsitingItem = await Inventory.find({ sellerId });
27-
if (!exsitingItem) {
28-
throw new BadRequestError('Could not find items with that sellerId');
29-
}
30-
res.status(200).send(exsitingItem);
31-
};
21+
// export const getMyItems = async (req: Request, res: Response) => {
22+
// console.log(req.body);
23+
// const { sellerId } = req.body;
24+
// const exsitingItem = await Inventory.find({ sellerId });
25+
// if (!exsitingItem) {
26+
// throw new BadRequestError('Could not find items with that sellerId');
27+
// }
28+
// res.status(200).send(exsitingItem);
29+
// };
3230

3331
export const updateItemInventory = async (req: Request, res: Response) => {
34-
const { itemId, units } = req.body;
35-
const exsitingItem = await Inventory.findOne({ itemId });
36-
if (!exsitingItem) {
32+
const { id, units } = req.body;
33+
console.log(req.body);
34+
const newInventory = await Inventory.findById(id);
35+
console.log(newInventory);
36+
if (!newInventory) {
3737
throw new BadRequestError('Item with that ID does not exist and could not update');
3838
}
39-
exsitingItem.units = units;
40-
await exsitingItem.save();
41-
res.status(200).send(exsitingItem);
39+
newInventory.units = units;
40+
await newInventory.save();
41+
try {
42+
await axios.post('http://localhost:3005/', {
43+
event: {
44+
type: EventTypes.INVENTORY_UPDATED,
45+
payload: newInventory,
46+
},
47+
});
48+
} catch (err) {
49+
console.log(`failed to submit to eventbus`);
50+
}
51+
52+
res.status(200).send(newInventory);
4253
};

examples_new/microservices/inventory/src/models/Inventory.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import mongoose from 'mongoose';
33
interface InventoryAttrs {
44
id: string;
55
itemName: string;
6-
sellerId: string; // some user's id)
7-
unitPrice: number;
86
}
97
// define item attributes
108
interface InventoryModel extends mongoose.Model<InventoryDoc> {
@@ -13,8 +11,6 @@ interface InventoryModel extends mongoose.Model<InventoryDoc> {
1311
//create item data in the database with these types;
1412
interface InventoryDoc extends mongoose.Document {
1513
itemName: string;
16-
sellerId: mongoose.Types.ObjectId;
17-
unitPrice: number;
1814
units: number;
1915
}
2016
// create the Schema in mongoose with defines requirements
@@ -23,16 +19,7 @@ const InventorySchema = new mongoose.Schema(
2319
itemName: {
2420
type: String,
2521
required: true,
26-
// unique: true,
27-
},
28-
sellerId: {
29-
type: mongoose.Types.ObjectId,
30-
required: true,
31-
ref: 'User',
32-
},
33-
unitPrice: {
34-
type: Number,
35-
required: true,
22+
unique: true,
3623
},
3724
units: {
3825
type: Number,
@@ -56,8 +43,6 @@ InventorySchema.statics.build = (attrs: InventoryAttrs) => {
5643
return new Inventory({
5744
_id: new mongoose.Types.ObjectId(attrs.id),
5845
itemName: attrs.itemName,
59-
sellerId: attrs.sellerId,
60-
unitPrice: attrs.unitPrice,
6146
});
6247
};
6348
const Inventory = mongoose.model<InventoryDoc, InventoryModel>('Inventory', InventorySchema);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ router.post('/', async (req, res) => {
1515
await newUser.save();
1616
break;
1717
case EventTypes.ITEM_CREATED:
18-
const newInventory = Inventory.build(event.payload);
18+
const newInventory = Inventory.build({
19+
id: event.payload.id,
20+
itemName: event.payload.itemName,
21+
});
1922
await newInventory.save();
2023
default:
2124
res.send({});
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import express from 'express';
2-
import { getAllItems, updateItemInventory, getMyItems } from '../controllers/inventory-controllers';
2+
import { getAllItems, updateItemInventory } from '../controllers/inventory-controllers';
3+
import { currentUser } from '@chronosrx/common';
34

45
const router = express.Router();
56

67
// router.post('/', async (req, res) => {
78
// const { event } = req.body;
89

910
// });
10-
11+
router.use(currentUser);
1112
router.get('/getAllItems', getAllItems);
12-
router.get('/getMyItems', getMyItems);
13+
// router.get('/getMyItems', getMyItems);
14+
1315
router.patch('/updateItemInventory', updateItemInventory);
1416

1517
export default router;

examples_new/microservices/inventory/src/test/setup.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import mongoose from 'mongoose';
33
import { Inventory } from '../models/Inventory';
44

55
declare global {
6-
function createItem(itemId: string, sellerId: string): void;
6+
function createItem(itemId: string): void;
77
}
88

99
let mongo: any;
@@ -33,13 +33,10 @@ afterAll(async () => {
3333
await mongoose.connection.close();
3434
});
3535

36-
global.createItem = async (itemId, sellerId) => {
36+
global.createItem = async itemId => {
3737
const data = Inventory.build({
38-
itemId: itemId,
38+
id: itemId,
3939
itemName: 'crap',
40-
sellerId: sellerId,
41-
units: 50,
42-
unitPrice: 5,
4340
});
4441
await data.save();
4542
};

0 commit comments

Comments
 (0)