import { MongoManager } from "@fluidframework/server-services-core" ; import { ISequencedDocumentMessage } from "@fluidframework/protocol-definitions" ; import { Router } from "express" ; import { Provider } from "nconf" ; import { getParam , queryParamToNumber } from "../../utils" ; async function getDeltas ( mongoManager , collectionName , tenantId , documentId , from ? , to ? ) { const query = { documentId , tenantId } ; if ( from !== undefined || to !== undefined ) { query [ "operation.sequenceNumber" ] = { } ; if ( from !== undefined ) { query [ "operation.sequenceNumber" ] . $gt = from ; } if ( to !== undefined ) { query [ "operation.sequenceNumber" ] . $lt = to ; } } const db = await mongoManager . getDatabase ( ) ; const collection = db . collection < any > ( collectionName ) ; const dbDeltas = await collection . find ( query , { "operation.sequenceNumber" : 1 } ) ; return dbDeltas . map ( ( delta ) => delta . operation ) ; } export function create ( config , mongoManager ) { const deltasCollectionName = config . get ( "mongo:collectionNames:deltas" ) ; const router = Router ( ) ; /**
     * Retrieves deltas for the given document. With an optional from and to range (both exclusive) specified
     */ router . get ( "/:tenantId/:id" , ( request , response , next ) => { const from = queryParamToNumber ( request . query . from ) ; const to = queryParamToNumber ( request . query . to ) ; const tenantId = getParam ( request . params , "tenantId" ) ; const deltasP = getDeltas ( mongoManager , deltasCollectionName , tenantId , getParam ( request . params , "id" ) , from , to ) ; deltasP . then ( ( deltas ) => { response . status ( 200 ) . json ( deltas ) ; } , ( error ) => { response . status ( 500 ) . json ( error ) ; } ) ; } ) ; return router ; }