Skip to content

Commit

Permalink
option to prevent run on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Jul 18, 2024
1 parent 37b848e commit fd7c968
Show file tree
Hide file tree
Showing 20 changed files with 126 additions and 62 deletions.
7 changes: 4 additions & 3 deletions data/config.defaultValues.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ export declare const configDefaultValues: {
'modules.inventoryScanner.reports.w311': ConfigScheduledFtpReport<ConfigFileSuffixXlsx>;
'modules.inventoryScanner.reports.w604': ConfigScheduledFtpReport<ConfigFileSuffixXlsx>;
'modules.worktechUpdate.isEnabled': boolean;
'modules.resourceItem.itemClass': string;
'modules.resourceItem.itemType': string;
'modules.resourceItem.unit': string;
'modules.worktechUpdate.runOnStartup': boolean;
'modules.worktechUpdate.resourceItem.itemClass': string;
'modules.worktechUpdate.resourceItem.itemType': string;
'modules.worktechUpdate.resourceItem.unit': string;
'modules.worktechUpdate.reports.w217': ConfigScheduledFtpReport<ConfigFileSuffixXlsx>;
'modules.worktechUpdate.reports.w223': ConfigScheduledFtpReport<ConfigFileSuffixXlsx>;
};
7 changes: 4 additions & 3 deletions data/config.defaultValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export const configDefaultValues = {
* Worktech Update
*/
'modules.worktechUpdate.isEnabled': false,
'modules.resourceItem.itemClass': 'FASTER',
'modules.resourceItem.itemType': 'FASTER',
'modules.resourceItem.unit': 'EA',
'modules.worktechUpdate.runOnStartup': true,
'modules.worktechUpdate.resourceItem.itemClass': 'FASTER',
'modules.worktechUpdate.resourceItem.itemType': 'FASTER',
'modules.worktechUpdate.resourceItem.unit': 'EA',
'modules.worktechUpdate.reports.w217': undefined,
'modules.worktechUpdate.reports.w223': undefined
};
7 changes: 4 additions & 3 deletions data/config.defaultValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ export const configDefaultValues = {
*/

'modules.worktechUpdate.isEnabled': false,
'modules.worktechUpdate.runOnStartup': true,

'modules.resourceItem.itemClass': 'FASTER',
'modules.resourceItem.itemType': 'FASTER',
'modules.resourceItem.unit': 'EA',
'modules.worktechUpdate.resourceItem.itemClass': 'FASTER',
'modules.worktechUpdate.resourceItem.itemType': 'FASTER',
'modules.worktechUpdate.resourceItem.unit': 'EA',

'modules.worktechUpdate.reports.w217':
undefined as unknown as ConfigScheduledFtpReport<ConfigFileSuffixXlsx>,
Expand Down
1 change: 1 addition & 0 deletions modules/worktechUpdate/helpers/moduleHelpers.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const moduleName = "Worktech Update Module";
1 change: 1 addition & 0 deletions modules/worktechUpdate/helpers/moduleHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const moduleName = 'Worktech Update Module';
1 change: 1 addition & 0 deletions modules/worktechUpdate/helpers/moduleHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const moduleName = 'Worktech Update Module'
6 changes: 3 additions & 3 deletions modules/worktechUpdate/helpers/worktechHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export async function getOrCreateStoreroomResourceItem(storeroomData) {
itemId: storeroomItemId,
externalItemId: storeroomData.storeroom,
itemDescription: storeroomData.storeroomDescription,
itemClass: getConfigProperty('modules.resourceItem.itemClass'),
itemType: getConfigProperty('modules.resourceItem.itemType'),
itemClass: getConfigProperty('modules.worktechUpdate.resourceItem.itemClass'),
itemType: getConfigProperty('modules.worktechUpdate.resourceItem.itemType'),
stock: 0,
quantityOnHand: 0,
unit: getConfigProperty('modules.resourceItem.unit'),
unit: getConfigProperty('modules.worktechUpdate.resourceItem.unit'),
unitCost: 0,
});
worktechStoreroomResourceItem = (await worktech.getItemByItemId(storeroomItemId));
Expand Down
6 changes: 3 additions & 3 deletions modules/worktechUpdate/helpers/worktechHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ export async function getOrCreateStoreroomResourceItem(
itemId: storeroomItemId,
externalItemId: storeroomData.storeroom,
itemDescription: storeroomData.storeroomDescription,
itemClass: getConfigProperty('modules.resourceItem.itemClass'),
itemType: getConfigProperty('modules.resourceItem.itemType'),
itemClass: getConfigProperty('modules.worktechUpdate.resourceItem.itemClass'),
itemType: getConfigProperty('modules.worktechUpdate.resourceItem.itemType'),
stock: 0,
quantityOnHand: 0,
unit: getConfigProperty('modules.resourceItem.unit'),
unit: getConfigProperty('modules.worktechUpdate.resourceItem.unit'),
unitCost: 0,
})

