diff --git a/lib/actions/holder-actions.js b/lib/actions/holder-actions.js --- a/lib/actions/holder-actions.js +++ b/lib/actions/holder-actions.js @@ -3,7 +3,6 @@ import invariant from 'invariant'; import * as React from 'react'; -import blobService from '../facts/blob-service.js'; import { type AuthMetadata, IdentityClientContext, @@ -14,8 +13,9 @@ } from '../types/holder-types.js'; import { toBase64URL } from '../utils/base64.js'; import { - makeBlobServiceEndpointURL, generateBlobHolder, + assignMultipleHolders, + removeMultipleHolders, } from '../utils/blob-service.js'; import { useDispatchActionPromise } from '../utils/redux-promise-utils.js'; import { useSelector } from '../utils/redux-utils.js'; @@ -50,8 +50,6 @@ +failed: MultipleBlobHolders, }; -// This function can be simplified when batch holders operations -// are implemented on Blob Service async function performBlobServiceHolderActions( action: 'establish' | 'remove', inputs: MultipleBlobHolders, @@ -61,53 +59,43 @@ return { succeeded: [], failed: [] }; } - const endpoint = - action === 'establish' - ? blobService.httpEndpoints.ASSIGN_HOLDER - : blobService.httpEndpoints.DELETE_BLOB; - const endpointURL = makeBlobServiceEndpointURL(endpoint); const defaultHeaders = createDefaultHTTPRequestHeaders(authMetadata); + const blobServiceCall = + action === 'establish' ? assignMultipleHolders : removeMultipleHolders; + + const requestInputs = inputs.map(({ blobHash, ...rest }) => ({ + ...rest, + blobHash: toBase64URL(blobHash), + })); + const response = await blobServiceCall(requestInputs, defaultHeaders); + if (response.result === 'success') { + return { succeeded: inputs, failed: [] }; + } + if (response.result === 'error') { + return { succeeded: [], failed: inputs }; + } - const promises = inputs.map(async input => { - const blobHash = toBase64URL(input.blobHash); - try { - const response = await fetch(endpointURL, { - method: endpoint.method, - body: JSON.stringify({ - holder: input.holder, - blob_hash: blobHash, - }), - headers: { - ...defaultHeaders, - 'content-type': 'application/json', - }, - }); - const holderAlreadyEstablishedResponse = - action === 'establish' && response.status === 409; - if (response.ok || holderAlreadyEstablishedResponse) { - return { ...input, success: true }; - } - return { ...input, success: false }; - } catch (e) { - return { ...input, success: false }; - } - }); + const failedRequestsSet = new Set( + response.failedRequests.map(({ blobHash, holder }) => + JSON.stringify({ blobHash, holder }), + ), + ); - const results = await Promise.all(promises); const succeeded = [], failed = []; - for (const { success, ...holderEntry } of results) { - if (success) { - succeeded.push(holderEntry); + for (const item of inputs) { + const stringifiedItem = JSON.stringify({ + blobHash: toBase64URL(item.blobHash), + holder: item.holder, + }); + if (failedRequestsSet.has(stringifiedItem)) { + failed.push(item); } else { - failed.push(holderEntry); + succeeded.push(item); } } - return { - succeeded, - failed, - }; + return { succeeded, failed }; } async function processHoldersAction(