@upstash/workflow 1.4.0 → 1.5.1-canary.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. package/README.md +9 -0
  2. package/astro.d.mts +19 -2
  3. package/astro.d.ts +19 -2
  4. package/astro.js +670 -159
  5. package/astro.mjs +1 -1
  6. package/{chunk-DRHEHWMS.mjs → chunk-J3PXPHSB.mjs} +828 -317
  7. package/cloudflare.d.mts +17 -2
  8. package/cloudflare.d.ts +17 -2
  9. package/cloudflare.js +670 -159
  10. package/cloudflare.mjs +1 -1
  11. package/docs/agents/examples.mdx +8 -0
  12. package/docs/agents/features.mdx +375 -0
  13. package/docs/agents/getting-started.mdx +139 -0
  14. package/docs/agents/overview.mdx +40 -0
  15. package/docs/agents/patterns/evaluator-optimizer.mdx +72 -0
  16. package/docs/agents/patterns/orchestrator-workers.mdx +95 -0
  17. package/docs/agents/patterns/parallelization.mdx +77 -0
  18. package/docs/agents/patterns/prompt-chaining.mdx +84 -0
  19. package/docs/basics/best-practices/at-least-one-step.mdx +57 -0
  20. package/docs/basics/best-practices/business-logic-in-steps.mdx +52 -0
  21. package/docs/basics/best-practices/deterministic-code.mdx +132 -0
  22. package/docs/basics/best-practices/idempotency.mdx +38 -0
  23. package/docs/basics/best-practices/nested-context-methods.mdx +38 -0
  24. package/docs/basics/best-practices/overview.mdx +35 -0
  25. package/docs/basics/best-practices/promise-any.mdx +9 -0
  26. package/docs/basics/best-practices/return-step-results.mdx +80 -0
  27. package/docs/basics/best-practices/workflow-limits.mdx +62 -0
  28. package/docs/basics/client/cancel.mdx +137 -0
  29. package/docs/basics/client/dlq/callback.mdx +21 -0
  30. package/docs/basics/client/dlq/delete.mdx +120 -0
  31. package/docs/basics/client/dlq/list.mdx +96 -0
  32. package/docs/basics/client/dlq/restart.mdx +164 -0
  33. package/docs/basics/client/dlq/resume.mdx +167 -0
  34. package/docs/basics/client/logs.mdx +139 -0
  35. package/docs/basics/client/notify.mdx +79 -0
  36. package/docs/basics/client/trigger.mdx +139 -0
  37. package/docs/basics/client/waiters.mdx +31 -0
  38. package/docs/basics/client.mdx +40 -0
  39. package/docs/basics/how.mdx +65 -0
  40. package/docs/basics/serve/advanced.mdx +435 -0
  41. package/docs/basics/serve.mdx +142 -0
  42. package/docs/changelog.mdx +174 -0
  43. package/docs/examples/allInOne.mdx +377 -0
  44. package/docs/examples/authWebhook.mdx +575 -0
  45. package/docs/examples/customRetry.mdx +309 -0
  46. package/docs/examples/customerOnboarding.mdx +230 -0
  47. package/docs/examples/dynamicWorkflow.mdx +203 -0
  48. package/docs/examples/eCommerceOrderFulfillment.mdx +281 -0
  49. package/docs/examples/imageProcessing.mdx +339 -0
  50. package/docs/examples/paymentRetry.mdx +438 -0
  51. package/docs/examples/waitForEvent.mdx +201 -0
  52. package/docs/features/dlq/callback.mdx +42 -0
  53. package/docs/features/dlq/delete.mdx +29 -0
  54. package/docs/features/dlq/restart.mdx +33 -0
  55. package/docs/features/dlq/resume.mdx +37 -0
  56. package/docs/features/dlq.mdx +39 -0
  57. package/docs/features/failure-callback.mdx +102 -0
  58. package/docs/features/failureFunction/advanced.mdx +36 -0
  59. package/docs/features/failureFunction/reliability.mdx +33 -0
  60. package/docs/features/flow-control/monitor.mdx +22 -0
  61. package/docs/features/flow-control/parallelism.mdx +45 -0
  62. package/docs/features/flow-control/rate-period.mdx +46 -0
  63. package/docs/features/flow-control.mdx +164 -0
  64. package/docs/features/invoke/serveMany.mdx +109 -0
  65. package/docs/features/invoke.mdx +39 -0
  66. package/docs/features/notify.mdx +127 -0
  67. package/docs/features/parallel-steps.mdx +41 -0
  68. package/docs/features/retries/prevent-retries.mdx +140 -0
  69. package/docs/features/retries.mdx +93 -0
  70. package/docs/features/wait-for-event.mdx +104 -0
  71. package/docs/features/wait.mdx +44 -0
  72. package/docs/features/webhooks.mdx +116 -0
  73. package/docs/getstarted.mdx +200 -0
  74. package/docs/howto/cancel.mdx +50 -0
  75. package/docs/howto/changes.mdx +33 -0
  76. package/docs/howto/configure.mdx +42 -0
  77. package/docs/howto/failures.mdx +185 -0
  78. package/docs/howto/get-token.mdx +18 -0
  79. package/docs/howto/local-development/development-server.mdx +130 -0
  80. package/docs/howto/local-development/local-tunnel.mdx +73 -0
  81. package/docs/howto/middlewares.mdx +263 -0
  82. package/docs/howto/migrations.mdx +335 -0
  83. package/docs/howto/multi-region.mdx +107 -0
  84. package/docs/howto/parallel-runs.mdx +52 -0
  85. package/docs/howto/readonly-token.mdx +19 -0
  86. package/docs/howto/realtime/basic.mdx +315 -0
  87. package/docs/howto/realtime/human-in-the-loop.mdx +454 -0
  88. package/docs/howto/redact-fields.mdx +61 -0
  89. package/docs/howto/retry-now.mdx +41 -0
  90. package/docs/howto/schedule.mdx +114 -0
  91. package/docs/howto/security.mdx +143 -0
  92. package/docs/howto/singleton.mdx +137 -0
  93. package/docs/howto/start.mdx +69 -0
  94. package/docs/howto/use-webhooks.mdx +256 -0
  95. package/docs/integrations/aisdk.mdx +316 -0
  96. package/docs/integrations/anthropic.mdx +51 -0
  97. package/docs/integrations/datadog.mdx +69 -0
  98. package/docs/integrations/openai.mdx +87 -0
  99. package/docs/integrations/prometheus.mdx +65 -0
  100. package/docs/integrations/resend.mdx +82 -0
  101. package/docs/llms-txt.mdx +4 -0
  102. package/docs/openapi.yaml +2946 -0
  103. package/docs/pricing.mdx +22 -0
  104. package/docs/quickstarts/astro.mdx +185 -0
  105. package/docs/quickstarts/cloudflare-workers.mdx +190 -0
  106. package/docs/quickstarts/express.mdx +190 -0
  107. package/docs/quickstarts/fastapi.mdx +282 -0
  108. package/docs/quickstarts/flask.mdx +282 -0
  109. package/docs/quickstarts/hono.mdx +221 -0
  110. package/docs/quickstarts/nextjs-fastapi.mdx +147 -0
  111. package/docs/quickstarts/nextjs-flask.mdx +147 -0
  112. package/docs/quickstarts/nuxt.mdx +169 -0
  113. package/docs/quickstarts/platforms.mdx +78 -0
  114. package/docs/quickstarts/solidjs.mdx +161 -0
  115. package/docs/quickstarts/svelte.mdx +170 -0
  116. package/docs/quickstarts/tanstack-start.mdx +205 -0
  117. package/docs/quickstarts/vercel-nextjs.mdx +233 -0
  118. package/docs/roadmap.mdx +8 -0
  119. package/docs/sdk/workflow-js.mdx +4 -0
  120. package/docs/sdk/workflow-py.mdx +4 -0
  121. package/docs/steps/api.mdx +64 -0
  122. package/docs/steps/call.mdx +183 -0
  123. package/docs/steps/cancel.mdx +25 -0
  124. package/docs/steps/createWebhook.mdx +40 -0
  125. package/docs/steps/invoke.mdx +100 -0
  126. package/docs/steps/notify.mdx +105 -0
  127. package/docs/steps/run.mdx +150 -0
  128. package/docs/steps/sleep.mdx +97 -0
  129. package/docs/steps/sleepUntil.mdx +88 -0
  130. package/docs/steps/waitForEvent.mdx +60 -0
  131. package/docs/steps/waitForWebhook.mdx +78 -0
  132. package/docs/steps.mdx +108 -0
  133. package/docs/troubleshooting/general.mdx +331 -0
  134. package/docs/troubleshooting/vercel.mdx +82 -0
  135. package/express.d.mts +19 -2
  136. package/express.d.ts +19 -2
  137. package/express.js +670 -159
  138. package/express.mjs +1 -1
  139. package/h3.d.mts +19 -2
  140. package/h3.d.ts +19 -2
  141. package/h3.js +670 -159
  142. package/h3.mjs +1 -1
  143. package/hono.d.mts +17 -2
  144. package/hono.d.ts +17 -2
  145. package/hono.js +670 -159
  146. package/hono.mjs +1 -1
  147. package/index.d.mts +50 -21
  148. package/index.d.ts +50 -21
  149. package/index.js +686 -171
  150. package/index.mjs +17 -13
  151. package/nextjs.d.mts +40 -2
  152. package/nextjs.d.ts +40 -2
  153. package/nextjs.js +670 -159
  154. package/nextjs.mjs +1 -1
  155. package/package.json +1 -1
  156. package/react-router.d.mts +16 -2
  157. package/react-router.d.ts +16 -2
  158. package/react-router.js +670 -159
  159. package/react-router.mjs +1 -1
  160. package/{serve-many-CG3BFvO3.d.mts → serve-many-BKukvLNl.d.mts} +1 -1
  161. package/{serve-many-iJF1IUXk.d.ts → serve-many-CrLKQpuZ.d.ts} +1 -1
  162. package/solidjs.d.mts +10 -1
  163. package/solidjs.d.ts +10 -1
  164. package/solidjs.js +670 -159
  165. package/solidjs.mjs +1 -1
  166. package/src/platforms/astro.ts +84 -0
  167. package/src/platforms/cloudflare.ts +127 -0
  168. package/src/platforms/express.ts +147 -0
  169. package/src/platforms/h3.ts +105 -0
  170. package/src/platforms/hono.ts +103 -0
  171. package/src/platforms/nextjs.ts +193 -0
  172. package/src/platforms/react-router.ts +92 -0
  173. package/src/platforms/solidjs.ts +59 -0
  174. package/src/platforms/svelte.ts +99 -0
  175. package/src/platforms/tanstack.ts +96 -0
  176. package/src/src/client/dlq.ts +387 -0
  177. package/src/src/client/filter-types.ts +166 -0
  178. package/src/src/client/index.ts +350 -0
  179. package/src/src/client/types.ts +501 -0
  180. package/src/src/client/utils.ts +307 -0
  181. package/src/src/constants.ts +82 -0
  182. package/src/src/context/api/anthropic.ts +58 -0
  183. package/src/src/context/api/base.ts +52 -0
  184. package/src/src/context/api/index.ts +24 -0
  185. package/src/src/context/api/openai.ts +125 -0
  186. package/src/src/context/api/resend.ts +57 -0
  187. package/src/src/context/auto-executor.ts +707 -0
  188. package/src/src/context/context.ts +681 -0
  189. package/src/src/context/index.ts +1 -0
  190. package/src/src/context/provider.ts +37 -0
  191. package/src/src/context/steps.ts +965 -0
  192. package/src/src/error.ts +203 -0
  193. package/src/src/index.ts +13 -0
  194. package/src/src/middleware/default-callbacks.ts +20 -0
  195. package/src/src/middleware/index.ts +2 -0
  196. package/src/src/middleware/logging.ts +39 -0
  197. package/src/src/middleware/manager.ts +148 -0
  198. package/src/src/middleware/middleware.ts +46 -0
  199. package/src/src/middleware/types.ts +72 -0
  200. package/src/src/qstash/headers.ts +397 -0
  201. package/src/src/qstash/step-config.ts +268 -0
  202. package/src/src/qstash/submit-steps.ts +284 -0
  203. package/src/src/serve/authorization.ts +118 -0
  204. package/src/src/serve/index.ts +475 -0
  205. package/src/src/serve/multi-region/handlers.ts +225 -0
  206. package/src/src/serve/multi-region/utils.ts +88 -0
  207. package/src/src/serve/options.ts +241 -0
  208. package/src/src/serve/serve-many.ts +117 -0
  209. package/src/src/serve/utils.ts +11 -0
  210. package/src/src/types.ts +590 -0
  211. package/src/src/utils.ts +141 -0
  212. package/src/src/workflow-parser.ts +472 -0
  213. package/src/src/workflow-requests.ts +575 -0
  214. package/svelte.d.mts +17 -2
  215. package/svelte.d.ts +17 -2
  216. package/svelte.js +670 -159
  217. package/svelte.mjs +1 -1
  218. package/tanstack.d.mts +15 -2
  219. package/tanstack.d.ts +15 -2
  220. package/tanstack.js +670 -159
  221. package/tanstack.mjs +1 -1
  222. package/{types-CekOpKvz.d.ts → types-CrgK5P6p.d.mts} +372 -32
  223. package/{types-CekOpKvz.d.mts → types-CrgK5P6p.d.ts} +372 -32
