Files
3cx_tapi/client/src/call-history.ts
T

91 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-11-04 22:59:32 +01:00
import * as chrono from 'chrono-node'
import { TapiContact } from './tapi-contact'
2024-10-14 10:59:19 +02:00
import { extractNumber } from './utils'
2024-10-15 16:45:21 +02:00
import GM_fetch from '@trim21/gm-fetch'
2026-03-19 13:38:06 +01:00
import { Config } from './config'
2026-03-27 11:00:10 +01:00
const telephoneIcon = require('./telephone.svg');
2020-11-04 22:59:32 +01:00
export class CallHistory {
private callerIds: { [number: string]: TapiContact } = {}
private updateCallHistoryEntry (call: HTMLElement, callerId: TapiContact) {
2021-11-30 14:11:34 +01:00
var span = call.querySelector(':scope > span')
this.showTimeManager(call, call.querySelector('.date').textContent, callerId)
2020-11-04 22:59:32 +01:00
if (callerId && callerId.tD_NAME !== '') {
2020-11-04 22:59:32 +01:00
var text = span.textContent
2021-11-30 14:11:34 +01:00
span.textContent = callerId.tD_NAME + ' ' + callerId.tD_NUMBER
2020-11-04 22:59:32 +01:00
}
}
2020-11-04 23:34:03 +01:00
private showTimeManager (call: HTMLElement, date: string, callerId: TapiContact) {
var dateParts = date.match(/^(?<date>.*), (?<duration>[0-9]{2}:[0-9]{2}:[0-9]{2})$/)
2020-11-04 22:59:32 +01:00
var duration = '00:00:00'
2020-11-04 23:34:03 +01:00
if (dateParts) {
date = dateParts.groups.date
duration = dateParts.groups.duration
2020-11-04 22:59:32 +01:00
}
2023-06-27 16:42:05 +02:00
var parsedDate = chrono.parseDate(date)
var parsedDateDe = chrono.de.parseDate(date)
if (parsedDateDe) {
parsedDate = parsedDateDe
2020-11-04 22:59:32 +01:00
}
if (!parsedDate) {
return
}
2020-11-04 23:34:03 +01:00
// Date parsing is awful, just assume the first number is the day of month
var day = date.match(/[0-9]+/)[0]
2020-11-04 22:59:32 +01:00
var parsedDuration = chrono.parseDate(duration)
2020-11-04 23:34:03 +01:00
console.log('TAPI call history time:', date, 'parsedDate:', parsedDate, 'duration:', duration, 'parsedDuration:', parsedDuration)
2020-11-04 22:59:32 +01:00
var connect = parsedDate.getFullYear().toString() +
(parsedDate.getMonth() + 1).toString().padStart(2, '0') + // (January gives 0)
2020-11-04 23:34:03 +01:00
day.toString().padStart(2, '0') +
2020-11-04 22:59:32 +01:00
parsedDate.getHours().toString().padStart(2, '0') +
parsedDate.getMinutes().toString().padStart(2, '0')
var length = (parsedDuration.getHours() * 60 + parsedDuration.getMinutes()).toString()
2021-11-30 14:11:34 +01:00
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
2020-11-05 08:58:20 +01:00
var a = document.createElement('a')
a.title = 'PM Zeitbuchung'
2020-11-04 22:59:32 +01:00
a.dataset.domizilLink = href
a.onclick = () => {
window.open(href)
}
2026-03-27 11:00:10 +01:00
a.innerHTML = telephoneIcon;
2021-11-30 14:11:34 +01:00
a.classList.add('btn');
a.classList.add('btn-plain');
2020-11-04 22:59:32 +01:00
toolbar.insertBefore(a, toolbar.firstChild)
}
public async showCallHistory (element: HTMLElement) {
2021-11-30 14:11:34 +01:00
var span = element.querySelector(':scope > span')
2020-11-04 22:59:32 +01:00
var number = extractNumber(span.textContent)
if (!number) {
this.updateCallHistoryEntry(element, undefined)
2020-11-04 22:59:32 +01:00
return
}
if (this.callerIds[number] !== undefined) {
this.updateCallHistoryEntry(element, this.callerIds[number])
} else {
2026-03-19 13:38:06 +01:00
var response = await GM_fetch(Config.tapi_server_url + '/callerid/' + encodeURIComponent(number))
2020-11-04 22:59:32 +01:00
var callerId: TapiContact = { tD_NAME: '' }
if (response.status === 200) {
2024-10-14 10:59:19 +02:00
callerId = await response.json() as TapiContact
2020-11-04 22:59:32 +01:00
}
console.log('TAPI call histroy callerid response', number, response, callerId)
this.callerIds[number] = callerId
this.updateCallHistoryEntry(element, callerId)
}
}
}