@isoftdata/universal-object-htp-utility
Advanced tools
+2
-2
@@ -209,3 +209,3 @@ import _ from "lodash" | ||
| await db.commitAndRelease(connectionWithTransaction) | ||
| responses.push(`affected rows: inventory ${multiRowUpsertResponse?.affectedRows}, ${multiRowInventoryOptionListUpsertResponse}, ${multiRowInventoryOptionUpsertResponse}, ${multiRowInventoryFileUpsertResponse}, ${multiRowInventorySourceUpsertResponse ? multiRowInventorySourceUpsertResponse : 'inventorysource affected rows: none' }`) | ||
| responses.push(`affected rows: inventory ${multiRowUpsertResponse?.affectedRows}, ${multiRowInventoryOptionListUpsertResponse}, ${multiRowInventoryOptionUpsertResponse}, ${[...multiRowInventoryFileUpsertResponse]}, ${multiRowInventorySourceUpsertResponse ? multiRowInventorySourceUpsertResponse : 'inventorysource affected rows: none' }`) | ||
| // Rollback transaction if there's an error | ||
@@ -310,3 +310,3 @@ } catch (err) { | ||
| await db.commitAndRelease(connectionWithTransaction) | ||
| responses.push(`affected rows: inventory ${initialResponse.inventoryInsertResponse?.affectedRows}, ${initialResponse.inventoryOptionListResponse}, ${inventoryOptionResponse}, ${initialResponse.inventoryFileResponse}, ${initialResponse.inventorySourceUpsertResponse ? initialResponse.inventorySourceUpsertResponse : 'inventorysource affected rows: none' }`) | ||
| responses.push(`affected rows: inventory ${initialResponse.inventoryInsertResponse?.affectedRows}, ${initialResponse.inventoryOptionListResponse}, ${inventoryOptionResponse}, ${initialResponse.inventoryFileResponse ? [...initialResponse.inventoryFileResponse] : ''}, ${initialResponse.inventorySourceUpsertResponse ? initialResponse.inventorySourceUpsertResponse : 'inventorysource affected rows: none' }`) | ||
| // responses.push(`affected rows: inventory ${inventoryInsertResponse.affectedRows}, ${inventoryOptionListResponse.affectedRows}, ${inventoryOptionResponse?.affectedRows}, ${inventoryFileResponse?.affectedRows}`) | ||
@@ -313,0 +313,0 @@ |
+84
-16
@@ -11,12 +11,9 @@ import { query } from '@isoftdata/utility-db' | ||
| const inventoryId = productInventoryRow.inventoryId | ||
| const storeId = productInventoryRow.storeId | ||
| const attachmentRows = productInventoryRow.attachments.map(function (attachment) { | ||
| if (attachment.id) { | ||
| return { | ||
| return { ...attachment, | ||
| inventoryid: inventoryId, | ||
| id: attachment.id, | ||
| public: attachment.public, | ||
| rank: attachment.rank, | ||
| file: attachment.file, | ||
| type: attachment.type, | ||
| productcode: productCode | ||
| productcode: productCode, | ||
| storeid: storeId | ||
| } | ||
@@ -48,4 +45,14 @@ | ||
| } | ||
| function convertManyPartVideoListRows(row) { | ||
| return { | ||
| productcode: row.productcode, | ||
| store: row.storeid, | ||
| partnum: row.inventoryid, | ||
| videotype: row.type, | ||
| videoid: row.video.id, | ||
| url: row.video.url, | ||
| dirty: "False" | ||
| } | ||
| } | ||
| function filterExistingFileListRows(newRows, existingRows) { | ||
@@ -119,2 +126,16 @@ const uniqueRows = newRows.filter(function(newRow) { | ||
| } | ||
| async function deletePartVideoListRows(htpConnection, deleteRows){ | ||
| //const deleteRows = nonPublicRows.map(row => ([productCode, 'inventoryid', row.inventoryid, row.id])) | ||
| try { | ||
| const queryOptions = { | ||
| sql: `DELETE FROM partvideolist WHERE (productcode, store, partnum, videoid) in (?)`, | ||
| values: [deleteRows] | ||
| } | ||
| const deleteResponse = await query(htpConnection, queryOptions) | ||
| return `non-public partvideolist delete affected rows: ${deleteResponse?.affectedRows}` | ||
| } catch (err) { | ||
| return `error deleting partvideolist rows: ${err}` | ||
| } | ||
| } | ||
| // Called in deleteInventoryFileData when an inventoryfile row is deleted | ||
@@ -157,2 +178,12 @@ async function deletePartImageListRow(connection, productCode, partNum, fileId) { | ||
| } | ||
| async function partVideoListMultiRowUpsertQuery(htpConnection, objectArray) { | ||
| // Get columns | ||
| const columns = Object.keys(objectArray[0]) | ||
| const values = objectArray.map(obj => Object.values(obj)) | ||
| const queryOptions = { | ||
| sql: 'INSERT INTO partvideolist (??) VALUES ? ON DUPLICATE KEY UPDATE `url` = values(`url`)', | ||
| values: [columns, values] | ||
| } | ||
| return await query(htpConnection, queryOptions) | ||
| } | ||
| async function partFileListMultiRowUpsertQuery(htpConnection, objectArray) { | ||
@@ -199,2 +230,21 @@ // Get columns | ||
| } | ||
| async function upsertPartVideoList(htpConnection, inventoryVideoRows, productCode) { | ||
| const responses = [] | ||
| // Filter non public rows and delete | ||
| const nonPublicRows = inventoryVideoRows.filter(row => row.public === 'False') | ||
| if (nonPublicRows.length) { | ||
| const deleteRows = nonPublicRows.map(row => ([productCode, row.store, row.inventoryid, row.video.id])) | ||
| const deleteResponse = await deletePartVideoListRows(htpConnection, deleteRows) | ||
| responses.push(deleteResponse) | ||
| } | ||
| const publicRows = inventoryVideoRows.filter(row => row.public === 'True') | ||
| // Convert | ||
| const convertedRows = publicRows.map(row => convertManyPartVideoListRows(row)) | ||
| // Upsert | ||
| const upsertResponse = await partVideoListMultiRowUpsertQuery(htpConnection, convertedRows) | ||
| responses.push(`partvideolist affected rows: ${upsertResponse?.affectedRows}`) | ||
| return responses | ||
| } | ||
| async function upsertPartFileList(htpConnection, inventoryFileRows, productCode) { | ||
@@ -241,8 +291,9 @@ const responses = [] | ||
| // Filter just the inventory rows with attachments | ||
| const attachmentRows = productInventoryRows.filter(row => row.attachments?.length > 0) | ||
| // Add inventoryid to all of the attachment rows for later | ||
| const modifiedAttachmentRows = attachmentRows.map(row => fluffAttachmentRows(row, productCode)).flat() | ||
| const inventoryRowsWithAttachments = productInventoryRows.filter(row => row.attachments?.length > 0) | ||
| // Add inventoryid and storeid to all of the attachment rows for later | ||
| const modifiedAttachmentRows = inventoryRowsWithAttachments.map(row => fluffAttachmentRows(row, productCode)).flat() | ||
| // Separate image and file rows since they're handled differently | ||
| const imageAttachmentRows = modifiedAttachmentRows.filter(row => row.type === 'Image') | ||
| const fileAttachmentRows = modifiedAttachmentRows.filter(row => row.type === 'PDF' || row.type === 'Text' || row.type === 'Unknown') | ||
| const fileAttachmentRows = modifiedAttachmentRows.filter(row => row.type === 'PDF' || row.type === 'Text') | ||
| const videoAttachmentRows = modifiedAttachmentRows.filter(row => row.type === 'YOUTUBE') | ||
| // Call the individual upsert functions in parallel using pProps | ||
@@ -256,3 +307,4 @@ // Prepare the filtered rows and create promises object | ||
| const filteredYoutubeAttachmentRows = videoAttachmentRows.length ? | ||
| _.uniqWith(videoAttachmentRows, _.isEqual) : []; | ||
| // Create an object with promises to run in parallel | ||
@@ -269,2 +321,5 @@ const promisesObj = {}; | ||
| if (filteredYoutubeAttachmentRows.length) { | ||
| promisesObj.videoResponse = upsertPartVideoList(htpConnection, filteredYoutubeAttachmentRows, productCode) | ||
| } | ||
| // Run promises in parallel if any exist | ||
@@ -282,2 +337,6 @@ if (Object.keys(promisesObj).length > 0) { | ||
| } | ||
| if (results.videoResponse) { | ||
| responses.push(results.videoResponse) | ||
| } | ||
| } | ||
@@ -383,3 +442,5 @@ return responses | ||
| const imageAttachmentRows = modifiedAttachmentRows.filter(row => row.type === 'Image') | ||
| const fileAttachmentRows = modifiedAttachmentRows.filter(row => row.type === 'PDF' || row.type === 'Text' || row.type === 'Unknown') | ||
| const fileAttachmentRows = modifiedAttachmentRows.filter(row => row.type === 'PDF' || row.type === 'Text') | ||
| const videoAttachmentRows = modifiedAttachmentRows.filter(row => row.type === 'YOUTUBE') | ||
| // Call the individual insert functions in parallel using pProps | ||
@@ -391,3 +452,4 @@ // Prepare the filtered rows and create promises object | ||
| _.uniqWith(fileAttachmentRows, _.isEqual) : []; | ||
| const filteredYoutubeAttachmentRows = videoAttachmentRows.length ? | ||
| _.uniqWith(videoAttachmentRows, _.isEqual) : []; | ||
| // Create an object with promises to run in parallel | ||
@@ -403,3 +465,5 @@ const promisesObj = {}; | ||
| } | ||
| if (filteredYoutubeAttachmentRows.length) { | ||
| promisesObj.videoResponse = upsertPartVideoList(htpConnection, filteredYoutubeAttachmentRows, productCode) | ||
| } | ||
| // Run promises in parallel if any exist | ||
@@ -417,4 +481,8 @@ if (Object.keys(promisesObj).length > 0) { | ||
| } | ||
| if (results.videoResponse) { | ||
| responses.push(results.videoResponse) | ||
| } | ||
| } | ||
| return responses | ||
| } | ||
+1
-1
| { | ||
| "name": "@isoftdata/universal-object-htp-utility", | ||
| "version": "3.15.1", | ||
| "version": "3.16.0", | ||
| "description": "Functions to convert universal objects to htp schema", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
137040
2.38%3076
2.19%