feat: Add TEST/PROD environment toggle and improve UNK validation
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { config } from 'dotenv';
|
||||
|
||||
import { Hono } from 'hono';
|
||||
import { serveStatic } from 'hono/bun';
|
||||
import { cors } from 'hono/cors';
|
||||
@@ -13,6 +14,31 @@ import { processNewJobRecord } from './extracted-graph-logic/post-record-integra
|
||||
// Load secrets from absolute path (not in project directory)
|
||||
config({ path: '/home/admin/secrets/.env' });
|
||||
|
||||
// ============================================================================
|
||||
// ENVIRONMENT SETTINGS (Toggle between TEST and PROD here)
|
||||
// ============================================================================
|
||||
const MODE = 'TEST'; // CHANGE THIS TO 'PROD' TO GO LIVE
|
||||
|
||||
const CONFIG = {
|
||||
TEST: {
|
||||
pbCollection: 'Job_Info_TestEnv',
|
||||
excelTable: 'Test_Table',
|
||||
description: '🧪 TEST ENVIRONMENT'
|
||||
},
|
||||
PROD: {
|
||||
pbCollection: process.env.PB_COLLECTION || 'Job_Info_Prod',
|
||||
excelTable: process.env.EXCEL_TABLE_NAME || 'Job_List',
|
||||
description: '🚀 PRODUCTION ENVIRONMENT'
|
||||
}
|
||||
};
|
||||
|
||||
const activeConfig = MODE === 'PROD' ? CONFIG.PROD : CONFIG.TEST;
|
||||
console.log('\n' + '*'.repeat(60));
|
||||
console.log(' RUNNING IN: ' + activeConfig.description);
|
||||
console.log(' PocketBase: ' + activeConfig.pbCollection);
|
||||
console.log(' Excel Table: ' + activeConfig.excelTable);
|
||||
console.log('*'.repeat(60) + '\n');
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
// ============================================================================
|
||||
@@ -249,13 +275,7 @@ let tableCache: {
|
||||
// Resolve the Excel table name from env; prefers EXCEL_TABLE_NAME (just the table's name),
|
||||
// and falls back to parsing EXCEL_TABLE if provided as a full path, else default to Test_Table.
|
||||
function resolveTableName(): string {
|
||||
const nameFromSimpleEnv = process.env.EXCEL_TABLE_NAME?.trim();
|
||||
if (nameFromSimpleEnv) return nameFromSimpleEnv;
|
||||
|
||||
const nameFromPath = process.env.EXCEL_TABLE?.split('/tables/').pop()?.trim();
|
||||
if (nameFromPath) return nameFromPath;
|
||||
|
||||
return 'Test_Table';
|
||||
return activeConfig.excelTable;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -648,6 +668,7 @@ async function updateExcelFolderLink(pbId: string, link: string) {
|
||||
|
||||
// API endpoint to submit job
|
||||
app.post('/api/submit', async (c) => {
|
||||
const pbCollection = activeConfig.pbCollection;
|
||||
let data: any;
|
||||
try {
|
||||
data = await c.req.json();
|
||||
@@ -742,7 +763,7 @@ app.post('/api/submit', async (c) => {
|
||||
}
|
||||
|
||||
// Get all existing job numbers and find the max
|
||||
const existingJobs = await pb.collection(process.env.PB_COLLECTION!).getFullList({
|
||||
const existingJobs = await pb.collection(pbCollection).getFullList({
|
||||
fields: 'Job_Number',
|
||||
sort: '-created',
|
||||
});
|
||||
@@ -763,7 +784,7 @@ app.post('/api/submit', async (c) => {
|
||||
delete data.pbToken;
|
||||
|
||||
// Create the record in PocketBase as the authenticated user
|
||||
const record = await pb.collection(process.env.PB_COLLECTION!).create(data);
|
||||
const record = await pb.collection(pbCollection).create(data);
|
||||
console.log(`✓ Created PocketBase record with Job_Number: ${newJobNumber}, ID: ${record.id}`);
|
||||
|
||||
// Persist calculated fields to PocketBase immediately after creation
|
||||
@@ -773,7 +794,7 @@ app.post('/api/submit', async (c) => {
|
||||
const voxerLinkVal = calculateVoxerLink(record);
|
||||
const jobCodesVal = calculateJobCodes(record);
|
||||
|
||||
await pb.collection(process.env.PB_COLLECTION!).update(record.id, {
|
||||
await pb.collection(pbCollection).update(record.id, {
|
||||
Job_Full_Name: fullName,
|
||||
Job_QB_Link: qbLink,
|
||||
Voxer_Link: voxerLinkVal,
|
||||
@@ -843,7 +864,7 @@ app.post('/api/submit', async (c) => {
|
||||
try {
|
||||
const dueStatic = calculateDueDateCounter(record);
|
||||
const activeStatic = calculateActive(record);
|
||||
await pb.collection(process.env.PB_COLLECTION!).update(record.id, {
|
||||
await pb.collection(pbCollection).update(record.id, {
|
||||
Job_Folder_Link: jobFolderLink || '',
|
||||
Due_Date_Counter: dueStatic,
|
||||
Active: activeStatic
|
||||
|
||||
Reference in New Issue
Block a user