private async unlockItems()

in src/dataServices/dynamoDbBundleService.ts [467:521]


    private async unlockItems(
        lockedItems: ItemRequest[],
        rollBack: boolean,
        tenantId?: string,
    ): Promise<{ successfulUnlock: boolean; locksFailedToRelease: ItemRequest[] }> {
        if (lockedItems.length === 0) {
            return { successfulUnlock: true, locksFailedToRelease: [] };
        }
        logger.info('Unlocking begins');

        const updateRequests: any[] = lockedItems.map((lockedItem) => {
            let newStatus = DOCUMENT_STATUS.AVAILABLE;
            // If the lockedItem was a result of a delete operation or if the lockedItem was the original version of an item that was UPDATED then
            // set the lockedItem's status to be "DELETED"
            if (
                (lockedItem.operation === 'delete' ||
                    (lockedItem.operation === 'update' && lockedItem.isOriginalUpdateItem)) &&
                !rollBack
            ) {
                newStatus = DOCUMENT_STATUS.DELETED;
            }
            return DynamoDbParamBuilder.buildUpdateDocumentStatusParam(
                null,
                newStatus,
                lockedItem.id,
                lockedItem.vid || 0,
                lockedItem.resourceType,
                tenantId,
            );
        });

        const updateRequestChunks = chunkArray(updateRequests, this.MAX_TRANSACTION_SIZE);
        const lockedItemChunks = chunkArray(lockedItems, this.MAX_TRANSACTION_SIZE);
        const params = updateRequestChunks.map((requestChunk: any) => {
            return {
                TransactItems: requestChunk,
            };
        });

        for (let i = 0; i < params.length; i += 1) {
            try {
                // eslint-disable-next-line no-await-in-loop
                await this.dynamoDb.transactWriteItems(params[i]).promise();
            } catch (e) {
                logger.error('Failed to unlock items', e);
                let locksFailedToRelease: ItemRequest[] = [];
                for (let j = i; j < lockedItemChunks.length; j += 1) {
                    locksFailedToRelease = locksFailedToRelease.concat(lockedItemChunks[j]);
                }
                return Promise.resolve({ successfulUnlock: false, locksFailedToRelease });
            }
        }
        logger.info('Finished unlocking');
        return Promise.resolve({ successfulUnlock: true, locksFailedToRelease: [] });
    }