From 613ec8aad072af1fa3e0f08c50f837d0f3578aa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arwid=20Thornstr=C3=B6m?= Date: Fri, 17 Sep 2021 15:40:50 +0200 Subject: [PATCH] 47: refactor interiors mutation to handle uri (#48) * 47: refactor interiors mutation to handle uri * removed log --- src/datasources/product-api.ts | 59 +++++++++++++++++------------- src/resolvers/products-resolver.ts | 4 +- src/schema.graphql | 2 +- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/datasources/product-api.ts b/src/datasources/product-api.ts index a15c809..8ecd7a4 100644 --- a/src/datasources/product-api.ts +++ b/src/datasources/product-api.ts @@ -445,29 +445,36 @@ export class ProductAPI extends BaseSQLDataSource { * * @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. + * @param uris The uris for the interiors, common for rooms and own uploades */ async addInteriorsToProductGroup( productId: number, groupId: number, - roomNames: string[], + uris: 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, - ); - } + // Create object to hold data as we go along all the db calls + // This object needs to support new own uploaded images, these should have + // some other folder prefix, like /interiors-buffer/. These will + // be inserted into DB first and then the image on s3 will be + // moved to /interior-images/ with the correct id. + const insertRegistry = uris.map((uri, index) => { + let id = null; + let roomName = null; + if (uri.indexOf('/interiors/') > -1) { + const spl = uri.split('/'); + roomName = `${spl[5].split('.')[0]}_${spl[4]}_${spl[3]}`; + } + if (uri.indexOf('/interior-images/') > -1) { + id = parseInt(uri.split('/')[2].split('.')[0]); + } + return { + id: id, + uri: uri, + roomName: roomName || null, + roomId: null, + position: index, + }; + }); // Get all the printproducts from the productId defined. // This is needed for the insert and for the check if a product @@ -512,14 +519,14 @@ export class ProductAPI extends BaseSQLDataSource { // 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], - ), - ); + insertRegistry.forEach((item) => { + let sql; + if (item.roomName) { + sql = `INSERT INTO interiors (print_id, room_id, position) VALUES (${insertPrintId}, (SELECT id FROM rooms WHERE name = '${item.roomName}'), ${item.position})`; + } else { + sql = `INSERT INTO interiors (id, print_id, position) VALUES (${item.id}, ${insertPrintId}, ${item.position})`; + } + insertPromises.push(this.knex.raw(sql)); }); return Promise.all(insertPromises); diff --git a/src/resolvers/products-resolver.ts b/src/resolvers/products-resolver.ts index f7b5416..9e3bbb0 100644 --- a/src/resolvers/products-resolver.ts +++ b/src/resolvers/products-resolver.ts @@ -125,13 +125,13 @@ export const productMutationTypeDefs = { }, async productGroupInteriors( _, - { productId, groupId, roomNames }, + { productId, groupId, uris }, { dataSources }, ) { await (dataSources.productApi).addInteriorsToProductGroup( productId, groupId, - roomNames, + uris, ); return (dataSources.productApi).getProduct(productId); }, diff --git a/src/schema.graphql b/src/schema.graphql index 0c91627..6b12ff3 100644 --- a/src/schema.graphql +++ b/src/schema.graphql @@ -441,7 +441,7 @@ type Mutation { markets: [ProductBlacklistingInput] ): Product productGroup(productId: Int!, groupIds: [Int]): Product - productGroupInteriors(productId: Int!, groupId: Int!, roomNames: [String]): Product + productGroupInteriors(productId: Int!, groupId: Int!, uris: [String]): Product productKeywords(productId: Int!, keywordIds: [Int]): Product productProportionsWarning( productId: Int!