import { Request , Response } from 'express' ; import { inject } from 'inversify' ; import { BaseHttpController , controller , httpPost , results } from 'inversify-express-utils' ; import { Logger } from 'winston' ; import TYPES from '../Bootstrap/Types' ; import { ApiVersion } from '../Domain/Api/ApiVersion' ; import { SyncResponseFactoryResolverInterface } from '../Domain/Item/SyncResponse/SyncResponseFactoryResolverInterface' ; import { PostToRealtimeExtensions } from '../Domain/UseCase/PostToRealtimeExtensions/PostToRealtimeExtensions' ; import { SyncItems } from '../Domain/UseCase/SyncItems' ; @ controller ( '/items' ) export class ItemsController extends BaseHttpController { constructor ( @ inject ( TYPES . SyncItems ) private syncItems , @ inject ( TYPES . SyncResponseFactoryResolver ) private syncResponseFactoryResolver , @ inject ( TYPES . PostToRealtimeExtensions ) private postToRealtimeExtensions , @ inject ( TYPES . Logger ) private logger ) { super ( ) ; } @ httpPost ( '/sync' , TYPES . AuthMiddleware ) public async sync ( request , response ) { let itemHashes = [ ] ; if ( 'items' in request . body ) { itemHashes = request . body . items ; } const syncResult = await this . syncItems . execute ( { userUuid : response . locals . user . uuid , itemHashes , computeIntegrityHash : request . body . compute_integrity === true , syncToken : request . body . sync_token , cursorToken : request . body . cursor_token , limit : request . body . limit , userAgent : request . headers [ 'user-agent' ] , contentType : request . body . content_type , apiVersion : request . body . api ?? ApiVersion . v20161215 , } ) ; try { await this . postToRealtimeExtensions . execute ( { userUuid : response . locals . user . uuid , itemHashes , } ) ; } catch ( error ) { this . logger . error ( `Failed posting items to extensions after sync: ${ error . message }` ) ; } const syncResponse = await this . syncResponseFactoryResolver . resolveSyncResponseFactoryVersion ( request . body . api ) . createResponse ( syncResult ) ; return this . json ( syncResponse ) ; } }