Skip to content

Commit

Permalink
Merch store db tests (#42)
Browse files Browse the repository at this point in the history
* removed required from the description field, throw error if team name is empty

* added db ops and tests

* removed unneeded chat should

* remove commits from previous ticket

* fixed formatting

* added products export

* minor fixes to unit testing
  • Loading branch information
klau1011 authored Jul 7, 2023
1 parent 5672b8b commit 7a9b5e4
Show file tree
Hide file tree
Showing 5 changed files with 558 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/backend/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import featurePermissions from './feature-permissions';
import newsletterRecipients from './newsletter-recipient';
import blogs from './blogs';
import geeseFeatures from './geese-features';
import products from './products'

export default {
users: {
Expand Down Expand Up @@ -42,4 +43,7 @@ export default {
geeseFeatures: {
...geeseFeatures(db),
},
products: {
...products(db),
}
};
143 changes: 143 additions & 0 deletions src/backend/db/products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import {
fromProductVariation,
toProductVariation,
} from '../models/products';

const getProducts = (db) => () =>
db('merch_products')
.then((res) => {
return res;
})
.catch((err) => {
console.error(`Error in getProducts: ${err}`);
throw err;
});

const getProductVariations = (db) => () =>
db('merch_product_variations')
.then((res) => {
return res.map(toProductVariation);
})
.catch((err) => {
console.error(`Error in getProductVariations: ${err}`);
throw err;
});

const getProductById = (db) => (id) =>
db('merch_products')
.where({ id })
.then((res) => {
return res;
})
.catch((err) => {
console.error(`Error in getProductById: ${err}`);
throw err;
});

const getProductVariationsById = (db) => (variationId) =>
db('merch_product_variations')
.where({ variationId })
.then((res) => {
return res.map(toProductVariation);
})
.catch((err) => {
console.error(`Error in getProductVariationsById: ${err}`);
throw err;
});

const addProduct = (db) => (productInfo) =>
db('merch_products')
.insert(productInfo)
.returning('id')
.then((response) => {
return response;
})
.catch((err) => {
console.error(`Error in addProduct: ${err}`);
throw err;
});

const addProductVariation = (db) => (productVariationInfo) =>
db('merch_product_variations')
.insert(fromProductVariation(productVariationInfo))
.returning('id')
.then((response) => {
return response;
})
.catch((err) => {
console.error(`Error in addProductVariation: ${err}`);
throw err;
});

const updateProductById = (db) => (id, productInfo) =>
db('merch_products')
.where({
id,
})
.update({
productInfo,
})
.then((response) => {
return response;
})
.catch((err) => {
console.error(`Error in updateProductById: ${err}`);
throw err;
});

const updateProductVariationById = (db) => (id, productVariationInfo) =>
db('merch_product_variations')
.where({
id,
})
.update({
...fromProductVariation(productVariationInfo),
})
.then((response) => {
return response;
})
.catch((err) => {
console.error(`Error in updateProductVariationById: ${err}`);
throw err;
});

const deleteProductById = (db) => (id) =>
db('merch_products')
.where({
id,
})
.del()
.then((response) => {
return response;
})
.catch((err) => {
console.error(`Error in deleteProductById: ${err}`);
throw err;
});

const deleteProductVariationById = (db) => (id) =>
db('merch_product_variations')
.where({
id,
})
.del()
.then((response) => {
return response;
})
.catch((err) => {
console.error(`Error in deleteProductVariationsById: ${err}`);
throw err;
});

export default (db) => ({
getProducts: getProducts(db),
getProductVariations: getProductVariations(db),
getProductById: getProductById(db),
getProductVariationsById: getProductVariationsById(db),
addProduct: addProduct(db),
addProductVariation: addProductVariation(db),
updateProductById: updateProductById(db),
updateProductVariationById: updateProductVariationById(db),
deleteProductById: deleteProductById(db),
deleteProductVariationById: deleteProductVariationById(db),
});
21 changes: 21 additions & 0 deletions src/backend/models/products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { parseTimeForResponse } from '../utils/db-dates';
import renameProps from '../utils/rename-props';


export const fromProductVariation = (productVariation) => ({
...renameProps(productVariation, {
variationName: 'variation_name',
productId: 'product_id',
lastUpdated: 'last_updated',
}),
last_updated: parseTimeForResponse(productVariation.lastUpdated),
});

export const toProductVariation = (productVariation) => ({
...renameProps(productVariation, {
variation_name: 'variationName',
product_id: 'productId',
last_updated: 'lastUpdated',
}),
lastUpdated: parseTimeForResponse(productVariation.last_updated),
});
Loading

0 comments on commit 7a9b5e4

Please sign in to comment.