import {Organization, Telegraf, TelegrafRequest, TelegrafsApi} from '../api' import {ILabel, ITelegraf, ServiceOptions} from '../types' import {addLabelDefaults} from './labels' import saga from '../utils/sagas' const addDefaults = (telegraf: Telegraf): ITelegraf => { return { ...telegraf, labels: (telegraf.labels || []).map(addLabelDefaults), } } const addDefaultsToAll = (telegrafs: Telegraf[]): ITelegraf[] => telegrafs.map(telegraf => addDefaults(telegraf)) export default class { private service: TelegrafsApi private serviceOptions: ServiceOptions constructor(basePath: string, baseOptions: ServiceOptions) { this.service = new TelegrafsApi({basePath, baseOptions}) this.serviceOptions = baseOptions } public async getAll(orgID: string = ''): Promise { const { data: {configurations}, } = await this.service.getTelegrafs(undefined, orgID, this.serviceOptions) return addDefaultsToAll(configurations || []) } public async getAllByOrg(org: Organization): Promise { if (!org.id) { throw new Error('organization must have an id') } const { data: {configurations}, } = await this.service.getTelegrafs(undefined, org.id, this.serviceOptions) return addDefaultsToAll(configurations || []) } public async getTOML(id: string): Promise { const {data} = await this.service.getTelegrafsID( id, undefined, 'application/toml' ) return data as string } public async get(id: string): Promise { const {data} = await this.service.getTelegrafsID( id, undefined, 'application/json' ) return addDefaults(data as Telegraf) } public async create(props: Telegraf): Promise { const {data} = await this.service.postTelegrafs( props, undefined, this.serviceOptions ) return addDefaults(data) } public async update( id: string, props: Partial ): Promise { const original = await this.get(id) const update = {...original, ...props} as TelegrafRequest const {data: updated} = await this.service.putTelegrafsID( id, update, undefined, this.serviceOptions ) return addDefaults(updated) } public async delete(id: string): Promise { const {data} = await this.service.deleteTelegrafsID( id, undefined, this.serviceOptions ) return data as Response } public async addLabel(id: string, label: ILabel): Promise { if (!label.id) { throw new Error('label must have id') } const {data} = await this.service.postTelegrafsIDLabels( id, { labelID: label.id, }, undefined, this.serviceOptions ) if (!data.label) { throw new Error('Failed to add label') } return addLabelDefaults(data.label) } public async removeLabel(id: string, label: ILabel): Promise { if (!label.id) { throw new Error('label must have id') } const {data} = await this.service.deleteTelegrafsIDLabelsID( id, label.id, undefined, this.serviceOptions ) return data } public async addLabels(id: string, labels: ILabel[]): Promise { const pendingLabels = labels.map(l => { return { action: async () => { return await this.addLabel(id, l) }, rollback: async (r?: ILabel) => { if (r && r.id) { this.removeLabel(id, r) } }, } }) return await saga(pendingLabels) } public async removeLabels(id: string, labels: ILabel[]): Promise { const promises = labels.map(l => this.removeLabel(id, l)) return Promise.all(promises) } }