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
+70 -5
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(
`SELECT productid2 FROM "product-products_products" WHERE productid1 = ${id}`,
)
.cache(MINUTE * 60)
const ids = await this.knex
.raw(
`SELECT productid2 FROM "product-products_products" WHERE productid1 = ${id}`,
)
.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);
}
}