Expand Down
39 changes: 24 additions & 15 deletions modules/worktechUpdate/initializeWorktechUpdateModule.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
import { dateToString, dateToTimePeriodString } from '@cityssm/utils-datetime';
import camelCase from 'camelcase';
import Debug from 'debug';
import exitHook from 'exit-hook';
import schedule from 'node-schedule';
import { getConfigProperty } from '../../helpers/functions.config.js';
import { initializeWorktechUpdateDatabase } from './database/databaseHelpers.js';
import directChargeHelperTask, { taskName as directChangeHelperTaskName } from './tasks/directChargeHelperTask.js';
import inventoryTransactionsTask from './tasks/inventoryTransactionsTask.js';
const debug = Debug('faster-web-helper:worktechUpdate');
import { moduleName } from './helpers/moduleHelpers.js';
import directChargeHelperTask, { taskName as directChargeHelperTaskName } from './tasks/directChargeHelperTask.js';
import inventoryTransactionsTask, { taskName as inventoryTransactionsTaskName } from './tasks/inventoryTransactionsTask.js';
const debug = Debug(`faster-web-helper:${camelCase(moduleName)}`);
const directChargeTransactionsConfig = getConfigProperty('modules.worktechUpdate.reports.w217');
const inventoryTransactionsConfig = getConfigProperty('modules.worktechUpdate.reports.w223');
export default async function initializeWorktechUpdateModule() {
debug('Initializing "Worktech Update Module"...');
debug(`Initializing "${moduleName}"...`);
/*
* Ensure the local database is available.
*/
initializeWorktechUpdateDatabase();
/*
* Direct Charge Helper Task
* Run on startup
*/
debug('Running "Direct Charge Helper Task" on startup...');
await directChargeHelperTask();
debug('Scheduling "Direct Charge Helper Task"...');
const directChargeHelperJob = schedule.scheduleJob(directChangeHelperTaskName, directChargeTransactionsConfig.schedule, directChargeHelperTask);
if (getConfigProperty('modules.worktechUpdate.runOnStartup')) {
debug(`Running "${directChargeHelperTaskName}" on startup...`);
await directChargeHelperTask();
debug(`Running "${inventoryTransactionsTaskName}" on startup...`);
await inventoryTransactionsTask();
}
/*
* Inventory Transactions Task
* Schedule jobs
*/
debug('Running "Inventory Transactions Task" on startup...');
await inventoryTransactionsTask();
debug('Scheduling "Inventory Transactions Task"...');
const inventoryTransactionsJob = schedule.scheduleJob('inventoryTransactionsTask', inventoryTransactionsConfig.schedule, inventoryTransactionsTask);
debug(`Scheduling "${directChargeHelperTaskName}"...`);
const directChargeHelperJob = schedule.scheduleJob(directChargeHelperTaskName, directChargeTransactionsConfig.schedule, directChargeHelperTask);
const directChargeHelperFirstRunDate = new Date(directChargeHelperJob.nextInvocation().getTime());
debug(`Scheduled to run "${directChargeHelperTaskName}" on ${dateToString(directChargeHelperFirstRunDate)} at ${dateToTimePeriodString(directChargeHelperFirstRunDate)}`);
debug(`Scheduling "${inventoryTransactionsTaskName}"...`);
const inventoryTransactionsJob = schedule.scheduleJob(inventoryTransactionsTaskName, inventoryTransactionsConfig.schedule, inventoryTransactionsTask);
const inventoryTransactionsFirstRunDate = new Date(inventoryTransactionsJob.nextInvocation().getTime());
debug(`Scheduled to run "${inventoryTransactionsTaskName}" on ${dateToString(inventoryTransactionsFirstRunDate)} at ${dateToTimePeriodString(inventoryTransactionsFirstRunDate)}`);
/*
* Set up exit hook
*/
Expand All @@ -36,5 +45,5 @@ export default async function initializeWorktechUpdateModule() {
directChargeHelperJob.cancel();
inventoryTransactionsJob.cancel();
});
debug('"Worktech Update Module" initialized.');
debug(`"${moduleName}" initialized.`);
}
57 changes: 40 additions & 17 deletions modules/worktechUpdate/initializeWorktechUpdateModule.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { dateToString, dateToTimePeriodString } from '@cityssm/utils-datetime'
import camelCase from 'camelcase'
import Debug from 'debug'
import exitHook from 'exit-hook'
import schedule from 'node-schedule'

