Add related mutations (#43)

This commit is contained in:
Niklas Fondberg
2021-09-15 14:01:07 +02:00
committed by GitHub
parent 0a786d6754
commit c4915cb15f
4 changed files with 96 additions and 6 deletions
+2
View File
@@ -12,6 +12,8 @@ export const dbConfig = {
return 'product-products';
} else if (output === 'order_rows_fields') {
return 'order-rows_fields';
} else if (output === 'product_products_products') {
return 'product-products_products';
}
return output;
},
+68 -3
View File
@@ -19,6 +19,7 @@ import {
} from '../types/product-types';
import { Maybe } from '../types/types';
import { booleanStringField, nullOrNumber } from './utils';
import { UserInputError } from 'apollo-server';
const MINUTE = 60;
@@ -33,7 +34,10 @@ export class ProductAPI extends BaseSQLDataSource {
// }
getWallpaperTypes(row: any): Array<ProductWallpaperType> {
return row.wallpapertypes?.map((t: number) => ProductWallpaperType[t]);
if (!row.wallpapertypes) {
return [];
}
return row.wallpapertypes.map((t: number) => ProductWallpaperType[t]);
}
getProductTypes(row: any): Array<ProductType> {
@@ -185,10 +189,10 @@ export class ProductAPI extends BaseSQLDataSource {
}
async getRelatedProducts(id: number): Promise<Array<Product>> {
const ids = await this.cachedRaw(
const ids = await this.knex
.raw(
`SELECT productid2 FROM "product-products_products" WHERE productid1 = ${id}`,
)
.cache(MINUTE * 60)
.then((data) => data.rows.map((r) => r.productid2));
return ids.map(async (id) => this.getProduct(id));
@@ -471,4 +475,65 @@ export class ProductAPI extends BaseSQLDataSource {
return Promise.all(promises);
}
async addRelatedProducts(
productId: number,
articleNumbers: Array<string>,
): Promise<Promise<void>[]> {
const whereClause = articleNumbers.map((a) => `'${a}'`).join(',');
const query = /* sql */ `
SELECT pp.productid, ppf.value FROM "product-products" pp
LEFT JOIN "product-products_fields" ppf
ON ppf.productid = pp.productid AND ppf.fieldid = 1
WHERE ppf.value IN (${whereClause});
`;
const res = await this.cachedRaw(query)
.cache(MINUTE)
.then((data) => data.rows);
if (!res.length) {
throw new UserInputError('No articles found');
}
const fieldsToInsert = res.map((item) => ({
productid1: productId,
productid2: item.productid,
}));
// Add this productId as related to its friends
res.forEach((item) => {
fieldsToInsert.push({
productid1: item.productid,
productid2: productId,
});
});
return this.knex.raw(`? ON CONFLICT (productid1, productid2) DO NOTHING;`, [
this.knex('product-products_products').insert(fieldsToInsert),
]);
}
/**
* Removes variant products from productid as well as the sent in
* productid from its variants
*/
async removeRelatedProducts(
productId,
relatedProductIds,
): Promise<Promise<void>[]> {
// remove connection between the friends id and this produdId
const promises = relatedProductIds.map((pId) => {
return this.knex('product-products_products')
.where('productid1', pId)
.where('productid2', productId)
.del();
});
// remove connection to friends for this productsid
const query = this.knex('product-products_products')
.where('productid1', productId)
.whereIn('productid2', relatedProductIds)
.del();
promises.push(query);
return Promise.all(promises);
}
}
+19
View File
@@ -161,4 +161,23 @@ export const productMutationTypeDefs = {
);
return (<ProductAPI>dataSources.productApi).getProduct(productId);
},
async addRelatedProducts(_, { productId, articleNumbers }, { dataSources }) {
await (<ProductAPI>dataSources.productApi).addRelatedProducts(
productId,
articleNumbers,
);
return (<ProductAPI>dataSources.productApi).getProduct(productId);
},
async removeRelatedProducts(
_,
{ productId, relatedProductIds },
{ dataSources },
) {
await (<ProductAPI>dataSources.productApi).removeRelatedProducts(
productId,
relatedProductIds,
);
return (<ProductAPI>dataSources.productApi).getProduct(productId);
},
};
+5 -1
View File
@@ -263,7 +263,7 @@ type Product {
ref2: String
ref3: String
related: [Product]
wallpaperTypes: [ProductWallpaperType]
wallpaperTypes: [ProductWallpaperType]!
fields: ProductFields
orientation: Orientation
"""
@@ -454,5 +454,9 @@ type Mutation {
productId: Int!
wallpaperTypes: [ProductWallpaperType]!
): Product
addRelatedProducts(productId: Int!, articleNumbers: [String]!): Product
removeRelatedProducts(productId: Int!, relatedProductIds: [Int]!): Product
addKeyword(name: String!, type: KeywordType): Keyword
}