add code redeem

This commit is contained in:
Emily
2024-11-08 15:14:09 +01:00
parent 4d7cfbb7b9
commit f06d7d78fc
9 changed files with 120 additions and 16 deletions

View File

@@ -0,0 +1,58 @@
<script lang="ts" setup>
import type { TApiSettings } from '@schema/ApiSettingsSchema';
import type { SettingsTemplateEntry } from './Template.vue';
const { project } = useProject();
const entries: SettingsTemplateEntry[] = [
{ id: 'acodes', title: 'Appsumo codes', text: 'Redeem appsumo codes' },
]
const { createAlert } = useAlert()
const currentCode = ref<string>("");
const redeeming = ref<boolean>(false);
const valid_codes = useFetch('/api/pay/valid_codes', signHeaders({ 'x-pid': project.value?._id.toString() ?? '' }));
async function redeemCode() {
redeeming.value = true;
try {
const res = await $fetch<TApiSettings>('/api/pay/redeem_appsumo_code', {
method: 'POST', ...signHeaders({
'Content-Type': 'application/json',
'x-pid': project.value?._id.toString() ?? ''
}),
body: JSON.stringify({ code: currentCode.value })
});
createAlert('Success', 'Code redeem success.', 'far fa-check', 5000);
valid_codes.refresh();
} catch (ex: any) {
createAlert('Error', ex?.response?.statusText || 'Unexpected error. Contact support.', 'far fa-error', 5000);
} finally {
currentCode.value = '';
}
redeeming.value = false;
}
</script>
<template>
<SettingsTemplate :entries="entries" :key="project?.name || 'NONE'">
<template #acodes>
<div class="flex items-center gap-4">
<LyxUiInput class="w-full px-4 py-2" placeholder="Appsumo code" v-model="currentCode"></LyxUiInput>
<LyxUiButton v-if="!redeeming" :disabled="currentCode.length == 0" @click="redeemCode()" type="primary">
Redeem
</LyxUiButton>
<div v-if="redeeming">
Redeeming...
</div>
</div>
<div class="text-lyx-text-darker mt-1 text-[.9rem] poppins">
Redeemed codes: {{ valid_codes.data.value?.count || '0' }}
</div>
</template>
</SettingsTemplate>
</template>

View File

