From 6067426326c2d81f200d21885c28220400758480 Mon Sep 17 00:00:00 2001 From: Ethan Liu Date: Wed, 21 Jun 2023 18:16:42 +0800 Subject: [PATCH] feat: Add promotion codes for Product Hunt/Twitter platforms --- CHANGE_LOG.md | 3 +- CHANGE_LOG.zh_CN.md | 3 +- prisma/schema.prisma | 38 ++++++----- src/app/api/licenses/activate/route.ts | 41 ++++++++++-- .../chatSection/chatFooter/index.tsx | 65 +++++++++++-------- src/components/premium/activate.tsx | 2 + src/locales/en.json | 2 + src/locales/zh-CN.json | 2 + 8 files changed, 104 insertions(+), 52 deletions(-) diff --git a/CHANGE_LOG.md b/CHANGE_LOG.md index 036f7af..d597818 100644 --- a/CHANGE_LOG.md +++ b/CHANGE_LOG.md @@ -2,7 +2,7 @@ ## v0.7.0 -> 2023-06-20 +> 2023-06-21 ### Fixed @@ -17,6 +17,7 @@ - Guidance when there is insufficient Token for adding: Start Free Trial or Recharge Token - Added token allocation details for Free trial and Premium - Added daily scheduled task to calculate token consumption +- Add promotion codes for Product Hunt/Twitter platforms ### Changed diff --git a/CHANGE_LOG.zh_CN.md b/CHANGE_LOG.zh_CN.md index 768d828..2351abb 100644 --- a/CHANGE_LOG.zh_CN.md +++ b/CHANGE_LOG.zh_CN.md @@ -2,7 +2,7 @@ ## v0.7.0 -> 2023-06-20 +> 2023-06-21 ### 修复 @@ -17,6 +17,7 @@ - 新增 Token 不足时的引导:开始免费试用 or 充值 Token - 新增 Free trial 和 Premium 的 token 赠送数额说明 - 新增定时任务每日核算 Token 消耗 +- 新增 Product Hunt/Twitter 平台活动码 ### 调整 diff --git a/prisma/schema.prisma b/prisma/schema.prisma index cc44247..87b7e37 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -47,24 +47,26 @@ model VerificationToken { } model User { - id String @id @default(cuid()) - name String? - email String? @unique - emailVerified DateTime? - image String? - sessions Session[] - accounts Account[] - createdAt DateTime @default(now()) - updatedAt DateTime? @updatedAt - recentlyUse DateTime? - share Share[] - costTokens Int @default(0) - costUSD Float @default(0) - Cost Cost[] - availableTokens Int @default(0) - freeTrialed Int @default(0) - license_type String? - License License[] + id String @id @default(cuid()) + name String? + email String? @unique + emailVerified DateTime? + image String? + sessions Session[] + accounts Account[] + createdAt DateTime @default(now()) + updatedAt DateTime? @updatedAt + recentlyUse DateTime? + share Share[] + costTokens Int @default(0) + costUSD Float @default(0) + Cost Cost[] + availableTokens Int @default(0) + license_type String? + License License[] + freeTrialed Int @default(0) + productHuntTrialed Int @default(0) + twitterTrialed Int @default(0) } model License { diff --git a/src/app/api/licenses/activate/route.ts b/src/app/api/licenses/activate/route.ts index 9f7af15..33bfb26 100644 --- a/src/app/api/licenses/activate/route.ts +++ b/src/app/api/licenses/activate/route.ts @@ -4,6 +4,19 @@ import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import { prisma } from "@/lib/prisma"; import { LResponseError } from "@/lib"; +const activityCode = [ + { + code: "L-GPT-PH-CODE", + label: "productHuntTrialed", + value: 15000, + }, + { + code: "L-GPT-TWITTER-CODE-2306", + label: "twitterTrialed", + value: 15000, + }, +]; + export async function POST(request: Request) { const session = await getServerSession(authOptions); @@ -17,6 +30,29 @@ export async function POST(request: Request) { const { license_key, instance_name } = await request.json(); + const findActivity = activityCode.find((p) => p.code === license_key); + + if (findActivity) { + if ((user as any)[findActivity.label]) { + return LResponseError("You have already used this license key."); + } + + const updateData = { + availableTokens: user.availableTokens + findActivity.value, + [findActivity.label]: 1, + }; + + await prisma.user.update({ + data: updateData, + where: { id: session?.user.id }, + }); + + return NextResponse.json( + { error: 0, data: { type: "activity" } }, + { status: 200 } + ); + } + try { const variants = await fetch("https://api.lemonsqueezy.com/v1/variants", { headers: { @@ -130,9 +166,6 @@ export async function POST(request: Request) { return NextResponse.json({ error: 0, data: { type } }, { status: 200 }); } catch (error) { console.log(error, "validate licenses error"); - return NextResponse.json( - { error: -1, msg: "validate licenses error" }, - { status: 500 } - ); + return LResponseError("validate licenses error"); } } diff --git a/src/components/chatSection/chatFooter/index.tsx b/src/components/chatSection/chatFooter/index.tsx index c200fff..b76f536 100644 --- a/src/components/chatSection/chatFooter/index.tsx +++ b/src/components/chatSection/chatFooter/index.tsx @@ -24,7 +24,7 @@ import { BASE_PROMPT, ChannelListItem, } from "@/hooks"; -import { Button, Confirm } from "@/components/ui"; +import { Button, Confirm, Link } from "@/components/ui"; import { useScrollToBottom } from "@/components/scrollToBottoms"; import { isMobile, cn, calcTokens } from "@/lib"; import type { IShare } from "@/app/api/share/route"; @@ -611,33 +611,42 @@ const ChatFooter: React.FC = () => { "dark:via-neutral-900 dark:to-neutral-900" )} > - {!!findChannel?.chat_list?.length && ( -
- - -
- )} +
+ {!!findChannel?.chat_list?.length && ( + <> + + + + )} + + {tCommon("docs")} + +
diff --git a/src/components/premium/activate.tsx b/src/components/premium/activate.tsx index 339fb9d..1d71244 100644 --- a/src/components/premium/activate.tsx +++ b/src/components/premium/activate.tsx @@ -60,6 +60,8 @@ const Activate = React.forwardRef((_, forwardedRef) => { toastInfo = t("tokens-recharged"); } else if (res.data.type === "license") { toastInfo = t("license-key-activated"); + } else if (res.data.type === "activity") { + toastInfo = t("gift-code-used-success"); } toast.success(toastInfo, { id: "activated", duration: 5000 }); diff --git a/src/locales/en.json b/src/locales/en.json index 77d801a..0a199d0 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -63,6 +63,7 @@ "add": "Add", "cancel": "Cancel", "create": "Create", + "docs": "Docs", "ok": "OK", "please-enter": "Please enter", "premium": "L-GPT Premium", @@ -105,6 +106,7 @@ "free-trial-already": "Free Trial already used", "get-free-trial": "Get L-GPT Free Trial", "get-more-token": "Get more tokens?", + "gift-code-used-success": "Gift code used successfully", "learn-more-license": "Learn more about L-GPT License...", "license": "L-GPT License", "license-activate": "L-GPT License Activate", diff --git a/src/locales/zh-CN.json b/src/locales/zh-CN.json index 4c3a5ff..8fa07bb 100644 --- a/src/locales/zh-CN.json +++ b/src/locales/zh-CN.json @@ -63,6 +63,7 @@ "add": "新 增", "cancel": "取 消", "create": "新 增", + "docs": "文档", "ok": "确 定", "please-enter": "请输入", "premium": "L-GPT 高级版", @@ -105,6 +106,7 @@ "free-trial-already": "已免费试用", "get-free-trial": "获取免费试用", "get-more-token": "获取更多Token?", + "gift-code-used-success": "礼品码使用成功", "learn-more-license": "了解更多关于L-GPT License的信息...", "license": "L-GPT 许可证", "license-activate": "L-GPT 许可证激活",