Skip to content

Commit bc6f026

Browse files
authored
Merge pull request #16 from oslabs-beta/item-service
Item service
2 parents 13e6268 + ee7ae31 commit bc6f026

37 files changed

+137
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","pid":67078,"argv":["/Users/kellychandler/.nvm/versions/node/v18.18.0/bin/node","/Users/kellychandler/git/Chronos/node_modules/jest-worker/build/workers/processChild.js"],"execArgv":[],"cwd":"/Users/kellychandler/git/Chronos","time":1703003763677,"ppid":67053,"coverageFilename":"/Users/kellychandler/git/Chronos/.nyc_output/0229daa2-f593-4686-90df-a8a6518601d8.json","externalId":"","uuid":"0229daa2-f593-4686-90df-a8a6518601d8","files":[]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","pid":67081,"argv":["/Users/kellychandler/.nvm/versions/node/v18.18.0/bin/node","/Users/kellychandler/git/Chronos/node_modules/jest-worker/build/workers/processChild.js"],"execArgv":[],"cwd":"/Users/kellychandler/git/Chronos","time":1703003763678,"ppid":67053,"coverageFilename":"/Users/kellychandler/git/Chronos/.nyc_output/03ee4578-80a9-4a81-b467-0d80001d342c.json","externalId":"","uuid":"03ee4578-80a9-4a81-b467-0d80001d342c","files":[]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","pid":67091,"argv":["/Users/kellychandler/.nvm/versions/node/v18.18.0/bin/node","/Users/kellychandler/git/Chronos/node_modules/jest-worker/build/workers/processChild.js"],"execArgv":[],"cwd":"/Users/kellychandler/git/Chronos","time":1703003763957,"ppid":67053,"coverageFilename":"/Users/kellychandler/git/Chronos/.nyc_output/1608e792-fdb0-45ea-9829-6400829d123d.json","externalId":"","uuid":"1608e792-fdb0-45ea-9829-6400829d123d","files":[]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","pid":67090,"argv":["/Users/kellychandler/.nvm/versions/node/v18.18.0/bin/node","/Users/kellychandler/git/Chronos/node_modules/jest-worker/build/workers/processChild.js"],"execArgv":[],"cwd":"/Users/kellychandler/git/Chronos","time":1703003763936,"ppid":67053,"coverageFilename":"/Users/kellychandler/git/Chronos/.nyc_output/1fd31a76-cb41-4e6a-a766-bf6049b6b6f3.json","externalId":"","uuid":"1fd31a76-cb41-4e6a-a766-bf6049b6b6f3","files":[]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","pid":67082,"argv":["/Users/kellychandler/.nvm/versions/node/v18.18.0/bin/node","/Users/kellychandler/git/Chronos/node_modules/jest-worker/build/workers/processChild.js"],"execArgv":[],"cwd":"/Users/kellychandler/git/Chronos","time":1703003763693,"ppid":67053,"coverageFilename":"/Users/kellychandler/git/Chronos/.nyc_output/216adaf0-a21c-4893-b2ca-fa40e3bd9fde.json","externalId":"","uuid":"216adaf0-a21c-4893-b2ca-fa40e3bd9fde","files":[]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","pid":67092,"argv":["/Users/kellychandler/.nvm/versions/node/v18.18.0/bin/node","/Users/kellychandler/git/Chronos/node_modules/jest-worker/build/workers/processChild.js"],"execArgv":[],"cwd":"/Users/kellychandler/git/Chronos","time":1703003763941,"ppid":67053,"coverageFilename":"/Users/kellychandler/git/Chronos/.nyc_output/5944b8b9-90e7-4021-ae85-14b6967c2e89.json","externalId":"","uuid":"5944b8b9-90e7-4021-ae85-14b6967c2e89","files":[]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","pid":67083,"argv":["/Users/kellychandler/.nvm/versions/node/v18.18.0/bin/node","/Users/kellychandler/git/Chronos/node_modules/jest-worker/build/workers/processChild.js"],"execArgv":[],"cwd":"/Users/kellychandler/git/Chronos","time":1703003763670,"ppid":67053,"coverageFilename":"/Users/kellychandler/git/Chronos/.nyc_output/71ff9111-e7a5-4a40-851e-2534c87255db.json","externalId":"","uuid":"71ff9111-e7a5-4a40-851e-2534c87255db","files":[]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","pid":67088,"argv":["/Users/kellychandler/.nvm/versions/node/v18.18.0/bin/node","/Users/kellychandler/git/Chronos/node_modules/jest-worker/build/workers/processChild.js"],"execArgv":[],"cwd":"/Users/kellychandler/git/Chronos","time":1703003763950,"ppid":67053,"coverageFilename":"/Users/kellychandler/git/Chronos/.nyc_output/7e7d4c6e-f5db-4d50-8a95-5ba6e900860c.json","externalId":"","uuid":"7e7d4c6e-f5db-4d50-8a95-5ba6e900860c","files":[]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","pid":67089,"argv":["/Users/kellychandler/.nvm/versions/node/v18.18.0/bin/node","/Users/kellychandler/git/Chronos/node_modules/jest-worker/build/workers/processChild.js"],"execArgv":[],"cwd":"/Users/kellychandler/git/Chronos","time":1703003763958,"ppid":67053,"coverageFilename":"/Users/kellychandler/git/Chronos/.nyc_output/916c1420-b609-4dce-bed2-14725a7d252a.json","externalId":"","uuid":"916c1420-b609-4dce-bed2-14725a7d252a","files":[]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","pid":67077,"argv":["/Users/kellychandler/.nvm/versions/node/v18.18.0/bin/node","/Users/kellychandler/git/Chronos/node_modules/jest-worker/build/workers/processChild.js"],"execArgv":[],"cwd":"/Users/kellychandler/git/Chronos","time":1703003763672,"ppid":67053,"coverageFilename":"/Users/kellychandler/git/Chronos/.nyc_output/a7632c4a-b227-4d72-a900-c4de463c44ed.json","externalId":"","uuid":"a7632c4a-b227-4d72-a900-c4de463c44ed","files":[]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"parent":null,"pid":67053,"argv":["/Users/kellychandler/.nvm/versions/node/v18.18.0/bin/node","/Users/kellychandler/git/Chronos/node_modules/.bin/jest"],"execArgv":[],"cwd":"/Users/kellychandler/git/Chronos","time":1703003762472,"ppid":67052,"coverageFilename":"/Users/kellychandler/git/Chronos/.nyc_output/ab05dfcf-c0e6-4621-9630-53b081db067c.json","externalId":"","uuid":"ab05dfcf-c0e6-4621-9630-53b081db067c","files":[]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","pid":67079,"argv":["/Users/kellychandler/.nvm/versions/node/v18.18.0/bin/node","/Users/kellychandler/git/Chronos/node_modules/jest-worker/build/workers/processChild.js"],"execArgv":[],"cwd":"/Users/kellychandler/git/Chronos","time":1703003763693,"ppid":67053,"coverageFilename":"/Users/kellychandler/git/Chronos/.nyc_output/c16bf4d6-2370-4ee3-bbb4-ae0945c44d60.json","externalId":"","uuid":"c16bf4d6-2370-4ee3-bbb4-ae0945c44d60","files":[]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","pid":67080,"argv":["/Users/kellychandler/.nvm/versions/node/v18.18.0/bin/node","/Users/kellychandler/git/Chronos/node_modules/jest-worker/build/workers/processChild.js"],"execArgv":[],"cwd":"/Users/kellychandler/git/Chronos","time":1703003763670,"ppid":67053,"coverageFilename":"/Users/kellychandler/git/Chronos/.nyc_output/cd04f9d6-379b-453d-aaa6-7f217fa7ab35.json","externalId":"","uuid":"cd04f9d6-379b-453d-aaa6-7f217fa7ab35","files":[]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","pid":67093,"argv":["/Users/kellychandler/.nvm/versions/node/v18.18.0/bin/node","/Users/kellychandler/git/Chronos/node_modules/jest-worker/build/workers/processChild.js"],"execArgv":[],"cwd":"/Users/kellychandler/git/Chronos","time":1703003763981,"ppid":67053,"coverageFilename":"/Users/kellychandler/git/Chronos/.nyc_output/e3781ebf-4b63-4b97-b92f-03c6c2618830.json","externalId":"","uuid":"e3781ebf-4b63-4b97-b92f-03c6c2618830","files":[]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","pid":67087,"argv":["/Users/kellychandler/.nvm/versions/node/v18.18.0/bin/node","/Users/kellychandler/git/Chronos/node_modules/jest-worker/build/workers/processChild.js"],"execArgv":[],"cwd":"/Users/kellychandler/git/Chronos","time":1703003763957,"ppid":67053,"coverageFilename":"/Users/kellychandler/git/Chronos/.nyc_output/f2685db6-67ae-41c0-b167-6a6573a60fea.json","externalId":"","uuid":"f2685db6-67ae-41c0-b167-6a6573a60fea","files":[]}

