private async updatedReferences()

in src/dataServices/dynamoDbBundleService.ts [359:457]


    private async updatedReferences(
        requests: BatchReadWriteRequest[],
        lockedItems: ItemRequest[],
        tenantId?: string,
    ): Promise<boolean> {
        const idToVersionId: Record<string, string> = {};
        lockedItems.forEach((itemRequest: ItemRequest) => {
            if (itemRequest.operation === 'update' && itemRequest.vid) {
                idToVersionId[`${itemRequest.resourceType}_${itemRequest.id}`] = `${itemRequest.vid + 1}`;
            }
        });

        const createOrUpdates = requests.filter((request: BatchReadWriteRequest) => {
            return request.operation === 'create' || request.operation === 'update';
        });

        requests.forEach((request: BatchReadWriteRequest) => {
            const key = `${request.resourceType}_${request.id}`;
            if (request.operation === 'create') {
                idToVersionId[key] = '1';
            }
            // Setting version id to '1' of resources in the bundle that have not been locked. because
            // if updateCreateSupported==true creates may come disguised as updates. During locking they obviously weren't found
            // now we don't want to search for them again and then fail because we can't find them.
            if (request.operation === 'update' && this.updateCreateSupported && !(key in idToVersionId)) {
                idToVersionId[key] = '1';
            }
        });

        const requestsWithReferencesThatMustBeLookedUp = createOrUpdates
            .filter((request: BatchReadWriteRequest) => {
                const versionedLinksArray = this.versionedLinks?.[request.resourceType];
                return !!versionedLinksArray;
            })
            .flatMap((request: BatchReadWriteRequest) => {
                const { resource } = request;
                return Object.entries(flatten(resource)).map((entry) => {
                    return {
                        resource: request.resource,
                        resourceType: request.resourceType,
                        path: entry[0],
                        value: entry[1],
                    };
                });
            })
            .filter(
                (item) => item.path.endsWith('.reference') && this.versionedLinks?.[item.resourceType].has(item.path),
            );

        const requestsForDDB: {
            resource: any;
            path: string;
            value: string;
            resourceType: string;
            id: string;
        }[] = [];
        requestsWithReferencesThatMustBeLookedUp.forEach((item: any) => {
            const fullUrlMatch = item.value.match(captureFullUrlParts);
            if (!fullUrlMatch) {
                return;
            }
            const resourceType = fullUrlMatch[2];
            const id = fullUrlMatch[3];
            let vid = fullUrlMatch[4];
            if (!vid) {
                const compoundId = `${resourceType}_${id}`;
                if (compoundId in idToVersionId) {
                    vid = idToVersionId[compoundId];
                    set(item.resource, item.path, `${item.value}/_history/${vid}`);
                } else {
                    requestsForDDB.push({
                        ...item,
                        resourceType,
                        id,
                    });
                }
            }
        });
        const responsesFromDDB: boolean[] = await Promise.all(
            requestsForDDB.map(async (item) => {
                try {
                    const itemResponse: GenericResponse = await this.dynamoDbHelper.getMostRecentResource(
                        item.resourceType,
                        item.id,
                        'meta',
                        tenantId,
                    );
                    const { meta } = itemResponse.resource;
                    set(item.resource, item.path, `${item.value}/_history/${meta.versionId}`);
                    return true;
                } catch (e) {
                    const msg = `Failed to find most recent version of ${item.resourceType} resource with id=${item.id}`;
                    logger.error(msg, e);
                    return false;
                }
            }),
        );
        return responsesFromDDB.every((item) => item);
    }