Skip to content

Commit

Permalink
Merge pull request #1093 from overthq/validation-and-polish
Browse files Browse the repository at this point in the history
Start adding validation
  • Loading branch information
koredefashokun authored Dec 4, 2024
2 parents 468c3ef + 669decd commit 9a3a65b
Show file tree
Hide file tree
Showing 100 changed files with 1,336 additions and 674 deletions.
4 changes: 2 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@apollo/server": "^4.10.4",
"@graphql-tools/schema": "^10.0.0",
"@prisma/client": "^5.13.0",
"@prisma/client": "^6.0.0",
"@sentry/cli": "^2.31.0",
"@sentry/node": "^7.109.0",
"@sentry/profiling-node": "^7.109.0",
Expand Down Expand Up @@ -45,7 +45,7 @@
"@types/lodash": "^4.14.198",
"@types/nodemailer": "^6.4.15",
"nodemon": "^3.1.7",
"prisma": "^5.13.0",
"prisma": "^6.0.0",
"ts-node": "^10.9.2",
"tsx": "^4.11.0",
"typescript": "^5.3.3"
Expand Down
76 changes: 39 additions & 37 deletions api/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["fullTextSearch"]
previewFeatures = ["fullTextSearchPostgres"]
}

datasource db {
Expand Down
4 changes: 4 additions & 0 deletions api/src/collections/card/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ interface DeleteCardArgs {
const deleteCard: Resolver<DeleteCardArgs> = async (_, { id }, ctx) => {
const card = await ctx.prisma.card.findUnique({ where: { id } });

if (!card) {
throw new Error('Card not found');
}

if (card.userId === ctx.user.id) {
return ctx.prisma.card.delete({ where: { id } });
} else {
Expand Down
20 changes: 8 additions & 12 deletions api/src/collections/cart-product/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ const removeFromCart: Resolver<RemoveProductArgs> = async (
{ cartId, productId },
ctx
) => {
const { userId } = await ctx.prisma.cart.findUnique({
where: {
id: cartId
}
});
const cart = await ctx.prisma.cart.findUnique({ where: { id: cartId } });

if (!cart) {
throw new Error('Cart not found');
}

if (userId === ctx.user.id) {
if (cart.userId === ctx.user.id) {
const product = await ctx.prisma.cartProduct.delete({
where: { cartId_productId: { cartId, productId } }
});
Expand All @@ -75,12 +75,8 @@ const updateCartProduct: Resolver<UpdateCartProductArgs> = (
ctx
) => {
return ctx.prisma.cartProduct.update({
where: {
cartId_productId: { cartId, productId }
},
data: {
quantity
}
where: { cartId_productId: { cartId, productId } },
data: { quantity }
});
};

Expand Down
4 changes: 4 additions & 0 deletions api/src/collections/cart/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const total: Resolver = async (parent, _, ctx) => {
.findUnique({ where: { id: parent.id } })
.products({ include: { product: true } });

if (!fetchedProducts) {
throw new Error('Cart not found');
}

const computedTotal = fetchedProducts.reduce((acc, p) => {
return acc + p.product.unitPrice * p.quantity;
}, 0);
Expand Down
1 change: 1 addition & 0 deletions api/src/collections/image/mutations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cloudinary from 'cloudinary';

import { Resolver } from '../../types/resolvers';

interface DeleteImageArgs {
Expand Down
4 changes: 4 additions & 0 deletions api/src/collections/order/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const createOrder: Resolver<CreateOrderArgs> = async (
}
});

if (!cart) {
throw new Error('Cart not found');
}

let total = 0;

const orderData = cart.products.map(p => {
Expand Down
4 changes: 4 additions & 0 deletions api/src/collections/order/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const total: Resolver = async (parent, _, ctx) => {
.findUnique({ where: { id: parent.id } })
.products();

if (!fetchedProducts) {
throw new Error('Order not found');
}

const computedTotal = fetchedProducts.reduce((acc, product) => {
return acc + product.unitPrice * product.quantity;
}, 0);
Expand Down
4 changes: 4 additions & 0 deletions api/src/collections/payout/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const createPayout: Resolver<CreatePayoutArgs> = async (
where: { id: ctx.storeId }
});

if (!store) {
throw new Error('Store not found');
}

// Extra validation (the frontend should cover this).
if (store.realizedRevenue < store.paidOut + amount) {
throw new Error('Insufficient funds');
Expand Down
12 changes: 12 additions & 0 deletions api/src/collections/product-category/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ const createProductCategory: Resolver<CreateProductCategoryArgs> = async (
{ input },
ctx
) => {
if (!ctx.storeId) {
throw new Error('Store not found');
}

const category = await ctx.prisma.storeProductCategory.create({
data: { storeId: ctx.storeId, name: input.name }
});
Expand All @@ -67,6 +71,10 @@ const editProductCategory: Resolver<EditProductCategoryArgs> = async (
{ categoryId, input },
ctx
) => {
if (!ctx.storeId) {
throw new Error('Store not found');
}

return ctx.prisma.storeProductCategory.update({
where: { id: categoryId },
data: input
Expand All @@ -82,6 +90,10 @@ const deleteProductCategory: Resolver<DeleteProductCategoryArgs> = async (
{ categoryId },
ctx
) => {
if (!ctx.storeId) {
throw new Error('Store not found');
}

const category = await ctx.prisma.storeProductCategory.delete({
where: { id: categoryId }
});
Expand Down
2 changes: 1 addition & 1 deletion api/src/collections/product-option/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const addProductOption: Resolver<AddProductOptionArgs> = async (
data: {
productId: input.productId,
name: input.name,
description: input.description
description: input.description ?? null
}
});

Expand Down
2 changes: 1 addition & 1 deletion api/src/collections/product-review/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const addProductReview: Resolver<AddProductReviewArgs> = async (
data: {
userId: ctx.user.id,
productId: input.productId,
body: input.body,
body: input.body ?? null,
rating: input.rating
}
});
Expand Down
2 changes: 1 addition & 1 deletion api/src/collections/product/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const products: Resolver<ProductsArgs> = (_, { filter, orderBy }, ctx) => {
};

interface OrdersArgs {
orderBy?: {
orderBy: {
createdAt?: 'asc' | 'desc';
updatedAt?: 'asc' | 'desc';
};
Expand Down
4 changes: 2 additions & 2 deletions api/src/collections/store/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface CreateStoreArgs {

const createStore: Resolver<CreateStoreArgs> = async (_, { input }, ctx) => {
const { storeImage, ...rest } = input;
let uploadedImage: UploadApiResponse;
let uploadedImage: UploadApiResponse | undefined;

if (storeImage) {
const { createReadStream } = await storeImage;
Expand Down Expand Up @@ -76,7 +76,7 @@ const editStore: Resolver<EditStoreArgs> = async (_, { input }, ctx) => {
uploadedUrl = url;
}

let bankAccountReference: string = undefined;
let bankAccountReference: string | undefined;

if (rest.bankAccountNumber && rest.bankCode) {
bankAccountReference = await createTransferReceipient(
Expand Down
14 changes: 9 additions & 5 deletions api/src/collections/store/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const currentStore: Resolver = (_, __, ctx) => {
};

interface StoresArgs {
filter?: {
name: StringWhere;
filter: {
name?: StringWhere;
};
}

Expand All @@ -32,7 +32,7 @@ const products: Resolver<ProductsArgs> = (parent, { filter, orderBy }, ctx) => {
};

interface OrdersArgs {
orderBy?: {
orderBy: {
createdAt?: 'asc' | 'desc';
updatedAt?: 'asc' | 'desc';
}[];
Expand Down Expand Up @@ -79,12 +79,16 @@ const followedByUser: Resolver = async (parent, _, ctx) => {
};

const cartId: Resolver = async (parent, _, ctx) => {
const { id } = await ctx.prisma.cart.findUnique({
const fetchedCart = await ctx.prisma.cart.findUnique({
where: { userId_storeId: { userId: ctx.user.id, storeId: parent.id } },
select: { id: true }
});

return id;
if (!fetchedCart) {
throw new Error('Cart not found');
}

return fetchedCart.id;
};

export default {
Expand Down
Loading

0 comments on commit 9a3a65b

Please sign in to comment.