From e52c7d4522642fb857b0ee66cf36044260e6a31c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arwid=20Thornstr=C3=B6m?= Date: Thu, 16 Sep 2021 09:42:06 +0200 Subject: [PATCH] 42: add product group interiors mutation (#44) --- src/datasources/interior-api.ts | 1 - src/datasources/product-api.ts | 85 ++++++++++++++++++++++++++++++ src/resolvers/products-resolver.ts | 12 +++++ src/schema.graphql | 1 + 4 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/datasources/interior-api.ts b/src/datasources/interior-api.ts index d5c0f6a..ca26bc4 100644 --- a/src/datasources/interior-api.ts +++ b/src/datasources/interior-api.ts @@ -109,7 +109,6 @@ export class InteriorAPI extends BaseSQLDataSource { .from('v_interior_image_urls') .orderBy('position') .where('print_id', printId) - .cache(MINUTE) .then((rows) => { return rows.map((row) => { const res = { diff --git a/src/datasources/product-api.ts b/src/datasources/product-api.ts index eb25911..4ddb378 100644 --- a/src/datasources/product-api.ts +++ b/src/datasources/product-api.ts @@ -440,6 +440,91 @@ export class ProductAPI extends BaseSQLDataSource { } } + /** + * @func productGroupInteriors + * + * @param productId The product id for the targeted product + * @param groupId The targeted group id, if product does not have group it will be created + * @param roomNames The roomNames that should be inserted to the product group, the position is the order of the list. + */ + async addInteriorsToProductGroup( + productId: number, + groupId: number, + roomNames: string[], + ): Promise[]> { + // We need to room ids from the roomNames to insert into interiors table + const roomIdResponse = await this.knex.raw( + `SELECT id, name FROM rooms WHERE name IN (${roomNames + .map((roomName) => `'${roomName}'`) + .join(',')});`, + ); + + const roomIdsForInsert = []; + // Sort the id and name correct + for (const roomName of roomNames) { + roomIdsForInsert.push( + roomIdResponse.rows.filter((item) => { + return item.name === roomName; + })[0].id, + ); + } + + // Get all the printproducts from the productId defined. + // This is needed for the insert and for the check if a product + // has the defined group or not. + const allPrintProducts = await this.knex.raw( + `SELECT printid, groupid FROM "product-printproducts" WHERE productid = ?`, + [productId], + ); + + // Predefine the print id needed for insert. + let insertPrintId; + + // Map out the group ids. + const currentGroupIds = allPrintProducts.rows.map(({ groupid }) => groupid); + + // This product does not have group with groupId + if (!currentGroupIds.includes(groupId)) { + await this.updateGroups(productId, currentGroupIds.concat([groupId])); + + // Get the new printId + const printIdsResponse = await this.knex.raw( + `SELECT printid FROM "product-printproducts" WHERE productid = ? AND groupid = ?`, + [productId, groupId], + ); + insertPrintId = printIdsResponse.rows.map(({ printid }) => printid)[0]; + } else { + // This product had the targeted groupid, then we use + // the previous query for printId to save some DB calls. + insertPrintId = allPrintProducts.rows + .map(({ printid, groupid }) => { + if (groupid === groupId) { + return printid; + } + }) + .filter(Boolean)[0]; // Remove falsy + } + + // Delete all prior interiors + await this.knex.raw(`DELETE FROM interiors WHERE print_id = ?`, [ + insertPrintId, + ]); + + // Insert room-ids, printids and position into "interiors" table. + const insertPromises = []; + roomIdsForInsert.forEach((roomId, index) => { + // The position is determined by the order of roomNames variable. + insertPromises.push( + this.knex.raw( + `INSERT INTO interiors (print_id, room_id, position) VALUES (?, ?, ?)`, + [insertPrintId, roomId, index], + ), + ); + }); + + return Promise.all(insertPromises); + } + async setProportionsWarning( productId: number, proportions: ProductProportionsInput, diff --git a/src/resolvers/products-resolver.ts b/src/resolvers/products-resolver.ts index cd65f2a..ff22a96 100644 --- a/src/resolvers/products-resolver.ts +++ b/src/resolvers/products-resolver.ts @@ -122,6 +122,18 @@ export const productMutationTypeDefs = { )).generateNewInteriors(product); return product; }, + async productGroupInteriors( + _, + { productId, groupId, roomNames }, + { dataSources }, + ) { + await (dataSources.productApi).addInteriorsToProductGroup( + productId, + groupId, + roomNames, + ); + return (dataSources.productApi).getProduct(productId); + }, async productKeywords(_, { productId, keywordIds }, { dataSources }) { await (dataSources.keywordApi).setProductKeywords( productId, diff --git a/src/schema.graphql b/src/schema.graphql index 1c58151..f7b47fb 100644 --- a/src/schema.graphql +++ b/src/schema.graphql @@ -441,6 +441,7 @@ type Mutation { markets: [ProductBlacklistingInput] ): Product productGroup(productId: Int!, groupIds: [Int]): Product + productGroupInteriors(productId: Int!, groupId: Int!, roomNames: [String]): Product productKeywords(productId: Int!, keywordIds: [Int]): Product productProportionsWarning( productId: Int!