From cfcbee96e82cb362b9a7e964db5eba1ab20673f7 Mon Sep 17 00:00:00 2001 From: Dan Gowans Date: Fri, 6 Dec 2024 14:26:08 -0500 Subject: [PATCH] add `createStockTransactionBatch()` --- eslint.config.js | 2 +- eslint.config.ts | 2 +- helpers/getAccountNumber.d.ts | 4 +- helpers/getAccountNumber.ts | 4 +- index.d.ts | 12 +- index.js | 10 ++ index.ts | 27 ++- package-lock.json | 16 +- package.json | 4 +- queries/equipment/addEquipment.d.ts | 4 +- queries/equipment/addEquipment.ts | 4 +- queries/equipment/getEquipment.d.ts | 4 +- queries/equipment/getEquipment.ts | 6 +- queries/items/addResourceItem.d.ts | 4 +- queries/items/addResourceItem.ts | 4 +- .../items/createStockTransactionBatch.d.ts | 25 +++ queries/items/createStockTransactionBatch.js | 99 +++++++++++ queries/items/createStockTransactionBatch.ts | 161 ++++++++++++++++++ queries/items/getItems.d.ts | 4 +- queries/items/getItems.ts | 6 +- queries/jobs/getActivities.d.ts | 8 +- queries/jobs/getActivities.js | 2 - queries/jobs/getActivities.ts | 17 +- queries/jobs/getJobActivityObjectCodes.d.ts | 4 +- queries/jobs/getJobActivityObjectCodes.ts | 6 +- queries/jobs/getJobs.d.ts | 4 +- queries/jobs/getJobs.ts | 6 +- queries/jobs/getObjectCodes.d.ts | 8 +- queries/jobs/getObjectCodes.js | 2 - queries/jobs/getObjectCodes.ts | 17 +- queries/systemId.d.ts | 6 +- queries/systemId.ts | 8 +- queries/workOrders/addWorkOrderResource.d.ts | 4 +- queries/workOrders/addWorkOrderResource.ts | 4 +- .../workOrders/deleteWorkOrderResource.d.ts | 4 +- queries/workOrders/deleteWorkOrderResource.ts | 4 +- queries/workOrders/getWorkOrderResources.d.ts | 8 +- queries/workOrders/getWorkOrderResources.js | 2 - queries/workOrders/getWorkOrderResources.ts | 15 +- queries/workOrders/getWorkOrders.d.ts | 5 +- queries/workOrders/getWorkOrders.js | 14 +- queries/workOrders/getWorkOrders.ts | 29 ++-- queries/workOrders/types.d.ts | 1 + queries/workOrders/types.ts | 2 + .../workOrders/updateWorkOrderResource.d.ts | 4 +- queries/workOrders/updateWorkOrderResource.ts | 4 +- test/items.test.js | 8 +- test/items.test.ts | 29 +++- 48 files changed, 483 insertions(+), 144 deletions(-) create mode 100644 queries/items/createStockTransactionBatch.d.ts create mode 100644 queries/items/createStockTransactionBatch.js create mode 100644 queries/items/createStockTransactionBatch.ts diff --git a/eslint.config.js b/eslint.config.js index 618610b..36ee016 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -3,7 +3,7 @@ import tseslint from 'typescript-eslint'; export const config = tseslint.config(...configCityssm, { rules: { '@typescript-eslint/no-unsafe-argument': 'warn', - '@typescript-eslint/no-unsafe-assignment': 'warn', + '@typescript-eslint/no-unsafe-assignment': 'off', '@typescript-eslint/no-unsafe-call': 'warn', '@typescript-eslint/no-unsafe-member-access': 'warn', '@typescript-eslint/no-unsafe-return': 'warn', diff --git a/eslint.config.ts b/eslint.config.ts index 8bac7fa..18f5fb5 100644 --- a/eslint.config.ts +++ b/eslint.config.ts @@ -4,7 +4,7 @@ import tseslint from 'typescript-eslint' export const config = tseslint.config(...configCityssm, { rules: { '@typescript-eslint/no-unsafe-argument': 'warn', - '@typescript-eslint/no-unsafe-assignment': 'warn', + '@typescript-eslint/no-unsafe-assignment': 'off', '@typescript-eslint/no-unsafe-call': 'warn', '@typescript-eslint/no-unsafe-member-access': 'warn', '@typescript-eslint/no-unsafe-return': 'warn', diff --git a/helpers/getAccountNumber.d.ts b/helpers/getAccountNumber.d.ts index 58061a3..f992d8b 100644 --- a/helpers/getAccountNumber.d.ts +++ b/helpers/getAccountNumber.d.ts @@ -1,4 +1,4 @@ -import type { mssqlTypes } from '@cityssm/mssql-multi-pool'; +import type { mssql } from '@cityssm/mssql-multi-pool'; export interface AccountNumberSource { accountNumber: string; accountNumberSource: // eslint-disable-next-line no-secrets/no-secrets @@ -11,4 +11,4 @@ export interface AccountNumberSource { * @param optionalObjectCode - An optional object code. * @returns - The account number and its source, if available. */ -export declare function getAccountNumberByWorkOrderNumberAndObjectCode(mssqlConfig: mssqlTypes.config, workOrderNumber: string, optionalObjectCode?: string): Promise; +export declare function getAccountNumberByWorkOrderNumberAndObjectCode(mssqlConfig: mssql.config, workOrderNumber: string, optionalObjectCode?: string): Promise; diff --git a/helpers/getAccountNumber.ts b/helpers/getAccountNumber.ts index c5c7d34..87d5fc8 100644 --- a/helpers/getAccountNumber.ts +++ b/helpers/getAccountNumber.ts @@ -1,4 +1,4 @@ -import type { mssqlTypes } from '@cityssm/mssql-multi-pool' +import type { mssql } from '@cityssm/mssql-multi-pool' import { accountSegmentSeparator } from '../apiConfig.js' import { getJobActivityObjectCodeByKeys } from '../queries/jobs/getJobActivityObjectCodes.js' @@ -23,7 +23,7 @@ export interface AccountNumberSource { * @returns - The account number and its source, if available. */ export async function getAccountNumberByWorkOrderNumberAndObjectCode( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, workOrderNumber: string, optionalObjectCode?: string ): Promise { diff --git a/index.d.ts b/index.d.ts index 5071e9b..668592f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,9 +1,10 @@ -import type { mssqlTypes } from '@cityssm/mssql-multi-pool'; +import type { mssql } from '@cityssm/mssql-multi-pool'; import type { DateString } from '@cityssm/utils-datetime'; import { type AccountNumberSource } from './helpers/getAccountNumber.js'; import { type AddEquipment } from './queries/equipment/addEquipment.js'; import type { EquipmentItem } from './queries/equipment/types.js'; import { type AddResourceItem } from './queries/items/addResourceItem.js'; +import { type CreateStockTransactionBatch } from './queries/items/createStockTransactionBatch.js'; import type { ResourceItem } from './queries/items/types.js'; import type { Activity, Job, JobActivityObjectCode, JobAssignedObjectCode, ObjectCode } from './queries/jobs/types.js'; import type { BigIntString } from './queries/types.js'; @@ -18,7 +19,7 @@ export declare class WorkTechAPI { /** * @param mssqlConfig - SQL Server configuration. */ - constructor(mssqlConfig: mssqlTypes.config); + constructor(mssqlConfig: mssql.config); /** * Retrieves a piece of equipment. * @param equipmentId - The equipment id. @@ -33,6 +34,12 @@ export declare class WorkTechAPI { */ getItemByItemId(itemId: string): Promise; addResourceItem(resourceItem: AddResourceItem): Promise; + /** + * Creates a new stock transaction batch. + * @param batch - The batch details + * @returns - The batch id. + */ + createStockTransactionBatch(batch: CreateStockTransactionBatch): Promise; /** * Retrieves a work order. * @param workOrderNumber - The work order number. @@ -150,6 +157,7 @@ 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'; diff --git a/index.js b/index.js index 0c0440e..cd1fdf7 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ import { getAccountNumberByWorkOrderNumberAndObjectCode } from './helpers/getAcc import { addEquipment } from './queries/equipment/addEquipment.js'; import { getEquipmentByEquipmentId } from './queries/equipment/getEquipment.js'; import { addResourceItem } from './queries/items/addResourceItem.js'; +import { createStockTransactionBatch } from './queries/items/createStockTransactionBatch.js'; import { getItemByItemId } from './queries/items/getItems.js'; import { getActivitiesAssignedToJobByFiscalYear, getActivityByActivityId } from './queries/jobs/getActivities.js'; import { getJobActivityObjectCodeByKeys } from './queries/jobs/getJobActivityObjectCodes.js'; @@ -45,6 +46,14 @@ export class WorkTechAPI { async addResourceItem(resourceItem) { return await addResourceItem(this.#mssqlConfig, resourceItem); } + /** + * Creates a new stock transaction batch. + * @param batch - The batch details + * @returns - The batch id. + */ + async createStockTransactionBatch(batch) { + return await createStockTransactionBatch(this.#mssqlConfig, batch); + } /** * Retrieves a work order. * @param workOrderNumber - The work order number. @@ -187,6 +196,7 @@ export { getEquipmentByEquipmentId } from './queries/equipment/getEquipment.js'; export { addEquipment } from './queries/equipment/addEquipment.js'; export { getItemByItemId } from './queries/items/getItems.js'; export { addResourceItem } from './queries/items/addResourceItem.js'; +export { 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'; diff --git a/index.ts b/index.ts index 27d856a..83b1d58 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,4 @@ -import type { mssqlTypes } from '@cityssm/mssql-multi-pool' +import type { mssql } from '@cityssm/mssql-multi-pool' import type { DateString } from '@cityssm/utils-datetime' import { @@ -15,6 +15,10 @@ 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 { @@ -60,12 +64,12 @@ import { * WorkTech API */ export class WorkTechAPI { - readonly #mssqlConfig: mssqlTypes.config + readonly #mssqlConfig: mssql.config /** * @param mssqlConfig - SQL Server configuration. */ - constructor(mssqlConfig: mssqlTypes.config) { + constructor(mssqlConfig: mssql.config) { this.#mssqlConfig = mssqlConfig } @@ -97,6 +101,17 @@ export class WorkTechAPI { 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 { + return await createStockTransactionBatch(this.#mssqlConfig, batch) + } + /** * Retrieves a work order. * @param workOrderNumber - The work order number. @@ -330,6 +345,12 @@ export { addResourceItem } from './queries/items/addResourceItem.js' +export { + type CreateStockTransactionBatch, + type CreateStockTransactionBatchEntry, + createStockTransactionBatch +} from './queries/items/createStockTransactionBatch.js' + export { getActivityByActivityId, getActivitiesAssignedToJobByFiscalYear diff --git a/package-lock.json b/package-lock.json index 07bf36e..38064b1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "@cityssm/worktech-api", - "version": "0.4.0", + "version": "0.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@cityssm/worktech-api", - "version": "0.4.0", + "version": "0.5.0", "license": "MIT", "dependencies": { - "@cityssm/mssql-multi-pool": "^4.0.0", + "@cityssm/mssql-multi-pool": "^4.1.0", "@cityssm/utils-datetime": "^1.3.0", "node-cache": "^5.1.2" }, @@ -2254,15 +2254,15 @@ } }, "node_modules/@cityssm/mssql-multi-pool": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/@cityssm/mssql-multi-pool/-/mssql-multi-pool-4.0.0.tgz", - "integrity": "sha512-3+4AsMUbL0/ze/iUPrJdyer/0FIX8H58H15k2WbT4K1u+M7PPCnHEtz+C63tVLkPco16Xzy6ZqT7lcJNV48Kkg==", + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/@cityssm/mssql-multi-pool/-/mssql-multi-pool-4.1.0.tgz", + "integrity": "sha512-UNhyJvTHNmHiQHdUZky4gV+ANrfPnO4ATbTR50yh9oIVWpuOPw2gxWuZcA61HwdFkMzGAJqsas8e2DR8qmjCAQ==", "license": "MIT", "dependencies": { "@types/mssql": "^9.1.5", - "debug": "^4.3.7", + "debug": "^4.4.0", "exit-hook": "^4.0.0", - "msnodesqlv8": "^4.2.1", + "msnodesqlv8": "^4.4.0", "mssql": "^11.0.1" }, "engines": { diff --git a/package.json b/package.json index a5ed91e..fdab253 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cityssm/worktech-api", - "version": "0.4.0", + "version": "0.5.0", "description": "Integrations with the WorkTech work order management system.", "type": "module", "exports": "./index.js", @@ -31,7 +31,7 @@ "prettier-config-cityssm": "^1.0.0" }, "dependencies": { - "@cityssm/mssql-multi-pool": "^4.0.0", + "@cityssm/mssql-multi-pool": "^4.1.0", "@cityssm/utils-datetime": "^1.3.0", "node-cache": "^5.1.2" } diff --git a/queries/equipment/addEquipment.d.ts b/queries/equipment/addEquipment.d.ts index 0c53d2d..62d93b2 100644 --- a/queries/equipment/addEquipment.d.ts +++ b/queries/equipment/addEquipment.d.ts @@ -1,4 +1,4 @@ -import type { mssqlTypes } from '@cityssm/mssql-multi-pool'; +import type { mssql } from '@cityssm/mssql-multi-pool'; import type { BigIntString } from '../types.js'; import type { EquipmentItem } from './types.js'; export interface AddEquipment extends Partial { @@ -6,4 +6,4 @@ export interface AddEquipment extends Partial { equipmentClass: string; equipmentDescription: string; } -export declare function addEquipment(mssqlConfig: mssqlTypes.config, equipment: AddEquipment): Promise; +export declare function addEquipment(mssqlConfig: mssql.config, equipment: AddEquipment): Promise; diff --git a/queries/equipment/addEquipment.ts b/queries/equipment/addEquipment.ts index 77aa60a..2f402e2 100644 --- a/queries/equipment/addEquipment.ts +++ b/queries/equipment/addEquipment.ts @@ -1,4 +1,4 @@ -import type { mssqlTypes } from '@cityssm/mssql-multi-pool' +import type { mssql } from '@cityssm/mssql-multi-pool' import { addResourceItem } from '../items/addResourceItem.js' import type { BigIntString } from '../types.js' @@ -12,7 +12,7 @@ export interface AddEquipment extends Partial { } export async function addEquipment( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, equipment: AddEquipment ): Promise { return await addResourceItem(mssqlConfig, { diff --git a/queries/equipment/getEquipment.d.ts b/queries/equipment/getEquipment.d.ts index b7b0752..4b68a15 100644 --- a/queries/equipment/getEquipment.d.ts +++ b/queries/equipment/getEquipment.d.ts @@ -1,4 +1,4 @@ -import { type mssqlTypes } from '@cityssm/mssql-multi-pool'; +import { type mssql } from '@cityssm/mssql-multi-pool'; import type { EquipmentItem } from './types.js'; /** * Retrieves a piece of equipment. @@ -6,4 +6,4 @@ import type { EquipmentItem } from './types.js'; * @param equipmentId - The equipment id. * @returns - The equipment record, if available. */ -export declare function getEquipmentByEquipmentId(mssqlConfig: mssqlTypes.config, equipmentId: string): Promise; +export declare function getEquipmentByEquipmentId(mssqlConfig: mssql.config, equipmentId: string): Promise; diff --git a/queries/equipment/getEquipment.ts b/queries/equipment/getEquipment.ts index 1a49357..1bd206d 100644 --- a/queries/equipment/getEquipment.ts +++ b/queries/equipment/getEquipment.ts @@ -1,6 +1,6 @@ import { connect, - type mssqlTypes + type mssql } from '@cityssm/mssql-multi-pool' import NodeCache from 'node-cache' @@ -53,7 +53,7 @@ const cache = new NodeCache({ * @returns - The equipment record, if available. */ export async function getEquipmentByEquipmentId( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, equipmentId: string ): Promise { let equipment: EquipmentItem | undefined = cache.get(equipmentId) @@ -67,7 +67,7 @@ export async function getEquipmentByEquipmentId( const equipmentResult = (await pool .request() .input('equipmentId', equipmentId) - .query(`${sql} and Item_ID = @equipmentId`)) as mssqlTypes.IResult + .query(`${sql} and Item_ID = @equipmentId`)) as mssql.IResult if (equipmentResult.recordset.length === 0) { return undefined diff --git a/queries/items/addResourceItem.d.ts b/queries/items/addResourceItem.d.ts index 498349f..cab6486 100644 --- a/queries/items/addResourceItem.d.ts +++ b/queries/items/addResourceItem.d.ts @@ -1,4 +1,4 @@ -import { type mssqlTypes } from '@cityssm/mssql-multi-pool'; +import { type mssql } from '@cityssm/mssql-multi-pool'; import type { BigIntString } from '../types.js'; import type { ResourceItem } from './types.js'; export interface AddResourceItem extends Partial { @@ -13,4 +13,4 @@ export interface AddResourceItem extends Partial { * @param resourceItem - The resource item fields. * @returns - The system id for the new resource item. */ -export declare function addResourceItem(mssqlConfig: mssqlTypes.config, resourceItem: AddResourceItem): Promise; +export declare function addResourceItem(mssqlConfig: mssql.config, resourceItem: AddResourceItem): Promise; diff --git a/queries/items/addResourceItem.ts b/queries/items/addResourceItem.ts index bfdb6fb..271d7ad 100644 --- a/queries/items/addResourceItem.ts +++ b/queries/items/addResourceItem.ts @@ -1,4 +1,4 @@ -import { connect, type mssqlTypes } from '@cityssm/mssql-multi-pool' +import { connect, type mssql } from '@cityssm/mssql-multi-pool' import { getLastSystemId, incrementLastSystemId } from '../systemId.js' import type { BigIntString } from '../types.js' @@ -20,7 +20,7 @@ export interface AddResourceItem extends Partial { * @returns - The system id for the new resource item. */ export async function addResourceItem( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, resourceItem: AddResourceItem ): Promise { if (resourceItem.itemId.length > 15) { diff --git a/queries/items/createStockTransactionBatch.d.ts b/queries/items/createStockTransactionBatch.d.ts new file mode 100644 index 0000000..8a769e3 --- /dev/null +++ b/queries/items/createStockTransactionBatch.d.ts @@ -0,0 +1,25 @@ +import { type mssql } from '@cityssm/mssql-multi-pool'; +import { type DateString } from '@cityssm/utils-datetime'; +export interface CreateStockTransactionBatch { + batchDescription?: string; + batchDate?: DateString; + userId?: string; + entries: CreateStockTransactionBatchEntry[]; +} +export interface CreateStockTransactionBatchEntry { + entryDate?: DateString; + workOrderNumber: string; + jobId?: string; + activityId?: string; + objectCode?: string; + itemNumber: string; + locationCode?: string; + quantity: number; +} +/** + * Creates a new stock transaction batch. + * @param mssqlConfig - SQL Server configuration. + * @param batch - The batch details + * @returns - The batch id. + */ +export declare function createStockTransactionBatch(mssqlConfig: mssql.config, batch: CreateStockTransactionBatch): Promise; diff --git a/queries/items/createStockTransactionBatch.js b/queries/items/createStockTransactionBatch.js new file mode 100644 index 0000000..d277fff --- /dev/null +++ b/queries/items/createStockTransactionBatch.js @@ -0,0 +1,99 @@ +// eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair +/* eslint-disable unicorn/no-null */ +import { connect } from '@cityssm/mssql-multi-pool'; +import { dateToString } from '@cityssm/utils-datetime'; +import { _getWorkOrderByWorkOrderNumber } from '../workOrders/getWorkOrders.js'; +const batchDefaults = { + batchType: 'Stock Transactions', + userId: '@cityssm/worktech-api' +}; +const batchEntryDefaults = { + transactionType: 'Stock Usage' +}; +/** + * Creates a new stock transaction batch. + * @param mssqlConfig - SQL Server configuration. + * @param batch - The batch details + * @returns - The batch id. + */ +export async function createStockTransactionBatch(mssqlConfig, batch) { + const pool = await connect(mssqlConfig); + const transaction = pool.transaction(); + try { + /* + * Create batch + */ + const userId = batch.userId ?? batchDefaults.userId; + const batchDate = batch.batchDate ?? dateToString(new Date()); + const batchDescription = (batch.batchDescription ?? `${batchDate} - ${batchDefaults.batchType}`).slice(0, 50); + await transaction.begin(); + const batchCreateResult = (await transaction + .request() + .input('Type', batchDefaults.batchType) + .input('Desc', batchDescription) + .input('UserID', userId) + .input('Date', batchDate) + .execute('WT_INSERT_WMBAC')); + const batchId = batchCreateResult.returnValue; + /* + * Insert batch entries + */ + const itemNumberToLocationCode = {}; + for (const entry of batch.entries) { + let jobId = entry.jobId; + let activityId = entry.activityId; + let objectCode = entry.objectCode; + if (jobId === undefined || + activityId === undefined || + objectCode === undefined) { + const workOrder = await _getWorkOrderByWorkOrderNumber(transaction.request(), entry.workOrderNumber); + if (workOrder !== undefined) { + jobId ??= workOrder.jobId ?? workOrder.itemId; + activityId ??= workOrder.activityId; + objectCode ??= workOrder.objectCode; + } + } + let locationCode = entry.locationCode ?? itemNumberToLocationCode[entry.itemNumber]; + if (locationCode === undefined) { + const locationCodeResult = (await transaction + .request() + .input('itemNumber', entry.itemNumber) + .query(`select top 1 LocationCode + from WMILN + where Item_ID = @itemNumber + order by DefaulLoc desc`)); + if (locationCodeResult.recordset.length > 0) { + locationCode = locationCodeResult.recordset[0].LocationCode; + } + } + const entryDate = entry.entryDate ?? batchDate; + await transaction + .request() + .input('BatchSysID', batchId) + .input('Date', entryDate) + .input('SItem_ID', entry.itemNumber) + .input('ExJob_ID', jobId) + .input('ExActv_ID', activityId) + .input('WONOs', entry.workOrderNumber) + .input('Accomp', null) + .input('TC_ID', null) + .input('Qty', entry.quantity) + .input('Units', null) + .input('POS_ID', null) + .input('VehUsed', null) + .input('VehUsedQty', null) + .input('StockItem_ID', entry.itemNumber) + .input('Stock', null) + .input('TransType', batchEntryDefaults.transactionType) + .input('UserName', userId) + .input('LocationCode', locationCode) + .execute('WT_INSERT_WMTSI'); + } + await transaction.commit(); + return batchId; + } + catch (error) { + await transaction.rollback(); + throw error; + } +} diff --git a/queries/items/createStockTransactionBatch.ts b/queries/items/createStockTransactionBatch.ts new file mode 100644 index 0000000..7ff7d62 --- /dev/null +++ b/queries/items/createStockTransactionBatch.ts @@ -0,0 +1,161 @@ +// eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair +/* eslint-disable unicorn/no-null */ + +import { connect, type mssql } from '@cityssm/mssql-multi-pool' +import { type DateString, dateToString } from '@cityssm/utils-datetime' + +import { _getWorkOrderByWorkOrderNumber } from '../workOrders/getWorkOrders.js' + +const batchDefaults = { + batchType: 'Stock Transactions', + userId: '@cityssm/worktech-api' +} + +const batchEntryDefaults = { + transactionType: 'Stock Usage' +} + +export interface CreateStockTransactionBatch { + batchDescription?: string + batchDate?: DateString + userId?: string + entries: CreateStockTransactionBatchEntry[] +} + +export interface CreateStockTransactionBatchEntry { + entryDate?: DateString + + workOrderNumber: string + jobId?: string + activityId?: string + objectCode?: string + + itemNumber: string + locationCode?: string + + quantity: number +} + +/** + * Creates a new stock transaction batch. + * @param mssqlConfig - SQL Server configuration. + * @param batch - The batch details + * @returns - The batch id. + */ +export async function createStockTransactionBatch( + mssqlConfig: mssql.config, + batch: CreateStockTransactionBatch +): Promise { + const pool = await connect(mssqlConfig) + + const transaction = pool.transaction() + + try { + /* + * Create batch + */ + + const userId = batch.userId ?? batchDefaults.userId + const batchDate = batch.batchDate ?? dateToString(new Date()) + + const batchDescription = ( + batch.batchDescription ?? `${batchDate} - ${batchDefaults.batchType}` + ).slice(0, 50) + + await transaction.begin() + + const batchCreateResult = (await transaction + .request() + .input('Type', batchDefaults.batchType) + .input('Desc', batchDescription) + .input('UserID', userId) + .input('Date', batchDate) + .execute('WT_INSERT_WMBAC')) as mssql.IProcedureResult + + const batchId = batchCreateResult.returnValue as number + + /* + * Insert batch entries + */ + + const itemNumberToLocationCode: Record = {} + + for (const entry of batch.entries) { + let jobId = entry.jobId + let activityId = entry.activityId + let objectCode = entry.objectCode + + if ( + jobId === undefined || + activityId === undefined || + objectCode === undefined + ) { + const workOrder = await _getWorkOrderByWorkOrderNumber( + transaction.request(), + entry.workOrderNumber + ) + + if (workOrder !== undefined) { + jobId ??= workOrder.jobId ?? workOrder.itemId + activityId ??= workOrder.activityId + objectCode ??= workOrder.objectCode + } + } + + let locationCode = + entry.locationCode ?? itemNumberToLocationCode[entry.itemNumber] + + if (locationCode === undefined) { + const locationCodeResult = (await transaction + .request() + .input('itemNumber', entry.itemNumber) + .query(`select top 1 LocationCode + from WMILN + where Item_ID = @itemNumber + order by DefaulLoc desc`)) as mssql.IResult<{ + LocationCode: string + }> + + if (locationCodeResult.recordset.length > 0) { + locationCode = locationCodeResult.recordset[0].LocationCode + } + } + + const entryDate = entry.entryDate ?? batchDate + + await transaction + .request() + + .input('BatchSysID', batchId) + .input('Date', entryDate) + .input('SItem_ID', entry.itemNumber) + .input('ExJob_ID', jobId) + .input('ExActv_ID', activityId) + .input('WONOs', entry.workOrderNumber) + + .input('Accomp', null) + .input('TC_ID', null) + .input('Qty', entry.quantity) + .input('Units', null) + .input('POS_ID', null) + .input('VehUsed', null) + .input('VehUsedQty', null) + + .input('StockItem_ID', entry.itemNumber) + .input('Stock', null) + .input('TransType', batchEntryDefaults.transactionType) + + .input('UserName', userId) + .input('LocationCode', locationCode) + + .execute('WT_INSERT_WMTSI') + } + + await transaction.commit() + + return batchId + } catch (error) { + await transaction.rollback() + throw error as Error + } +} diff --git a/queries/items/getItems.d.ts b/queries/items/getItems.d.ts index 324f95c..848f0f8 100644 --- a/queries/items/getItems.d.ts +++ b/queries/items/getItems.d.ts @@ -1,4 +1,4 @@ -import { type mssqlTypes } from '@cityssm/mssql-multi-pool'; +import { type mssql } from '@cityssm/mssql-multi-pool'; import type { ResourceItem } from './types.js'; /** * Retrieves an item. @@ -6,4 +6,4 @@ import type { ResourceItem } from './types.js'; * @param itemId - The item id. * @returns - The item, if available. */ -export declare function getItemByItemId(mssqlConfig: mssqlTypes.config, itemId: string): Promise; +export declare function getItemByItemId(mssqlConfig: mssql.config, itemId: string): Promise; diff --git a/queries/items/getItems.ts b/queries/items/getItems.ts index b6febdb..823de8e 100644 --- a/queries/items/getItems.ts +++ b/queries/items/getItems.ts @@ -1,4 +1,4 @@ -import { connect, type mssqlTypes } from '@cityssm/mssql-multi-pool' +import { connect, type mssql } from '@cityssm/mssql-multi-pool' import NodeCache from 'node-cache' import { cacheTimeToLiveSeconds } from '../../apiConfig.js' @@ -45,7 +45,7 @@ const cache = new NodeCache({ * @returns - The item, if available. */ export async function getItemByItemId( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, itemId: string ): Promise { let item: ResourceItem | undefined = cache.get(itemId) @@ -59,7 +59,7 @@ export async function getItemByItemId( const itemResult = (await pool .request() .input('itemId', itemId) - .query(`${sql} and Item_ID = @itemId`)) as mssqlTypes.IResult + .query(`${sql} and Item_ID = @itemId`)) as mssql.IResult if (itemResult.recordset.length === 0) { return undefined diff --git a/queries/jobs/getActivities.d.ts b/queries/jobs/getActivities.d.ts index 7d779f6..285e5a8 100644 --- a/queries/jobs/getActivities.d.ts +++ b/queries/jobs/getActivities.d.ts @@ -1,4 +1,4 @@ -import { type mssqlTypes } from '@cityssm/mssql-multi-pool'; +import { type mssql } from '@cityssm/mssql-multi-pool'; import type { Activity } from './types.js'; /** * Retrieves an activity. @@ -6,7 +6,7 @@ import type { Activity } from './types.js'; * @param activityId - The activity id * @returns - The activity, if available. */ -export declare function getActivityByActivityId(mssqlConfig: mssqlTypes.config, activityId: string): Promise; +export declare function getActivityByActivityId(mssqlConfig: mssql.config, activityId: string): Promise; /** * Retrieves the activity associated with a given job and fiscal year. * @param mssqlConfig - SQL Server configuration @@ -15,7 +15,7 @@ export declare function getActivityByActivityId(mssqlConfig: mssqlTypes.config, * @param fiscalYear - The fiscal year * @returns - An array of activities. */ -export declare function getActivityAssignedToJobByActivityIdAndFiscalYear(mssqlConfig: mssqlTypes.config, jobId: string, activityId: string, fiscalYear: number | string): Promise; +export declare function getActivityAssignedToJobByActivityIdAndFiscalYear(mssqlConfig: mssql.config, jobId: string, activityId: string, fiscalYear: number | string): Promise; /** * Retrieves the activities associated with a given job and fiscal year. * @param mssqlConfig - SQL Server configuration @@ -23,4 +23,4 @@ export declare function getActivityAssignedToJobByActivityIdAndFiscalYear(mssqlC * @param fiscalYear - The fiscal year * @returns - An array of activities. */ -export declare function getActivitiesAssignedToJobByFiscalYear(mssqlConfig: mssqlTypes.config, jobId: string, fiscalYear: number | string): Promise; +export declare function getActivitiesAssignedToJobByFiscalYear(mssqlConfig: mssql.config, jobId: string, fiscalYear: number | string): Promise; diff --git a/queries/jobs/getActivities.js b/queries/jobs/getActivities.js index 3042676..d605ceb 100644 --- a/queries/jobs/getActivities.js +++ b/queries/jobs/getActivities.js @@ -1,5 +1,3 @@ -// eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ import { connect } from '@cityssm/mssql-multi-pool'; import NodeCache from 'node-cache'; import { cacheTimeToLiveSeconds } from '../../apiConfig.js'; diff --git a/queries/jobs/getActivities.ts b/queries/jobs/getActivities.ts index 23165b7..b808026 100644 --- a/queries/jobs/getActivities.ts +++ b/queries/jobs/getActivities.ts @@ -1,9 +1,6 @@ -// eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ - import { connect, - type mssqlTypes + type mssql } from '@cityssm/mssql-multi-pool' import NodeCache from 'node-cache' @@ -31,7 +28,7 @@ const cache = new NodeCache({ * @returns - The activity, if available. */ export async function getActivityByActivityId( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, activityId: string ): Promise { let activityObject: Activity | undefined = cache.get(activityId) @@ -45,7 +42,7 @@ export async function getActivityByActivityId( const result = (await pool .request() .input('activityId', activityId) - .query(`${sql} where Actv_ID = @activityId`)) as mssqlTypes.IResult + .query(`${sql} where Actv_ID = @activityId`)) as mssql.IResult if (result.recordset.length === 0) { return undefined @@ -67,7 +64,7 @@ export async function getActivityByActivityId( * @returns - An array of activities. */ export async function getActivityAssignedToJobByActivityIdAndFiscalYear( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, jobId: string, activityId: string, fiscalYear: number | string @@ -82,7 +79,7 @@ export async function getActivityAssignedToJobByActivityIdAndFiscalYear( .query( `${sql} where Actv_ID = @activityId and Actv_ID in (select Actv_ID from WMJACA with (nolock) where Job_ID = @jobId and Year = @fiscalYear)` - )) as mssqlTypes.IResult + )) as mssql.IResult if (result.recordset.length === 0) { return undefined @@ -99,7 +96,7 @@ export async function getActivityAssignedToJobByActivityIdAndFiscalYear( * @returns - An array of activities. */ export async function getActivitiesAssignedToJobByFiscalYear( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, jobId: string, fiscalYear: number | string ): Promise { @@ -111,7 +108,7 @@ export async function getActivitiesAssignedToJobByFiscalYear( .input('fiscalYear', fiscalYear) .query( `${sql} where Actv_ID in (select Actv_ID from WMJACA with (nolock) where Job_ID = @jobId and Year = @fiscalYear)` - )) as mssqlTypes.IResult + )) as mssql.IResult return result.recordset } diff --git a/queries/jobs/getJobActivityObjectCodes.d.ts b/queries/jobs/getJobActivityObjectCodes.d.ts index c4d431d..cffca60 100644 --- a/queries/jobs/getJobActivityObjectCodes.d.ts +++ b/queries/jobs/getJobActivityObjectCodes.d.ts @@ -1,4 +1,4 @@ -import { type mssqlTypes } from '@cityssm/mssql-multi-pool'; +import { type mssql } from '@cityssm/mssql-multi-pool'; import type { JobActivityObjectCode } from './types.js'; /** * Retrieves a job - activity - object code. @@ -10,7 +10,7 @@ import type { JobActivityObjectCode } from './types.js'; * @param keys.fiscalYear - The fiscal year. * @returns - The job - activity - object code combination if available. */ -export declare function getJobActivityObjectCodeByKeys(mssqlConfig: mssqlTypes.config, keys: { +export declare function getJobActivityObjectCodeByKeys(mssqlConfig: mssql.config, keys: { jobId: string; activityId: string; objectCode: string; diff --git a/queries/jobs/getJobActivityObjectCodes.ts b/queries/jobs/getJobActivityObjectCodes.ts index 0c9c2d8..80a3cae 100644 --- a/queries/jobs/getJobActivityObjectCodes.ts +++ b/queries/jobs/getJobActivityObjectCodes.ts @@ -1,4 +1,4 @@ -import { connect, type mssqlTypes } from '@cityssm/mssql-multi-pool' +import { connect, type mssql } from '@cityssm/mssql-multi-pool' import type { JobActivityObjectCode } from './types.js' @@ -20,7 +20,7 @@ const sql = `SELECT [Job_ID] as jobId, * @returns - The job - activity - object code combination if available. */ export async function getJobActivityObjectCodeByKeys( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, keys: { jobId: string activityId: string @@ -40,7 +40,7 @@ export async function getJobActivityObjectCodeByKeys( where Job_ID = @jobId and Actv_ID = @activityId and ObjCode = @objectCode - and Year = @fiscalYear`)) as mssqlTypes.IResult + and Year = @fiscalYear`)) as mssql.IResult if (result.recordset.length === 0) { return undefined diff --git a/queries/jobs/getJobs.d.ts b/queries/jobs/getJobs.d.ts index ff0bbe4..93e64dc 100644 --- a/queries/jobs/getJobs.d.ts +++ b/queries/jobs/getJobs.d.ts @@ -1,4 +1,4 @@ -import { type mssqlTypes } from '@cityssm/mssql-multi-pool'; +import { type mssql } from '@cityssm/mssql-multi-pool'; import type { Job } from './types.js'; /** * Retrieves a job. @@ -6,4 +6,4 @@ import type { Job } from './types.js'; * @param jobId - The job id * @returns - The job, if available. */ -export declare function getJobByJobId(mssqlConfig: mssqlTypes.config, jobId: string): Promise; +export declare function getJobByJobId(mssqlConfig: mssql.config, jobId: string): Promise; diff --git a/queries/jobs/getJobs.ts b/queries/jobs/getJobs.ts index 44520ef..f65fc30 100644 --- a/queries/jobs/getJobs.ts +++ b/queries/jobs/getJobs.ts @@ -1,6 +1,6 @@ import { connect, - type mssqlTypes + type mssql } from '@cityssm/mssql-multi-pool' import NodeCache from 'node-cache' @@ -40,7 +40,7 @@ const cache = new NodeCache({ * @returns - The job, if available. */ export async function getJobByJobId( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, jobId: string ): Promise { let jobObject: Job | undefined = cache.get(jobId) @@ -55,7 +55,7 @@ export async function getJobByJobId( const jobResult = await pool .request() .input('jobId', jobId) - .query(`${sql} where Job_ID = @jobId`) as mssqlTypes.IResult + .query(`${sql} where Job_ID = @jobId`) as mssql.IResult if (jobResult.recordset.length === 0) { return undefined diff --git a/queries/jobs/getObjectCodes.d.ts b/queries/jobs/getObjectCodes.d.ts index 6ebf789..70a692f 100644 --- a/queries/jobs/getObjectCodes.d.ts +++ b/queries/jobs/getObjectCodes.d.ts @@ -1,4 +1,4 @@ -import { type mssqlTypes } from '@cityssm/mssql-multi-pool'; +import { type mssql } from '@cityssm/mssql-multi-pool'; import type { JobAssignedObjectCode, ObjectCode } from './types.js'; /** * Retrieves an object code. @@ -6,7 +6,7 @@ import type { JobAssignedObjectCode, ObjectCode } from './types.js'; * @param objectCode - The object code * @returns - The object code, if available. */ -export declare function getObjectCodeByObjectCode(mssqlConfig: mssqlTypes.config, objectCode: string): Promise; +export declare function getObjectCodeByObjectCode(mssqlConfig: mssql.config, objectCode: string): Promise; /** * Retrieves a list of object codes associated with a given job and fiscal year. * @param mssqlConfig - SQL Server configuration. @@ -14,7 +14,7 @@ export declare function getObjectCodeByObjectCode(mssqlConfig: mssqlTypes.config * @param fiscalYear - The fiscal year. * @returns - An array of object codes assigned to a given job. */ -export declare function getObjectCodesAssignedToJobByFiscalYear(mssqlConfig: mssqlTypes.config, jobId: string, fiscalYear: number | string): Promise; +export declare function getObjectCodesAssignedToJobByFiscalYear(mssqlConfig: mssql.config, jobId: string, fiscalYear: number | string): Promise; /** * Retrieves an object code associated with a given job and fiscal year. * @param mssqlConfig - SQL Server configuration. @@ -23,4 +23,4 @@ export declare function getObjectCodesAssignedToJobByFiscalYear(mssqlConfig: mss * @param fiscalYear - The fiscal year. * @returns - The object code, if available. */ -export declare function getObjectCodeAssignedToJobByObjectCodeAndFiscalYear(mssqlConfig: mssqlTypes.config, jobId: string, objectCode: string, fiscalYear: number | string): Promise; +export declare function getObjectCodeAssignedToJobByObjectCodeAndFiscalYear(mssqlConfig: mssql.config, jobId: string, objectCode: string, fiscalYear: number | string): Promise; diff --git a/queries/jobs/getObjectCodes.js b/queries/jobs/getObjectCodes.js index d35a099..de1e4be 100644 --- a/queries/jobs/getObjectCodes.js +++ b/queries/jobs/getObjectCodes.js @@ -1,5 +1,3 @@ -/* eslint-disable @eslint-community/eslint-comments/disable-enable-pair */ -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ import { connect } from '@cityssm/mssql-multi-pool'; import NodeCache from 'node-cache'; import { cacheTimeToLiveSeconds } from '../../apiConfig.js'; diff --git a/queries/jobs/getObjectCodes.ts b/queries/jobs/getObjectCodes.ts index 974c0cb..f77445c 100644 --- a/queries/jobs/getObjectCodes.ts +++ b/queries/jobs/getObjectCodes.ts @@ -1,7 +1,4 @@ -/* eslint-disable @eslint-community/eslint-comments/disable-enable-pair */ -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ - -import { connect, type mssqlTypes } from '@cityssm/mssql-multi-pool' +import { connect, type mssql } from '@cityssm/mssql-multi-pool' import NodeCache from 'node-cache' import { cacheTimeToLiveSeconds } from '../../apiConfig.js' @@ -25,7 +22,7 @@ const cache = new NodeCache({ * @returns - The object code, if available. */ export async function getObjectCodeByObjectCode( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, objectCode: string ): Promise { let objectCodeObject: ObjectCode | undefined = cache.get(objectCode) @@ -42,7 +39,7 @@ export async function getObjectCodeByObjectCode( .input('objectCode', objectCode) .query( `${sql} where CodeID = @objectCode` - )) as mssqlTypes.IResult + )) as mssql.IResult if (result.recordset.length === 0) { return undefined @@ -71,7 +68,7 @@ const jobAssignedSql = `SELECT o.[OCSysID] as objectCodeSystemId, * @returns - An array of object codes assigned to a given job. */ export async function getObjectCodesAssignedToJobByFiscalYear( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, jobId: string, fiscalYear: number | string ): Promise { @@ -84,7 +81,7 @@ export async function getObjectCodesAssignedToJobByFiscalYear( .input('fiscalYear', fiscalYear) .query( `${jobAssignedSql} where j.Job_ID = @jobId and j.Year = @fiscalYear` - )) as mssqlTypes.IResult + )) as mssql.IResult return result.recordset } @@ -98,7 +95,7 @@ export async function getObjectCodesAssignedToJobByFiscalYear( * @returns - The object code, if available. */ export async function getObjectCodeAssignedToJobByObjectCodeAndFiscalYear( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, jobId: string, objectCode: string, fiscalYear: number | string @@ -114,7 +111,7 @@ export async function getObjectCodeAssignedToJobByObjectCodeAndFiscalYear( .query( `${jobAssignedSql} where o.CodeID = @objectCode and j.Job_ID = @jobId and j.Year = @fiscalYear` - )) as mssqlTypes.IResult + )) as mssql.IResult if (result.recordset.length === 0) { return undefined diff --git a/queries/systemId.d.ts b/queries/systemId.d.ts index 015a309..37b0945 100644 --- a/queries/systemId.d.ts +++ b/queries/systemId.d.ts @@ -1,12 +1,12 @@ -import type { mssqlTypes } from '@cityssm/mssql-multi-pool'; +import type { mssql } from '@cityssm/mssql-multi-pool'; /** * Returns the last used system id. * @param transaction - An open database transaction. * @returns - The last used system id. */ -export declare function getLastSystemId(transaction: mssqlTypes.Transaction): Promise<`${number}` | undefined>; +export declare function getLastSystemId(transaction: mssql.Transaction): Promise<`${number}` | undefined>; /** * Increments the last used system id. * @param transaction - An open database transaction. */ -export declare function incrementLastSystemId(transaction: mssqlTypes.Transaction): Promise; +export declare function incrementLastSystemId(transaction: mssql.Transaction): Promise; diff --git a/queries/systemId.ts b/queries/systemId.ts index b8212d1..7dc8f39 100644 --- a/queries/systemId.ts +++ b/queries/systemId.ts @@ -1,7 +1,7 @@ // eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair /* eslint-disable @typescript-eslint/no-unsafe-assignment */ -import type { mssqlTypes } from '@cityssm/mssql-multi-pool' +import type { mssql } from '@cityssm/mssql-multi-pool' const tableId = 'SRISYSID' @@ -11,14 +11,14 @@ const tableId = 'SRISYSID' * @returns - The last used system id. */ export async function getLastSystemId( - transaction: mssqlTypes.Transaction + transaction: mssql.Transaction ): Promise<`${number}` | undefined> { const idResult = (await transaction .request() .input('tableId', tableId) .query( 'select Last_Id as systemId from AUTO_KEYS where Table_Id = @tableId' - )) as mssqlTypes.IResult<{ systemId: `${number}` }> + )) as mssql.IResult<{ systemId: `${number}` }> if (idResult.recordset.length > 0) { return idResult.recordset[0].systemId @@ -32,7 +32,7 @@ export async function getLastSystemId( * @param transaction - An open database transaction. */ export async function incrementLastSystemId( - transaction: mssqlTypes.Transaction + transaction: mssql.Transaction ): Promise { await transaction .request() diff --git a/queries/workOrders/addWorkOrderResource.d.ts b/queries/workOrders/addWorkOrderResource.d.ts index fb3e758..835e2c9 100644 --- a/queries/workOrders/addWorkOrderResource.d.ts +++ b/queries/workOrders/addWorkOrderResource.d.ts @@ -1,4 +1,4 @@ -import { type mssqlTypes } from '@cityssm/mssql-multi-pool'; +import { type mssql } from '@cityssm/mssql-multi-pool'; import type { BigIntString } from '../types.js'; import type { WorkOrderResource } from './types.js'; export interface AddWorkOrderResource extends Partial { @@ -11,4 +11,4 @@ export interface AddWorkOrderResource extends Partial { * @param workOrderResource - The work order resource fields. * @returns - The system id for the new resource record. */ -export declare function addWorkOrderResource(mssqlConfig: mssqlTypes.config, workOrderResource: AddWorkOrderResource): Promise; +export declare function addWorkOrderResource(mssqlConfig: mssql.config, workOrderResource: AddWorkOrderResource): Promise; diff --git a/queries/workOrders/addWorkOrderResource.ts b/queries/workOrders/addWorkOrderResource.ts index fc677de..91557ea 100644 --- a/queries/workOrders/addWorkOrderResource.ts +++ b/queries/workOrders/addWorkOrderResource.ts @@ -1,4 +1,4 @@ -import { connect, type mssqlTypes } from '@cityssm/mssql-multi-pool' +import { connect, type mssql } from '@cityssm/mssql-multi-pool' import { dateToString, dateToTimeString } from '@cityssm/utils-datetime' import { getItemByItemId } from '../items/getItems.js' @@ -20,7 +20,7 @@ export interface AddWorkOrderResource extends Partial { * @returns - The system id for the new resource record. */ export async function addWorkOrderResource( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, workOrderResource: AddWorkOrderResource ): Promise { /* diff --git a/queries/workOrders/deleteWorkOrderResource.d.ts b/queries/workOrders/deleteWorkOrderResource.d.ts index a173ca1..a14df42 100644 --- a/queries/workOrders/deleteWorkOrderResource.d.ts +++ b/queries/workOrders/deleteWorkOrderResource.d.ts @@ -1,4 +1,4 @@ -import { type mssqlTypes } from '@cityssm/mssql-multi-pool'; +import { type mssql } from '@cityssm/mssql-multi-pool'; import type { BigIntString } from '../types.js'; /** * Deletes a resource on a work order. @@ -6,4 +6,4 @@ import type { BigIntString } from '../types.js'; * @param serviceRequestItemSystemId - The work order resource id. * @returns - True when the delete is processed successfully. */ -export declare function deleteWorkOrderResource(mssqlConfig: mssqlTypes.config, serviceRequestItemSystemId: BigIntString): Promise; +export declare function deleteWorkOrderResource(mssqlConfig: mssql.config, serviceRequestItemSystemId: BigIntString): Promise; diff --git a/queries/workOrders/deleteWorkOrderResource.ts b/queries/workOrders/deleteWorkOrderResource.ts index fd27e7a..6b53503 100644 --- a/queries/workOrders/deleteWorkOrderResource.ts +++ b/queries/workOrders/deleteWorkOrderResource.ts @@ -1,4 +1,4 @@ -import { connect, type mssqlTypes } from '@cityssm/mssql-multi-pool' +import { connect, type mssql } from '@cityssm/mssql-multi-pool' import type { BigIntString } from '../types.js' @@ -9,7 +9,7 @@ import type { BigIntString } from '../types.js' * @returns - True when the delete is processed successfully. */ export async function deleteWorkOrderResource( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, serviceRequestItemSystemId: BigIntString ): Promise { const pool = await connect(mssqlConfig) diff --git a/queries/workOrders/getWorkOrderResources.d.ts b/queries/workOrders/getWorkOrderResources.d.ts index fa66089..b5711a0 100644 --- a/queries/workOrders/getWorkOrderResources.d.ts +++ b/queries/workOrders/getWorkOrderResources.d.ts @@ -1,4 +1,4 @@ -import { type mssqlTypes } from '@cityssm/mssql-multi-pool'; +import { type mssql } from '@cityssm/mssql-multi-pool'; import { type DateString } from '@cityssm/utils-datetime'; import type { WorkOrderResource } from './types.js'; /** @@ -7,7 +7,7 @@ import type { WorkOrderResource } from './types.js'; * @param workOrderNumber - The work order number. * @returns - An array of resources associated with a work order. */ -export declare function getWorkOrderResourcesByWorkOrderNumber(mssqlConfig: mssqlTypes.config, workOrderNumber: string): Promise; +export declare function getWorkOrderResourcesByWorkOrderNumber(mssqlConfig: mssql.config, workOrderNumber: string): Promise; /** * Retrieves a list of work order resources. * @param mssqlConfig - SQL Server configuration. @@ -15,11 +15,11 @@ export declare function getWorkOrderResourcesByWorkOrderNumber(mssqlConfig: mssq * @param startDateTimeTo - The maximum start date. * @returns - An array of resources between a given start time range. */ -export declare function getWorkOrderResourcesByStartDateTimeRange(mssqlConfig: mssqlTypes.config, startDateTimeFrom: Date | string, startDateTimeTo: Date | string): Promise; +export declare function getWorkOrderResourcesByStartDateTimeRange(mssqlConfig: mssql.config, startDateTimeFrom: Date | string, startDateTimeTo: Date | string): Promise; /** * Retrieves a list of work order resources. * @param mssqlConfig - SQL Server configuration. * @param startDateString - The start date. * @returns - An array of resources on a given start date. */ -export declare function getWorkOrderResourcesByStartDate(mssqlConfig: mssqlTypes.config, startDateString: DateString): Promise; +export declare function getWorkOrderResourcesByStartDate(mssqlConfig: mssql.config, startDateString: DateString): Promise; diff --git a/queries/workOrders/getWorkOrderResources.js b/queries/workOrders/getWorkOrderResources.js index 664a2b7..b358ffc 100644 --- a/queries/workOrders/getWorkOrderResources.js +++ b/queries/workOrders/getWorkOrderResources.js @@ -1,5 +1,3 @@ -// eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ import { connect } from '@cityssm/mssql-multi-pool'; import { dateToString, dateToTimeString } from '@cityssm/utils-datetime'; const sql = `SELECT [SRISysID] as serviceRequestItemSystemId, diff --git a/queries/workOrders/getWorkOrderResources.ts b/queries/workOrders/getWorkOrderResources.ts index f9d3909..720c1a2 100644 --- a/queries/workOrders/getWorkOrderResources.ts +++ b/queries/workOrders/getWorkOrderResources.ts @@ -1,9 +1,6 @@ -// eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ - import { connect, - type mssqlTypes + type mssql } from '@cityssm/mssql-multi-pool' import { type DateString, @@ -36,7 +33,7 @@ const sql = `SELECT [SRISysID] as serviceRequestItemSystemId, * @returns - An array of resources associated with a work order. */ export async function getWorkOrderResourcesByWorkOrderNumber( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, workOrderNumber: string ): Promise { // eslint-disable-next-line @typescript-eslint/no-unsafe-argument @@ -47,7 +44,7 @@ export async function getWorkOrderResourcesByWorkOrderNumber( .input('workOrderNumber', workOrderNumber) .query( `${sql} where WONOs = @workOrderNumber` - )) as mssqlTypes.IResult + )) as mssql.IResult return resourcesResult.recordset } @@ -60,7 +57,7 @@ export async function getWorkOrderResourcesByWorkOrderNumber( * @returns - An array of resources between a given start time range. */ export async function getWorkOrderResourcesByStartDateTimeRange( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, startDateTimeFrom: Date | string, startDateTimeTo: Date | string ): Promise { @@ -85,7 +82,7 @@ export async function getWorkOrderResourcesByStartDateTimeRange( .input('startDateTo', startDateToString) .query( `${sql} where SchedDateTime between @startDateFrom and @startDateTo` - )) as mssqlTypes.IResult + )) as mssql.IResult return resourcesResult.recordset } @@ -97,7 +94,7 @@ export async function getWorkOrderResourcesByStartDateTimeRange( * @returns - An array of resources on a given start date. */ export async function getWorkOrderResourcesByStartDate( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, startDateString: DateString ): Promise { return await getWorkOrderResourcesByStartDateTimeRange( diff --git a/queries/workOrders/getWorkOrders.d.ts b/queries/workOrders/getWorkOrders.d.ts index ecf8539..4e7fd5d 100644 --- a/queries/workOrders/getWorkOrders.d.ts +++ b/queries/workOrders/getWorkOrders.d.ts @@ -1,4 +1,4 @@ -import { type mssqlTypes } from '@cityssm/mssql-multi-pool'; +import { type mssql } from '@cityssm/mssql-multi-pool'; import type { WorkOrder } from './types.js'; /** * Retrieves a work order. @@ -6,4 +6,5 @@ import type { WorkOrder } from './types.js'; * @param workOrderNumber - The work order number. * @returns - The work order, if available. */ -export declare function getWorkOrderByWorkOrderNumber(mssqlConfig: mssqlTypes.config, workOrderNumber: string): Promise; +export declare function getWorkOrderByWorkOrderNumber(mssqlConfig: mssql.config, workOrderNumber: string): Promise; +export declare function _getWorkOrderByWorkOrderNumber(request: mssql.Request, workOrderNumber: string): Promise; diff --git a/queries/workOrders/getWorkOrders.js b/queries/workOrders/getWorkOrders.js index 7f8f08d..b20b33c 100644 --- a/queries/workOrders/getWorkOrders.js +++ b/queries/workOrders/getWorkOrders.js @@ -1,5 +1,3 @@ -// eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ import { connect } from '@cityssm/mssql-multi-pool'; import NodeCache from 'node-cache'; import { cacheTimeToLiveSeconds } from '../../apiConfig.js'; @@ -25,9 +23,11 @@ const sql = `SELECT [SRQISysID] as serviceRequestSystemId, coalesce([Details], '') as details, coalesce([Priority], '') as priority, + coalesce([Item_ID], '') as itemId, coalesce([ExJob_ID], '') as jobId, coalesce([Actv_ID], '') as activityId, coalesce([ObjCode], '') as objectCode, + coalesce([ServiceClass], '') as serviceClass, coalesce([ServiceType], '') as serviceType, coalesce([Year], '') as fiscalYear, @@ -57,14 +57,16 @@ const cache = new NodeCache({ * @returns - The work order, if available. */ export async function getWorkOrderByWorkOrderNumber(mssqlConfig, workOrderNumber) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + const pool = await connect(mssqlConfig); + return await _getWorkOrderByWorkOrderNumber(pool.request(), workOrderNumber); +} +export async function _getWorkOrderByWorkOrderNumber(request, workOrderNumber) { let workOrder = cache.get(workOrderNumber); if (workOrder !== undefined) { return workOrder; } - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - const pool = await connect(mssqlConfig); - const workOrderResult = (await pool - .request() + const workOrderResult = (await request .input('workOrderNumber', workOrderNumber) .query(`${sql} where WONOs = @workOrderNumber`)); if (workOrderResult.recordset.length === 0) { diff --git a/queries/workOrders/getWorkOrders.ts b/queries/workOrders/getWorkOrders.ts index ec76b78..aadccf4 100644 --- a/queries/workOrders/getWorkOrders.ts +++ b/queries/workOrders/getWorkOrders.ts @@ -1,7 +1,4 @@ -// eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ - -import { connect, type mssqlTypes } from '@cityssm/mssql-multi-pool' +import { connect, type mssql } from '@cityssm/mssql-multi-pool' import NodeCache from 'node-cache' import { cacheTimeToLiveSeconds } from '../../apiConfig.js' @@ -30,9 +27,11 @@ const sql = `SELECT [SRQISysID] as serviceRequestSystemId, coalesce([Details], '') as details, coalesce([Priority], '') as priority, + coalesce([Item_ID], '') as itemId, coalesce([ExJob_ID], '') as jobId, coalesce([Actv_ID], '') as activityId, coalesce([ObjCode], '') as objectCode, + coalesce([ServiceClass], '') as serviceClass, coalesce([ServiceType], '') as serviceType, coalesce([Year], '') as fiscalYear, @@ -64,7 +63,17 @@ const cache = new NodeCache({ * @returns - The work order, if available. */ export async function getWorkOrderByWorkOrderNumber( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, + workOrderNumber: string +): Promise { + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + const pool = await connect(mssqlConfig) + + return await _getWorkOrderByWorkOrderNumber(pool.request(), workOrderNumber) +} + +export async function _getWorkOrderByWorkOrderNumber( + request: mssql.Request, workOrderNumber: string ): Promise { let workOrder: WorkOrder | undefined = cache.get(workOrderNumber) @@ -73,15 +82,9 @@ export async function getWorkOrderByWorkOrderNumber( return workOrder } - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - const pool = await connect(mssqlConfig) - - const workOrderResult = (await pool - .request() + const workOrderResult = (await request .input('workOrderNumber', workOrderNumber) - .query( - `${sql} where WONOs = @workOrderNumber` - )) as mssqlTypes.IResult + .query(`${sql} where WONOs = @workOrderNumber`)) as mssql.IResult if (workOrderResult.recordset.length === 0) { return undefined diff --git a/queries/workOrders/types.d.ts b/queries/workOrders/types.d.ts index 1ce0b26..54d4deb 100644 --- a/queries/workOrders/types.d.ts +++ b/queries/workOrders/types.d.ts @@ -17,6 +17,7 @@ export interface WorkOrder { subject: string; details: string; priority: string; + itemId: string; jobId: string; activityId: string; objectCode: string; diff --git a/queries/workOrders/types.ts b/queries/workOrders/types.ts index a0087bb..fa4a47b 100644 --- a/queries/workOrders/types.ts +++ b/queries/workOrders/types.ts @@ -23,9 +23,11 @@ export interface WorkOrder { details: string priority: string + itemId: string jobId: string activityId: string objectCode: string + serviceClass: string serviceType: string fiscalYear: string diff --git a/queries/workOrders/updateWorkOrderResource.d.ts b/queries/workOrders/updateWorkOrderResource.d.ts index 4018543..70edb99 100644 --- a/queries/workOrders/updateWorkOrderResource.d.ts +++ b/queries/workOrders/updateWorkOrderResource.d.ts @@ -1,4 +1,4 @@ -import { type mssqlTypes } from '@cityssm/mssql-multi-pool'; +import { type mssql } from '@cityssm/mssql-multi-pool'; import type { BigIntString } from '../types.js'; import type { WorkOrderResource } from './types.js'; export interface UpdateWorkOrderResource extends Partial { @@ -17,4 +17,4 @@ export interface UpdateWorkOrderResource extends Partial { * @param workOrderResource - The work order resource fields. * @returns - True when the update is processed successfully. */ -export declare function updateWorkOrderResource(mssqlConfig: mssqlTypes.config, workOrderResource: UpdateWorkOrderResource): Promise; +export declare function updateWorkOrderResource(mssqlConfig: mssql.config, workOrderResource: UpdateWorkOrderResource): Promise; diff --git a/queries/workOrders/updateWorkOrderResource.ts b/queries/workOrders/updateWorkOrderResource.ts index 2cc4366..efc6eda 100644 --- a/queries/workOrders/updateWorkOrderResource.ts +++ b/queries/workOrders/updateWorkOrderResource.ts @@ -1,4 +1,4 @@ -import { connect, type mssqlTypes } from '@cityssm/mssql-multi-pool' +import { connect, type mssql } from '@cityssm/mssql-multi-pool' import { dateToString, dateToTimeString } from '@cityssm/utils-datetime' import type { BigIntString } from '../types.js' @@ -23,7 +23,7 @@ export interface UpdateWorkOrderResource extends Partial { * @returns - True when the update is processed successfully. */ export async function updateWorkOrderResource( - mssqlConfig: mssqlTypes.config, + mssqlConfig: mssql.config, workOrderResource: UpdateWorkOrderResource ): Promise { /* diff --git a/test/items.test.js b/test/items.test.js index f7e91e5..7bd9ee8 100644 --- a/test/items.test.js +++ b/test/items.test.js @@ -1,8 +1,8 @@ import assert from 'node:assert'; import { after, describe, it } from 'node:test'; import { releaseAll } from '@cityssm/mssql-multi-pool'; -import { addResourceItem, getItemByItemId } from '../index.js'; -import { invalidItemId, mssqlConfig, validItemId } from './config.js'; +import { addResourceItem, createStockTransactionBatch, getItemByItemId } from '../index.js'; +import { invalidItemId, mssqlConfig, stockTransactionBatch, validItemId } from './config.js'; await describe('queries/items', async () => { after(async () => { await releaseAll(); @@ -32,4 +32,8 @@ await describe('queries/items', async () => { assert.strictEqual(item.itemSystemId, itemSystemId); assert.strictEqual(item.itemId, newItemId); }); + await it('Creates a stock transactions batch', async () => { + const batchId = await createStockTransactionBatch(mssqlConfig, stockTransactionBatch); + assert(batchId !== undefined); + }); }); diff --git a/test/items.test.ts b/test/items.test.ts index 131d321..e7b715a 100644 --- a/test/items.test.ts +++ b/test/items.test.ts @@ -3,9 +3,18 @@ import { after, describe, it } from 'node:test' import { releaseAll } from '@cityssm/mssql-multi-pool' -import { addResourceItem, getItemByItemId } from '../index.js' - -import { invalidItemId, mssqlConfig, validItemId } from './config.js' +import { + addResourceItem, + createStockTransactionBatch, + getItemByItemId +} from '../index.js' + +import { + invalidItemId, + mssqlConfig, + stockTransactionBatch, + validItemId +} from './config.js' await describe('queries/items', async () => { after(async () => { @@ -27,9 +36,8 @@ await describe('queries/items', async () => { assert.strictEqual(item, undefined) }) - await it('Creates a new resource item', async() => { - - const newItemId = `TEST_${Math.round(Date.now()/1000)}` + await it('Creates a new resource item', async () => { + const newItemId = `TEST_${Math.round(Date.now() / 1000)}` let item = await getItemByItemId(mssqlConfig, newItemId) @@ -48,4 +56,13 @@ await describe('queries/items', async () => { assert.strictEqual(item.itemSystemId, itemSystemId) assert.strictEqual(item.itemId, newItemId) }) + + await it('Creates a stock transactions batch', async () => { + const batchId = await createStockTransactionBatch( + mssqlConfig, + stockTransactionBatch + ) + + assert(batchId !== undefined) + }) })