Skip to content

Commit

Permalink
v2
Browse files Browse the repository at this point in the history
  • Loading branch information
whatuserever committed Feb 15, 2025
1 parent 2af84a3 commit abe3b56
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { z } from 'zod';
import { type Config } from '../../commonTypes';
import { isOriginalConfig } from './versions/original';
import { migrateOriginalToV1, v1ConfigSchema } from './versions/v1';
import { migrateOriginalToV1 } from './versions/v1';
import { migrateV1ToV2, v2ConfigSchema } from './versions/v2';

const latestConfigSchema = v1ConfigSchema;
const latestConfigSchema = v2ConfigSchema;

// 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> = {};
const migrations: Record<number, (config: any) => any> = {
1: migrateV1ToV2,
};

export function migrateConfig(config: unknown): Config {
let currentConfig = config;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { z } from 'zod';
import {
outputVendorNameSchema,
outputVendorsSchema as v1OutputVendorsSchema,
ynabConfigSchema as v1YnabConfigSchema,
} from './original';

import { v1ConfigSchema } from './v1';

export const ynabConfigSchema = v1YnabConfigSchema.extend({
...v1YnabConfigSchema.shape,
options: v1YnabConfigSchema.shape.options.extend({
accountNumbersToYnabAccountIds: z.record(
z.string(),
z.object({
ynabBudgetId: z.string(),
ynabAccountId: z.string(),
}),
),
}),
});

export const outputVendorsSchema = v1OutputVendorsSchema.extend({
...v1OutputVendorsSchema.shape,
ynab: ynabConfigSchema.optional(),
});

export const v2ConfigSchema = v1ConfigSchema.extend({
...v1ConfigSchema.shape,
version: z.literal(2),
outputVendors: outputVendorsSchema,
});

// Only difference is that YnabConfig.accountNumbersToYnabAccountIds was a Record<string, string>
// but is now a Record<string, { ynabBudgetId: string, ynabAccountId: string }>
export function migrateV1ToV2(v1Config: z.infer<typeof v1ConfigSchema>): z.infer<typeof v2ConfigSchema> {
const v1ConfigParsed = v1ConfigSchema.parse(v1Config);
const v1YnabConfig = v1ConfigParsed.outputVendors[outputVendorNameSchema.Values.ynab];
const v1OutputVendors = v1ConfigParsed.outputVendors;

let v2YnabConfig = undefined;

if (v1YnabConfig) {
const ynabBudgetId = v1YnabConfig.options.budgetId;
const v1AccountMapping = v1YnabConfig.options.accountNumbersToYnabAccountIds;
const v2AccountMapping = Object.fromEntries(
Object.entries(v1AccountMapping).map(([accountNumber, ynabAccountId]) => {
return [accountNumber, { ynabBudgetId, ynabAccountId }];
}),
);

v2YnabConfig = {
...v1YnabConfig,
options: {
...v1YnabConfig.options,
accountNumbersToYnabAccountIds: v2AccountMapping,
},
};
}

return {
...v1ConfigParsed,
version: 2,
outputVendors: {
...v1OutputVendors,
ynab: v2YnabConfig,
},
};
}

0 comments on commit abe3b56

Please sign in to comment.