-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: convertkit syncing * make convertkit work * let them cook * add purchase to convertkit props * add changeset * sigh * fix tests after discount changes * fix builds
- Loading branch information
Showing
16 changed files
with
396 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@coursebuilder/core": patch | ||
--- | ||
|
||
various changes for ProNextJS etc release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
"value-based-design", | ||
"epic-web", | ||
"pro-nextjs", | ||
"astro-party", | ||
"js-visualized", | ||
"egghead" | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
apps/pro-nextjs/src/inngest/functions/convertkit/add-purchased-convertkit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { emailListProvider } from '@/coursebuilder/email-list-provider' | ||
import { db } from '@/db' | ||
import { purchases, users } from '@/db/schema' | ||
import { inngest } from '@/inngest/inngest.server' | ||
import { format } from 'date-fns' | ||
import { eq } from 'drizzle-orm' | ||
|
||
import { NEW_PURCHASE_CREATED_EVENT } from '@coursebuilder/core/inngest/commerce/event-new-purchase-created' | ||
|
||
export const addPurchasesConvertkit = inngest.createFunction( | ||
{ | ||
id: `add-purchase-convertkit`, | ||
name: 'Add Purchase Convertkit', | ||
idempotency: 'event.user.email', | ||
}, | ||
{ event: NEW_PURCHASE_CREATED_EVENT }, | ||
async ({ event, step }) => { | ||
const user = await step.run('get user', async () => { | ||
return db.query.users.findFirst({ | ||
where: eq(users.id, event.user.id), | ||
with: { | ||
accounts: true, | ||
purchases: true, | ||
}, | ||
}) | ||
}) | ||
|
||
if (!user) throw new Error('No user found') | ||
|
||
const purchase = await step.run('get purchase', async () => { | ||
return db.query.purchases.findFirst({ | ||
where: eq(purchases.id, event.data.purchaseId), | ||
}) | ||
}) | ||
|
||
if (!purchase) throw new Error('No purchase found') | ||
|
||
const convertkitUser = await step.run('get convertkit user', async () => { | ||
console.log('get ck user', { user }) | ||
return emailListProvider.getSubscriberByEmail(user.email) | ||
}) | ||
|
||
if (convertkitUser && emailListProvider.updateSubscriberFields) { | ||
await step.run('update convertkit user', async () => { | ||
return emailListProvider.updateSubscriberFields?.({ | ||
subscriberId: convertkitUser.id, | ||
fields: { | ||
purchased_pronextjs_course_on: format( | ||
new Date(purchase.createdAt), | ||
'yyyy-MM-dd HH:mm:ss z', | ||
), | ||
}, | ||
}) | ||
}) | ||
console.log(`synced convertkit tags for ${purchase.id}`) | ||
} else { | ||
console.log(`no convertkit tags to sync for ${user.email}`) | ||
} | ||
|
||
return 'No discord account found for user' | ||
}, | ||
) |
138 changes: 138 additions & 0 deletions
138
apps/pro-nextjs/src/inngest/functions/sync-purchase-tags.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { emailListProvider } from '@/coursebuilder/email-list-provider' | ||
import { db } from '@/db' | ||
import { | ||
accounts, | ||
purchases as purchasesTable, | ||
users as usersTable, | ||
} from '@/db/schema' | ||
import { inngest } from '@/inngest/inngest.server' | ||
import { format } from 'date-fns' | ||
import { and, eq, inArray } from 'drizzle-orm' | ||
import { z } from 'zod' | ||
|
||
export const SYNC_PURCHASE_TAGS_EVENT = 'purchase/sync-tags' | ||
|
||
export type SyncPurchaseTags = { | ||
name: typeof SYNC_PURCHASE_TAGS_EVENT | ||
data: {} | ||
} | ||
|
||
export const syncPurchaseTags = inngest.createFunction( | ||
{ id: `sync-purchase-tags`, name: `Sync Purchase Tags` }, | ||
{ | ||
event: SYNC_PURCHASE_TAGS_EVENT, | ||
}, | ||
async ({ event, step }) => { | ||
const validPurchases = await step.run('get purchases', async () => { | ||
return db.query.purchases.findMany({ | ||
where: inArray(purchasesTable.status, ['Valid', 'Restricted']), | ||
}) | ||
}) | ||
|
||
for (const purchase of validPurchases) { | ||
const userId = purchase.userId | ||
|
||
if (!userId) continue | ||
|
||
const user = await step.run('get user', async () => { | ||
return db.query.users.findFirst({ | ||
where: eq(usersTable.id, userId), | ||
}) | ||
}) | ||
|
||
if (!user) throw new Error('No user found') | ||
|
||
// const discordAccount = await step.run( | ||
// 'check if discord is connected', | ||
// async () => { | ||
// return db.query.accounts.findFirst({ | ||
// where: and( | ||
// eq(accounts.userId, user.id), | ||
// eq(accounts.provider, 'discord'), | ||
// ), | ||
// }) | ||
// }, | ||
// ) | ||
// | ||
// if (discordAccount) { | ||
// console.log('discord account is connected') | ||
// const DiscordMemberBasicSchema = z.object({ | ||
// user: z.object({ | ||
// id: z.string(), | ||
// }), | ||
// roles: z.array(z.string()), | ||
// }) | ||
// | ||
// let discordMember = await step.run('get discord member', async () => { | ||
// return DiscordMemberBasicSchema.parse( | ||
// await fetchJsonAsDiscordBot<DiscordMember | DiscordError>( | ||
// `guilds/${env.DISCORD_GUILD_ID}/members/${discordAccount.providerAccountId}`, | ||
// ), | ||
// ) | ||
// }) | ||
// | ||
// console.log('discord member', discordMember.user.id) | ||
// | ||
// await step.run('update basic discord roles for user', async () => { | ||
// console.log('updating discord roles', 'user' in discordMember) | ||
// | ||
// const roles = Array.from( | ||
// new Set([...discordMember.roles, env.DISCORD_MEMBER_ROLE_ID]), | ||
// ) | ||
// | ||
// console.log('roles', { roles }) | ||
// | ||
// return await fetchJsonAsDiscordBot( | ||
// `guilds/${env.DISCORD_GUILD_ID}/members/${discordMember.user.id}`, | ||
// { | ||
// method: 'PATCH', | ||
// body: JSON.stringify({ | ||
// roles, | ||
// }), | ||
// headers: { | ||
// 'Content-Type': 'application/json', | ||
// }, | ||
// }, | ||
// ) | ||
// }) | ||
// | ||
// let relaodedMember = await step.run( | ||
// 'reload discord member', | ||
// async () => { | ||
// return await fetchJsonAsDiscordBot<DiscordMember | DiscordError>( | ||
// `guilds/${env.DISCORD_GUILD_ID}/members/${discordMember.user.id}`, | ||
// ) | ||
// }, | ||
// ) | ||
// | ||
// | ||
// | ||
// console.log({ relaodedMember }) | ||
// } | ||
|
||
const convertkitUser = await step.run('get convertkit user', async () => { | ||
console.log('get ck user', { user }) | ||
return emailListProvider.getSubscriberByEmail(user.email) | ||
}) | ||
|
||
if (convertkitUser && emailListProvider.updateSubscriberFields) { | ||
await step.run('update convertkit user', async () => { | ||
return emailListProvider.updateSubscriberFields?.({ | ||
subscriberId: convertkitUser.id, | ||
fields: { | ||
purchased_pronextjs_course_on: format( | ||
new Date(purchase.createdAt), | ||
'yyyy-MM-dd HH:mm:ss z', | ||
), | ||
}, | ||
}) | ||
}) | ||
console.log(`synced convertkit tags for ${purchase.id}`) | ||
} else { | ||
console.log(`no convertkit tags to sync for ${user.email}`) | ||
} | ||
|
||
await step.sleep('sleep for 200ms', '200ms') | ||
} | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.