From 2bcc993aaaf766ce8612fbdbf1dcdce9e6f1ca22 Mon Sep 17 00:00:00 2001 From: Nikolai R Kristiansen Date: Sat, 4 Dec 2021 23:30:44 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Show=20details=20about=20purchase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐛 Update user balance --- frontend/src/App.tsx | 4 +++- .../features/deposit/createDeposit.graphql | 1 + frontend/src/features/register/Register.tsx | 23 +++++++++++-------- .../src/features/register/createOrder.graphql | 20 +++++++++++++++- frontend/src/types.ts | 21 +++++++++++++---- main/api/mutations.py | 21 +++++++++++------ main/api/types.py | 11 +++++++++ schema.graphql | 10 ++++++++ 8 files changed, 87 insertions(+), 24 deletions(-) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 6de0e2e..bebd471 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -15,7 +15,9 @@ const App: React.FC = () => (
-
+
+ +
diff --git a/frontend/src/features/deposit/createDeposit.graphql b/frontend/src/features/deposit/createDeposit.graphql index aea6bf4..63c1262 100644 --- a/frontend/src/features/deposit/createDeposit.graphql +++ b/frontend/src/features/deposit/createDeposit.graphql @@ -9,6 +9,7 @@ mutation CreateDeposit($userId: ID!, $amount: Int!) { firstName lastName profile { + id balance } } diff --git a/frontend/src/features/register/Register.tsx b/frontend/src/features/register/Register.tsx index af913c3..5b45736 100644 --- a/frontend/src/features/register/Register.tsx +++ b/frontend/src/features/register/Register.tsx @@ -10,13 +10,13 @@ import { useRegister } from "./useRegister"; const USER_EXTERNAL = "external"; -type OrderLine = { +type OrderLineInput = { productId: string; amount: number; }; -const sumTotal = (products: Product[], order: OrderLine[], isExternal: boolean) => { - const prices = order.map((orderLine: OrderLine) => { +const sumTotal = (products: Product[], order: OrderLineInput[], isExternal: boolean) => { + const prices = order.map((orderLine: OrderLineInput) => { const product = products.find((product: Product) => product.id === orderLine.productId); if (!product) return 0; return orderLine.amount * (isExternal ? product.salePriceExt : product.salePriceInt); @@ -27,7 +27,7 @@ const sumTotal = (products: Product[], order: OrderLine[], isExternal: boolean) export const Register: React.FC = () => { const { users, products, loading, showAll, setShowAll } = useRegister(); const [selectedUser, setSelectedUser] = useState(""); - const [order, setOrder] = useState([]); + const [order, setOrder] = useState([]); const [createOrderMutation, { loading: mutateLoading }] = useMutation(CreateOrderDocument, { variables: { customerId: selectedUser, isExternal: selectedUser === USER_EXTERNAL, orderLines: order }, }); @@ -39,7 +39,7 @@ export const Register: React.FC = () => { }; const addToOrder = (productId: string) => { let added = false; - const newOrder = order.map((orderLine: OrderLine) => { + const newOrder = order.map((orderLine: OrderLineInput) => { if (orderLine.productId === productId) { added = true; return { ...orderLine, amount: orderLine.amount + 1 }; @@ -54,8 +54,6 @@ export const Register: React.FC = () => { const total = sumTotal(products, order, selectedUser === USER_EXTERNAL); - console.log(selectedUser, order); - if (loading) { return
Loading...
; } @@ -72,7 +70,12 @@ export const Register: React.FC = () => { const { data } = res; const createOrderResponse = data?.createOrder; if (createOrderResponse?.__typename === "CreateOrderSuccess") { - showToast("Woop"); + const { order } = createOrderResponse; + const orderLines = (order?.orderlines || []) + .map((orderLine) => `${orderLine.amount} ${orderLine.product.name}`) + .join(", "); + const who = !order.customer ? "Ekstern" : `${order.customer?.firstName} ${order.customer?.lastName}`; + showToast(`${who} kjøpte: ${orderLines}`); reset(); return; } @@ -118,8 +121,8 @@ export const Register: React.FC = () => { key={product.id} product={product} onProduct={() => addToOrder(product.id)} - amount={order.find((orderLine: OrderLine) => orderLine.productId === product.id)?.amount || 0} - active={order.some((orderLine: OrderLine) => orderLine.productId === product.id)} + amount={order.find((orderLine: OrderLineInput) => orderLine.productId === product.id)?.amount || 0} + active={order.some((orderLine: OrderLineInput) => orderLine.productId === product.id)} /> ))} diff --git a/frontend/src/features/register/createOrder.graphql b/frontend/src/features/register/createOrder.graphql index 317a185..1bacce8 100644 --- a/frontend/src/features/register/createOrder.graphql +++ b/frontend/src/features/register/createOrder.graphql @@ -1,10 +1,28 @@ -mutation CreateOrder($customerId: ID!, $orderLines:[OrderLineInput!]!, $isExternal: Boolean) { +mutation CreateOrder($customerId: ID!, $orderLines:[OrderLineInput!]!, $isExternal: Boolean!) { createOrder(customerId: $customerId, orderLines: $orderLines, isExternal: $isExternal) { ... on CreateOrderSuccess { order { id + isExternal customer { id + firstName + lastName + profile { + id + balance + } + } + orderlines { + id + amount + unitPrice + product { + id + name + salePriceExt + salePriceInt + } } orderSum } diff --git a/frontend/src/types.ts b/frontend/src/types.ts index 2efdd42..387898b 100644 --- a/frontend/src/types.ts +++ b/frontend/src/types.ts @@ -86,7 +86,18 @@ export type Order = { created: Scalars['DateTime']; customer?: Maybe; id: Scalars['ID']; + isExternal: Scalars['Boolean']; orderSum: Scalars['Float']; + orderlines?: Maybe>; +}; + +export type OrderLine = { + __typename?: 'OrderLine'; + amount: Scalars['Int']; + id: Scalars['ID']; + order: Order; + product: Product; + unitPrice: Scalars['Float']; }; export type OrderLineInput = { @@ -150,7 +161,7 @@ export type CreateDepositMutationVariables = Exact<{ }>; -export type CreateDepositMutation = { __typename?: 'Mutation', createDeposit: { __typename?: 'AboveMaxSaldo', error: boolean, message: string, maxSaldo: number, above: number } | { __typename?: 'CreateDepositSuccess', transaction: { __typename?: 'Transaction', id: string, amount: number, user: { __typename?: 'User', id: string, firstName: string, lastName: string, profile?: { __typename?: 'UserProfile', balance: number } | null | undefined } } } | { __typename?: 'FormErrors', error: boolean, message: string, fields: Array<{ __typename?: 'FieldError', field: string, message: string }> } }; +export type CreateDepositMutation = { __typename?: 'Mutation', createDeposit: { __typename?: 'AboveMaxSaldo', error: boolean, message: string, maxSaldo: number, above: number } | { __typename?: 'CreateDepositSuccess', transaction: { __typename?: 'Transaction', id: string, amount: number, user: { __typename?: 'User', id: string, firstName: string, lastName: string, profile?: { __typename?: 'UserProfile', id: string, balance: number } | null | undefined } } } | { __typename?: 'FormErrors', error: boolean, message: string, fields: Array<{ __typename?: 'FieldError', field: string, message: string }> } }; export type AllProductsQueryVariables = Exact<{ active: Scalars['Boolean']; @@ -167,14 +178,14 @@ export type AllUsersQuery = { __typename?: 'Query', allUsers: Array<{ __typename export type CreateOrderMutationVariables = Exact<{ customerId: Scalars['ID']; orderLines: Array | OrderLineInput; - isExternal?: Maybe; + isExternal: Scalars['Boolean']; }>; -export type CreateOrderMutation = { __typename?: 'Mutation', createOrder: { __typename?: 'CreateOrderSuccess', order: { __typename?: 'Order', id: string, orderSum: number, customer?: { __typename?: 'User', id: string } | null | undefined } } | { __typename?: 'FormErrors', error: boolean, message: string, fields: Array<{ __typename?: 'FieldError', field: string, message: string }> } | { __typename?: 'InsufficientFunds', error: boolean, message: string, amountLacking: number } }; +export type CreateOrderMutation = { __typename?: 'Mutation', createOrder: { __typename?: 'CreateOrderSuccess', order: { __typename?: 'Order', id: string, isExternal: boolean, orderSum: number, customer?: { __typename?: 'User', id: string, firstName: string, lastName: string, profile?: { __typename?: 'UserProfile', id: string, balance: number } | null | undefined } | null | undefined, orderlines?: Array<{ __typename?: 'OrderLine', id: string, amount: number, unitPrice: number, product: { __typename?: 'Product', id: string, name: string, salePriceExt: number, salePriceInt: number } }> | null | undefined } } | { __typename?: 'FormErrors', error: boolean, message: string, fields: Array<{ __typename?: 'FieldError', field: string, message: string }> } | { __typename?: 'InsufficientFunds', error: boolean, message: string, amountLacking: number } }; -export const CreateDepositDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"CreateDeposit"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"userId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"amount"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Int"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"createDeposit"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"userId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"userId"}}},{"kind":"Argument","name":{"kind":"Name","value":"amount"},"value":{"kind":"Variable","name":{"kind":"Name","value":"amount"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"CreateDepositSuccess"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"transaction"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"amount"}},{"kind":"Field","name":{"kind":"Name","value":"user"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"firstName"}},{"kind":"Field","name":{"kind":"Name","value":"lastName"}},{"kind":"Field","name":{"kind":"Name","value":"profile"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"balance"}}]}}]}}]}}]}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"AboveMaxSaldo"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"error"}},{"kind":"Field","name":{"kind":"Name","value":"message"}},{"kind":"Field","name":{"kind":"Name","value":"maxSaldo"}},{"kind":"Field","name":{"kind":"Name","value":"above"}}]}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"FormErrors"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"error"}},{"kind":"Field","name":{"kind":"Name","value":"message"}},{"kind":"Field","name":{"kind":"Name","value":"fields"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"field"}},{"kind":"Field","name":{"kind":"Name","value":"message"}}]}}]}}]}}]}}]} as unknown as DocumentNode; +export const CreateDepositDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"CreateDeposit"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"userId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"amount"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Int"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"createDeposit"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"userId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"userId"}}},{"kind":"Argument","name":{"kind":"Name","value":"amount"},"value":{"kind":"Variable","name":{"kind":"Name","value":"amount"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"CreateDepositSuccess"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"transaction"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"amount"}},{"kind":"Field","name":{"kind":"Name","value":"user"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"firstName"}},{"kind":"Field","name":{"kind":"Name","value":"lastName"}},{"kind":"Field","name":{"kind":"Name","value":"profile"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"balance"}}]}}]}}]}}]}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"AboveMaxSaldo"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"error"}},{"kind":"Field","name":{"kind":"Name","value":"message"}},{"kind":"Field","name":{"kind":"Name","value":"maxSaldo"}},{"kind":"Field","name":{"kind":"Name","value":"above"}}]}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"FormErrors"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"error"}},{"kind":"Field","name":{"kind":"Name","value":"message"}},{"kind":"Field","name":{"kind":"Name","value":"fields"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"field"}},{"kind":"Field","name":{"kind":"Name","value":"message"}}]}}]}}]}}]}}]} as unknown as DocumentNode; export const AllProductsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"allProducts"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"active"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Boolean"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"allProducts"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"filters"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"active"},"value":{"kind":"Variable","name":{"kind":"Name","value":"active"}}}]}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"imageUrl"}},{"kind":"Field","name":{"kind":"Name","value":"salePriceExt"}},{"kind":"Field","name":{"kind":"Name","value":"salePriceInt"}}]}}]}}]} as unknown as DocumentNode; export const AllUsersDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"allUsers"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"allUsers"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"firstName"}},{"kind":"Field","name":{"kind":"Name","value":"lastName"}},{"kind":"Field","name":{"kind":"Name","value":"profile"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"balance"}},{"kind":"Field","name":{"kind":"Name","value":"image"}},{"kind":"Field","name":{"kind":"Name","value":"lastPurchaseDate"}}]}}]}}]}}]} as unknown as DocumentNode; -export const CreateOrderDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"CreateOrder"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"customerId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"orderLines"}},"type":{"kind":"NonNullType","type":{"kind":"ListType","type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"OrderLineInput"}}}}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"isExternal"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"Boolean"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"createOrder"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"customerId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"customerId"}}},{"kind":"Argument","name":{"kind":"Name","value":"orderLines"},"value":{"kind":"Variable","name":{"kind":"Name","value":"orderLines"}}},{"kind":"Argument","name":{"kind":"Name","value":"isExternal"},"value":{"kind":"Variable","name":{"kind":"Name","value":"isExternal"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"CreateOrderSuccess"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"order"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"customer"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}}]}},{"kind":"Field","name":{"kind":"Name","value":"orderSum"}}]}}]}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"InsufficientFunds"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"error"}},{"kind":"Field","name":{"kind":"Name","value":"message"}},{"kind":"Field","name":{"kind":"Name","value":"amountLacking"}}]}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"FormErrors"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"error"}},{"kind":"Field","name":{"kind":"Name","value":"message"}},{"kind":"Field","name":{"kind":"Name","value":"fields"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"field"}},{"kind":"Field","name":{"kind":"Name","value":"message"}}]}}]}}]}}]}}]} as unknown as DocumentNode; \ No newline at end of file +export const CreateOrderDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"CreateOrder"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"customerId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"orderLines"}},"type":{"kind":"NonNullType","type":{"kind":"ListType","type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"OrderLineInput"}}}}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"isExternal"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Boolean"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"createOrder"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"customerId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"customerId"}}},{"kind":"Argument","name":{"kind":"Name","value":"orderLines"},"value":{"kind":"Variable","name":{"kind":"Name","value":"orderLines"}}},{"kind":"Argument","name":{"kind":"Name","value":"isExternal"},"value":{"kind":"Variable","name":{"kind":"Name","value":"isExternal"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"CreateOrderSuccess"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"order"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"isExternal"}},{"kind":"Field","name":{"kind":"Name","value":"customer"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"firstName"}},{"kind":"Field","name":{"kind":"Name","value":"lastName"}},{"kind":"Field","name":{"kind":"Name","value":"profile"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"balance"}}]}}]}},{"kind":"Field","name":{"kind":"Name","value":"orderlines"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"amount"}},{"kind":"Field","name":{"kind":"Name","value":"unitPrice"}},{"kind":"Field","name":{"kind":"Name","value":"product"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"salePriceExt"}},{"kind":"Field","name":{"kind":"Name","value":"salePriceInt"}}]}}]}},{"kind":"Field","name":{"kind":"Name","value":"orderSum"}}]}}]}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"InsufficientFunds"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"error"}},{"kind":"Field","name":{"kind":"Name","value":"message"}},{"kind":"Field","name":{"kind":"Name","value":"amountLacking"}}]}},{"kind":"InlineFragment","typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"FormErrors"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"error"}},{"kind":"Field","name":{"kind":"Name","value":"message"}},{"kind":"Field","name":{"kind":"Name","value":"fields"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"field"}},{"kind":"Field","name":{"kind":"Name","value":"message"}}]}}]}}]}}]}}]} as unknown as DocumentNode; \ No newline at end of file diff --git a/main/api/mutations.py b/main/api/mutations.py index 2c65c15..0d2971d 100644 --- a/main/api/mutations.py +++ b/main/api/mutations.py @@ -36,11 +36,11 @@ CreateOrderResponse = strawberry.union("CreateOrderResponse", [CreateOrderSucces def create_order(customer_id: ID, order_lines: list[OrderLineInput], is_external: bool = False) -> CreateOrderResponse: # clean and validate try: - customer = UserModel.objects.get(pk=customer_id) + user = UserModel.objects.get(pk=customer_id) except (UserModel.DoesNotExist, ValueError): - customer = None + user = None - if customer is None and not is_external: + if user is None and not is_external: message = "Du har ikke valgt hvem som skal kjøpe." return FormErrors( message=message, @@ -60,18 +60,25 @@ def create_order(customer_id: ID, order_lines: list[OrderLineInput], is_external price = product.sale_price_ext if is_external else product.sale_price_int ols.append(models.OrderLine(product=product, amount=ol.amount, unit_price=price)) order_sum += ol.amount * price + product.inventory_amount -= ol.amount + product.save() - if not is_external and customer.profile.balance <= order_sum: + if not is_external and user.profile.balance <= order_sum: return InsufficientFunds( - message="Du har ikke nok penger på bok.", amount_lacking=abs(order_sum - customer.profile.balance) + message="Du har ikke nok penger på bok.", amount_lacking=abs(order_sum - profile.balance) ) # create - order = models.Order.objects.create(customer=customer, order_sum=order_sum) + order = models.Order.objects.create(customer=user, order_sum=order_sum) for ol in ols: ol.order = order models.OrderLine.objects.bulk_create(ols) + # update balance + if not is_external: + user.profile.balance -= order_sum + user.profile.save() + return CreateOrderSuccess(order=order) @@ -110,7 +117,7 @@ def create_deposit(user_id: ID, amount: int) -> CreateDepositResponse: # 1337 is special <3 if amount + user.profile.balance > settings.BBS_SALDO_MAX and amount + user.profile.balance != 1337: return AboveMaxSaldo( - message='Du prøver å sette inn for mye penger', + message="Du prøver å sette inn for mye penger", max_saldo=settings.BBS_SALDO_MAX, above=amount + user.profile.balance - settings.BBS_SALDO_MAX, ) diff --git a/main/api/types.py b/main/api/types.py index feaee05..0087e1c 100644 --- a/main/api/types.py +++ b/main/api/types.py @@ -51,6 +51,17 @@ class Order: customer: "User" order_sum: auto created: auto + orderlines: list["OrderLine"] + is_external: bool + + +@strawberry_django.type(models.OrderLine) +class OrderLine: + id: auto + order: "Order" + product: "Product" + amount: auto + unit_price: auto @strawberry_django.type(models.Transaction) diff --git a/schema.graphql b/schema.graphql index a63d554..d53172f 100644 --- a/schema.graphql +++ b/schema.graphql @@ -53,6 +53,16 @@ type Order { customer: User orderSum: Float! created: DateTime! + orderlines: [OrderLine!] + isExternal: Boolean! +} + +type OrderLine { + id: ID! + order: Order! + product: Product! + amount: Int! + unitPrice: Float! } input OrderLineInput {