Added ref spotlight lookup.

This commit is contained in:
Tom Butcher 2025-12-03 13:11:53 +00:00
parent 944ad34f89
commit 7a947ed2fd
2 changed files with 79 additions and 30 deletions

View File

@ -26,32 +26,67 @@ import { courierModel } from '../../schemas/management/courier.schema.js';
// Map prefixes to models and id fields // Map prefixes to models and id fields
const PREFIX_MODEL_MAP = { const PREFIX_MODEL_MAP = {
PRN: { model: printerModel, idField: '_id', type: 'printer' }, PRN: { model: printerModel, idField: '_id', type: 'printer', referenceField: '_reference' },
FIL: { model: filamentModel, idField: '_id', type: 'filament' }, FIL: { model: filamentModel, idField: '_id', type: 'filament', referenceField: '_reference' },
GCF: { model: gcodeFileModel, idField: '_id', type: 'gcodeFile' }, GCF: { model: gcodeFileModel, idField: '_id', type: 'gcodeFile', referenceField: '_reference' },
JOB: { model: jobModel, idField: '_id', type: 'job' }, JOB: { model: jobModel, idField: '_id', type: 'job', referenceField: '_reference' },
PRT: { model: partModel, idField: '_id', type: 'part' }, PRT: { model: partModel, idField: '_id', type: 'part', referenceField: '_reference' },
PRD: { model: productModel, idField: '_id', type: 'product' }, PRD: { model: productModel, idField: '_id', type: 'product', referenceField: '_reference' },
VEN: { model: vendorModel, idField: '_id', type: 'vendor' }, VEN: { model: vendorModel, idField: '_id', type: 'vendor', referenceField: '_reference' },
SJB: { model: subJobModel, idField: '_id', type: 'subJob' }, SJB: { model: subJobModel, idField: '_id', type: 'subJob', referenceField: '_reference' },
FLS: { model: filamentStockModel, idField: '_id', type: 'filamentStock' }, FLS: {
SEV: { model: stockEventModel, idField: '_id', type: 'stockEvent' }, model: filamentStockModel,
SAU: { model: stockAuditModel, idField: '_id', type: 'stockAudit' }, idField: '_id',
PTS: { model: partStockModel, idField: '_id', type: 'partStock' }, type: 'filamentStock',
PDS: { model: null, idField: '_id', type: 'productStock' }, // No productStockModel found referenceField: '_reference',
ADL: { model: auditLogModel, idField: '_id', type: 'auditLog' }, },
USR: { model: userModel, idField: '_id', type: 'user' }, SEV: { model: stockEventModel, idField: '_id', type: 'stockEvent', referenceField: '_reference' },
NTY: { model: noteTypeModel, idField: '_id', type: 'noteType' }, SAU: { model: stockAuditModel, idField: '_id', type: 'stockAudit', referenceField: '_reference' },
NTE: { model: noteModel, idField: '_id', type: 'note' }, PTS: { model: partStockModel, idField: '_id', type: 'partStock', referenceField: '_reference' },
DSZ: { model: documentSizeModel, idField: '_id', type: 'documentSize' }, PDS: { model: null, idField: '_id', type: 'productStock', referenceField: '_reference' }, // No productStockModel found
DTP: { model: documentTemplateModel, idField: '_id', type: 'documentTemplate' }, ADL: { model: auditLogModel, idField: '_id', type: 'auditLog', referenceField: '_reference' },
DPR: { model: documentPrinterModel, idField: '_id', type: 'documentPrinter' }, USR: { model: userModel, idField: '_id', type: 'user', referenceField: '_reference' },
DJB: { model: documentJobModel, idField: '_id', type: 'documentJob' }, NTY: { model: noteTypeModel, idField: '_id', type: 'noteType', referenceField: '_reference' },
HST: { model: hostModel, idField: '_id', type: 'host' }, NTE: { model: noteModel, idField: '_id', type: 'note', referenceField: '_reference' },
FLE: { model: fileModel, idField: '_id', type: 'file' }, DSZ: {
POR: { model: purchaseOrderModel, idField: '_id', type: 'purchaseOrder' }, model: documentSizeModel,
COS: { model: courierServiceModel, idField: '_id', type: 'courierService' }, idField: '_id',
COR: { model: courierModel, idField: '_id', type: 'courier' }, type: 'documentSize',
referenceField: '_reference',
},
DTP: {
model: documentTemplateModel,
idField: '_id',
type: 'documentTemplate',
referenceField: '_reference',
},
DPR: {
model: documentPrinterModel,
idField: '_id',
type: 'documentPrinter',
referenceField: '_reference',
},
DJB: {
model: documentJobModel,
idField: '_id',
type: 'documentJob',
referenceField: '_reference',
},
HST: { model: hostModel, idField: '_id', type: 'host', referenceField: '_reference' },
FLE: { model: fileModel, idField: '_id', type: 'file', referenceField: '_reference' },
POR: {
model: purchaseOrderModel,
idField: '_id',
type: 'purchaseOrder',
referenceField: '_reference',
},
COS: {
model: courierServiceModel,
idField: '_id',
type: 'courierService',
referenceField: '_reference',
},
COR: { model: courierModel, idField: '_id', type: 'courier', referenceField: '_reference' },
}; };
/** /**

View File

@ -60,7 +60,7 @@ export const getSpotlightRouteHandler = async (req, res) => {
res.status(200).send([]); res.status(200).send([]);
return; return;
} }
const prefix = query.substring(0, 3); const prefix = query.substring(0, 3).toUpperCase();
const delimiter = query.substring(3, 4); const delimiter = query.substring(3, 4);
const suffix = query.substring(4); const suffix = query.substring(4);
@ -72,18 +72,32 @@ export const getSpotlightRouteHandler = async (req, res) => {
res.status(400).send({ error: 'Invalid or unsupported prefix' }); res.status(400).send({ error: 'Invalid or unsupported prefix' });
return; return;
} }
const { model, idField, type } = prefixEntry; const { model, idField, type, referenceField } = prefixEntry;
const suffixLength = suffix.length;
// Validate ObjectId if the idField is '_id' // Validate ObjectId if the idField is '_id'
if (idField === '_id' && !mongoose.Types.ObjectId.isValid(suffix)) { if (
idField === '_id' &&
referenceField === '_reference' &&
!mongoose.Types.ObjectId.isValid(suffix) &&
suffixLength != 12
) {
res.status(200).send([]); res.status(200).send([]);
return; return;
} }
// Find the object by the correct field // Find the object by the correct field
const queryObj = {}; const queryObj = {};
if (suffixLength == 12) {
queryObj[referenceField] = suffix.toUpperCase();
} else {
queryObj[idField] = suffix.toLowerCase(); queryObj[idField] = suffix.toLowerCase();
}
let doc = await model.findOne(queryObj).lean(); let doc = await model.findOne(queryObj).lean();
if (!doc) { if (!doc) {
res.status(200).send([]); res.status(200).send([]);
return; return;