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, Orientation,
ProductsFilterInput, ProductsFilterInput,
ProductInfoInput, ProductInfoInput,
ProductBlacklistInput,
ProductBlacklistMarketInput,
} from '../types/product-types'; } from '../types/product-types';
import { Maybe } from '../types/types'; import { Maybe } from '../types/types';
import { convertToPossibleType } from './utils'; import { convertToPossibleType } from './utils';
@@ -200,4 +202,31 @@ export class ProductAPI extends BaseSQLDataSource {
); );
return Promise.all(promises); 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);
}
} }
+1 -1
View File
@@ -69,7 +69,7 @@ async function main() {
resolvers, resolvers,
dataSources, dataSources,
context, context,
introspection: false, introspection: true,
}); });
await server.listen(); await server.listen();
+7
View File
@@ -87,4 +87,11 @@ export const productMutationTypeDefs = {
); );
return (<ProductAPI>dataSources.productApi).getProduct(productId); return (<ProductAPI>dataSources.productApi).getProduct(productId);
}, },
async productBlacklisting(_, { productId, markets }, { dataSources }) {
await (<ProductAPI>dataSources.productApi).updateBlacklisting(
productId,
markets,
);
return (<ProductAPI>dataSources.productApi).getProduct(productId);
},
}; };
+12
View File
@@ -382,6 +382,18 @@ input ProductInfoInput {
visible: Boolean! visible: Boolean!
} }
input ProductBlacklistingInput {
"""
Only ones used are ProductGroup: PHOTO_WALLPAPER CANVAS WALLPAPER POSTER
"""
marketId: Int!
groupIds: [Int]
}
type Mutation { type Mutation {
productInfo(productId: Int!, info: ProductInfoInput!): Product productInfo(productId: Int!, info: ProductInfoInput!): Product
productBlacklisting(
productId: Int!
markets: [ProductBlacklistingInput]
): Product
} }
+10
View File
@@ -15,6 +15,16 @@ export interface ProductInfoInput {
visible: boolean; visible: boolean;
} }
export interface ProductBlacklistMarketInput {
marketId: number;
groupIds: Array<number>;
}
export interface ProductBlacklistInput {
productId: number;
markets: Array<ProductBlacklistMarketInput>;
}
// query types
export interface ProductsFilterInput { export interface ProductsFilterInput {
pagination: PaginationInput; pagination: PaginationInput;
filter?: ProductsFilter; filter?: ProductsFilter;