@@ -0,0 +1,575 @@
1
+ ---
2
+ title: "Auth Provider Webhook"
3
+ ---
4
+
5
+ This example demonstrates an authentication provider webhook process using Upstash Workflow.
6
+ The workflow handles the user creation, trial management, email reminders and notifications.
7
+
8
+ ## Use Case
9
+
10
+ Our workflow will:
11
+
12
+ 1. Receive a webhook event from an authentication provider (e.g. Firebase, Auth0, Clerk etc.)
13
+ 2. Create a new user in our database
14
+ 3. Create a new user in Stripe
15
+ 4. Start a trial in Stripe
16
+ 5. Send a welcome email
17
+ 6. Send a reminder email if the user hasn't solved any questions in the last 7 days
18
+ 7. Send a trial warning email if the user hasn't upgraded 2 days before the trial ends
19
+ 8. Send a trial ended email if the user hasn't upgraded
20
+
21
+ ## Code Example
22
+
23
+ <CodeGroup>
24
+
25
+ ```typescript api/workflow/route.ts
26
+ import { serve } from "@upstash/workflow/nextjs";
27
+ import { WorkflowContext } from '@upstash/qstash/workflow'
28
+
29
+ /**
30
+ * This can be the payload of the user created webhook event coming from your
31
+ * auth provider (e.g. Firebase, Auth0, Clerk etc.)
32
+ */
33
+ type UserCreatedPayload = {
34
+ name: string;
35
+ email: string;
36
+ };
37
+
38
+ export const { POST } = serve<UserCreatedPayload>(async (context) => {
39
+ const { name, email } = context.requestPayload;
40
+
41
+ const { userid } = await context.run("sync user", async () => {
42
+ return await createUserInDatabase({ name, email });
43
+ });
44
+
45
+ await context.run("create new user in stripe", async () => {
46
+ await createNewUserInStripe(email);
47
+ });
48
+
49
+ await context.run("start trial in Stripe", async () => {
50
+ await startTrialInStripe(email);
51
+ });
52
+
53
+ await context.run("send welcome email", async () => {
54
+ await sendEmail(
55
+ email,
56
+ "Welcome to our platform!, You have 14 days of free trial."
57
+ );
58
+ });
59
+
60
+ await context.sleep("wait", 7 * 24 * 60 * 60);
61
+
62
+ // get user stats and send email with them
63
+ const stats = await context.run("get user stats", async () => {
64
+ return await getUserStats(userid);
65
+ });
66
+ await sendProblemSolvedEmail({context, email, stats});
67
+
68
+ // wait until there are two days to the end of trial period
69
+ // and check upgrade status
70
+ await context.sleep("wait for trial warning", 5 * 24 * 60 * 60);
71
+ const isUpgraded = await context.run("check upgraded plan", async () => {
72
+ return await checkUpgradedPlan(email);
73
+ });
74
+
75
+ // end the workflow if upgraded
76
+ if (isUpgraded) return;
77
+
78
+ await context.run("send trial warning email", async () => {
79
+ await sendEmail(
80
+ email,
81
+ "Your trial is about to end in 2 days. Please upgrade your plan to keep using our platform."
82
+ );
83
+ });
84
+
85
+ await context.sleep("wait for trial end", 2 * 24 * 60 * 60);
86
+
87
+ await context.run("send trial end email", async () => {
88
+ await sendEmail(
89
+ email,
90
+ "Your trial has ended. Please upgrade your plan to keep using our platform."
91
+ );
92
+ });
93
+ });
94
+
95
+ async function sendProblemSolvedEmail({
96
+ context,
97
+ email,
98
+ stats,
99
+ }: {
100
+ context: WorkflowContext<UserCreatedPayload>;
101
+ email: string;
102
+ stats: { totalProblemsSolved: number };
103
+ }) {
104
+ if (stats.totalProblemsSolved === 0) {
105
+ await context.run("send no answers email", async () => {
106
+ await sendEmail(
107
+ email,
108
+ "Hey, you haven't solved any questions in the last 7 days..."
109
+ );
110
+ });
111
+ } else {
112
+ await context.run("send stats email", async () => {
113
+ await sendEmail(
114
+ email,
115
+ `You have solved ${stats.totalProblemsSolved} problems in the last 7 days. Keep it up!`
116
+ );
117
+ });
118
+ }
119
+ }
120
+
121
+ async function createUserInDatabase({
122
+ name,
123
+ email,
124
+ }: {
125
+ name: string;
126
+ email: string;
127
+ }) {
128
+ console.log("Creating a user in the database:", name, email);
129
+ return { userid: "12345" };
130
+ }
131
+
132
+ async function createNewUserInStripe(email: string) {
133
+ // Implement logic to create a new user in Stripe
134
+ console.log("Creating a user in Stripe for", email);
135
+ }
136
+
137
+ async function startTrialInStripe(email: string) {
138
+ // Implement logic to start a trial in Stripe
139
+ console.log("Starting a trial of 14 days in Stripe for", email);
140
+ }
141
+
142
+ async function getUserStats(userid: string) {
143
+ // Implement logic to get user stats
144
+ console.log("Getting user stats for", userid);
145
+ return {
146
+ totalProblemsSolved: 10_000,
147
+ mostInterestedTopic: "JavaScript",
148
+ };
149
+ }
150
+
151
+ async function checkUpgradedPlan(email: string) {
152
+ // Implement logic to check if the user has upgraded the plan
153
+ console.log("Checking if the user has upgraded the plan", email);
154
+ return false;
155
+ }
156
+
157
+ async function sendEmail(email: string, content: string) {
158
+ // Implement logic to send an email
159
+ console.log("Sending email to", email, content);
160
+ }
161
+ ```
162
+
163
+ ```python main.py
164
+ from fastapi import FastAPI
165
+ from typing import Dict, TypedDict
166
+ from upstash_workflow.fastapi import Serve
167
+ from upstash_workflow import AsyncWorkflowContext
168
+
169
+ app = FastAPI()
170
+ serve = Serve(app)
171
+
172
+
173
+ class UserCreatedPayload(TypedDict):
174
+ name: str
175
+ email: str
176
+
177
+
178
+ class UserStats(TypedDict):
179
+ total_problems_solved: int
180
+ most_interested_topic: str
181
+
182
+
183
+ async def create_user_in_database(name: str, email: str) -> Dict[str, str]:
184
+ print("Creating a user in the database:", name, email)
185
+ return {"userid": "12345"}
186
+
187
+
188
+ async def create_new_user_in_stripe(email: str) -> None:
189
+ # Implement logic to create a new user in Stripe
190
+ print("Creating a user in Stripe for", email)
191
+
192
+
193
+ async def start_trial_in_stripe(email: str) -> None:
194
+ # Implement logic to start a trial in Stripe
195
+ print("Starting a trial of 14 days in Stripe for", email)
196
+
197
+
198
+ async def get_user_stats(userid: str) -> UserStats:
199
+ # Implement logic to get user stats
200
+ print("Getting user stats for", userid)
201
+ return {"total_problems_solved": 10000, "most_interested_topic": "Python"}
202
+
203
+
204
+ async def check_upgraded_plan(email: str) -> bool:
205
+ # Implement logic to check if the user has upgraded the plan
206
+ print("Checking if the user has upgraded the plan", email)
207
+ return False
208
+
209
+
210
+ async def send_email(email: str, content: str) -> None:
211
+ # Implement logic to send an email
212
+ print("Sending email to", email, content)
213
+
214
+
215
+ async def send_problem_solved_email(
216
+ context: AsyncWorkflowContext[UserCreatedPayload], email: str, stats: UserStats
217
+ ) -> None:
218
+ if stats["total_problems_solved"] == 0:
219
+
220
+ async def _send_no_answers_email() -> None:
221
+ await send_email(
222
+ email, "Hey, you haven't solved any questions in the last 7 days..."
223
+ )
224
+
225
+ await context.run("send no answers email", _send_no_answers_email)
226
+ else:
227
+
228
+ async def _send_stats_email() -> None:
229
+ await send_email(
230
+ email,
231
+ f"You have solved {stats['total_problems_solved']} problems in the last 7 days. Keep it up!",
232
+ )
233
+
234
+ await context.run("send stats email", _send_stats_email)
235
+
236
+
237
+ @serve.post("/auth-provider-webhook")
238
+ async def auth_provider_webhook(
239
+ context: AsyncWorkflowContext[UserCreatedPayload],
240
+ ) -> None:
241
+ payload = context.request_payload
242
+ name = payload["name"]
243
+ email = payload["email"]
244
+
245
+ async def _sync_user() -> str:
246
+ return await create_user_in_database(name, email)
247
+
248
+ result = await context.run("sync user", _sync_user)
249
+ userid = result["userid"]
250
+
251
+ async def _create_new_user_in_stripe() -> None:
252
+ await create_new_user_in_stripe(email)
253
+
254
+ await context.run("create new user in stripe", _create_new_user_in_stripe)
255
+
256
+ async def _start_trial_in_stripe() -> None:
257
+ await start_trial_in_stripe(email)
258
+
259
+ await context.run("start trial in Stripe", _start_trial_in_stripe)
260
+
261
+ async def _send_welcome_email() -> None:
262
+ await send_email(
263
+ email, "Welcome to our platform!, You have 14 days of free trial."
264
+ )
265
+
266
+ await context.run("send welcome email", _send_welcome_email)
267
+
268
+ await context.sleep("wait", 7 * 24 * 60 * 60)
269
+
270
+ # get user stats and send email with them
271
+
272
+ async def _get_user_stats() -> UserStats:
273
+ return await get_user_stats(userid)
274
+
275
+ stats: UserStats = await context.run("get user stats", _get_user_stats)
276
+
277
+ await send_problem_solved_email(context, email, stats)
278
+
279
+ # wait until there are two days to the end of trial period and check upgrade status
280
+ await context.sleep("wait for trial warning", 5 * 24 * 60 * 60)
281
+
282
+ async def _check_upgraded_plan() -> bool:
283
+ return await check_upgraded_plan(email)
284
+
285
+ is_upgraded = await context.run("check upgraded plan", _check_upgraded_plan)
286
+
287
+ # end the workflow if upgraded
288
+ if is_upgraded:
289
+ return
290
+
291
+ async def _send_trial_warning_email() -> None:
292
+ await send_email(
293
+ email,
294
+ "Your trial is about to end in 2 days. Please upgrade your plan to keep using our platform.",
295
+ )
296
+
297
+ await context.run("send trial warning email", _send_trial_warning_email)
298
+
299
+ await context.sleep("wait for trial end", 2 * 24 * 60 * 60)
300
+
301
+ async def _send_trial_end_email() -> None:
302
+ await send_email(
303
+ email,
304
+ "Your trial has ended. Please upgrade your plan to keep using our platform.",
305
+ )
306
+
307
+ await context.run("send trial end email", _send_trial_end_email)
308
+
309
+ ```
310
+
311
+ </CodeGroup>
312
+
313
+ ## Code Breakdown
314
+
315
+ ### 1. Sync User
316
+
317
+ We start by creating a new user in our database:
318
+
319
+ <CodeGroup>
320
+
321
+ ```typescript TypeScript
322
+ const { userid } = await context.run("sync user", async () => {
323
+ return await createUserInDatabase({ name, email });
324
+ });
325
+ ```
326
+
327
+ ```python Python
328
+ async def _sync_user() -> str:
329
+ return await create_user_in_database(name, email)
330
+
331
+ result = await context.run("sync user", _sync_user)
332
+ userid = result["userid"]
333
+
334
+ ```
335
+
336
+ </CodeGroup>
337
+
338
+ ### 2. Create New User in Stripe
339
+
340
+ Next, we create a new user in Stripe:
341
+
342
+ <CodeGroup>
343
+
344
+ ```typescript TypeScript
345
+ await context.run("create new user in stripe", async () => {
346
+ await createNewUserInStripe(email);
347
+ });
348
+ ```
349
+
350
+ ```python Python
351
+ async def _create_new_user_in_stripe() -> None:
352
+ await create_new_user_in_stripe(email)
353
+
354
+ await context.run("create new user in stripe", _create_new_user_in_stripe)
355
+
356
+ ```
357
+
358
+ </CodeGroup>
359
+
360
+ ### 3. Start Trial in Stripe
361
+
362
+ We start a trial in Stripe:
363
+
364
+ <CodeGroup>
365
+
366
+ ```typescript TypeScript
367
+ await context.run("start trial in Stripe", async () => {
368
+ await startTrialInStripe(email);
369
+ });
370
+ ```
371
+
372
+ ```python Python
373
+ async def _start_trial_in_stripe() -> None:
374
+ await start_trial_in_stripe(email)
375
+
376
+ await context.run("start trial in Stripe", _start_trial_in_stripe)
377
+
378
+ ```
379
+
380
+ </CodeGroup>
381
+
382
+ ### 4. Send Welcome Email
383
+
384
+ We send a welcome email to the user:
385
+
386
+ <CodeGroup>
387
+
388
+ ```typescript TypeScript
389
+ await context.run("send welcome email", async () => {
390
+ await sendEmail(
391
+ email,
392
+ "Welcome to our platform!, You have 14 days of free trial."
393
+ );
394
+ });
395
+ ```
396
+
397
+ ```python Python
398
+ async def _send_welcome_email() -> None:
399
+ await send_email(
400
+ email, "Welcome to our platform!, You have 14 days of free trial."
401
+ )
402
+
403
+ await context.run("send welcome email", _send_welcome_email)
404
+
405
+ ```
406
+
407
+ </CodeGroup>
408
+
409
+ ### 5. Send Reminder Email
410
+
411
+ After 7 days, we check if the user has solved any questions. If not, we send a reminder email:
412
+
413
+ <CodeGroup>
414
+
415
+ ```typescript TypeScript
416
+ await context.sleep("wait", 7 * 24 * 60 * 60);
417
+
418
+ const stats = await context.run("get user stats", async () => {
419
+ return await getUserStats(userid);
420
+ });
421
+ await sendProblemSolvedEmail({context, email, stats});
422
+ ```
423
+
424
+ ```python Python
425
+ await context.sleep("wait", 7 * 24 * 60 * 60)
426
+
427
+ async def _get_user_stats() -> UserStats:
428
+ return await get_user_stats(userid)
429
+
430
+ stats: UserStats = await context.run("get user stats", _get_user_stats)
431
+
432
+ await send_problem_solved_email(context, email, stats)
433
+
434
+ ```
435
+
436
+ </CodeGroup>
437
+
438
+ The `sendProblemSolvedEmail` method:
439
+
440
+ <CodeGroup>
441
+
442
+ ```typescript TypeScript
443
+ async function sendProblemSolvedEmail({
444
+ context,
445
+ email,
446
+ stats,
447
+ }: {
448
+ context: WorkflowContext<UserCreatedPayload>;
449
+ email: string;
450
+ stats: { totalProblemsSolved: number };
451
+ }) {
452
+ if (stats.totalProblemsSolved === 0) {
453
+ await context.run("send no answers email", async () => {
454
+ await sendEmail(
455
+ email,
456
+ "Hey, you haven't solved any questions in the last 7 days..."
457
+ );
458
+ });
459
+ } else {
460
+ await context.run("send stats email", async () => {
461
+ await sendEmail(
462
+ email,
463
+ `You have solved ${stats.totalProblemsSolved} problems in the last 7 days. Keep it up!`
464
+ );
465
+ });
466
+ }
467
+ }
468
+ ```
469
+
470
+ ```python Python
471
+ async def send_problem_solved_email(
472
+ context: AsyncWorkflowContext[UserCreatedPayload], email: str, stats: UserStats
473
+ ) -> None:
474
+ if stats["total_problems_solved"] == 0:
475
+
476
+ async def _send_no_answers_email() -> None:
477
+ await send_email(
478
+ email, "Hey, you haven't solved any questions in the last 7 days..."
479
+ )
480
+
481
+ await context.run("send no answers email", _send_no_answers_email)
482
+ else:
483
+
484
+ async def _send_stats_email() -> None:
485
+ await send_email(
486
+ email,
487
+ f"You have solved {stats['total_problems_solved']} problems in the last 7 days. Keep it up!",
488
+ )
489
+
490
+ await context.run("send stats email", _send_stats_email)
491
+
492
+ ```
493
+
494
+ </CodeGroup>
495
+
496
+ ### 6. Send Trial Warning Email
497
+
498
+ If the user hasn't upgraded 2 days before the trial ends, we send a trial warning email:
499
+
500
+ <CodeGroup>
501
+
502
+ ```typescript TypeScript
503
+ await context.sleep("wait for trial warning", 5 * 24 * 60 * 60);
504
+
505
+ const isUpgraded = await context.run("check upgraded plan", async () => {
506
+ return await checkUpgradedPlan(email);
507
+ });
508
+
509
+ if (isUpgraded) return;
510
+
511
+ await context.run("send trial warning email", async () => {
512
+ await sendEmail(
513
+ email,
514
+ "Your trial is about to end in 2 days. Please upgrade your plan to keep using our platform."
515
+ );
516
+ });
517
+ ```
518
+
519
+ ```python Python
520
+ await context.sleep("wait for trial warning", 5 * 24 * 60 * 60)
521
+
522
+ async def _check_upgraded_plan() -> bool:
523
+ return await check_upgraded_plan(email)
524
+
525
+ is_upgraded = await context.run("check upgraded plan", _check_upgraded_plan)
526
+
527
+ # end the workflow if upgraded
528
+ if is_upgraded:
529
+ return
530
+
531
+ async def _send_trial_warning_email() -> None:
532
+ await send_email(
533
+ email,
534
+ "Your trial is about to end in 2 days. Please upgrade your plan to keep using our platform.",
535
+ )
536
+
537
+ await context.run("send trial warning email", _send_trial_warning_email)
538
+
539
+ ```
540
+
541
+ </CodeGroup>
542
+
543
+ If they upgraded, we end the workflow by returning.
544
+
545
+ ### 7. Send Trial Ended Email
546
+
547
+ If the user hasn't upgraded after the trial ends, we send a trial ended email:
548
+
549
+ <CodeGroup>
550
+
551
+ ```typescript TypeScript
552
+ await context.sleep("wait for trial end", 2 * 24 * 60 * 60);
553
+
554
+ await context.run("send trial end email", async () => {
555
+ await sendEmail(
556
+ email,
557
+ "Your trial has ended. Please upgrade your plan to keep using our platform."
558
+ );
559
+ });
560
+ ```
561
+
562
+ ```python Python
563
+ await context.sleep("wait for trial end", 2 * 24 * 60 * 60)
564
+
565
+ async def _send_trial_end_email() -> None:
566
+ await send_email(
567
+ email,
568
+ "Your trial has ended. Please upgrade your plan to keep using our platform.",
569
+ )
570
+
571
+ await context.run("send trial end email", _send_trial_end_email)
572
+
573
+ ```
574
+
575
+ </CodeGroup>