Files
3cx_tapi/client/src/call-history.ts
T
2026-03-27 11:00:10 +01:00

91 lines
3.2 KiB
TypeScript

import * as chrono from 'chrono-node'
import { TapiContact } from './tapi-contact'
import { extractNumber } from './utils'
import GM_fetch from '@trim21/gm-fetch'
import { Config } from './config'
const telephoneIcon = require('./telephone.svg');
export class CallHistory {
private callerIds: { [number: string]: TapiContact } = {}
private updateCallHistoryEntry (call: HTMLElement, callerId: TapiContact) {
var span = call.querySelector(':scope > span')
this.showTimeManager(call, call.querySelector('.date').textContent, callerId)
if (callerId && callerId.tD_NAME !== '') {
var text = span.textContent
span.textContent = callerId.tD_NAME + ' ' + callerId.tD_NUMBER
}
}
private showTimeManager (call: HTMLElement, date: string, callerId: TapiContact) {
var dateParts = date.match(/^(?<date>.*), (?<duration>[0-9]{2}:[0-9]{2}:[0-9]{2})$/)
var duration = '00:00:00'
if (dateParts) {
date = dateParts.groups.date
duration = dateParts.groups.duration
}
var parsedDate = chrono.parseDate(date)
var parsedDateDe = chrono.de.parseDate(date)
if (parsedDateDe) {
parsedDate = parsedDateDe
}
if (!parsedDate) {
return
}
// Date parsing is awful, just assume the first number is the day of month
var day = date.match(/[0-9]+/)[0]
var parsedDuration = chrono.parseDate(duration)
console.log('TAPI call history time:', date, 'parsedDate:', parsedDate, 'duration:', duration, 'parsedDuration:', parsedDuration)
var connect = parsedDate.getFullYear().toString() +
(parsedDate.getMonth() + 1).toString().padStart(2, '0') + // (January gives 0)
day.toString().padStart(2, '0') +
parsedDate.getHours().toString().padStart(2, '0') +
parsedDate.getMinutes().toString().padStart(2, '0')
var length = (parsedDuration.getHours() * 60 + parsedDuration.getMinutes()).toString()
var toolbar = call.querySelector('call-history-options')
var href = 'domizil://PM/Zeitbuchung?'
if (callerId && callerId.tD_ID) {
href += 'KontaktId=' + callerId.tD_ID + '&'
}
href += 'connect=' + connect + '&length=' + length
var a = document.createElement('a')
a.title = 'PM Zeitbuchung'
a.dataset.domizilLink = href
a.onclick = () => {
window.open(href)
}
a.innerHTML = telephoneIcon;
a.classList.add('btn');
a.classList.add('btn-plain');
toolbar.insertBefore(a, toolbar.firstChild)
}
public async showCallHistory (element: HTMLElement) {
var span = element.querySelector(':scope > span')
var number = extractNumber(span.textContent)
if (!number) {
this.updateCallHistoryEntry(element, undefined)
return
}
if (this.callerIds[number] !== undefined) {
this.updateCallHistoryEntry(element, this.callerIds[number])
} else {
var response = await GM_fetch(Config.tapi_server_url + '/callerid/' + encodeURIComponent(number))
var callerId: TapiContact = { tD_NAME: '' }
if (response.status === 200) {
callerId = await response.json() as TapiContact
}
console.log('TAPI call histroy callerid response', number, response, callerId)
this.callerIds[number] = callerId
this.updateCallHistoryEntry(element, callerId)
}
}
}