20: printproduct group mutation (#21)

* Finished the mutation for productGroups

* Fixes after CR

* added enums for typeid
This commit is contained in:
Arwid Thornström
2021-09-03 15:11:06 +02:00
committed by GitHub
parent a4b847c7be
commit 8d83205747
5 changed files with 146 additions and 1 deletions
+1
View File
@@ -90,5 +90,6 @@ jspm_packages
*.sublime-project
*.sublime-workspace
.vscode/
.env
src/__test__/export
+121 -1
View File
@@ -14,10 +14,11 @@ import {
ProductInfoInput,
ProductBlacklistInput,
ProductBlacklistMarketInput,
ProductMaterials,
ProductSizeTypes,
} from '../types/product-types';
import { Maybe } from '../types/types';
import { convertToPossibleType } from './utils';
import { isNode } from 'graphql/language/ast';
const MINUTE = 60;
@@ -229,4 +230,123 @@ export class ProductAPI extends BaseSQLDataSource {
});
return Promise.all(insertsPromises);
}
async updateGroups(productId: number, groupIds: number[]): Promise<any[]> {
// Fetch the current state
const allPrintProducts = await this.knex.raw(
`SELECT printid, groupid FROM "product-printproducts" WHERE productid = ?`,
[productId],
);
const currentGroups = allPrintProducts.rows.map((item) => item.groupid);
// What we should add
const shouldAdd = groupIds.filter((group) => {
return !currentGroups.includes(group);
});
// What we should delete
const shouldDelete = allPrintProducts.rows.filter((item) => {
return !groupIds.includes(item.groupid);
});
// DELETE
if (shouldDelete.length > 0) {
// Delete from product-printproducts_materials
const printProductIds = shouldDelete
.map((item) => item.printid)
.join(',');
// Delete from materials first, must be in sequence since we have constraint in materials to printproducts
await this.knex.raw(
`DELETE FROM "product-printproducts_materials" WHERE printid IN (${printProductIds})`,
);
// Delete from product-printproducts
await this.knex.raw(
`DELETE FROM "product-printproducts" WHERE printid IN (${printProductIds})`,
);
}
// INSERT
if (shouldAdd.length > 0) {
const insertionProducts = [];
// Insert in product-printproducts
// TODO: Depracate typeid, its not used to check repeating
const insertQuery = `INSERT INTO "product-printproducts" (productid, groupid, typeid) VALUES (?, ?, ?)`;
shouldAdd.forEach((groupId) => {
insertionProducts.push(
this.knex.raw(insertQuery, [
productId,
groupId,
groupId === ProductGroup.WALLPAPER
? ProductSizeTypes.TILING
: ProductSizeTypes.SCALING,
]),
);
});
// Run parallell
await Promise.all(insertionProducts);
// Fetch the new printids to be able to insert new materials
const newPrintIds = await this.knex.raw(
`SELECT printid, groupid FROM "product-printproducts" WHERE productid = ${productId} AND groupid IN (${shouldAdd.join(
',',
)})`,
);
// Insert printIds into product-printproducts_materials
const insertMaterials = [];
const insertMaterialQuery = `INSERT INTO "product-printproducts_materials" (printid, materialid) VALUES (?, ?)`;
newPrintIds.rows.forEach(({ printid, groupid }) => {
switch (groupid) {
case ProductGroup.PHOTO_WALLPAPER:
case ProductGroup.WALLPAPER:
insertMaterials.push(
this.knex.raw(insertMaterialQuery, [
printid,
ProductMaterials.STANDARD_WALLPAPER,
]),
);
insertMaterials.push(
this.knex.raw(insertMaterialQuery, [
printid,
ProductMaterials.SELF_ADHESIVE_WALLPAPER,
]),
);
insertMaterials.push(
this.knex.raw(insertMaterialQuery, [
printid,
ProductMaterials.PREMIUM_WALLPAPER,
]),
);
break;
case ProductGroup.CANVAS:
insertMaterials.push(
this.knex.raw(insertMaterialQuery, [
printid,
ProductMaterials.STANDARD_CANVAS,
]),
);
break;
case ProductGroup.POSTER:
insertMaterials.push(
this.knex.raw(insertMaterialQuery, [
printid,
ProductMaterials.PREMIUM_POSTER,
]),
);
break;
case ProductGroup.FRAMED_PRINT:
insertMaterials.push(
this.knex.raw(insertMaterialQuery, [
printid,
ProductMaterials.PREMIUM_FRAMED_PRINT,
]),
);
break;
}
});
return Promise.all(insertMaterials);
}
}
}
+8
View File
@@ -1,6 +1,7 @@
import { ProductAPI } from '../datasources/product-api';
import {
Product,
ProductGroup,
ProductListResult,
ProductsFilterInput,
} from '../types/product-types';
@@ -94,6 +95,13 @@ export const productMutationTypeDefs = {
);
return (<ProductAPI>dataSources.productApi).getProduct(productId);
},
async productGroup(_, { productId, groupIds }, { dataSources }) {
await (<ProductAPI>dataSources.productApi).updateGroups(
productId,
groupIds,
);
return (<ProductAPI>dataSources.productApi).getProduct(productId);
},
async productKeywords(_, { productId, keywordIds }, { dataSources }) {
await (<KeywordAPI>dataSources.keywordApi).setProductKeywords(
productId,
+1
View File
@@ -402,5 +402,6 @@ type Mutation {
productId: Int!
markets: [ProductBlacklistingInput]
): Product
productGroup(productId: Int!, groupIds: [Int]): Product
productKeywords(productId: Int!, keywordIds: [Int]): Product
}
+15
View File
@@ -51,6 +51,21 @@ export enum ProductGroup {
UNKNOWN = 9,
}
export enum ProductMaterials {
STANDARD_WALLPAPER = 1,
SELF_ADHESIVE_WALLPAPER = 2,
STANDARD_CANVAS = 3,
PREMIUM_WALLPAPER = 4,
PREMIUM_POSTER = 5,
PREMIUM_FRAMED_PRINT = 6,
}
export enum ProductSizeTypes {
SCALING = 1,
FIXED = 2,
TILING = 3,
}
export enum ProductType {
UNKNOWN = 0,
PHOTO = 1,