47: refactor interiors mutation to handle uri (#48)
* 47: refactor interiors mutation to handle uri * removed log
This commit is contained in:
@@ -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<Promise<void>[]> {
|
||||
// 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);
|
||||
|
||||
@@ -125,13 +125,13 @@ export const productMutationTypeDefs = {
|
||||
},
|
||||
async productGroupInteriors(
|
||||
_,
|
||||
{ productId, groupId, roomNames },
|
||||
{ productId, groupId, uris },
|
||||
{ dataSources },
|
||||
) {
|
||||
await (<ProductAPI>dataSources.productApi).addInteriorsToProductGroup(
|
||||
productId,
|
||||
groupId,
|
||||
roomNames,
|
||||
uris,
|
||||
);
|
||||
return (<ProductAPI>dataSources.productApi).getProduct(productId);
|
||||
},
|
||||
|
||||
+1
-1
@@ -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!
|
||||
|
||||
Reference in New Issue
Block a user