import {Label as APILabel, LabelsApi} from '../api' import {ILabel, ServiceOptions} from '../types' import {ILabelProperties} from '../types' import saga from '../utils/sagas' const DEFAULT_LABEL_COLOR = '#326BBA' export const addLabelDefaults = (l: APILabel): ILabel => ({ ...l, properties: { ...l.properties, // add defualt color hex if missing color: (l.properties || {}).color || DEFAULT_LABEL_COLOR, description: (l.properties || {}).description || '', }, }) export default class { private service: LabelsApi private serviceOptions: ServiceOptions constructor(basePath: string, baseOptions: ServiceOptions) { this.service = new LabelsApi({basePath, baseOptions}) this.serviceOptions = baseOptions } public async get(id: string): Promise { const { data: {label}, } = await this.service.getLabelsID(id, undefined, this.serviceOptions) if (!label) { throw new Error('Failed to get label') } return addLabelDefaults(label) } public async getAll(orgID: string): Promise { const { data: {labels}, } = await this.service.getLabels(undefined, orgID, this.serviceOptions) return (labels || []).map(addLabelDefaults) } public async create(request: { orgID: string name: string properties: ILabelProperties }): Promise { const { data: {label}, } = await this.service.postLabels(request, this.serviceOptions) if (!label) { throw new Error('Failed to create label') } return addLabelDefaults(label) } public async createAll( labels: { orgID: string name: string properties: ILabelProperties }[] ): Promise { const pendingLabels = labels.map(r => { return { action: async () => { return await this.create(r) }, rollback: async (r?: ILabel) => { if (r && r.id) { this.delete(r.id) } }, } }) return await saga(pendingLabels) } public async update(id: string, updates: Partial): Promise { const original = await this.get(id) const { data: {label}, } = await this.service.patchLabelsID( id, { ...original, ...updates, }, undefined, this.serviceOptions ) if (!label) { throw new Error('Failed to update label') } return addLabelDefaults(label) } public async delete(id: string): Promise { const {data} = await this.service.deleteLabelsID( id, undefined, this.serviceOptions ) return data } }