Skip to content

Commit

Permalink
Refactoring the catalog APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
treoden committed Nov 26, 2023
1 parent aec67dc commit 1a3068d
Show file tree
Hide file tree
Showing 26 changed files with 463 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ module.exports = async (data, context) => {
}
// Merge hook context with context
Object.assign(hookContext, context);
const result = await hookable(createAttribute, hookContext)(
const attribute = await hookable(createAttribute, hookContext)(
data,
connection
);
await commit(connection);
return result;
return attribute;
} catch (e) {
await rollback(connection);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ module.exports = async (uuid, context) => {
}
// Merge hook context with context
Object.assign(hookContext, context);
const collection = await hookable(deleteAttribute, hookContext)(
const attribute = await hookable(deleteAttribute, hookContext)(
uuid,
connection
);
await commit(connection);
return collection;
return attribute;
} catch (e) {
await rollback(connection);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ async function updateAttributeData(uuid, data, connection) {
throw new Error('Requested attribute not found');
}
try {
const result = await update('attribute')
const attribute = await update('attribute')
.given(data)
.where('uuid', '=', uuid)
.execute(connection);
return result;
return attribute;
} catch (e) {
if (!e.message.includes('No data was provided')) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ async function insertCategoryData(data, connection) {
}
}

const result = await insert('category').given(data).execute(connection);
const category = await insert('category').given(data).execute(connection);
const description = await insert('category_description')
.given(data)
.prime('category_description_category_id', result.insertId)
.prime('category_description_category_id', category.insertId)
.execute(connection);

return {
...description,
...result
...category
};
}

Expand Down Expand Up @@ -90,12 +90,12 @@ module.exports = async (data, context) => {
}
// Merge hook context with context
Object.assign(hookContext, context);
const result = await hookable(createCategory, hookContext)(
const category = await hookable(createCategory, hookContext)(
data,
connection
);
await commit(connection);
return result;
return category;
} catch (e) {
await rollback(connection);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ module.exports = async (uuid, context) => {
}
// Merge hook context with context
Object.assign(hookContext, context);
const collection = await hookable(deleteCategory, hookContext)(
const category = await hookable(deleteCategory, hookContext)(
uuid,
connection
);
await commit(connection);
return collection;
return category;
} catch (e) {
await rollback(connection);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const categoryDataSchema = require('./categoryDataSchema.json');

function validateCategoryDataBeforeInsert(data) {
const ajv = getAjv();
categoryDataSchema.required = ['status'];
categoryDataSchema.required = [];
const jsonSchema = getValueSync(
'updateCategoryDataJsonSchema',
categoryDataSchema
Expand All @@ -33,35 +33,43 @@ function validateCategoryDataBeforeInsert(data) {
}

async function updateCategoryData(uuid, data, connection) {
const category = await select()
.from('category')
.where('uuid', '=', uuid)
.load(connection);

const query = select().from('category');
query
.leftJoin('category_description')
.on(
'category_description.category_description_category_id',
'=',
'category.category_id'
);
const category = await query.where('uuid', '=', uuid).load(connection);
if (!category) {
throw new Error('Requested category not found');
}
const result = await update('category')
.given(data)
.where('uuid', '=', uuid)
.execute(connection);

let description = {};
try {
description = await update('category_description')
const newCategory = await update('category')
.given(data)
.where('uuid', '=', uuid)
.execute(connection);
Object.assign(category, newCategory);
} catch (e) {
if (!e.message.includes('No data was provided')) {
throw e;
}
}
try {
const description = await update('category_description')
.given(data)
.where('category_description_category_id', '=', category.category_id)
.execute(connection);
Object.assign(category, description);
} catch (e) {
if (!e.message.includes('No data was provided')) {
throw e;
}
}

return {
...result,
...description
};
return category;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ function validateCollectionDataBeforeInsert(data) {
}

async function insertCollectionData(data, connection) {
const result = await insert('collection').given(data).execute(connection);
return result;
const collection = await insert('collection').given(data).execute(connection);
return collection;
}

/**
Expand Down Expand Up @@ -68,12 +68,12 @@ module.exports = async (data, context) => {
}
// Merge hook context with context
Object.assign(hookContext, context);
const result = await hookable(createCollection, hookContext)(
const collection = await hookable(createCollection, hookContext)(
data,
connection
);
await commit(connection);
return result;
return collection;
} catch (e) {
await rollback(connection);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ async function updateCollectionData(uuid, data, connection) {
}

try {
const result = await update('collection')
const newCollection = await update('collection')
.given(data)
.where('uuid', '=', uuid)
.execute(connection);

return result;
return newCollection;
} catch (e) {
if (!e.message.includes('No data was provided')) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ async function insertProductImages(images, productId, connection) {
}

async function insertProductData(data, connection) {
const result = await insert('product').given(data).execute(connection);
const product = await insert('product').given(data).execute(connection);
const description = await insert('product_description')
.given(data)
.prime('product_description_product_id', result.product_id)
.prime('product_description_product_id', product.product_id)
.execute(connection);

return {
...description,
...result
...product
};
}

Expand Down Expand Up @@ -226,9 +226,12 @@ module.exports = async (data, context) => {
}
// Merge hook context with context
Object.assign(hookContext, context);
const result = await hookable(createProduct, hookContext)(data, connection);
const product = await hookable(createProduct, hookContext)(
data,
connection
);
await commit(connection);
return result;
return product;
} catch (e) {
await rollback(connection);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ module.exports = async (uuid, context) => {
}
// Merge hook context with context
Object.assign(hookContext, context);
const collection = await hookable(deleteProduct, hookContext)(
const product = await hookable(deleteProduct, hookContext)(
uuid,
connection
);
await commit(connection);
return collection;
return product;
} catch (e) {
await rollback(connection);
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const productDataSchema = require('./productDataSchema.json');

function validateProductDataBeforeUpdate(data) {
const ajv = getAjv();
productDataSchema.required = ['sku'];
productDataSchema.required = [];
const jsonSchema = getValueSync(
'updateProductDataJsonSchema',
productDataSchema
Expand Down Expand Up @@ -255,16 +255,37 @@ async function updateProductImages(images, productId, connection) {
}

async function updateProductData(uuid, data, connection) {
const product = await update('product')
.given(data)
.where('uuid', '=', uuid)
.execute(connection);
let description = {};
const query = select().from('product');
query
.leftJoin('product_description')
.on(
'product_description.product_description_product_id',
'=',
'product.product_id'
);
const product = await query.where('uuid', '=', uuid).load(connection);
if (!product) {
throw new Error('Requested product not found');
}

try {
const 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;
}
}

try {
description = await update('product_description')
const description = await update('product_description')
.given(data)
.where('product_description_product_id', '=', product.product_id)
.execute(connection);
Object.assign(product, description);
} catch (e) {
if (!e.message.includes('No data was provided')) {
throw e;
Expand All @@ -280,10 +301,7 @@ async function updateProductData(uuid, data, connection) {
.execute(connection);
}

return {
...product,
...description
};
return product;
}

/**
Expand Down Expand Up @@ -355,13 +373,13 @@ module.exports = async (uuid, data, context) => {
}
// Merge hook context with context
Object.assign(hookContext, context);
const collection = await hookable(updateProduct, hookContext)(
const product = await hookable(updateProduct, hookContext)(
uuid,
data,
connection
);
await commit(connection);
return collection;
return product;
} catch (e) {
await rollback(connection);
throw e;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const createPage = require('../../services/page/createPage');

// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, delegate) => {
const data = request.body;
const result = await createPage(data, {
routeId: request.currentRoute.id
});

return result;
};
Loading

0 comments on commit 1a3068d

Please sign in to comment.