Add product blacklisting mutation (#18)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user