import { RestApiLink } from '../../interfaces/element.interface' import { CountingWorkRecord, CountingWorkRecordIdentity, IWorkRecord, WorkRecordConfiguration, WorkRecordData, } from '../../interfaces/workRecord.interface' import { Api } from '../api' import { ELEMENTS_API } from './elements' import { CONTENT_TYPES } from '.' export type WorkRecordDataResponse = Record< string, { links?: RestApiLink[] work_record: IWorkRecord } > export const WORK_RECORDS_API = '/service/records' export class WorkRecords { api: Api constructor(api: Api) { this.api = api } async create( type: string, hash: string, organization: string, data: WorkRecordData, ) { const requestData = { organization: { id: organization, }, workRecord: data, } return this.api.apisauce.post<{ hash: string; links: RestApiLink[] }>( `${ELEMENTS_API}/elements/${type}/${hash}/work-records`, requestData, { headers: { 'content-type': CONTENT_TYPES.WORK_RECORD, }, }, ) } async get(type: string, hash: string, recordId: string) { return this.api.apisauce.get<{ links?: RestApiLink[] work_record: IWorkRecord }>(`${ELEMENTS_API}/elements/${type}/${hash}/work-records/${recordId}`, { cache: false, }) } async archive(type: string, hash: string, recordId: string) { return this.api.apisauce.delete( `${ELEMENTS_API}/elements/${type}/${hash}/work-records/${recordId}`, { headers: { 'content-type': CONTENT_TYPES.WORK_RECORD, }, cache: false, }, ) } createCountingWorkRecord( type: string, hash: string, organization: string, data: WorkRecordData, ) { const requestData = { element: { type, hash, }, organization, ...data, } return this.api.apisauce.post( `${WORK_RECORDS_API}/record`, requestData, ) } listCountingWorkRecords( element: string, options?: { limit: number; offset: number; sort: string }, ) { const opts = options || { limit: 20, offset: 0, sort: 'user:1,timestamp:-1', } return this.api.apisauce.get( `${WORK_RECORDS_API}/record`, { limit: opts.limit, offset: opts.offset, sort: opts.sort, element: element, }, { cache: false, }, ) } listConfigurations(organization: string, type: string) { return this.api.apisauce.get( `${WORK_RECORDS_API}/configuration?organization=${organization}&type=${type}`, ) } getConfiguration(hash: string) { return this.api.apisauce.get( `${WORK_RECORDS_API}/configuration/${hash}`, ) } }