Skip to content

Commit

Permalink
update pending records in admin
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Dec 2, 2024
1 parent 0ca21ab commit 54e2d6c
Show file tree
Hide file tree
Showing 27 changed files with 739 additions and 68 deletions.
2 changes: 1 addition & 1 deletion modules/inventoryScanner/database/getScannerRecords.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default function getScannerRecords(filters, userOptions = {}) {
${sqlWhereClause}
order by s.scanDate desc, s.scanTime desc, s.recordId desc`;
if (options.limit !== -1) {
sql += `limit ${options.limit.toString()}`;
sql += ` limit ${options.limit.toString()}`;
}
/*
* Do Query
Expand Down
2 changes: 1 addition & 1 deletion modules/inventoryScanner/database/getScannerRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default function getScannerRecords(
order by s.scanDate desc, s.scanTime desc, s.recordId desc`

if (options.limit !== -1) {
sql += `limit ${options.limit.toString()}`
sql += ` limit ${options.limit.toString()}`
}

/*
Expand Down
12 changes: 9 additions & 3 deletions modules/inventoryScanner/database/updateScannerRecord.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
type ScannerRecordUpdateField = 'repairId' | 'itemStoreroom' | 'unitPrice';
export declare function updateScannerRecord(recordId: number, fieldToUpdate: ScannerRecordUpdateField, fieldValue: string | number, updateUserName: string): boolean;
export {};
export interface UpdateScannerRecordForm {
recordId: string;
workOrderNumber: string;
repairId: string;
itemNumber: string;
quantity: string;
unitPrice: string;
}
export declare function updateScannerRecord(recordForm: UpdateScannerRecordForm, updateUser: FasterWebHelperSessionUser): boolean;
10 changes: 7 additions & 3 deletions modules/inventoryScanner/database/updateScannerRecord.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import sqlite from 'better-sqlite3';
import { databasePath } from './helpers.database.js';
export function updateScannerRecord(recordId, fieldToUpdate, fieldValue, updateUserName) {
export function updateScannerRecord(recordForm, updateUser) {
const database = sqlite(databasePath);
const result = database
.prepare(`update InventoryScannerRecords
set ${fieldToUpdate} = ?,
set workOrderNumber = ?,
repairId = ?,
itemNumber = ?,
quantity = ?,
unitPrice = ?,
recordUpdate_userName = ?,
recordUpdate_timeMillis = ?
where recordId = ?
and recordDelete_timeMillis is null
and recordSync_timeMillis is null`)
.run(fieldValue, updateUserName, Date.now(), recordId);
.run(recordForm.workOrderNumber, recordForm.repairId === '' ? undefined : recordForm.repairId, recordForm.itemNumber, recordForm.quantity, recordForm.unitPrice === '' ? undefined : recordForm.unitPrice, updateUser.userName, Date.now(), recordForm.recordId);
database.close();
return result.changes > 0;
}
32 changes: 25 additions & 7 deletions modules/inventoryScanner/database/updateScannerRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,45 @@ import sqlite from 'better-sqlite3'

import { databasePath } from './helpers.database.js'

type ScannerRecordUpdateField = 'repairId' | 'itemStoreroom' | 'unitPrice'
export interface UpdateScannerRecordForm {
recordId: string
workOrderNumber: string
repairId: string
itemNumber: string
quantity: string
unitPrice: string
}

export function updateScannerRecord(
recordId: number,
fieldToUpdate: ScannerRecordUpdateField,
fieldValue: string | number,
updateUserName: string
recordForm: UpdateScannerRecordForm,
updateUser: FasterWebHelperSessionUser
): boolean {
const database = sqlite(databasePath)

const result = database
.prepare(
`update InventoryScannerRecords
set ${fieldToUpdate} = ?,
set workOrderNumber = ?,
repairId = ?,
itemNumber = ?,
quantity = ?,
unitPrice = ?,
recordUpdate_userName = ?,
recordUpdate_timeMillis = ?
where recordId = ?
and recordDelete_timeMillis is null
and recordSync_timeMillis is null`
)
.run(fieldValue, updateUserName, Date.now(), recordId)
.run(
recordForm.workOrderNumber,
recordForm.repairId === '' ? undefined : recordForm.repairId,
recordForm.itemNumber,
recordForm.quantity,
recordForm.unitPrice === '' ? undefined : recordForm.unitPrice,
updateUser.userName,
Date.now(),
recordForm.recordId
)

database.close()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type ScannerRecordUpdateField = 'repairId' | 'itemStoreroom' | 'unitPrice';
export declare function updateScannerRecordField(recordId: number, fieldToUpdate: ScannerRecordUpdateField, fieldValue: string | number, updateUserName: string): boolean;
export {};
16 changes: 16 additions & 0 deletions modules/inventoryScanner/database/updateScannerRecordField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import sqlite from 'better-sqlite3';
import { databasePath } from './helpers.database.js';
export function updateScannerRecordField(recordId, fieldToUpdate, fieldValue, updateUserName) {
const database = sqlite(databasePath);
const result = database
.prepare(`update InventoryScannerRecords
set ${fieldToUpdate} = ?,
recordUpdate_userName = ?,
recordUpdate_timeMillis = ?
where recordId = ?
and recordDelete_timeMillis is null
and recordSync_timeMillis is null`)
.run(fieldValue, updateUserName, Date.now(), recordId);
database.close();
return result.changes > 0;
}
30 changes: 30 additions & 0 deletions modules/inventoryScanner/database/updateScannerRecordField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sqlite from 'better-sqlite3'

import { databasePath } from './helpers.database.js'

type ScannerRecordUpdateField = 'repairId' | 'itemStoreroom' | 'unitPrice'

export function updateScannerRecordField(
recordId: number,
fieldToUpdate: ScannerRecordUpdateField,
fieldValue: string | number,
updateUserName: string
): boolean {
const database = sqlite(databasePath)

const result = database
.prepare(
`update InventoryScannerRecords
set ${fieldToUpdate} = ?,
recordUpdate_userName = ?,
recordUpdate_timeMillis = ?
where recordId = ?
and recordDelete_timeMillis is null
and recordSync_timeMillis is null`
)
.run(fieldValue, updateUserName, Date.now(), recordId)

database.close()

return result.changes > 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Request, Response } from 'express';
interface DoGetRepairIdsForm {
workOrderNumber: string;
}
export default function handler(request: Request<unknown, unknown, DoGetRepairIdsForm>, response: Response): void;
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import getWorkOrderValidationRecords from '../../database/getWorkOrderValidationRecords.js';
import { getWorkOrderTypeFromWorkOrderNumber } from '../../helpers/workOrders.js';
export default function handler(request, response) {
const workOrderType = getWorkOrderTypeFromWorkOrderNumber(request.body.workOrderNumber);
const records = getWorkOrderValidationRecords(request.body.workOrderNumber, workOrderType);
response.json({ records });
}
24 changes: 24 additions & 0 deletions modules/inventoryScanner/handlers/post-admin/doGetRepairRecords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { Request, Response } from 'express'

import getWorkOrderValidationRecords from '../../database/getWorkOrderValidationRecords.js'
import { getWorkOrderTypeFromWorkOrderNumber } from '../../helpers/workOrders.js'

interface DoGetRepairIdsForm {
workOrderNumber: string
}

export default function handler(
request: Request<unknown, unknown, DoGetRepairIdsForm>,
response: Response
): void {
const workOrderType = getWorkOrderTypeFromWorkOrderNumber(
request.body.workOrderNumber
)

const records = getWorkOrderValidationRecords(
request.body.workOrderNumber,
workOrderType
)

response.json({ records })
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Request, Response } from 'express';
import { type UpdateScannerRecordForm } from '../../database/updateScannerRecord.js';
export default function handler(request: Request<unknown, unknown, UpdateScannerRecordForm>, response: Response): void;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import getScannerRecords from '../../database/getScannerRecords.js';
import { updateScannerRecord } from '../../database/updateScannerRecord.js';
export default function handler(request, response) {
const success = updateScannerRecord(request.body, request.session.user);
const pendingRecords = getScannerRecords({ isSynced: false }, { limit: -1 });
response.json({ success, pendingRecords });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Request, Response } from 'express'

import getScannerRecords from '../../database/getScannerRecords.js'
import {
type UpdateScannerRecordForm,
updateScannerRecord
} from '../../database/updateScannerRecord.js'

export default function handler(
request: Request<unknown, unknown, UpdateScannerRecordForm>,
response: Response
): void {
const success = updateScannerRecord(
request.body,
request.session.user as FasterWebHelperSessionUser
)

const pendingRecords = getScannerRecords({ isSynced: false }, { limit: -1 })

response.json({ success, pendingRecords })
}
4 changes: 4 additions & 0 deletions modules/inventoryScanner/handlers/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import { Router } from 'express';
import handler_inventoryScanner from './get-admin/inventoryScanner.js';
import handler_doGetInventory from './post-admin/doGetInventory.js';
import handler_doGetPendingRecords from './post-admin/doGetPendingRecords.js';
import handler_doGetRepairRecords from './post-admin/doGetRepairRecords.js';
import handler_doUpdatePendingRecord from './post-admin/doUpdatePendingRecord.js';
export const router = Router();
router.get('/', handler_inventoryScanner);
router.post('/doGetPendingRecords', handler_doGetPendingRecords);
router.post('/doGetRepairRecords', handler_doGetRepairRecords);
router.post('/doUpdatePendingRecord', handler_doUpdatePendingRecord);
router.post('/doGetInventory', handler_doGetInventory);
export default router;
6 changes: 6 additions & 0 deletions modules/inventoryScanner/handlers/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ import { Router } from 'express'
import handler_inventoryScanner from './get-admin/inventoryScanner.js'
import handler_doGetInventory from './post-admin/doGetInventory.js'
import handler_doGetPendingRecords from './post-admin/doGetPendingRecords.js'
import handler_doGetRepairRecords from './post-admin/doGetRepairRecords.js'
import handler_doUpdatePendingRecord from './post-admin/doUpdatePendingRecord.js'

export const router = Router()

router.get('/', handler_inventoryScanner)

router.post('/doGetPendingRecords', handler_doGetPendingRecords)

router.post('/doGetRepairRecords', handler_doGetRepairRecords)

router.post('/doUpdatePendingRecord', handler_doUpdatePendingRecord)

router.post('/doGetInventory', handler_doGetInventory)

export default router
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import schedule from 'node-schedule';
import { getItemValidationRecordsByItemNumber } from '../../database/getItemValidationRecords.js';
import getScannerRecords from '../../database/getScannerRecords.js';
import getWorkOrderValidationRecords from '../../database/getWorkOrderValidationRecords.js';
import { updateScannerRecord } from '../../database/updateScannerRecord.js';
import { updateScannerRecordField } from '../../database/updateScannerRecordField.js';
import { moduleName } from '../../helpers/module.js';
const minimumMillisBetweenRuns = minutesToMillis(2);
const lastRunMillis = 0;
Expand All @@ -30,7 +30,7 @@ function updateRecordsFromValidationTask() {
if (workOrderValidationRecords.length > 0 && record.repairId === null) {
for (const workOrderValidationRecord of workOrderValidationRecords) {
if (workOrderValidationRecord.repairId !== null) {
updateScannerRecord(record.recordId, 'repairId', workOrderValidationRecord.repairId, taskUserName);
updateScannerRecordField(record.recordId, 'repairId', workOrderValidationRecord.repairId, taskUserName);
break;
}
}
Expand All @@ -40,10 +40,10 @@ function updateRecordsFromValidationTask() {
: [];
if (itemValidationRecords.length > 0) {
if (record.itemStoreroom === null) {
updateScannerRecord(record.recordId, 'itemStoreroom', itemValidationRecords[0].itemStoreroom, taskUserName);
updateScannerRecordField(record.recordId, 'itemStoreroom', itemValidationRecords[0].itemStoreroom, taskUserName);
}
if (record.unitPrice === null) {
updateScannerRecord(record.recordId, 'unitPrice', itemValidationRecords[0].unitPrice, taskUserName);
updateScannerRecordField(record.recordId, 'unitPrice', itemValidationRecords[0].unitPrice, taskUserName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import schedule from 'node-schedule'
import { getItemValidationRecordsByItemNumber } from '../../database/getItemValidationRecords.js'
import getScannerRecords from '../../database/getScannerRecords.js'
import getWorkOrderValidationRecords from '../../database/getWorkOrderValidationRecords.js'
import { updateScannerRecord } from '../../database/updateScannerRecord.js'
import { updateScannerRecordField } from '../../database/updateScannerRecordField.js'
import { moduleName } from '../../helpers/module.js'

const minimumMillisBetweenRuns = minutesToMillis(2)
Expand Down Expand Up @@ -45,7 +45,7 @@ function updateRecordsFromValidationTask(): void {
if (workOrderValidationRecords.length > 0 && record.repairId === null) {
for (const workOrderValidationRecord of workOrderValidationRecords) {
if (workOrderValidationRecord.repairId !== null) {
updateScannerRecord(
updateScannerRecordField(
record.recordId,
'repairId',
workOrderValidationRecord.repairId,
Expand All @@ -63,7 +63,7 @@ function updateRecordsFromValidationTask(): void {

if (itemValidationRecords.length > 0) {
if (record.itemStoreroom === null) {
updateScannerRecord(
updateScannerRecordField(
record.recordId,
'itemStoreroom',
itemValidationRecords[0].itemStoreroom,
Expand All @@ -72,7 +72,7 @@ function updateRecordsFromValidationTask(): void {
}

if (record.unitPrice === null) {
updateScannerRecord(
updateScannerRecordField(
record.recordId,
'unitPrice',
itemValidationRecords[0].unitPrice,
Expand Down
3 changes: 2 additions & 1 deletion modules/inventoryScanner/types.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Loading

0 comments on commit 54e2d6c

Please sign in to comment.