20: printproduct group mutation (#21)
* Finished the mutation for productGroups * Fixes after CR * added enums for typeid
This commit is contained in:
@@ -90,5 +90,6 @@ jspm_packages
|
|||||||
*.sublime-project
|
*.sublime-project
|
||||||
*.sublime-workspace
|
*.sublime-workspace
|
||||||
|
|
||||||
|
.vscode/
|
||||||
.env
|
.env
|
||||||
src/__test__/export
|
src/__test__/export
|
||||||
|
|||||||
@@ -14,10 +14,11 @@ import {
|
|||||||
ProductInfoInput,
|
ProductInfoInput,
|
||||||
ProductBlacklistInput,
|
ProductBlacklistInput,
|
||||||
ProductBlacklistMarketInput,
|
ProductBlacklistMarketInput,
|
||||||
|
ProductMaterials,
|
||||||
|
ProductSizeTypes,
|
||||||
} from '../types/product-types';
|
} from '../types/product-types';
|
||||||
import { Maybe } from '../types/types';
|
import { Maybe } from '../types/types';
|
||||||
import { convertToPossibleType } from './utils';
|
import { convertToPossibleType } from './utils';
|
||||||
import { isNode } from 'graphql/language/ast';
|
|
||||||
|
|
||||||
const MINUTE = 60;
|
const MINUTE = 60;
|
||||||
|
|
||||||
@@ -229,4 +230,123 @@ export class ProductAPI extends BaseSQLDataSource {
|
|||||||
});
|
});
|
||||||
return Promise.all(insertsPromises);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { ProductAPI } from '../datasources/product-api';
|
import { ProductAPI } from '../datasources/product-api';
|
||||||
import {
|
import {
|
||||||
Product,
|
Product,
|
||||||
|
ProductGroup,
|
||||||
ProductListResult,
|
ProductListResult,
|
||||||
ProductsFilterInput,
|
ProductsFilterInput,
|
||||||
} from '../types/product-types';
|
} from '../types/product-types';
|
||||||
@@ -94,6 +95,13 @@ export const productMutationTypeDefs = {
|
|||||||
);
|
);
|
||||||
return (<ProductAPI>dataSources.productApi).getProduct(productId);
|
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 }) {
|
async productKeywords(_, { productId, keywordIds }, { dataSources }) {
|
||||||
await (<KeywordAPI>dataSources.keywordApi).setProductKeywords(
|
await (<KeywordAPI>dataSources.keywordApi).setProductKeywords(
|
||||||
productId,
|
productId,
|
||||||
|
|||||||
@@ -402,5 +402,6 @@ type Mutation {
|
|||||||
productId: Int!
|
productId: Int!
|
||||||
markets: [ProductBlacklistingInput]
|
markets: [ProductBlacklistingInput]
|
||||||
): Product
|
): Product
|
||||||
|
productGroup(productId: Int!, groupIds: [Int]): Product
|
||||||
productKeywords(productId: Int!, keywordIds: [Int]): Product
|
productKeywords(productId: Int!, keywordIds: [Int]): Product
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,6 +51,21 @@ export enum ProductGroup {
|
|||||||
UNKNOWN = 9,
|
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 {
|
export enum ProductType {
|
||||||
UNKNOWN = 0,
|
UNKNOWN = 0,
|
||||||
PHOTO = 1,
|
PHOTO = 1,
|
||||||
|
|||||||
Reference in New Issue
Block a user