@@ -7,6 +7,7 @@ const items = [
{ label: 'General', slot: 'general' },
{ label: 'Members', slot: 'members' },
{ label: 'Billing', slot: 'billing' },
{ label: 'Codes', slot: 'codes' },
{ label: 'Account', slot: 'account' }
]
@@ -27,6 +28,9 @@ const items = [
<template #billing>
<SettingsBilling :key="refreshKey"></SettingsBilling>
</template>
<template #codes>
<SettingsCodes :key="refreshKey"></SettingsCodes>
</template>
<template #account>
<SettingsAccount :key="refreshKey"></SettingsAccount>
</template>

View File

@@ -1,9 +1,7 @@
import { getPlanFromId } from "@data/PREMIUM";
import StripeService from '~/server/services/StripeService';
import { PREMIUM_PLAN } from "../../../../shared/data/PREMIUM";
import { checkAppsumoCode, useAppsumoCode } from "~/server/services/AppsumoService";
import { canTryAppsumoCode, checkAppsumoCode, useAppsumoCode, useTryAppsumoCode } from "~/server/services/AppsumoService";
import StripeService from '~/server/services/StripeService';
function getPlanToActivate(current_plan_id: number) {
if (current_plan_id === PREMIUM_PLAN.FREE.ID) {
@@ -28,24 +26,26 @@ export default defineEventHandler(async event => {
const data = await getRequestData(event, { requireSchema: false, allowGuests: false, allowLitlyx: false });
if (!data) return;
const { project, pid } = data;
const { project, pid, user } = data;
const body = await readBody(event);
const { code } = body;
const valid = await checkAppsumoCode(code);
const canTry = await canTryAppsumoCode(pid);
if (!canTry) return setResponseStatus(event, 400, 'You tried too much codes. Please contact support.');
await useTryAppsumoCode(pid, code);
if (!valid) return setResponseStatus(event, 400, 'Current plan not found');
const valid = await checkAppsumoCode(code);
if (!valid) return setResponseStatus(event, 400, 'Code not valid');
const currentPlan = getPlanFromId(project.premium_type);
if (!currentPlan) return setResponseStatus(event, 400, 'Current plan not found');
const planToActivate = getPlanToActivate(currentPlan.ID);
if (!planToActivate) return setResponseStatus(event, 400, 'Cannot use code on current plan');
await StripeService.deleteSubscription(project.subscription_id);
await StripeService.createSubscription(project.customer_id, planToActivate.ID);
await useAppsumoCode(code);
await useAppsumoCode(pid, code);
});

View File

@@ -0,0 +1,14 @@
import { AppsumoCodeTryModel } from "@schema/AppsumoCodeTrySchema";
export default defineEventHandler(async event => {
const data = await getRequestData(event, { requireSchema: false, allowGuests: false, allowLitlyx: false });
if (!data) return;
const { pid } = data;
const tryRes = await AppsumoCodeTryModel.findOne({ project_id: pid }, { valid_codes: 1 });
if (!tryRes) return { count: 0 }
return { count: tryRes.valid_codes.length }
});

View File

@@ -133,7 +133,7 @@ async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
if (!price) return { error: 'Price not found' }
const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
if (!PLAN) return { error: 'Plan not found' }
if (!PLAN) return { error: `Plan not found. Price: ${price}. TestMode: ${StripeService.testMode}` }
await addSubscriptionToProject(project._id.toString(), PLAN, subscription_id, currentSubscription.current_period_start, currentSubscription.current_period_end)

View File

@@ -1,13 +1,26 @@
import { AppsumoCodeModel } from '@schema/AppsumoCode'
import { AppsumoCodeModel } from '@schema/AppsumoCodeSchema';
import { AppsumoCodeTryModel } from '@schema/AppsumoCodeTrySchema';
export async function canTryAppsumoCode(project_id: string) {
const tries = await AppsumoCodeTryModel.findOne({ project_id });
if (!tries) return true;
if (tries.codes.length >= 30) return false;
return true;
}
export async function useTryAppsumoCode(project_id: string, code: string) {
await AppsumoCodeTryModel.updateOne({ project_id }, { $push: { codes: code } }, { upsert: true });
}
export async function checkAppsumoCode(code: string) {
const target = await AppsumoCodeModel.exists({ code, used_at: { $exists: false } });
return target;
}
export async function useAppsumoCode(code: string) {
export async function useAppsumoCode(project_id: string, code: string) {
await AppsumoCodeTryModel.updateOne({ project_id }, { $push: { valid_codes: code } }, { upsert: true });
await AppsumoCodeModel.updateOne({ code }, { used_at: Date.now() });
}

View File

@@ -131,7 +131,7 @@ export const PREMIUM_PLAN: Record<PREMIUM_TAG, PREMIUM_DATA> = {
ID: 6001,
COUNT_LIMIT: 50_000,
AI_MESSAGE_LIMIT: 30,
PRICE: '',
PRICE: 'price_1QIXwbB2lPUiVs9VKSsoksaU',
PRICE_TEST: '',
COST: 0
},
@@ -139,7 +139,7 @@ export const PREMIUM_PLAN: Record<PREMIUM_TAG, PREMIUM_DATA> = {
ID: 6002,
COUNT_LIMIT: 150_000,
AI_MESSAGE_LIMIT: 100,
PRICE: '',
PRICE: 'price_1QIXxRB2lPUiVs9VrjaVRoOl',
PRICE_TEST: '',
COST: 0
},
@@ -147,7 +147,7 @@ export const PREMIUM_PLAN: Record<PREMIUM_TAG, PREMIUM_DATA> = {
ID: 6003,
COUNT_LIMIT: 500_000,
AI_MESSAGE_LIMIT: 3_000,
PRICE: '',
PRICE: 'price_1QIXy8B2lPUiVs9VQBOUPAoE',
PRICE_TEST: '',
COST: 0
},

View File

@@ -0,0 +1,15 @@
import { model, Schema, Types } from 'mongoose';
export type TAppsumoCodeTry = {
project_id: Types.ObjectId,
codes: string[],
valid_codes: string[],
}
const AppsumoCodeTrySchema = new Schema<TAppsumoCodeTry>({
project_id: { type: Schema.Types.ObjectId, required: true, unique: true, index: 1 },
codes: [{ type: String }],
valid_codes: [{ type: String }]
});
export const AppsumoCodeTryModel = model<TAppsumoCodeTry>('appsumo_codes_tries', AppsumoCodeTrySchema);