47: refactor interiors mutation to handle uri (#48)

* 47: refactor interiors mutation to handle uri

* removed log
This commit is contained in:
Arwid Thornström
2021-09-17 15:40:50 +02:00
committed by GitHub
parent 042a3ed0fa
commit 613ec8aad0
3 changed files with 36 additions and 29 deletions
+33 -26
View File
@@ -445,29 +445,36 @@ export class ProductAPI extends BaseSQLDataSource {
* *
* @param productId The product id for the targeted product * @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 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( async addInteriorsToProductGroup(
productId: number, productId: number,
groupId: number, groupId: number,
roomNames: string[], uris: string[],
): Promise<Promise<void>[]> { ): Promise<Promise<void>[]> {
// We need to room ids from the roomNames to insert into interiors table // Create object to hold data as we go along all the db calls
const roomIdResponse = await this.knex.raw( // This object needs to support new own uploaded images, these should have
`SELECT id, name FROM rooms WHERE name IN (${roomNames // some other folder prefix, like /interiors-buffer/. These will
.map((roomName) => `'${roomName}'`) // be inserted into DB first and then the image on s3 will be
.join(',')});`, // moved to /interior-images/ with the correct id.
); const insertRegistry = uris.map((uri, index) => {
let id = null;
const roomIdsForInsert = []; let roomName = null;
// Sort the id and name correct if (uri.indexOf('/interiors/') > -1) {
for (const roomName of roomNames) { const spl = uri.split('/');
roomIdsForInsert.push( roomName = `${spl[5].split('.')[0]}_${spl[4]}_${spl[3]}`;
roomIdResponse.rows.filter((item) => { }
return item.name === roomName; if (uri.indexOf('/interior-images/') > -1) {
})[0].id, 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. // Get all the printproducts from the productId defined.
// This is needed for the insert and for the check if a product // 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. // Insert room-ids, printids and position into "interiors" table.
const insertPromises = []; const insertPromises = [];
roomIdsForInsert.forEach((roomId, index) => { insertRegistry.forEach((item) => {
// The position is determined by the order of roomNames variable. let sql;
insertPromises.push( if (item.roomName) {
this.knex.raw( sql = `INSERT INTO interiors (print_id, room_id, position) VALUES (${insertPrintId}, (SELECT id FROM rooms WHERE name = '${item.roomName}'), ${item.position})`;
`INSERT INTO interiors (print_id, room_id, position) VALUES (?, ?, ?)`, } else {
[insertPrintId, roomId, index], sql = `INSERT INTO interiors (id, print_id, position) VALUES (${item.id}, ${insertPrintId}, ${item.position})`;
), }
); insertPromises.push(this.knex.raw(sql));
}); });
return Promise.all(insertPromises); return Promise.all(insertPromises);
+2 -2
View File
@@ -125,13 +125,13 @@ export const productMutationTypeDefs = {
}, },
async productGroupInteriors( async productGroupInteriors(
_, _,
{ productId, groupId, roomNames }, { productId, groupId, uris },
{ dataSources }, { dataSources },
) { ) {
await (<ProductAPI>dataSources.productApi).addInteriorsToProductGroup( await (<ProductAPI>dataSources.productApi).addInteriorsToProductGroup(
productId, productId,
groupId, groupId,
roomNames, uris,
); );
return (<ProductAPI>dataSources.productApi).getProduct(productId); return (<ProductAPI>dataSources.productApi).getProduct(productId);
}, },
+1 -1
View File
@@ -441,7 +441,7 @@ type Mutation {
markets: [ProductBlacklistingInput] markets: [ProductBlacklistingInput]
): Product ): Product
productGroup(productId: Int!, groupIds: [Int]): 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 productKeywords(productId: Int!, keywordIds: [Int]): Product
productProportionsWarning( productProportionsWarning(
productId: Int! productId: Int!