Skip to content
This repository has been archived by the owner on Nov 2, 2024. It is now read-only.

Commit

Permalink
✨ Make more admin for the webshop
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlevay committed Aug 22, 2023
1 parent 005c9ab commit bc1ac53
Show file tree
Hide file tree
Showing 21 changed files with 1,128 additions and 204 deletions.
16 changes: 16 additions & 0 deletions backend/services/core/graphql.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9324,6 +9324,22 @@
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "releaseDate",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Date",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { Knex } from "knex";

import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('product_inventory', (table) => {
table.dropColumn('release_date');
});
await knex.schema.alterTable('product', (table) => {
table.timestamp('release_date').notNullable();
})
table.timestamp('release_date').notNullable().defaultTo(knex.fn.now());
});
}


export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('product_inventory', (table) => {
table.timestamp('release_date').defaultTo(knex.fn.now());
});
await knex.schema.alterTable('product', (table) => {
table.dropColumn('release_date');
})
});
}

Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { Knex } from "knex";

import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('product', (table) => {
table.decimal('price', 8, 2).notNullable().alter();
});
}


export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('product', (table) => {
table.integer('price').notNullable().alter();
});
}

29 changes: 17 additions & 12 deletions backend/services/core/src/datasources/WebshopAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,40 +193,45 @@ export default class WebshopAPI extends dbUtils.KnexDataSource {
addInventory(ctx: context.UserContext, inventoryInput: gql.CreateInventoryInput):
Promise<gql.Maybe<gql.Product>> {
return this.withAccess('webshop:create', ctx, async () => {
const product = await this.knex<sql.Product>(TABLE.PRODUCT).where({ id: inventoryInput.productId }).first();
const product = await this.knex<sql.Product>(TABLE.PRODUCT)
.where({ id: inventoryInput.productId }).first();
if (!product) throw new Error('Product not found');
const newInventory = await this.knex<sql.ProductInventory>(TABLE.PRODUCT_INVENTORY).insert({
product_id: inventoryInput.productId,
variant: inventoryInput.variant,
quantity: inventoryInput.quantity,
product_discount_id: inventoryInput.discountId,
});
if(!newInventory) throw new Error('Failed to create inventory');
if (!newInventory) throw new Error('Failed to create inventory');
return this.getProductById(ctx, inventoryInput.productId);
});
}

updateInventory(ctx: context.UserContext, inventoryInput: gql.UpdateInventoryInput):
Promise<gql.Maybe<gql.Product>> {
return this.withAccess('webshop:create', ctx, async () => {
const inventory = await this.knex<sql.ProductInventory>(TABLE.PRODUCT_INVENTORY).where({ id: inventoryInput.inventoryId }).first();
const inventory = await this.knex<sql.ProductInventory>(TABLE.PRODUCT_INVENTORY)
.where({ id: inventoryInput.inventoryId }).first();
if (!inventory) throw new Error('Inventory not found');
await this.knex<sql.ProductInventory>(TABLE.PRODUCT_INVENTORY).where({ id: inventoryInput.inventoryId }).update({
variant: inventoryInput.variant,
quantity: inventoryInput.quantity,
product_discount_id: inventoryInput.discountId,
});
await this.knex<sql.ProductInventory>(TABLE.PRODUCT_INVENTORY)
.where({ id: inventoryInput.inventoryId }).update({
variant: inventoryInput.variant,
quantity: inventoryInput.quantity,
product_discount_id: inventoryInput.discountId,
});
return this.getProductById(ctx, inventory.product_id);
});
}

deleteInventory(ctx: context.UserContext, inventoryId: UUID): Promise<boolean> {
return this.withAccess('webshop:create', ctx, async () => {
const inventory = await this.knex<sql.ProductInventory>(TABLE.PRODUCT_INVENTORY).where({ id: inventoryId }).first();
const inventory = await this.knex<sql.ProductInventory>(TABLE.PRODUCT_INVENTORY)
.where({ id: inventoryId }).first();
if (!inventory) throw new Error('Inventory not found');
await this.knex<sql.ProductInventory>(TABLE.PRODUCT_INVENTORY).where({ id: inventoryId }).update({
deleted_at: new Date(),
});
await this.knex<sql.ProductInventory>(TABLE.PRODUCT_INVENTORY)
.where({ id: inventoryId }).update({
deleted_at: new Date(),
});
return true;
});
}
Expand Down
1 change: 1 addition & 0 deletions backend/services/core/src/schemas/webshop.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Product {
imageUrl: String!
inventory: [ProductInventory]!
category: ProductCategory
releaseDate: Date!
}

input CreateProductInput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const convertProduct = (
price: product.price,
imageUrl: product.image_url,
maxPerUser: product.max_per_user,
releaseDate: product.release_date,
category,
inventory,
});
Expand Down
2 changes: 2 additions & 0 deletions backend/services/core/src/types/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,7 @@ export type Product = {
maxPerUser: Scalars['Int']['output'];
name: Scalars['String']['output'];
price: Scalars['Float']['output'];
releaseDate: Scalars['Date']['output'];
};

export type ProductCategory = {
Expand Down Expand Up @@ -2822,6 +2823,7 @@ export type ProductResolvers<ContextType = any, ParentType extends ResolversPare
maxPerUser?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
name?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
price?: Resolver<ResolversTypes['Float'], ParentType, ContextType>;
releaseDate?: Resolver<ResolversTypes['Date'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

Expand Down
3 changes: 2 additions & 1 deletion backend/services/core/tests/products/productsFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Product, ProductCategory, CreateProductInput, CreateInventoryInput } from '~/src/types/graphql';
import { Product, ProductCategory, CreateProductInput } from '~/src/types/graphql';

export const hej = '';

Expand All @@ -20,5 +20,6 @@ export function expectedProduct(
name: category.name,
description: category.description,
},
releaseDate: new Date(),
};
}
106 changes: 105 additions & 1 deletion frontend/api/products.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ query Products($categoryId: UUID) {
price
maxPerUser
imageUrl
releaseDate
inventory {
id
variant
quantity
}
category {
id
name
description
}
}
}

query Product($id: UUID!) {
product(id: $id) {
id
name
description
price
maxPerUser
imageUrl
releaseDate
inventory {
id
variant
Expand All @@ -27,7 +50,7 @@ query ProductCategories {
}
}

mutation CreateProduct($input: ProductInput!) {
mutation CreateProduct($input: CreateProductInput!) {
webshop {
createProduct(input: $input) {
id
Expand All @@ -48,4 +71,85 @@ mutation CreateProduct($input: ProductInput!) {
}
}
}
}

mutation UpdateProduct($input: UpdateProductInput!) {
webshop {
updateProduct(input: $input) {
id
name
description
price
maxPerUser
imageUrl
inventory {
id
variant
quantity
}
category {
id
name
description
}
}
}
}

mutation CreateInventory($input: CreateInventoryInput!) {
webshop {
addInventory(input: $input) {
id
name
description
price
maxPerUser
imageUrl
inventory {
id
variant
quantity
}
category {
id
name
description
}
}
}
}

mutation UpdateInventory($input: UpdateInventoryInput!) {
webshop {
updateInventory(input: $input) {
id
name
description
price
maxPerUser
imageUrl
inventory {
id
variant
quantity
}
category {
id
name
description
}
}
}
}

mutation DeleteInventory($inventoryId: UUID!) {
webshop {
deleteInventory(inventoryId: $inventoryId)
}
}

mutation DeleteProduct($productId: UUID!) {
webshop {
deleteProduct(productId: $productId)
}
}
Loading

0 comments on commit bc1ac53

Please sign in to comment.