forked from brafdlog/caspion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05b6b16
commit 97cbd8a
Showing
6 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,10 +98,12 @@ | |
"react-dom": "^18.3.1", | ||
"svelte": "^4.2.19", | ||
"web-vitals": "^4.2.3", | ||
"ynab": "^1.19.0" | ||
"ynab": "^1.19.0", | ||
"zod": "^3.24.2" | ||
}, | ||
"resolutions": { | ||
"underscore": "1.12.1", | ||
"ws": "8.17.1" | ||
} | ||
}, | ||
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/main/src/backend/configManager/configMigration/configMigrator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { z } from 'zod'; | ||
import { type Config } from '../../commonTypes'; | ||
import { isOriginalConfig } from './versions/original'; | ||
import { migrateOriginalToV1, v1ConfigSchema } from './versions/v1'; | ||
|
||
const latestConfigSchema = v1ConfigSchema; | ||
|
||
// migrations[n] should be a function that converts version n to version n+1 | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const migrations: Record<number, (config: any) => any> = {}; | ||
|
||
export function migrateConfig(config: unknown): Config { | ||
let currentConfig = config; | ||
// original config does not have version key and must be handled separately | ||
if (isOriginalConfig(config)) { | ||
currentConfig = migrateOriginalToV1(config); | ||
} | ||
let currentVersion = getConfigVersion(currentConfig); | ||
|
||
while (migrations[currentVersion]) { | ||
currentConfig = migrations[currentVersion](currentConfig); | ||
currentVersion = getConfigVersion(currentConfig); | ||
} | ||
|
||
return latestConfigSchema.parse(currentConfig) as Config; | ||
} | ||
|
||
function getConfigVersion(config: unknown): keyof typeof migrations { | ||
const versionSchema = z.object({ version: z.number().int().positive() }); | ||
return versionSchema.parse(config).version; | ||
} |
94 changes: 94 additions & 0 deletions
94
packages/main/src/backend/configManager/configMigration/versions/original.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { z } from 'zod'; | ||
|
||
export const outputVendorNameSchema = z.enum(['ynab', 'googleSheets', 'json', 'csv']); | ||
|
||
export const companyTypeSchema = z.enum([ | ||
'hapoalim', | ||
'hapoalimBeOnline', | ||
'beinleumi', | ||
'union', | ||
'amex', | ||
'isracard', | ||
'visaCal', | ||
'max', | ||
'leumiCard', | ||
'otsarHahayal', | ||
'discount', | ||
'mercantile', | ||
'mizrahi', | ||
'leumi', | ||
'massad', | ||
'yahav', | ||
'behatsdaa', | ||
'beyahadBishvilha', | ||
'oneZero', | ||
'pagi', | ||
]); | ||
|
||
export const googleSheetsConfigSchema = z.object({ | ||
active: z.boolean(), | ||
options: z.object({ | ||
credentials: z.any(), | ||
spreadsheetId: z.string(), | ||
}), | ||
}); | ||
|
||
export const ynabConfigSchema = z.object({ | ||
active: z.boolean(), | ||
options: z.object({ | ||
accessToken: z.string(), | ||
accountNumbersToYnabAccountIds: z.record(z.string(), z.string()), | ||
budgetId: z.string(), | ||
maxPayeeNameLength: z.number().optional(), | ||
}), | ||
}); | ||
|
||
export const jsonConfigSchema = z.object({ | ||
active: z.boolean(), | ||
options: z.object({ | ||
filePath: z.string(), | ||
}), | ||
}); | ||
|
||
export const csvConfigSchema = z.object({ | ||
active: z.boolean(), | ||
options: z.object({ | ||
filePath: z.string(), | ||
}), | ||
}); | ||
|
||
export const outputVendorsSchema = z.object({ | ||
[outputVendorNameSchema.Values.googleSheets]: googleSheetsConfigSchema.optional(), | ||
[outputVendorNameSchema.Values.ynab]: ynabConfigSchema.optional(), | ||
[outputVendorNameSchema.Values.json]: jsonConfigSchema.optional(), | ||
[outputVendorNameSchema.Values.csv]: csvConfigSchema.optional(), | ||
}); | ||
|
||
export const accountToScrapeConfigSchema = z.object({ | ||
id: z.string(), | ||
key: companyTypeSchema, | ||
name: z.string(), | ||
loginFields: z.any(), | ||
active: z.boolean().optional(), | ||
}); | ||
|
||
export const scrapingSchema = z.object({ | ||
numDaysBack: z.number(), | ||
showBrowser: z.boolean(), | ||
accountsToScrape: z.array(accountToScrapeConfigSchema), | ||
chromiumPath: z.string().optional(), | ||
maxConcurrency: z.number().optional(), | ||
timeout: z.number(), | ||
periodicScrapingIntervalHours: z.number().optional(), | ||
}); | ||
|
||
export const originalConfigSchema = z.object({ | ||
outputVendors: outputVendorsSchema, | ||
scraping: scrapingSchema, | ||
useReactUI: z.boolean().optional(), | ||
}); | ||
|
||
export function isOriginalConfig(obj: unknown): obj is z.infer<typeof originalConfigSchema> { | ||
const parseResult = originalConfigSchema.strict().safeParse(obj); | ||
return parseResult.success; | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/main/src/backend/configManager/configMigration/versions/v1.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { z } from 'zod'; | ||
import { originalConfigSchema } from './original'; | ||
|
||
export const v1ConfigSchema = originalConfigSchema.extend({ version: z.literal(1) }); | ||
|
||
export function migrateOriginalToV1(v1Config: z.infer<typeof originalConfigSchema>): z.infer<typeof v1ConfigSchema> { | ||
return { | ||
...v1Config, | ||
version: 1, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6493,3 +6493,8 @@ [email protected]: | |
version "3.23.8" | ||
resolved "https://registry.yarnpkg.com/zod/-/zod-3.23.8.tgz#e37b957b5d52079769fb8097099b592f0ef4067d" | ||
integrity sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g== | ||
|
||
zod@^3.24.2: | ||
version "3.24.2" | ||
resolved "https://registry.yarnpkg.com/zod/-/zod-3.24.2.tgz#8efa74126287c675e92f46871cfc8d15c34372b3" | ||
integrity sha512-lY7CDW43ECgW9u1TcT3IoXHflywfVqDYze4waEz812jR/bZ8FHDsl7pFQoSZTz5N+2NqRXs8GBwnAwo3ZNxqhQ== |