summaryrefslogtreecommitdiffstats
path: root/backend/src/resolvers/Mutation.js
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-04-04 10:38:21 -0400
committerWes Bos <wesbos@gmail.com>2018-04-04 10:38:21 -0400
commita75b2491758a451283e2a373bb3fbe886520b345 (patch)
treeb45d3c6e0685106c0b08049687244fbc8169b27a /backend/src/resolvers/Mutation.js
parent00df450c84f126c75c4adcd322947f8b77a589d9 (diff)
force network policy
Diffstat (limited to 'backend/src/resolvers/Mutation.js')
-rw-r--r--backend/src/resolvers/Mutation.js26
1 files changed, 16 insertions, 10 deletions
diff --git a/backend/src/resolvers/Mutation.js b/backend/src/resolvers/Mutation.js
index 35037b8..9127b04 100644
--- a/backend/src/resolvers/Mutation.js
+++ b/backend/src/resolvers/Mutation.js
@@ -29,6 +29,7 @@ const mutations = {
async signin(parent, { email, password }, ctx, info) {
const user = await ctx.db.query.user({ where: { email } });
+ console.log(user);
if (!user) {
throw new Error(`No such user found for email: ${email}`);
}
@@ -64,16 +65,18 @@ const mutations = {
},
async deleteItem(parent, args, ctx, info) {
- // TODO - handle auth for deleting an item
- // You Should Either Own this item, or have CAN_DELETE in roles
- return ctx.db.mutation.deleteItem(
- {
- where: {
- id: args.id,
- },
- },
- info
- );
+ const where = {
+ id: args.id,
+ };
+ // 1. find the item
+ const item = await ctx.db.query.item({ where }, `{ user {id}, title, id, description }`);
+ // 2. Make sure they own it, or are an admin
+ if (item.user.id !== ctx.request.user.id || !ctx.request.user.permissions.includes('ADMIN')) {
+ throw new Error("You aren't allowed to delete that item!");
+ }
+
+ // You Should Either Own this item, or have ITEMDELETE in roles
+ return ctx.db.mutation.deleteItem({ where }, info);
},
async updateItem(parent, args, ctx, info) {
@@ -204,12 +207,15 @@ const mutations = {
info
);
},
+
// delete that cart item
async removeFromCart(parent, args, ctx, info) {
+ // TODO: add userId to where
return ctx.db.mutation.deleteCartItem({
where: { id: args.id },
});
},
+
async createOrder(parent, args, ctx, info) {
const userId = getUserId(ctx);
const user = await ctx.db.query.user(