pha-hermes 1.11.1 → 1.12.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.
@@ -2,6 +2,29 @@ import { AxiosInstance } from 'axios'
2
2
  import { TimesheetDayEntry } from '../../models'
3
3
  import { SF_API_VERSION } from '../apiClient'
4
4
  import FormData from 'form-data'
5
+
6
+ interface SFTimesheetHourField {
7
+ Id: string
8
+ Date__c: Date
9
+ Callback__c: number
10
+ HoursEBAdjusted__c: number
11
+ Misc__c: number
12
+ OT1__c: number
13
+ OT2__c: number
14
+ OnCall__c: number
15
+ Misc1__c: number
16
+ Misc_1_Hours__c: number
17
+ Misc_2_Hours__c: number
18
+ Misc_3_Hours__c: number
19
+ Misc_4_Hours__c: number
20
+ Stat1__c: number
21
+ Stat2__c: number
22
+ In_Charge__c: number
23
+ Shift_Travel_Stipend__c: number
24
+ LastModifiedDate: Date
25
+ }
26
+ type SFTimesheetHourFieldKeys = (keyof SFTimesheetHourField)[]
27
+
5
28
  export class SFTimesheetClient {
6
29
  private axiosInstance: AxiosInstance
7
30
 
@@ -38,13 +61,23 @@ export class SFTimesheetClient {
38
61
  }
39
62
  }
40
63
 
41
- async getTimesheetId(workorderId: string): Promise<string> {
64
+ async getTimesheetIds(workorderId: string, periodStartDate?: Date, periodEndDate?: Date): Promise<string[]> {
42
65
  // Find all timesheet Ids that belong to this WO
43
66
  const url = `/services/data/${SF_API_VERSION}/query`
44
- const query = `SELECT Id
67
+ let query = `SELECT Id
45
68
  FROM Timesheet__c
46
- WHERE WorkOrder__c = '${workorderId}'
47
- `
69
+ WHERE WorkOrder__c = '${workorderId}'`
70
+ try {
71
+ if (periodStartDate && periodEndDate) {
72
+ query += `
73
+ AND PayPeriodStartDate__c <= ${new Date(periodStartDate).toISOString().substring(0, 10)}
74
+ AND PayPeriodEndDate__c >= ${new Date(periodEndDate).toISOString().substring(0, 10)}
75
+ `
76
+ }
77
+ }
78
+ catch (error) {
79
+ console.error('Invalid period dates', error)
80
+ }
48
81
  const {
49
82
  data: { records }
50
83
  } = await this.axiosInstance.get(url, {
@@ -77,63 +110,34 @@ export class SFTimesheetClient {
77
110
  return timesheets
78
111
  }
79
112
 
80
- async getTimesheetHoursIds(workorderId: string): Promise<string[]> {
81
- const timesheetIds = await this.getTimesheetId(workorderId)
82
- const timesheetId = timesheetIds[0]
83
- // First we find the correct timesheet hours id
84
- const url = `/services/data/${SF_API_VERSION}/query`
85
- const query = `SELECT Id
86
- FROM TimesheetHour__c
87
- WHERE Timesheet__c = '${timesheetId}'`
88
- const {
89
- data: { records }
90
- } = await this.axiosInstance.get(url, {
91
- params: { q: query }
92
- })
93
- return records.map(({ Id }) => Id)
94
- }
113
+ async getTimesheetHours(workorderId: string, fields?: SFTimesheetHourFieldKeys[], periodStartDate?: Date, periodEndDate?: Date): Promise<TimesheetDayEntry[]> {
114
+ const timesheetIds = await this.getTimesheetIds(workorderId, periodStartDate, periodEndDate)
95
115
 
96
- async getTimesheetHoursId(workorderId: string, timesheetDate: Date): Promise<string[]> {
97
- const timesheetIds = await this.getTimesheetId(workorderId)
98
- const formattedDate = timesheetDate.toISOString().split('T')[0]
99
- const allHourIds: string[] = []
116
+ const allHours: SFTimesheetHourField[] = []
100
117
  const url = `/services/data/${SF_API_VERSION}/query`
101
-
102
118
  for (const timesheetId of timesheetIds) {
103
- const query = `
104
- SELECT Id
105
- FROM TimesheetHour__c
106
- WHERE Timesheet__c = '${timesheetId}'
107
- AND Date__c = ${formattedDate}
108
- `
119
+ let query = `
120
+ SELECT ${fields?.join(',') || 'FIELDS(STANDARD)'}
121
+ FROM TimesheetHour__c
122
+ WHERE Timesheet__c = '${timesheetId}'`
109
123
  try {
124
+ if (fields?.length) {
125
+ query += `
126
+ AND Date__c >= ${new Date(periodStartDate).toISOString().substring(0, 10)}
127
+ AND Date__c <= ${new Date(periodEndDate).toISOString().substring(0, 10)}`
128
+ }
129
+
110
130
  const {
111
131
  data: { records }
112
- } = await this.axiosInstance.get(url, {
132
+ } = await this.axiosInstance.get<{ records: SFTimesheetHourField[] }>(url, {
113
133
  params: { q: query }
114
134
  })
115
-
116
- const ids = records.map(record => record.Id)
117
- allHourIds.push(...ids)
135
+ allHours.push(...records)
118
136
  } catch (err) {
119
137
  console.error(`Failed to fetch`, err)
120
- return []
121
138
  }
122
139
  }
123
-
124
- return allHourIds
125
- }
126
-
127
- async getTimesheetHours(workorderId: string): Promise<TimesheetDayEntry[]> {
128
- const timesheetHourIds = await this.getTimesheetHoursIds(workorderId)
129
- return Promise.all(
130
- timesheetHourIds.map(async id =>
131
- this.axiosInstance
132
- .get(`/services/data/${SF_API_VERSION}/sobjects/TimeSheetHour__c/${id}`)
133
- .then(res => res.data)
134
- .then(toTimesheetDayEntry)
135
- )
136
- )
140
+ return allHours.map(toTimesheetDayEntry)
137
141
  }
138
142
 
139
143
  async getPayPeriods(): Promise<any> {
@@ -214,7 +218,7 @@ export class SFTimesheetClient {
214
218
  }
215
219
  }
216
220
 
217
- function toTimesheetDayEntry(raw: any): TimesheetDayEntry {
221
+ function toTimesheetDayEntry(raw: SFTimesheetHourField): TimesheetDayEntry {
218
222
  return {
219
223
  id: raw.Id,
220
224
  date: raw.Date__c,
package/src/api/.env DELETED
@@ -1,5 +0,0 @@
1
- I THINK THIS ONE IS WRONG
2
- SF_CLIENT_ID=3MVG9TZvGM_0NqB09H2fPRxeiDEsovly3D10kwNjOYJNTwBhMc5JHQgCdaSM.2N1V04tb_Tuyj_qktBPNueQd
3
- SF_CLIENT_SECRET=6F4D40003CF5B156A2AD2BFAD384E74AA9FFFD574E8753EAC485E3C478C50F9F
4
- GRANT_TYPE=client_credentials
5
- SALESFORCE_URL=https://do0000000d247eaa--phealth.sandbox.my.salesforce.com