import { getConfigProperty } from '../../helpers/functions.config.js'

import { initializeWorktechUpdateDatabase } from './database/databaseHelpers.js'
import { moduleName } from './helpers/moduleHelpers.js'
import directChargeHelperTask, {
taskName as directChangeHelperTaskName
taskName as directChargeHelperTaskName
} from './tasks/directChargeHelperTask.js'
import inventoryTransactionsTask from './tasks/inventoryTransactionsTask.js'
import inventoryTransactionsTask, {
taskName as inventoryTransactionsTaskName
} from './tasks/inventoryTransactionsTask.js'

const debug = Debug('faster-web-helper:worktechUpdate')
const debug = Debug(`faster-web-helper:${camelCase(moduleName)}`)

const directChargeTransactionsConfig = getConfigProperty(
'modules.worktechUpdate.reports.w217'
Expand All @@ -21,7 +26,7 @@ const inventoryTransactionsConfig = getConfigProperty(
)

export default async function initializeWorktechUpdateModule(): Promise<void> {
debug('Initializing "Worktech Update Module"...')
debug(`Initializing "${moduleName}"...`)

/*
* Ensure the local database is available.
Expand All @@ -30,33 +35,51 @@ export default async function initializeWorktechUpdateModule(): Promise<void> {
initializeWorktechUpdateDatabase()

/*
* Direct Charge Helper Task
* Run on startup
*/

debug('Running "Direct Charge Helper Task" on startup...')
await directChargeHelperTask()
if (getConfigProperty('modules.worktechUpdate.runOnStartup')) {
debug(`Running "${directChargeHelperTaskName}" on startup...`)
await directChargeHelperTask()

debug('Scheduling "Direct Charge Helper Task"...')
debug(`Running "${inventoryTransactionsTaskName}" on startup...`)
await inventoryTransactionsTask()
}

/*
* Schedule jobs
*/

debug(`Scheduling "${directChargeHelperTaskName}"...`)
const directChargeHelperJob = schedule.scheduleJob(
directChangeHelperTaskName,
directChargeHelperTaskName,
directChargeTransactionsConfig.schedule,
directChargeHelperTask
)

/*
* Inventory Transactions Task
*/
const directChargeHelperFirstRunDate = new Date(
directChargeHelperJob.nextInvocation().getTime()
)

debug('Running "Inventory Transactions Task" on startup...')
await inventoryTransactionsTask()
debug(
`Scheduled to run "${directChargeHelperTaskName}" on ${dateToString(directChargeHelperFirstRunDate)} at ${dateToTimePeriodString(directChargeHelperFirstRunDate)}`
)

debug('Scheduling "Inventory Transactions Task"...')
debug(`Scheduling "${inventoryTransactionsTaskName}"...`)
const inventoryTransactionsJob = schedule.scheduleJob(
'inventoryTransactionsTask',
inventoryTransactionsTaskName,
inventoryTransactionsConfig.schedule,
inventoryTransactionsTask
)

const inventoryTransactionsFirstRunDate = new Date(
inventoryTransactionsJob.nextInvocation().getTime()
)

debug(
`Scheduled to run "${inventoryTransactionsTaskName}" on ${dateToString(inventoryTransactionsFirstRunDate)} at ${dateToTimePeriodString(inventoryTransactionsFirstRunDate)}`
)

/*
* Set up exit hook
*/
Expand All @@ -67,5 +90,5 @@ export default async function initializeWorktechUpdateModule(): Promise<void> {
inventoryTransactionsJob.cancel()
})

debug('"Worktech Update Module" initialized.')
debug(`"${moduleName}" initialized.`)
}
2 changes: 1 addition & 1 deletion modules/worktechUpdate/tasks/directChargeHelperTask.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export declare const taskName = "directChangeHelperTask";
export declare const taskName = "Direct Change Helper Task";
/**
* - Maintains mappings between Faster document numbers and Worktech work order numbers.
* - Tracks "Return to Vendor" transactions.
Expand Down
6 changes: 4 additions & 2 deletions modules/worktechUpdate/tasks/directChargeHelperTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'node:fs/promises';
import { parseW217ExcelReport } from '@cityssm/faster-api/xlsxReports.js';
import { dateStringToInteger, timeStringToInteger } from '@cityssm/utils-datetime';
import { WorkTechAPI } from '@cityssm/worktech-api';
import camelCase from 'camelcase';
import Debug from 'debug';
import { getConfigProperty } from '../../../helpers/functions.config.js';
import { downloadFilesToTemp } from '../../../helpers/functions.sftp.js';
Expand All @@ -10,8 +11,9 @@ import addWorkOrderNumberMapping from '../database/addWorkOrderNumberMapping.js'
import getReturnToVendorRecord from '../database/getReturnToVendorRecord.js';
import getWorkOrderNumberMapping from '../database/getWorkOrderNumberMapping.js';
import updateWorkOrderNumberMapping from '../database/updateWorkOrderNumberMapping.js';
export const taskName = 'directChangeHelperTask';
const debug = Debug(`faster-web-helper:worktechUpdate:${taskName}`);
import { moduleName } from '../helpers/moduleHelpers.js';
export const taskName = 'Direct Change Helper Task';
const debug = Debug(`faster-web-helper:${camelCase(moduleName)}:${camelCase(taskName)}`);
const worktech = new WorkTechAPI(getConfigProperty('worktech'));
const directChargeTransactionsConfig = getConfigProperty('modules.worktechUpdate.reports.w217');
async function _updateWorkOrderNumberMappings(report, data) {
Expand Down
6 changes: 4 additions & 2 deletions modules/worktechUpdate/tasks/directChargeHelperTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
timeStringToInteger
} from '@cityssm/utils-datetime'
import { WorkTechAPI } from '@cityssm/worktech-api'
import camelCase from 'camelcase'
import Debug from 'debug'

import { getConfigProperty } from '../../../helpers/functions.config.js'
Expand All @@ -19,11 +20,12 @@ import addWorkOrderNumberMapping from '../database/addWorkOrderNumberMapping.js'
import getReturnToVendorRecord from '../database/getReturnToVendorRecord.js'
import getWorkOrderNumberMapping from '../database/getWorkOrderNumberMapping.js'
import updateWorkOrderNumberMapping from '../database/updateWorkOrderNumberMapping.js'
import { moduleName } from '../helpers/moduleHelpers.js'
import type { ReturnToVendorRecord } from '../worktechUpdateTypes.js'

export const taskName = 'directChangeHelperTask'
export const taskName = 'Direct Change Helper Task'

const debug = Debug(`faster-web-helper:worktechUpdate:${taskName}`)
const debug = Debug(`faster-web-helper:${camelCase(moduleName)}:${camelCase(taskName)}`)

const worktech = new WorkTechAPI(getConfigProperty('worktech'))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export declare const taskName = "inventoryTransactionsTask";
export declare const taskName = "Inventory Transactions Task";
export default function runInventoryTransactionsTask(): Promise<void>;
8 changes: 5 additions & 3 deletions modules/worktechUpdate/tasks/inventoryTransactionsTask.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { parseW223ExcelReport } from '@cityssm/faster-api/xlsxReports.js';
import { dateStringToInteger } from '@cityssm/utils-datetime';
import { WorkTechAPI } from '@cityssm/worktech-api';
import camelCase from 'camelcase';
import Debug from 'debug';
import { getConfigProperty } from '../../../helpers/functions.config.js';
import { downloadFilesToTemp } from '../../../helpers/functions.sftp.js';
import getReturnToVendorRecord from '../database/getReturnToVendorRecord.js';
import getWorkOrderNumberMapping from '../database/getWorkOrderNumberMapping.js';
import { moduleName } from '../helpers/moduleHelpers.js';
import { buildWorkOrderResourceDescriptionHash, getOrCreateStoreroomResourceItem, getWorkOrderResources } from '../helpers/worktechHelpers.js';
export const taskName = 'inventoryTransactionsTask';
const debug = Debug(`faster-web-helper:worktechUpdate:${taskName}`);
export const taskName = 'Inventory Transactions Task';
const debug = Debug(`faster-web-helper:${camelCase(moduleName)}:${camelCase(taskName)}`);
const worktech = new WorkTechAPI(getConfigProperty('worktech'));
const inventoryTransactionsConfig = getConfigProperty('modules.worktechUpdate.reports.w223');
export default async function runInventoryTransactionsTask() {
Expand Down Expand Up @@ -119,7 +121,7 @@ export default async function runInventoryTransactionsTask() {
.filter((possibleResourceItem) => {
return (possibleResourceItem.itemId ===
worktechStoreroomResourceItem.itemId &&
possibleResourceItem.workDescription.includes(transactionData.itemNumber) === true &&
possibleResourceItem.workDescription.includes(transactionData.itemNumber) &&
possibleResourceItem.quantity > 0 &&
possibleResourceItem.unitPrice ===
transactionData.unitTrueCost);
Expand Down
8 changes: 5 additions & 3 deletions modules/worktechUpdate/tasks/inventoryTransactionsTask.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { parseW223ExcelReport } from '@cityssm/faster-api/xlsxReports.js'
import { type DateString, dateStringToInteger } from '@cityssm/utils-datetime'
import { WorkTechAPI } from '@cityssm/worktech-api'
import camelCase from 'camelcase'
import Debug from 'debug'

import { getConfigProperty } from '../../../helpers/functions.config.js'
import { downloadFilesToTemp } from '../../../helpers/functions.sftp.js'
import getReturnToVendorRecord from '../database/getReturnToVendorRecord.js'
import getWorkOrderNumberMapping from '../database/getWorkOrderNumberMapping.js'
import { moduleName } from '../helpers/moduleHelpers.js'
import {
type W223HashableTransactionReportData,
buildWorkOrderResourceDescriptionHash,
getOrCreateStoreroomResourceItem,
getWorkOrderResources
} from '../helpers/worktechHelpers.js'

export const taskName = 'inventoryTransactionsTask'
export const taskName = 'Inventory Transactions Task'

const debug = Debug(`faster-web-helper:worktechUpdate:${taskName}`)
const debug = Debug(`faster-web-helper:${camelCase(moduleName)}:${camelCase(taskName)}`)

const worktech = new WorkTechAPI(getConfigProperty('worktech'))

Expand Down Expand Up @@ -198,7 +200,7 @@ export default async function runInventoryTransactionsTask(): Promise<void> {
worktechStoreroomResourceItem.itemId &&
possibleResourceItem.workDescription.includes(
transactionData.itemNumber
) === true &&
) &&
possibleResourceItem.quantity > 0 &&
possibleResourceItem.unitPrice ===
transactionData.unitTrueCost
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@cityssm/worktech-api": "^0.3.2",
"basic-ftp": "^5.0.5",
"better-sqlite3": "^11.1.2",
"camelcase": "^8.0.0",
"debug": "^4.3.5",
"exit-hook": "^4.0.0",
"node-schedule": "^2.1.1"
Expand Down
Loading

0 comments on commit fd7c968

Please sign in to comment.