42: add product group interiors mutation (#44)
This commit is contained in:
@@ -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 = {
|
||||
|
||||
@@ -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<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,
|
||||
);
|
||||
}
|
||||
|
||||
// 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,
|
||||
|
||||
@@ -122,6 +122,18 @@ export const productMutationTypeDefs = {
|
||||
)).generateNewInteriors(product);
|
||||
return product;
|
||||
},
|
||||
async productGroupInteriors(
|
||||
_,
|
||||
{ productId, groupId, roomNames },
|
||||
{ dataSources },
|
||||
) {
|
||||
await (<ProductAPI>dataSources.productApi).addInteriorsToProductGroup(
|
||||
productId,
|
||||
groupId,
|
||||
roomNames,
|
||||
);
|
||||
return (<ProductAPI>dataSources.productApi).getProduct(productId);
|
||||
},
|
||||
async productKeywords(_, { productId, keywordIds }, { dataSources }) {
|
||||
await (<KeywordAPI>dataSources.keywordApi).setProductKeywords(
|
||||
productId,
|
||||
|
||||
@@ -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!
|
||||
|
||||
Reference in New Issue
Block a user