Add product blacklisting mutation (#18)

This commit is contained in:
Niklas Fondberg
2021-09-01 15:27:52 +02:00
committed by GitHub
parent cbb0024359
commit cb4a7c40b7
5 changed files with 59 additions and 1 deletions
+29
View File
@@ -12,6 +12,8 @@ import {
Orientation,
ProductsFilterInput,
ProductInfoInput,
ProductBlacklistInput,
ProductBlacklistMarketInput,
} from '../types/product-types';
import { Maybe } from '../types/types';
import { convertToPossibleType } from './utils';
@@ -200,4 +202,31 @@ export class ProductAPI extends BaseSQLDataSource {
);
return Promise.all(promises);
}
async updateBlacklisting(
productId: number,
markets: Array<ProductBlacklistMarketInput>,
) {
const sql = /* sql */ `
INSERT INTO product_blacklist (product_id, group_id, market_id, created_at, updated_at)
VALUES (?, ?, ?, now(), now())
ON CONFLICT (product_id, group_id, market_id)
DO NOTHING`;
const insertsPromises = [];
const deleteQuery = this.knex.raw(
/* sql */ `
DELETE FROM product_blacklist WHERE product_id = ?
`,
[productId],
);
insertsPromises.push(deleteQuery);
markets.forEach((market) => {
market.groupIds.forEach((groupId) => {
const query = this.knex.raw(sql, [productId, groupId, market.marketId]);
insertsPromises.push(query);
});
});
return Promise.all(insertsPromises);
}
}