pha-hermes 1.28.0 → 1.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.28.0",
2
+ "version": "1.29.0",
3
3
  "main": "dist/index.js",
4
4
  "typings": "dist/index.d.ts",
5
5
  "files": [
@@ -75,7 +75,16 @@ export class LipheAuthenticator {
75
75
  const response = await this.axiosTokenInstance.post('v2/signin', form, {
76
76
  headers: form.getHeaders()
77
77
  })
78
- const token = response?.data?.data?.token
78
+
79
+ let responseData = response.data
80
+ if (typeof responseData === 'string') {
81
+ const jsonMatch = responseData.match(/^\{.*?}(?=HTTP|$)/)
82
+ if (jsonMatch) {
83
+ responseData = JSON.parse(jsonMatch[0])
84
+ }
85
+ }
86
+
87
+ const token = responseData?.data?.token
79
88
  if (!token) {
80
89
  throw new Error('Authentication failed: no token return')
81
90
  }
@@ -2,6 +2,7 @@ import axios, { AxiosInstance } from 'axios'
2
2
  import { LipheAuthenticator } from './auth/authClient'
3
3
  import { LiphePractitionerClient } from './practitioner/practitionerClient'
4
4
  import { ContractRequestPeriodClient } from './contract_request_period/contractRequestPeriodClient'
5
+ import { TimesheetClient } from './timesheets/timesheetClient'
5
6
  import axiosRetry from 'axios-retry'
6
7
 
7
8
  export class LipheApiClient {
@@ -10,6 +11,7 @@ export class LipheApiClient {
10
11
  private authenticator: LipheAuthenticator
11
12
  public practitionerClient: LiphePractitionerClient
12
13
  public contractRequestPeriodClient: ContractRequestPeriodClient
14
+ public timesheetClient: TimesheetClient
13
15
 
14
16
  constructor(
15
17
  baseUrl: string,
@@ -39,6 +41,7 @@ export class LipheApiClient {
39
41
 
40
42
  this.practitionerClient = new LiphePractitionerClient(this.axiosInstance)
41
43
  this.contractRequestPeriodClient = new ContractRequestPeriodClient(this.axiosInstance)
44
+ this.timesheetClient = new TimesheetClient(this.axiosInstance)
42
45
  }
43
46
  async init() {
44
47
  await this.authenticator.initializeAuth()
@@ -0,0 +1,101 @@
1
+ import type { AxiosInstance } from 'axios'
2
+ import FormData from 'form-data'
3
+
4
+ export interface TimesheetParams {
5
+ workorder_id: string
6
+ start: Date
7
+ end: Date
8
+ }
9
+
10
+ export interface CallbackTimesheetParams extends TimesheetParams {
11
+ reason: string
12
+ }
13
+
14
+ export interface StandbyTimesheetParams extends TimesheetParams {
15
+ calls_order: string
16
+ }
17
+
18
+ export const TimesheetType = {
19
+ Regular: 'regular',
20
+ Callback: 'callback',
21
+ Overtime: 'overtime',
22
+ Standby: 'standby'
23
+ } as const
24
+
25
+ export interface TimesheetResponse {
26
+ success: boolean
27
+ data?: any
28
+ error?: string
29
+ }
30
+
31
+ export class TimesheetClient {
32
+ private axios: AxiosInstance
33
+
34
+ constructor(axiosInstance: AxiosInstance) {
35
+ this.axios = axiosInstance
36
+ }
37
+
38
+ async sendRegularTimesheet(params: TimesheetParams): Promise<TimesheetResponse> {
39
+ return this.sendTimesheet(TimesheetType.Regular, params)
40
+ }
41
+
42
+ async sendCallbackTimesheet(params: CallbackTimesheetParams): Promise<TimesheetResponse> {
43
+ return this.sendTimesheet(TimesheetType.Callback, params)
44
+ }
45
+
46
+ async sendOvertimeTimesheet(params: TimesheetParams): Promise<TimesheetResponse> {
47
+ return this.sendTimesheet(TimesheetType.Overtime, params)
48
+ }
49
+
50
+ async sendStandbyTimesheet(params: StandbyTimesheetParams): Promise<TimesheetResponse> {
51
+ return this.sendTimesheet(TimesheetType.Standby, params)
52
+ }
53
+
54
+ private async sendTimesheet(
55
+ type: (typeof TimesheetType)[keyof typeof TimesheetType],
56
+ params: TimesheetParams | CallbackTimesheetParams | StandbyTimesheetParams
57
+ ): Promise<TimesheetResponse> {
58
+ try {
59
+ const form = new FormData()
60
+ form.append('workorder_id', params.workorder_id)
61
+ form.append('start', this.formatDateTime(params.start))
62
+ form.append('end', this.formatDateTime(params.end))
63
+
64
+ if (type === TimesheetType.Callback && 'reason' in params) {
65
+ form.append('reason', params.reason)
66
+ }
67
+
68
+ if (type === TimesheetType.Standby && 'calls_order' in params) {
69
+ form.append('calls_order', params.calls_order)
70
+ }
71
+
72
+ const response = await this.axios.post(`v3/workorderLog/${type}`, form, {
73
+ headers: form.getHeaders()
74
+ })
75
+
76
+ return {
77
+ success: true,
78
+ data: response.data
79
+ }
80
+ } catch (error) {
81
+ return {
82
+ success: false,
83
+ error: error.message || 'Failed to send timesheet',
84
+ data: error.response?.data
85
+ }
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Converts a Date object to the required string format: "YYYY-MM-DD HH:mm"
91
+ */
92
+ private formatDateTime(date: Date): string {
93
+ const year = date.getFullYear()
94
+ const month = String(date.getMonth() + 1).padStart(2, '0')
95
+ const day = String(date.getDate()).padStart(2, '0')
96
+ const hours = String(date.getHours()).padStart(2, '0')
97
+ const minutes = String(date.getMinutes()).padStart(2, '0')
98
+
99
+ return `${year}-${month}-${day} ${hours}:${minutes}`
100
+ }
101
+ }