.nyc_output/processinfo/index.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"processes":{"0229daa2-f593-4686-90df-a8a6518601d8":{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","children":[]},"03ee4578-80a9-4a81-b467-0d80001d342c":{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","children":[]},"1608e792-fdb0-45ea-9829-6400829d123d":{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","children":[]},"1fd31a76-cb41-4e6a-a766-bf6049b6b6f3":{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","children":[]},"216adaf0-a21c-4893-b2ca-fa40e3bd9fde":{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","children":[]},"5944b8b9-90e7-4021-ae85-14b6967c2e89":{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","children":[]},"71ff9111-e7a5-4a40-851e-2534c87255db":{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","children":[]},"7e7d4c6e-f5db-4d50-8a95-5ba6e900860c":{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","children":[]},"916c1420-b609-4dce-bed2-14725a7d252a":{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","children":[]},"a7632c4a-b227-4d72-a900-c4de463c44ed":{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","children":[]},"ab05dfcf-c0e6-4621-9630-53b081db067c":{"parent":null,"children":["0229daa2-f593-4686-90df-a8a6518601d8","03ee4578-80a9-4a81-b467-0d80001d342c","1608e792-fdb0-45ea-9829-6400829d123d","1fd31a76-cb41-4e6a-a766-bf6049b6b6f3","216adaf0-a21c-4893-b2ca-fa40e3bd9fde","5944b8b9-90e7-4021-ae85-14b6967c2e89","71ff9111-e7a5-4a40-851e-2534c87255db","7e7d4c6e-f5db-4d50-8a95-5ba6e900860c","916c1420-b609-4dce-bed2-14725a7d252a","a7632c4a-b227-4d72-a900-c4de463c44ed","c16bf4d6-2370-4ee3-bbb4-ae0945c44d60","cd04f9d6-379b-453d-aaa6-7f217fa7ab35","e3781ebf-4b63-4b97-b92f-03c6c2618830","f2685db6-67ae-41c0-b167-6a6573a60fea"]},"c16bf4d6-2370-4ee3-bbb4-ae0945c44d60":{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","children":[]},"cd04f9d6-379b-453d-aaa6-7f217fa7ab35":{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","children":[]},"e3781ebf-4b63-4b97-b92f-03c6c2618830":{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","children":[]},"f2685db6-67ae-41c0-b167-6a6573a60fea":{"parent":"ab05dfcf-c0e6-4621-9630-53b081db067c","children":[]}},"files":{},"externalIds":{}}

examples_new/microservices/auth/src/routes/auth-router.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from 'express';
22
import { signup, login, logout, getCurrentUser } from '../controllers/auth-controller';
3-
import { currentUser } from '@chronosrx/common';
3+
import { currentUser } from '@chronosrx/common'
44

55
const router = express.Router();
66

examples_new/microservices/items/src/__test__/createItem.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import request from 'supertest';
22
import { app } from '../app';
33

44
it('fails if no itemName or unitPrice are provided', async () => {
5-
const cookie = await global.login();
5+
const cookie = await global.login('test');
66
await request(app)
77
.post('/api/items/createItem')
88
.set('Cookie', cookie)
99
.send({
10-
itemName: 'missingThePrice',
10+
itemName: 'missingPrice',
1111
})
1212
.expect(400);
1313

@@ -21,7 +21,7 @@ it('fails if no itemName or unitPrice are provided', async () => {
2121
});
2222

2323
it('fails if itemPrice is not a number', async () => {
24-
const cookie = await global.login();
24+
const cookie = await global.login('test');
2525
await request(app)
2626
.post('/api/items/createItem')
2727
.set('Cookie', cookie)
@@ -34,7 +34,7 @@ it('fails if itemPrice is not a number', async () => {
3434

3535
it('creates a new item with the valid inputs', async () => {
3636
// LOG SAMPLE COOKIE FOR DEBUGGING global.login() helper fx
37-
const cookie = await global.login();
37+
const cookie = await global.login('Hugh Jazz');
3838
console.log(cookie);
3939

4040
await request(app)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import request from 'supertest';
2+
import { app } from '../app';
3+
4+
it('fails if itemName does not exist', async () => {
5+
const cookie = await global.login('test');
6+
7+
await request(app)
8+
.post('/api/items/createItem')
9+
.set('Cookie', cookie)
10+
.send({
11+
itemName: 'chicken',
12+
unitPrice: 5,
13+
})
14+
.expect(201);
15+
16+
const response = await request(app)
17+
.delete('/api/items/deleteItem')
18+
.send({
19+
itemName: 'beef',
20+
})
21+
.expect(400);
22+
});
23+
24+
it('fails if sellerId does not match appropriate seller', async () => {
25+
const user1cookie = await global.login('test');
26+
const user2cookie = await global.login('badguy');
27+
await request(app)
28+
.post('/api/items/createItem')
29+
.set('Cookie', user1cookie)
30+
.send({
31+
itemName: 'chicken',
32+
unitPrice: 5,
33+
})
34+
.expect(201);
35+
36+
await request(app)
37+
.delete('/api/items/deleteItem')
38+
.set('Cookie', user2cookie)
39+
.send({
40+
itemName: 'chicken',
41+
})
42+
.expect(400);
43+
});
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
11
import { Request, Response } from 'express';
22
import axios, { AxiosError } from 'axios';
3-
import { BadRequestError, CurrentUserRequest, Events } from '@chronosrx/common';
3+
import { BadRequestError, CurrentUserRequest, EventTypes, NotAuthorizedError } from '@chronosrx/common';
44
import { Item } from '../models/items';
55
import { User } from '../models/users';
6+
import { RemoveUserOptions } from 'mongodb';
67

8+
// current users can create items to sell
79
export const createItem = async (req: CurrentUserRequest, res: Response) => {
10+
11+
// req.body consists of itemName and unitPrice
812
const { itemName, unitPrice } = req.body;
13+
// sellerId will come from cookie assigned to authorized users
914
const sellerId = req.currentUser!;
10-
15+
// seller must have a cookie assigned in order to sell items
1116
if (!sellerId) {
1217
throw new BadRequestError('Only registered users can list items for sale');
1318
}
1419
const seller = User.findById(sellerId);
1520
if (!seller) {
1621
throw new BadRequestError('Only registered users can list items for sale');
1722
}
18-
23+
// both itemName and unitPrice are required inputs and unitPrice must be a number
1924
if (!itemName || !unitPrice) {
2025
throw new BadRequestError('Invalid inputs');
2126
}
22-
2327
if (typeof unitPrice !== 'number') {
2428
throw new BadRequestError('Unit price must be a number');
2529
}
26-
30+
// new item is created with the build method and then saved
2731
const newItem = Item.build({
2832
itemName,
2933
sellerId,
3034
unitPrice,
3135
});
3236
await newItem.save();
3337

38+
// posting event to event bus
3439
try {
3540
await axios.post('http://localhost:3005/', {
3641
event: {
37-
type: Events.ITEM_CREATED,
42+
type: EventTypes.ITEM_CREATED,
3843
payload: newItem,
3944
},
4045
});
@@ -47,3 +52,45 @@ export const createItem = async (req: CurrentUserRequest, res: Response) => {
4752
}
4853
res.status(201).send(newItem);
4954
};
55+
56+
// sellers can delete their item
57+
export const deleteItem = async (req: CurrentUserRequest, res: Response) => {
58+
const { itemName } = req.body;
59+
const sellerId = req.currentUser!;
60+
// if sellerId doesn't exist, throw error as only authorized users can delete items
61+
if (!sellerId) {
62+
throw new NotAuthorizedError();
63+
}
64+
// if only item "owner" can delete item so sellerId must match
65+
const seller = User.findById(sellerId);
66+
if (!seller) {
67+
throw new NotAuthorizedError();
68+
}
69+
// search for itemName given by user
70+
const findItem = await Item.findOne({ itemName });
71+
console.log(findItem);
72+
if (!findItem) {
73+
throw new BadRequestError('Item does not exist');
74+
}
75+
// delete Item
76+
else {
77+
Item.deleteOne({ itemName });
78+
}
79+
// posting event to event bus
80+
try {
81+
await axios.post('http://localhost:3005/', {
82+
event: {
83+
type: EventTypes.ITEM_DELETED,
84+
payload: findItem,
85+
},
86+
});
87+
} catch (err) {
88+
console.log(
89+
`❌ itemController.deleteItem: Failed to emit ITEM_DELETED to event-bus: ${
90+
(err as AxiosError).message || 'unknown error'
91+
}`
92+
);
93+
}
94+
console.log(`${itemName} has been deleted`);
95+
res.status(201);
96+
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import express from 'express';
2-
import { createItem } from '../controllers/itemController';
2+
import { createItem, deleteItem } from '../controllers/itemController';
33
import { currentUser, requireAuth } from '@chronosrx/common';
44

55
const router = express.Router();
66

77
router.use(currentUser);
88
router.use(requireAuth);
99
router.post('/createItem', createItem);
10+
router.delete('/deleteItem', deleteItem);
1011

1112
export default router;

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import jwt from 'jsonwebtoken';
44
import { User } from '../models/users';
55

66
declare global {
7-
var login: () => Promise<string[]>;
7+
var login: (username: string) => Promise<string[]>;
88
}
99

1010
let mongo: any;
@@ -32,13 +32,13 @@ afterAll(async () => {
3232
await mongoose.connection.close();
3333
});
3434

35-
global.login = async () => {
35+
global.login = async (username: string) => {
3636
// create a random MongoDB ID
3737
const testUserId = new mongoose.Types.ObjectId().toHexString();
3838
// create a user with the random ID
3939
const testUser = User.build({
4040
userId: testUserId,
41-
username: 'test',
41+
username,
4242
});
4343
await testUser.save();
4444

0 commit comments

Comments
 (0)