Skip to content

Commit 9e14803

Browse files
committed
fixed CodeRabbit issues involving zod schema and certain functions
1 parent 64459fa commit 9e14803

File tree

6 files changed

+300
-60
lines changed

6 files changed

+300
-60
lines changed

eslint.config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export default defineConfig([
3939
alwaysTryTypes: true,
4040
project: ["src/api/tsconfig.json", "src/ui/tsconfig.json"],
4141
},
42+
node: {
43+
paths: ["src/api", "src/ui"],
44+
extensions: [".js", ".ts", ".jsx", ".tsx"],
45+
},
4246
},
4347
},
4448

src/api/functions/stripe.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,23 @@ export const checkOrCreateCustomer = async ({
362362
customerName,
363363
stripeApiKey,
364364
}: checkCustomerParams): Promise<CheckOrCreateResult> => {
365+
const normalizedEmail = customerEmail.trim().toLowerCase();
366+
const [, domainPart] = normalizedEmail.split("@");
367+
368+
if (!domainPart) {
369+
throw new Error(`Could not derive email domain for "${customerEmail}".`);
370+
}
371+
372+
const normalizedDomain = domainPart.toLowerCase();
373+
365374
const lock = createLock({
366375
adapter: new IoredisAdapter(redisClient),
367-
key: `stripe:${acmOrg}:${emailDomain}`,
376+
key: `stripe:${acmOrg}:${normalizedDomain}`,
368377
retryAttempts: 5,
369378
retryDelay: 300,
370379
}) as SimpleLock;
371380

372-
const pk = `${acmOrg}#${emailDomain}`;
373-
const normalizedEmail = customerEmail.trim().toLowerCase();
381+
const pk = `${acmOrg}#${normalizedDomain}`;
374382

375383
return await lock.using(async () => {
376384
const checkCustomer = new QueryCommand({
@@ -511,11 +519,19 @@ export const addInvoice = async ({
511519
dynamoClient,
512520
stripeApiKey,
513521
}: InvoiceAddParams): Promise<CheckOrCreateResult> => {
514-
const pk = `${acmOrg}#${emailDomain}`;
522+
const normalizedEmail = contactEmail.trim().toLowerCase();
523+
const [, domainPart] = normalizedEmail.split("@");
524+
525+
if (!domainPart) {
526+
throw new Error(`Could not derive email domain for "${contactEmail}".`);
527+
}
528+
529+
const normalizedDomain = domainPart.toLowerCase();
530+
const pk = `${acmOrg}#${normalizedDomain}`;
515531

516532
const result = await checkOrCreateCustomer({
517533
acmOrg,
518-
emailDomain,
534+
emailDomain: normalizedDomain,
519535
redisClient,
520536
dynamoClient,
521537
customerEmail: contactEmail,
@@ -541,7 +557,11 @@ export const addInvoice = async ({
541557
},
542558
{ removeUndefinedValues: true },
543559
),
560+
ConditionExpression:
561+
"attribute_not_exists(primaryKey) AND attribute_not_exists(sortKey)",
544562
},
563+
},
564+
{
545565
Update: {
546566
TableName: genericConfig.StripePaymentsDynamoTableName,
547567
Key: {

src/api/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
"prettier:write": "prettier --write *.ts **/*.ts"
1616
},
1717
"dependencies": {
18-
"@aws-sdk/s3-request-presigner": "^3.914.0",
19-
"@aws-sdk/client-s3": "^3.914.0",
2018
"@aws-sdk/client-dynamodb": "^3.922.0",
2119
"@aws-sdk/client-lambda": "^3.922.0",
20+
"@aws-sdk/client-s3": "^3.922.0",
2221
"@aws-sdk/client-secrets-manager": "^3.922.0",
2322
"@aws-sdk/client-ses": "^3.922.0",
2423
"@aws-sdk/client-sqs": "^3.922.0",
2524
"@aws-sdk/client-sts": "^3.922.0",
25+
"@aws-sdk/s3-request-presigner": "^3.922.0",
2626
"@aws-sdk/signature-v4-crt": "^3.922.0",
2727
"@aws-sdk/util-dynamodb": "^3.922.0",
2828
"@azure/msal-node": "^3.8.1",
@@ -75,4 +75,4 @@
7575
"pino-pretty": "^13.1.1",
7676
"yaml": "^2.8.1"
7777
}
78-
}
78+
}

src/api/routes/stripe.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,21 @@ const stripeRoutes: FastifyPluginAsync = async (fastify, _options) => {
150150
});
151151
}
152152

153+
const stripe = new Stripe(secretApiConfig.stripe_secret_key as string);
154+
155+
const dynamicPrice = await stripe.prices.create({
156+
unit_amount: request.body.invoiceAmountUsd * 100, // USD → cents
157+
currency: "usd",
158+
product_data: {
159+
name: `Invoice ${request.body.invoiceId}`,
160+
},
161+
});
153162
const checkoutUrl = await createCheckoutSessionWithCustomer({
154163
customerId: result.customerId,
155164
stripeApiKey: secretApiConfig.stripe_secret_key as string,
156165
items: [
157166
{
158-
price: "<PRICE_ID_OR_DYNAMICALLY_CREATED_PRICE>",
167+
price: dynamicPrice.id,
159168
quantity: 1,
160169
},
161170
],

src/common/types/stripe.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as z from "zod/v4";
22

33
export const invoiceLinkPostResponseSchema = z.object({
44
id: z.string().min(1),
5-
link: z.string().url()
5+
link: z.string().url(),
66
});
77

88
export const invoiceLinkPostRequestSchema = z.object({
@@ -22,7 +22,7 @@ export type PostInvoiceLinkResponse = z.infer<
2222

2323
export const createInvoicePostResponseSchema = z.object({
2424
id: z.string().min(1),
25-
link: z.url()
25+
link: z.url(),
2626
});
2727

2828
export const createInvoiceConflictResponseSchema = z.object({
@@ -53,15 +53,17 @@ export const createInvoicePostRequestSchema = z.object({
5353
invoiceAmountUsd: z.number().min(50),
5454
contactName: z.string().min(1),
5555
contactEmail: z.email(),
56-
acmOrg: z.string().min(1)
56+
acmOrg: z.string().min(1),
5757
});
5858

5959
export type PostCreateInvoiceRequest = z.infer<
60-
typeof createInvoicePostRequestSchema>;
60+
typeof createInvoicePostRequestSchema
61+
>;
6162

6263

6364
export type PostCreateInvoiceResponse = z.infer<
64-
typeof createInvoicePostResponseSchema>;
65+
typeof createInvoicePostResponseSchema
66+
>;
6567

6668
export const invoiceLinkGetResponseSchema = z.array(
6769
z.object({
@@ -71,9 +73,10 @@ export const invoiceLinkGetResponseSchema = z.array(
7173
active: z.boolean(),
7274
invoiceId: z.string().min(1),
7375
invoiceAmountUsd: z.number().min(50),
74-
createdAt: z.union([z.string().datetime(), z.null()])
76+
createdAt: z.union([z.string().datetime(), z.null()]),
7577
})
7678
);
7779

7880
export type GetInvoiceLinksResponse = z.infer<
79-
typeof invoiceLinkGetResponseSchema>;
81+
typeof invoiceLinkGetResponseSchema
82+
>;

0 commit comments

Comments
 (0)