Skip to content

Commit 8640c65

Browse files
committed
pass in logger to loops/client
1 parent 4023496 commit 8640c65

File tree

3 files changed

+72
-37
lines changed

3 files changed

+72
-37
lines changed

evals/git-evals/email-eval-results.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ export async function sendEvalResultsEmail(
174174
): Promise<boolean> {
175175
console.log(`📧 Sending eval results email to ${recipientEmail}...`)
176176
const emailContent = formatEvalSummaryForEmail(evalResults, analyses, title)
177-
const result = await sendBasicEmail(recipientEmail, emailContent)
177+
const result = await sendBasicEmail({
178+
email: recipientEmail,
179+
data: emailContent,
180+
logger: console,
181+
})
178182
return result.success
179183
}

packages/internal/src/loops/client.ts

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import db from '@codebuff/common/db'
22
import * as schema from '@codebuff/common/db/schema'
3-
import { logger } from '@codebuff/common/util/logger'
43
import { eq } from 'drizzle-orm'
54
import { LoopsClient, APIError } from 'loops'
65

76
import type { LoopsEmailData, SendEmailResult } from './types'
7+
import type { ParamsExcluding, WithDefaults } from '@codebuff/types/common'
8+
import type { Logger } from '@codebuff/types/logger'
89

910
const ORGANIZATION_INVITATION_TRANSACTIONAL_ID = 'cmbikixxm15xo4a0iiemzkzw1'
1011
const BASIC_TRANSACTIONAL_ID = 'cmb8pafk92r820w0i7lkplkt2'
@@ -16,10 +17,22 @@ if (process.env.LOOPS_API_KEY) {
1617
}
1718

1819
async function sendTransactionalEmail(
19-
transactionalId: string,
20-
email: string,
21-
dataVariables: Record<string, any> = {},
20+
params: WithDefaults<
21+
{
22+
transactionalId: string
23+
email: string
24+
dataVariables: Record<string, any>
25+
logger: Logger
26+
},
27+
'dataVariables'
28+
>,
2229
): Promise<SendEmailResult> {
30+
const withDefaults = {
31+
dataVariables: {},
32+
...params,
33+
}
34+
const { transactionalId, email, dataVariables, logger } = withDefaults
35+
2336
if (!loopsClient) {
2437
return {
2538
success: false,
@@ -63,11 +76,14 @@ async function sendTransactionalEmail(
6376
}
6477
}
6578

66-
export async function sendSignupEventToLoops(
67-
userId: string,
68-
email: string | null,
69-
name: string | null,
70-
): Promise<void> {
79+
export async function sendSignupEventToLoops(params: {
80+
userId: string
81+
email: string | null
82+
name: string | null
83+
logger: Logger
84+
}): Promise<void> {
85+
const { userId, email, name, logger } = params
86+
7187
if (!loopsClient) {
7288
logger.warn({ userId }, 'Loops SDK not initialized. Skipping signup event.')
7389
return
@@ -116,9 +132,12 @@ export async function sendSignupEventToLoops(
116132
}
117133
}
118134

119-
export async function sendOrganizationInvitationEmail(
120-
data: LoopsEmailData, // data no longer contains firstName
121-
): Promise<SendEmailResult> {
135+
export async function sendOrganizationInvitationEmail(params: {
136+
data: LoopsEmailData // data no longer contains firstName
137+
logger: Logger
138+
}): Promise<SendEmailResult> {
139+
const { data, logger } = params
140+
122141
let lookedUpFirstName: string = 'there' // Default to 'there'
123142
try {
124143
const inviteeUserRecord = await db
@@ -142,25 +161,36 @@ export async function sendOrganizationInvitationEmail(
142161
// Continue with default name 'there'
143162
}
144163

145-
return sendTransactionalEmail(
146-
ORGANIZATION_INVITATION_TRANSACTIONAL_ID,
147-
data.email,
148-
{
164+
return sendTransactionalEmail({
165+
transactionalId: ORGANIZATION_INVITATION_TRANSACTIONAL_ID,
166+
email: data.email,
167+
dataVariables: {
149168
firstName: lookedUpFirstName, // Use the looked-up or default name
150169
organizationName: data.organizationName || '', // data.organizationName is still expected
151170
inviterName: data.inviterName || '', // data.inviterName is still expected
152171
invitationUrl: data.invitationUrl || '', // data.invitationUrl is still expected
153172
role: data.role || 'member', // data.role is still expected
154173
},
155-
)
174+
logger,
175+
})
156176
}
157177

158178
export async function sendBasicEmail(
159-
email: string,
160-
data: { subject: string; message: string },
179+
params: {
180+
data: { subject: string; message: string }
181+
} & ParamsExcluding<
182+
typeof sendTransactionalEmail,
183+
'transactionalId' | 'dataVariables'
184+
>,
161185
): Promise<SendEmailResult> {
162-
return sendTransactionalEmail(BASIC_TRANSACTIONAL_ID, email, {
163-
subject: data.subject,
164-
message: data.message,
186+
const { data } = params
187+
188+
return sendTransactionalEmail({
189+
...params,
190+
transactionalId: BASIC_TRANSACTIONAL_ID,
191+
dataVariables: {
192+
subject: data.subject,
193+
message: data.message,
194+
},
165195
})
166196
}

web/src/app/api/auth/[...nextauth]/auth-options.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ import type { Adapter } from 'next-auth/adapters'
1919

2020
import { logger } from '@/util/logger'
2121

22-
async function createAndLinkStripeCustomer(
23-
userId: string,
24-
email: string | null,
22+
async function createAndLinkStripeCustomer(params: {
23+
userId: string
24+
email: string | null
2525
name: string | null
26-
): Promise<string | null> {
26+
}): Promise<string | null> {
27+
const { userId, email, name } = params
28+
2729
if (!email || !name) {
2830
logger.warn(
2931
{ userId },
@@ -228,11 +230,10 @@ export const authOptions: NextAuthOptions = {
228230
return
229231
}
230232

231-
const customerId = await createAndLinkStripeCustomer(
232-
userData.id,
233-
userData.email,
234-
userData.name
235-
)
233+
const customerId = await createAndLinkStripeCustomer({
234+
...userData,
235+
userId: userData.id,
236+
})
236237

237238
if (customerId) {
238239
await createInitialCreditGrant({
@@ -243,11 +244,11 @@ export const authOptions: NextAuthOptions = {
243244
}
244245

245246
// Call the imported function
246-
await loops.sendSignupEventToLoops(
247-
userData.id,
248-
userData.email,
249-
userData.name
250-
)
247+
await loops.sendSignupEventToLoops({
248+
...userData,
249+
userId: userData.id,
250+
logger,
251+
})
251252

252253
trackEvent({
253254
event: AnalyticsEvent.SIGNUP,

0 commit comments

Comments
 (0)