import { IActionParams, IActionId, IActionReadParams, IOFAPIResponse, IActionFilter, IEstateRecord, IEstateFilesRecord, IFileDescriptor } from "../types"; import { IActionGetParams } from "../types/actions"; import { ALL_ESTATE_FIELDS } from '../models'; export default abstract class OnOfficeAPIClient { protected abstract fetchAction( actionId: IActionId, resourceType: string, indentifier: string, resourceId: string, parameters: IActionParams ): Promise> getEstates(filter: IActionFilter = {}, data: string[] = ALL_ESTATE_FIELDS) { return this.readResource('estate', { data, filter }); } getEstate(id: number, data: string[] = ALL_ESTATE_FIELDS) { return this.readResource('estate', { data }, id).then(res => res[0]); } getEstateImages(id: number) { return this.getResource('estatepictures', { estateids: [id], categories: ['Foto'], size: 'original', language: 'GER' }).then(records => new Array().concat(...records.map(({ elements }) => elements))); } getResource(type: string, parameters: IActionGetParams) { return this.fetchAction( 'urn:onoffice-de-ns:smart:2.5:smartml:action:get', type, '', '', parameters ).then(res => OnOfficeAPIClient.reduceActionsResponse(res)).then(results => results[0].data.records); } readResource(type: string, parameters: IActionReadParams, id: string | number = '') { return this.fetchAction( 'urn:onoffice-de-ns:smart:2.5:smartml:action:read', type, '', String(id), parameters ).then(res => OnOfficeAPIClient.reduceActionsResponse(res)).then(results => results[0].data.records); } unlockProvider(parameterCacheId: string, isRegularCustomer = 0) { return this.fetchAction( "urn:onoffice-de-ns:smart:2.5:smartml:action:do", "unlockProvider", "", "", { parameterCacheId, isRegularCustomer } ) } static reduceActionsResponse(res: IOFAPIResponse) { return res.response.results; } }