From 8cea3b5db6e24144e351f9c9b50ca9004eabc74c Mon Sep 17 00:00:00 2001 From: Niklas Fondberg Date: Fri, 3 Dec 2021 11:48:10 +0100 Subject: [PATCH] Add mass edit of visible, browsable and blacklisting (#86) * Add massedit WIP * Add massedit * Review comments * Remove more debug --- src/datasources/product-api.ts | 33 ++++++++++++++++++++++++++ src/resolvers/products-resolver.ts | 37 ++++++++++++++++++++++++++++++ src/schema.graphql | 10 ++++++++ 3 files changed, 80 insertions(+) diff --git a/src/datasources/product-api.ts b/src/datasources/product-api.ts index 9e0fad1..d8626f6 100644 --- a/src/datasources/product-api.ts +++ b/src/datasources/product-api.ts @@ -463,6 +463,39 @@ export class ProductAPI extends BaseSQLDataSource { return Promise.all(promises); } + /** + * @function massUpdatePublishing + * @description Mass update visible and browsable + */ + async massUpdatePublishing( + productIds: Array, + visible: boolean, + browsable: boolean, + ) { + return this.knex + .table('product-products') + .update({ + browsable: browsable ? 1 : 0, + visible: visible ? 1 : 0, + }) + .whereIn('productid', productIds); + } + + /** + * @function massUpdateBlacklisting + * @description Mass update blacklisting + */ + async massUpdateBlacklisting( + productIds: Array, + markets: Array, + ) { + const promises = productIds.map((pId) => + this.updateBlacklisting(pId, markets), + ); + + return Promise.all(promises); + } + /** * @function updateFocusPoint * @description Updates the focus point on a motif diff --git a/src/resolvers/products-resolver.ts b/src/resolvers/products-resolver.ts index 12a7107..e3b2e28 100644 --- a/src/resolvers/products-resolver.ts +++ b/src/resolvers/products-resolver.ts @@ -416,4 +416,41 @@ export const productMutationTypeDefs = { ); return (dataSources.productApi).getProduct(productId); }, + /** + * @function massUpdatePublishing + * @description Mutate the publishing for products + */ + async massUpdatePublishing( + _, + { productIds, visible, browsable }, + { dataSources, auth }, + ) { + checkAccess(['products.write'], auth); + await (dataSources.productApi).massUpdatePublishing( + productIds, + visible, + browsable, + ); + return { + value: 'OK', + }; + }, + /** + * @function massUpdateBlacklisting + * @description Mutate the blacklisting for products + */ + async massUpdateBlacklisting( + _, + { productIds, markets }, + { dataSources, auth }, + ) { + checkAccess(['products.write'], auth); + await (dataSources.productApi).massUpdateBlacklisting( + productIds, + markets, + ); + return { + value: 'OK', + }; + }, }; diff --git a/src/schema.graphql b/src/schema.graphql index f1e47bf..11783b4 100644 --- a/src/schema.graphql +++ b/src/schema.graphql @@ -518,4 +518,14 @@ type Mutation { removeCategoriesFromProduct(productId: Int!, categoryIds: [Int]!): Product updateProductComments(productId: Int!, comments: String!): Product + + massUpdatePublishing( + productIds: [Int]! + visible: Boolean + browsable: Boolean + ): StringResult + massUpdateBlacklisting( + productIds: [Int]! + markets: [ProductBlacklistingInput] + ): StringResult }