Skip to content

Commit

Permalink
Sort out state shape
Browse files Browse the repository at this point in the history
  • Loading branch information
rupertbates committed Jan 29, 2025
1 parent e9519c0 commit 743f0fa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
31 changes: 18 additions & 13 deletions support-workers/src/typescript/lambdas/createPaymentMethodLambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@ import type {
DirectDebitPaymentFields,
PaymentFields,
PayPalPaymentFields,
// SepaPaymentFields,
StripePaymentFields,
} from '../model/paymentFields';
import type {
DirectDebitPaymentMethod,
PaymentMethod,
PayPalPaymentMethod,
// SepaPaymentMethod,
StripePaymentMethod,
} from '../model/paymentMethod';
import type { Stage } from '../model/stage';
import { stageFromEnvironment } from '../model/stage';
import type {
CreatePaymentMethodState,
CreateSalesforceContactState,
User,
} from '../model/stateSchemas';
import { createPaymentMethodStateSchema } from '../model/stateSchemas';
import {
createPaymentMethodStateSchema,
wrapperSchemaForState,
} from '../model/stateSchemas';
import { ServiceHandler } from '../services/config';
import { getPayPalConfig, PayPalService } from '../services/payPal';
import { getStripeConfig, StripeService } from '../services/stripe';
import { getIfDefined } from '../util/nullAndUndefined';

const stage = process.env.stage as Stage;
const stage = stageFromEnvironment();
const stripeServiceHandler = new ServiceHandler(stage, async (stage) => {
const config = await getStripeConfig(stage);
return new StripeService(config);
Expand All @@ -35,11 +36,11 @@ const paypalServiceHandler = new ServiceHandler(stage, async (stage) => {
return new PayPalService(config);
});

export const handler = async (
state: unknown,
): Promise<CreateSalesforceContactState> => {
export const handler = async (state: unknown) => {
console.log(`Input is ${JSON.stringify(state)}`);
const createPaymentMethodState = createPaymentMethodStateSchema.parse(state);
const createPaymentMethodState = wrapperSchemaForState(
createPaymentMethodStateSchema,
).parse(state).state;
return createSalesforceContactState(
createPaymentMethodState,
await createPaymentMethod(
Expand Down Expand Up @@ -71,13 +72,17 @@ export function createPaymentMethod(
}

export function createSalesforceContactState(
state: CreatePaymentMethodState,
inputState: CreatePaymentMethodState,
paymentMethod: PaymentMethod,
): CreateSalesforceContactState {
return {
...state,
) {
const outputState: CreateSalesforceContactState = {
...inputState,
paymentMethod,
};

return {
state: outputState,
};
}

export async function createStripePaymentMethod(
Expand Down
8 changes: 8 additions & 0 deletions support-workers/src/typescript/model/stage.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
export type Stage = 'CODE' | 'PROD';

export const stageFromEnvironment = (): Stage => {
const stage = process.env.STAGE;
if (stage === undefined) {
throw new Error('STAGE is not defined as an environment variable');
}
return stage as Stage;
};
8 changes: 8 additions & 0 deletions support-workers/src/typescript/model/stateSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,11 @@ export const createSalesforceContactStateSchema = baseStateSchema.merge(
export type CreateSalesforceContactState = z.infer<
typeof createSalesforceContactStateSchema
>;

export function wrapperSchemaForState<SchemaType extends z.ZodTypeAny>(
stateSchema: SchemaType,
) {
return z.object({
state: stateSchema,
});
}

0 comments on commit 743f0fa

Please sign in to comment.