-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
392 lines (360 loc) · 11.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import type { mssql } from '@cityssm/mssql-multi-pool'
import type { DateString } from '@cityssm/utils-datetime'
import {
type AccountNumberSource,
getAccountNumberByWorkOrderNumberAndObjectCode
} from './helpers/getAccountNumber.js'
import {
type AddEquipment,
addEquipment
} from './queries/equipment/addEquipment.js'
import { getEquipmentByEquipmentId } from './queries/equipment/getEquipment.js'
import type { EquipmentItem } from './queries/equipment/types.js'
import {
type AddResourceItem,
addResourceItem
} from './queries/items/addResourceItem.js'
import {
type CreateStockTransactionBatch,
createStockTransactionBatch
} from './queries/items/createStockTransactionBatch.js'
import { getItemByItemId } from './queries/items/getItems.js'
import type { ResourceItem } from './queries/items/types.js'
import {
getActivitiesAssignedToJobByFiscalYear,
getActivityByActivityId
} from './queries/jobs/getActivities.js'
import { getJobActivityObjectCodeByKeys } from './queries/jobs/getJobActivityObjectCodes.js'
import { getJobByJobId } from './queries/jobs/getJobs.js'
import {
getObjectCodeAssignedToJobByObjectCodeAndFiscalYear,
getObjectCodeByObjectCode,
getObjectCodesAssignedToJobByFiscalYear
} from './queries/jobs/getObjectCodes.js'
import type {
Activity,
Job,
JobActivityObjectCode,
JobAssignedObjectCode,
ObjectCode
} from './queries/jobs/types.js'
import type { BigIntString } from './queries/types.js'
import {
type AddWorkOrderResource,
addWorkOrderResource
} from './queries/workOrders/addWorkOrderResource.js'
import { deleteWorkOrderResource } from './queries/workOrders/deleteWorkOrderResource.js'
import {
getWorkOrderResourcesByStartDate,
getWorkOrderResourcesByStartDateTimeRange,
getWorkOrderResourcesByWorkOrderNumber
} from './queries/workOrders/getWorkOrderResources.js'
import { getWorkOrderByWorkOrderNumber } from './queries/workOrders/getWorkOrders.js'
import type {
WorkOrder,
WorkOrderResource
} from './queries/workOrders/types.js'
import {
type UpdateWorkOrderResource,
updateWorkOrderResource
} from './queries/workOrders/updateWorkOrderResource.js'
/**
* WorkTech API
*/
export class WorkTechAPI {
readonly #mssqlConfig: mssql.config
/**
* @param mssqlConfig - SQL Server configuration.
*/
constructor(mssqlConfig: mssql.config) {
this.#mssqlConfig = mssqlConfig
}
/**
* Retrieves a piece of equipment.
* @param equipmentId - The equipment id.
* @returns - The equipment record, if available.
*/
async getEquipmentByEquipmentId(
equipmentId: string
): Promise<EquipmentItem | undefined> {
return await getEquipmentByEquipmentId(this.#mssqlConfig, equipmentId)
}
async addEquipment(equipment: AddEquipment): Promise<BigIntString> {
return await addEquipment(this.#mssqlConfig, equipment)
}
/**
* Retrieves an item.
* @param itemId - The item id.
* @returns - The item, if available.
*/
async getItemByItemId(itemId: string): Promise<ResourceItem | undefined> {
return await getItemByItemId(this.#mssqlConfig, itemId)
}
async addResourceItem(resourceItem: AddResourceItem): Promise<BigIntString> {
return await addResourceItem(this.#mssqlConfig, resourceItem)
}
/**
* Creates a new stock transaction batch.
* @param batch - The batch details
* @returns - The batch id.
*/
async createStockTransactionBatch(
batch: CreateStockTransactionBatch
): Promise<number> {
return await createStockTransactionBatch(this.#mssqlConfig, batch)
}
/**
* Retrieves a work order.
* @param workOrderNumber - The work order number.
* @returns - The work order, if available.
*/
async getWorkOrderByWorkOrderNumber(
workOrderNumber: string
): Promise<WorkOrder | undefined> {
return await getWorkOrderByWorkOrderNumber(
this.#mssqlConfig,
workOrderNumber
)
}
/**
* Retrieves a list of work order resources.
* @param workOrderNumber - The work order number.
* @returns - An array of resources associated with a work order.
*/
async getWorkOrderResourcesByWorkOrderNumber(
workOrderNumber: string
): Promise<WorkOrderResource[]> {
return await getWorkOrderResourcesByWorkOrderNumber(
this.#mssqlConfig,
workOrderNumber
)
}
/**
* Retrieves a list of work order resources.
* @param startDateFrom - The minimum start date.
* @param startDateTo - The maximum start date.
* @returns - An array of resources between a given start time range.
*/
async getWorkOrderResourcesByStartDateTimeRange(
startDateFrom: Date | string,
startDateTo: Date | string
): Promise<WorkOrderResource[]> {
return await getWorkOrderResourcesByStartDateTimeRange(
this.#mssqlConfig,
startDateFrom,
startDateTo
)
}
/**
* Retrieves a list of work order resources.
* @param startDateString - The start date.
* @returns - An array of resources on a given start date.
*/
async getWorkOrderResourcesByStartDate(
startDateString: DateString
): Promise<WorkOrderResource[]> {
return await getWorkOrderResourcesByStartDate(
this.#mssqlConfig,
startDateString
)
}
/**
* Adds a resource to a work order.
* @param workOrderResource - The work order resource fields.
* @returns - The system id for the new resource record.
*/
async addWorkOrderResource(
workOrderResource: AddWorkOrderResource
): Promise<BigIntString> {
return await addWorkOrderResource(this.#mssqlConfig, workOrderResource)
}
/**
* Updates a resource on a work order.
* Note that only a subset of fields can be updated,
* and each group must have all fields within it's grouping defined to be updated.
* - workDescription
* - serviceRequestSystemId, workOrderNumber
* - startDateTime
* - quantity, unitPrice, baseAmount
* @param workOrderResource - The work order resource fields.
* @returns - True when the update is processed successfully.
*/
async updateWorkOrderResource(
workOrderResource: UpdateWorkOrderResource
): Promise<boolean> {
return await updateWorkOrderResource(this.#mssqlConfig, workOrderResource)
}
/**
* Deletes a resource on a work order.
* @param serviceRequestItemSystemId - The work order resource id.
* @returns - True when the delete is processed successfully.
*/
async deleteWorkOrderResource(
serviceRequestItemSystemId: BigIntString
): Promise<boolean> {
return await deleteWorkOrderResource(
this.#mssqlConfig,
serviceRequestItemSystemId
)
}
/**
* Retrieves a job.
* @param jobId - The job id
* @returns - The job, if available.
*/
async getJobByJobId(jobId: string): Promise<Job | undefined> {
return await getJobByJobId(this.#mssqlConfig, jobId)
}
/**
* Retrieves an activity.
* @param activityId - The activity id
* @returns - The activity, if available.
*/
async getActivityByActivityId(
activityId: string
): Promise<Activity | undefined> {
return await getActivityByActivityId(this.#mssqlConfig, activityId)
}
/**
* Retrieves the activities associated with a given job and fiscal year.
* @param jobId - The job id
* @param fiscalYear - The fiscal year
* @returns - An array of activities.
*/
async getActivitiesAssignedToJobByFiscalYear(
jobId: string,
fiscalYear: number | string
): Promise<Activity[]> {
return await getActivitiesAssignedToJobByFiscalYear(
this.#mssqlConfig,
jobId,
fiscalYear
)
}
/**
* Retrieves an object code.
* @param objectCode - The object code
* @returns - The object code, if available.
*/
async getObjectCodeByObjectCode(
objectCode: string
): Promise<ObjectCode | undefined> {
return await getObjectCodeByObjectCode(this.#mssqlConfig, objectCode)
}
/**
* Retrieves a list of object codes associated with a given job and fiscal year.
* @param jobId - The job id.
* @param fiscalYear - The fiscal year.
* @returns - An array of object codes assigned to a given job.
*/
async getObjectCodesAssignedToJobByFiscalYear(
jobId: string,
fiscalYear: number | string
): Promise<JobAssignedObjectCode[]> {
return await getObjectCodesAssignedToJobByFiscalYear(
this.#mssqlConfig,
jobId,
fiscalYear
)
}
/**
* Retrieves an object code associated with a given job and fiscal year.
* @param jobId - The job id.
* @param objectCode - The object code.
* @param fiscalYear - The fiscal year.
* @returns - The object code, if available.
*/
async getObjectCodeAssignedToJobByObjectCodeAndFiscalYear(
jobId: string,
objectCode: string,
fiscalYear: number | string
): Promise<JobAssignedObjectCode | undefined> {
return await getObjectCodeAssignedToJobByObjectCodeAndFiscalYear(
this.#mssqlConfig,
jobId,
objectCode,
fiscalYear
)
}
/**
* Retrieves a job - activity - object code.
* @param keys - The keys to search on.
* @param keys.jobId - The job id.
* @param keys.activityId - The activity id.
* @param keys.objectCode - The object code.
* @param keys.fiscalYear - The fiscal year.
* @returns - The job - activity - object code combination if available.
*/
async getJobActivityObjectCodeByKeys(keys: {
jobId: string
activityId: string
objectCode: string
fiscalYear: number | string
}): Promise<JobActivityObjectCode | undefined> {
return await getJobActivityObjectCodeByKeys(this.#mssqlConfig, keys)
}
/**
* Retrieves an account number for a given work order.
* @param workOrderNumber - The work order number.
* @param optionalObjectCode - An optional object code.
* @returns - The account number and its source, if available.
*/
async getAccountNumberByWorkOrderNumberAndObjectCode(
workOrderNumber: string,
optionalObjectCode?: string
): Promise<AccountNumberSource | undefined> {
return await getAccountNumberByWorkOrderNumberAndObjectCode(
this.#mssqlConfig,
workOrderNumber,
optionalObjectCode
)
}
}
export { getAccountNumberByWorkOrderNumberAndObjectCode } from './helpers/getAccountNumber.js'
export { getEquipmentByEquipmentId } from './queries/equipment/getEquipment.js'
export { addEquipment } from './queries/equipment/addEquipment.js'
export { getItemByItemId } from './queries/items/getItems.js'
export {
type AddResourceItem,
addResourceItem
} from './queries/items/addResourceItem.js'
export {
type CreateStockTransactionBatch,
type CreateStockTransactionBatchEntry,
createStockTransactionBatch
} from './queries/items/createStockTransactionBatch.js'
export {
getActivityByActivityId,
getActivitiesAssignedToJobByFiscalYear
} from './queries/jobs/getActivities.js'
export { getJobByJobId } from './queries/jobs/getJobs.js'
export {
getObjectCodeByObjectCode,
getObjectCodesAssignedToJobByFiscalYear,
getObjectCodeAssignedToJobByObjectCodeAndFiscalYear
} from './queries/jobs/getObjectCodes.js'
export { getJobActivityObjectCodeByKeys } from './queries/jobs/getJobActivityObjectCodes.js'
export { getWorkOrderByWorkOrderNumber } from './queries/workOrders/getWorkOrders.js'
export { addWorkOrderResource } from './queries/workOrders/addWorkOrderResource.js'
export {
getWorkOrderResourcesByStartDate,
getWorkOrderResourcesByStartDateTimeRange,
getWorkOrderResourcesByWorkOrderNumber
} from './queries/workOrders/getWorkOrderResources.js'
export { updateWorkOrderResource } from './queries/workOrders/updateWorkOrderResource.js'
export { deleteWorkOrderResource } from './queries/workOrders/deleteWorkOrderResource.js'
/*
* Export Types
*/
export type { EquipmentItem } from './queries/equipment/types.js'
export type { ResourceItem } from './queries/items/types.js'
export type {
Job,
Activity,
ObjectCode,
JobActivityObjectCode
} from './queries/jobs/types.js'
export type {
WorkOrder,
WorkOrderResource
} from './queries/workOrders/types.js'