3.4 KiB
3.4 KiB
Refactoring Summary: Server-Side Transformation
Overview
Moved the job transformation logic from the Electron app to the Lambda function to enable server-side processing and sharing of types between codebases.
Changes Made
1. Shared Types Directory (/shared/types/)
Created a shared types directory that both the Electron app and serverless Lambda can use:
raw-job-data.interface.ts- Defines theRawJobDataObjectinterface with all fields from the decoded job dataes-job-object.interface.ts- Defines theESJobObjectinterface for the transformed estimate scrubber formatindex.ts- Exports all shared types for easy importing
2. Serverless Changes
serverless/src/lib/transformEstimate.ts
- Moved transformation logic from
src/main/estimate-scrubber/es-transformer.ts - Exports
transformJobForEstimateScrubber()function - Imports shared types from
../../../shared/types - Handles all field omissions and transformations server-side
serverless/src/handlers/scrub.ts
- Updated to receive
rawJobinstead of pre-transformedestimate - Calls
transformJobForEstimateScrubber()to transform on the server - Sets
v_typeusing the existinggetVehicleType()function - Updated TypeScript interfaces to use shared types
serverless/package.json
- Added
lodashdependency - Added
@types/lodashdev dependency
serverless/tsconfig.json
- Updated
includeto add"../shared/**/*"for shared types access
3. Electron App Changes
src/main/estimate-scrubber/estimate-scrubber.ts
- Removed call to
TransformJobForEstimateScrubber() - Now sends raw job object directly to Lambda as
rawJob - Removed import of
es-transformer - Transformation now happens server-side
tsconfig.node.json
- Updated
includearray to add"shared/**/*"for shared types access
4. Files That Can Be Deprecated (Optional)
src/main/estimate-scrubber/es-transformer.ts- Logic moved to serverlesssrc/main/estimate-scrubber/es-job-object.interface.ts- Moved to shared types
Benefits
- Single Source of Truth: Types are defined once in
/shared/types/and used by both codebases - Server-Side Processing: Transformation logic runs on Lambda, reducing client-side processing
- Easier Maintenance: Updates to transformation logic or types only need to be made in one place
- Type Safety: Both Electron app and Lambda use the same TypeScript interfaces
- Reduced Bundle Size: Electron app no longer includes transformation logic
API Contract Changes
Before
POST /scrub
{
esApiKey: string,
estimate: ESJobObject, // Pre-transformed
fileName: string
}
After
POST /scrub
{
esApiKey: string,
rawJob: RawJobDataObject // Raw, untransformed
}
Migration Notes
- The Lambda function now handles all transformation logic
- Ensure both codebases have access to the
/shared/types/directory - Run
npm installin the serverless directory to get new dependencies - The
fileNameparameter is now generated server-side based on the raw job data
Testing Recommendations
- Test that raw job objects are correctly sent from Electron to Lambda
- Verify transformation logic produces identical output to previous version
- Ensure PDF generation and report URLs are working correctly
- Check that error handling still works as expected
- Validate that logs are being written correctly with transformed data