20: printproduct group mutation (#21)
* Finished the mutation for productGroups * Fixes after CR * added enums for typeid
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user