export async function getDocumentHistory()

in src/GetDocumentHistory.ts [73:111]


export async function getDocumentHistory(txn: TransactionExecutor, tableName: string, keyAttributeName: string, keyAttributeValue: string, fromDateISO?: string, toDateISO?: string): Promise<dom.Value[]> {
    const fcnName = "[GetDocumentHistory.getDocumentRevisionByIdAndBlock]"
    const startTime: number = new Date().getTime();
    let documentId: string;

    try {
        const documentIds = await getDocumentIdsAndVersions(txn, tableName, keyAttributeName, keyAttributeValue);
        documentId = documentIds[0].id;

        validateTableNameConstrains(tableName);
        let query = `SELECT * FROM history( ${tableName} ) AS h WHERE h.metadata.id = ? `;

        if (fromDateISO) {
            validateStringAsISODateTime(fromDateISO);
            if (toDateISO) {
                validateStringAsISODateTime(toDateISO);
                query = `SELECT * FROM history( ${tableName}, \`${fromDateISO}\`, \`${toDateISO}\`) AS h WHERE h.metadata.id = ? `;
            } else {
                query = `SELECT * FROM history( ${tableName}, \`${fromDateISO}\`) AS h WHERE h.metadata.id = ? `;
            }
        }

        logger.debug(`${fcnName} Retrieving document history for Id: ${documentId}`);
        logger.debug(`${fcnName} Query statement: ${query}`);

        const result: Result = await txn.execute(query, documentId);
        const endTime = new Date().getTime();
        logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`)
        const resultList: dom.Value[] = result.getResultList();
        if (resultList.length === 0) {
            throw `${fcnName} Unable to find document history with Id: ${documentId}`;
        }
        return resultList;
    } catch (err) {
        const endTime: number = new Date().getTime();
        logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`)
        throw new Error(`${fcnName} ${err}`);
    }
}