From 3f5dd37f382c571cb8e09365a5e9898e4ca9d183 Mon Sep 17 00:00:00 2001 From: Dev Singh Date: Wed, 5 Feb 2025 11:11:02 -0600 Subject: [PATCH] basic setup --- cloudformation/main.yml | 17 ++++++++++++++--- src/api/functions/stripe.ts | 20 ++++++++++++-------- src/api/routes/stripe.ts | 26 ++++++++++++++++++++++++-- src/common/config.ts | 2 ++ 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/cloudformation/main.yml b/cloudformation/main.yml index 0ced15f..85ca210 100644 --- a/cloudformation/main.yml +++ b/cloudformation/main.yml @@ -273,11 +273,22 @@ Resources: PointInTimeRecoverySpecification: PointInTimeRecoveryEnabled: false AttributeDefinitions: - - AttributeName: id + - AttributeName: userId + AttributeType: S + - AttributeName: linkId AttributeType: S KeySchema: - - AttributeName: id - KeyType: HASH + - AttributeName: userId + KeyType: "HASH" + - AttributeName: linkId + KeyType: "RANGE" + GlobalSecondaryIndexes: + - IndexName: LinkIdIndex + KeySchema: + - AttributeName: linkId + KeyType: "HASH" + Projection: + ProjectionType: "ALL" CacheRecordsTable: Type: 'AWS::DynamoDB::Table' diff --git a/src/api/functions/stripe.ts b/src/api/functions/stripe.ts index ea681e0..dc2e58b 100644 --- a/src/api/functions/stripe.ts +++ b/src/api/functions/stripe.ts @@ -8,7 +8,6 @@ export type StripeLinkCreateParams = { contactEmail: string; createdBy: string; stripeApiKey: string; - logger: FastifyBaseLogger; }; /** @@ -22,9 +21,13 @@ export const createStripeLink = async ({ contactName, contactEmail, createdBy, - logger, stripeApiKey, -}: StripeLinkCreateParams): Promise => { +}: StripeLinkCreateParams): Promise<{ + linkId: string; + priceId: string; + productId: string; + url: string; +}> => { const stripe = new Stripe(stripeApiKey); const description = `Created For: ${contactName} (${contactEmail}) by ${createdBy}.`; const product = await stripe.products.create({ @@ -44,9 +47,10 @@ export const createStripeLink = async ({ }, ], }); - logger.info( - { type: "audit", actor: createdBy, target: invoiceId }, - "Created Stripe payment link", - ); - return paymentLink.url; + return { + url: paymentLink.url, + linkId: paymentLink.id, + productId: product.id, + priceId: price.id, + }; }; diff --git a/src/api/routes/stripe.ts b/src/api/routes/stripe.ts index 8bdf0bf..e1c311e 100644 --- a/src/api/routes/stripe.ts +++ b/src/api/routes/stripe.ts @@ -1,3 +1,5 @@ +import { PutItemCommand } from "@aws-sdk/client-dynamodb"; +import { marshall } from "@aws-sdk/util-dynamodb"; import { createStripeLink, StripeLinkCreateParams, @@ -52,10 +54,30 @@ const stripeRoutes: FastifyPluginAsync = async (fastify, _options) => { const payload: StripeLinkCreateParams = { ...request.body, createdBy: request.username, - logger: request.log, stripeApiKey: secretApiConfig.stripe_secret_key as string, }; - const url = await createStripeLink(payload); + const { url, linkId, priceId, productId } = + await createStripeLink(payload); + const invoiceId = request.body.invoiceId; + const dynamoCommand = new PutItemCommand({ + TableName: genericConfig.StripeLinksDynamoTableName, + Item: marshall({ + userId: request.username, + linkId, + priceId, + productId, + url, + }), + }); + await fastify.dynamoClient.send(dynamoCommand); + request.log.info( + { + type: "audit", + actor: request.username, + target: `Link ${linkId} | Invoice ${invoiceId}`, + }, + "Created Stripe payment link", + ); reply.status(201).send({ invoiceId: request.body.invoiceId, link: url }); }, ); diff --git a/src/common/config.ts b/src/common/config.ts index 3c18171..bfdf844 100644 --- a/src/common/config.ts +++ b/src/common/config.ts @@ -22,6 +22,7 @@ export type ConfigType = { export type GenericConfigType = { EventsDynamoTableName: string; CacheDynamoTableName: string; + StripeLinksDynamoTableName: string; ConfigSecretName: string; UpcomingEventThresholdSeconds: number; AwsRegion: string; @@ -46,6 +47,7 @@ export const execCouncilTestingGroupId = "dbe18eb2-9675-46c4-b1ef-749a6db4fedd"; const genericConfig: GenericConfigType = { EventsDynamoTableName: "infra-core-api-events", + StripeLinksDynamoTableName: "infra-core-api-stripe-links", CacheDynamoTableName: "infra-core-api-cache", ConfigSecretName: "infra-core-api-config", UpcomingEventThresholdSeconds: 1800, // 30 mins