Skip to content

Commit ee7ae31

Browse files
committed
items broken
1 parent 04719c5 commit ee7ae31

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

examples_new/microservices/items/src/controllers/itemController.ts

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
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';
66
import { RemoveUserOptions } from 'mongodb';
77

88
// current users can create items to sell
99
export const createItem = async (req: CurrentUserRequest, res: Response) => {
10+
1011
// req.body consists of itemName and unitPrice
1112
const { itemName, unitPrice } = req.body;
1213
// sellerId will come from cookie assigned to authorized users
@@ -38,7 +39,7 @@ export const createItem = async (req: CurrentUserRequest, res: Response) => {
3839
try {
3940
await axios.post('http://localhost:3005/', {
4041
event: {
41-
type: Events.ITEM_CREATED,
42+
type: EventTypes.ITEM_CREATED,
4243
payload: newItem,
4344
},
4445
});
@@ -58,12 +59,12 @@ export const deleteItem = async (req: CurrentUserRequest, res: Response) => {
5859
const sellerId = req.currentUser!;
5960
// if sellerId doesn't exist, throw error as only authorized users can delete items
6061
if (!sellerId) {
61-
throw new BadRequestError('Only registered users can delete items');
62+
throw new NotAuthorizedError();
6263
}
6364
// if only item "owner" can delete item so sellerId must match
6465
const seller = User.findById(sellerId);
6566
if (!seller) {
66-
throw new BadRequestError('Only registered users can delete items');
67+
throw new NotAuthorizedError();
6768
}
6869
// search for itemName given by user
6970
const findItem = await Item.findOne({ itemName });
@@ -76,20 +77,20 @@ export const deleteItem = async (req: CurrentUserRequest, res: Response) => {
7677
Item.deleteOne({ itemName });
7778
}
7879
// posting event to event bus
79-
// try {
80-
// await axios.post('http://localhost:3005/', {
81-
// event: {
82-
// type: Events.ITEM_DELETED,
83-
// payload: findItem,
84-
// },
85-
// });
86-
// } catch (err) {
87-
// console.log(
88-
// `❌ itemController.deleteItem: Failed to emit ITEM_DELETED to event-bus: ${
89-
// (err as AxiosError).message || 'unknown error'
90-
// }`
91-
// );
92-
// }
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+
}
9394
console.log(`${itemName} has been deleted`);
9495
res.status(201);
9596
};

0 commit comments

Comments
 (0)