Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #429

Merged
merged 4 commits into from
Jan 6, 2024
Merged

Dev #429

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
const { error } = require('@evershop/evershop/src/lib/log/debuger');
const {
getConnection
} = require('@evershop/evershop/src/lib/postgres/connection');
const { pool } = require('@evershop/evershop/src/lib/postgres/connection');
const {
INVALID_PAYLOAD,
OK,
INTERNAL_SERVER_ERROR
} = require('@evershop/evershop/src/lib/util/httpStatus');
const {
startTransaction,
rollback,
commit,
select,
update
} = require('@evershop/postgres-query-builder');
const { select } = require('@evershop/postgres-query-builder');
const updateProduct = require('../../services/product/updateProduct');

// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, delegate, next) => {
const { category_id } = request.params;
const { product_id } = request.body;
const connection = await getConnection();
await startTransaction(connection);
try {
// Check if the category is exists
const category = await select()
.from('category')
.where('uuid', '=', category_id)
.load(connection);
.load(pool);
if (!category) {
response.status(INVALID_PAYLOAD);
return response.json({
Expand All @@ -39,7 +30,7 @@ module.exports = async (request, response, delegate, next) => {
const product = await select()
.from('product')
.where('uuid', '=', product_id)
.load(connection);
.load(pool);
if (!product) {
response.status(INVALID_PAYLOAD);
return response.json({
Expand All @@ -52,7 +43,7 @@ module.exports = async (request, response, delegate, next) => {
.from('product')
.where('category_id', '=', category.category_id)
.and('product_id', '=', product.product_id)
.load(connection);
.load(pool);
if (productCategory) {
response.status(OK);
return response.json({
Expand All @@ -61,14 +52,15 @@ module.exports = async (request, response, delegate, next) => {
});
}

// Assign the product to the category
await update('product')
.given({
await updateProduct(
product_id,
{
category_id: category.category_id
})
.where('product_id', '=', product.product_id)
.execute(connection);
await commit(connection);
},
{
routeId: request.currentRoute.id
}
);
response.status(OK);
return response.json({
success: true,
Expand All @@ -78,7 +70,6 @@ module.exports = async (request, response, delegate, next) => {
}
});
} catch (e) {
await rollback(connection);
error(e);
response.status(INTERNAL_SERVER_ERROR);
return response.json({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
const {
getConnection
} = require('@evershop/evershop/src/lib/postgres/connection');
const { pool } = require('@evershop/evershop/src/lib/postgres/connection');
const {
INVALID_PAYLOAD,
OK,
INTERNAL_SERVER_ERROR
} = require('@evershop/evershop/src/lib/util/httpStatus');
const {
startTransaction,
rollback,
commit,
select,
update
} = require('@evershop/postgres-query-builder');
const { select } = require('@evershop/postgres-query-builder');
const updateProduct = require('../../services/product/updateProduct');

// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, delegate, next) => {
const { category_id, product_id } = request.params;
const connection = await getConnection();
await startTransaction(connection);
try {
// Check if the category is exists
const category = await select()
.from('category')
.where('uuid', '=', category_id)
.load(connection);
.load(pool);

if (!category) {
response.status(INVALID_PAYLOAD);
Expand All @@ -38,7 +29,7 @@ module.exports = async (request, response, delegate, next) => {
const product = await select()
.from('product')
.where('uuid', '=', product_id)
.load(connection);
.load(pool);

if (!product) {
response.status(INVALID_PAYLOAD);
Expand All @@ -49,14 +40,7 @@ module.exports = async (request, response, delegate, next) => {
}

// Remove the product from the category
await update('product')
.given({
category_id: null
})
.where('product_id', '=', product.product_id)
.execute(connection);

await commit(connection);
await updateProduct(product_id, { category_id: null });
response.status(OK);
response.json({
success: true,
Expand All @@ -66,7 +50,6 @@ module.exports = async (request, response, delegate, next) => {
}
});
} catch (e) {
await rollback(connection);
response.status(INTERNAL_SERVER_ERROR);
response.json({
success: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,12 @@ async function updateProductData(uuid, data, connection) {
throw new Error('Requested product not found');
}

let newProduct;
try {
const newProduct = await update('product')
newProduct = await update('product')
.given(data)
.where('uuid', '=', uuid)
.execute(connection);
Object.assign(product, newProduct);
} catch (e) {
if (!e.message.includes('No data was provided')) {
throw e;
Expand All @@ -294,17 +294,22 @@ async function updateProductData(uuid, data, connection) {

// Update product category and tax class to all products in same variant group
if (product.variant_group_id) {
const sharedData = {
tax_class: product.tax_class,
category_id: product.category_id
};
await update('product')
.given(sharedData)
.where('variant_group_id', '=', product.variant_group_id)
.and('product_id', '<>', product.product_id)
.execute(connection);
const sharedData = {};
if (newProduct.tax_class !== product.tax_class) {
sharedData.tax_class = newProduct.tax_class;
}
if (newProduct.category_id !== product.category_id) {
sharedData.category_id = newProduct.category_id;
}
if (Object.keys(sharedData).length > 0) {
await update('product')
.given(sharedData)
.where('variant_group_id', '=', product.variant_group_id)
.and('product_id', '<>', product.product_id)
.execute(connection);
}
}

Object.assign(product, newProduct);
return product;
}

Expand Down
50 changes: 0 additions & 50 deletions packages/evershop/src/modules/oms/api/bestsellers/loadData.js

This file was deleted.

5 changes: 0 additions & 5 deletions packages/evershop/src/modules/oms/api/bestsellers/route.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
extend type Product {
soldQty: Int
}

extend type Query {
bestSellers: [Product]
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
const { select } = require('@evershop/postgres-query-builder');
const { pool } = require('@evershop/evershop/src/lib/postgres/connection');
const { camelCase } = require('@evershop/evershop/src/lib/util/camelCase');
const {
getProductsBaseQuery
} = require('@evershop/evershop/src/modules/catalog/services/getProductsBaseQuery');
const { sql } = require('@evershop/postgres-query-builder');

module.exports = {
Query: {
bestSellers: async () => {
const query = select();
query
.from('product')
.leftJoin('product_description')
.on(
'product.product_id',
'=',
'product_description.product_description_product_id'
);
const query = getProductsBaseQuery();
query
.leftJoin('order_item')
.on('product.product_id', '=', 'order_item.product_id');

query
.select('product.*')
.select('product_description.*')
.select('SUM(order_item.qty)', 'qty')
.select(sql('"product".*'))
.select(sql('"product_description".*'))
.select(sql('"product_inventory".*'))
.select(sql('"product_image".*'))
.select('SUM(order_item.qty)', 'soldQty')
.select('SUM(order_item.product_id)', 'sum')
.where('order_item_id', 'IS NOT NULL', null);
query.groupBy('order_item.product_id').orderBy('qty', 'DESC').limit(0, 5);
query
.groupBy(
'order_item.product_id',
'product.product_id',
'product_description.product_description_id',
'product_inventory.product_inventory_id',
'product_image.product_image_id'
)
.orderBy('qty', 'DESC')
.limit(0, 5);
const results = await query.execute(pool);
return results.map((p) => camelCase(p));
}
Expand Down
Loading
Loading