mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 15:58:38 +01:00
add code redeem
This commit is contained in:
58
dashboard/components/settings/Codes.vue
Normal file
58
dashboard/components/settings/Codes.vue
Normal 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>
|
||||||
@@ -7,6 +7,7 @@ const items = [
|
|||||||
{ label: 'General', slot: 'general' },
|
{ label: 'General', slot: 'general' },
|
||||||
{ label: 'Members', slot: 'members' },
|
{ label: 'Members', slot: 'members' },
|
||||||
{ label: 'Billing', slot: 'billing' },
|
{ label: 'Billing', slot: 'billing' },
|
||||||
|
{ label: 'Codes', slot: 'codes' },
|
||||||
{ label: 'Account', slot: 'account' }
|
{ label: 'Account', slot: 'account' }
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -27,6 +28,9 @@ const items = [
|
|||||||
<template #billing>
|
<template #billing>
|
||||||
<SettingsBilling :key="refreshKey"></SettingsBilling>
|
<SettingsBilling :key="refreshKey"></SettingsBilling>
|
||||||
</template>
|
</template>
|
||||||
|
<template #codes>
|
||||||
|
<SettingsCodes :key="refreshKey"></SettingsCodes>
|
||||||
|
</template>
|
||||||
<template #account>
|
<template #account>
|
||||||
<SettingsAccount :key="refreshKey"></SettingsAccount>
|
<SettingsAccount :key="refreshKey"></SettingsAccount>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
import { getPlanFromId } from "@data/PREMIUM";
|
import { getPlanFromId } from "@data/PREMIUM";
|
||||||
import StripeService from '~/server/services/StripeService';
|
|
||||||
import { PREMIUM_PLAN } from "../../../../shared/data/PREMIUM";
|
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) {
|
function getPlanToActivate(current_plan_id: number) {
|
||||||
if (current_plan_id === PREMIUM_PLAN.FREE.ID) {
|
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 });
|
const data = await getRequestData(event, { requireSchema: false, allowGuests: false, allowLitlyx: false });
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
|
|
||||||
const { project, pid } = data;
|
const { project, pid, user } = data;
|
||||||
|
|
||||||
const body = await readBody(event);
|
const body = await readBody(event);
|
||||||
|
|
||||||
const { code } = body;
|
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);
|
const currentPlan = getPlanFromId(project.premium_type);
|
||||||
if (!currentPlan) return setResponseStatus(event, 400, 'Current plan not found');
|
if (!currentPlan) return setResponseStatus(event, 400, 'Current plan not found');
|
||||||
const planToActivate = getPlanToActivate(currentPlan.ID);
|
const planToActivate = getPlanToActivate(currentPlan.ID);
|
||||||
if (!planToActivate) return setResponseStatus(event, 400, 'Cannot use code on current plan');
|
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 StripeService.createSubscription(project.customer_id, planToActivate.ID);
|
||||||
|
|
||||||
await useAppsumoCode(code);
|
await useAppsumoCode(pid, code);
|
||||||
|
|
||||||
});
|
});
|
||||||
14
dashboard/server/api/pay/valid_codes.ts
Normal file
14
dashboard/server/api/pay/valid_codes.ts
Normal 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 }
|
||||||
|
|
||||||
|
});
|
||||||
@@ -133,7 +133,7 @@ async function onPaymentSuccess(event: Event.InvoicePaidEvent) {
|
|||||||
if (!price) return { error: 'Price not found' }
|
if (!price) return { error: 'Price not found' }
|
||||||
|
|
||||||
const PLAN = getPlanFromPrice(price, StripeService.testMode || false);
|
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)
|
await addSubscriptionToProject(project._id.toString(), PLAN, subscription_id, currentSubscription.current_period_start, currentSubscription.current_period_end)
|
||||||
|
|
||||||
|
|||||||
@@ -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) {
|
export async function checkAppsumoCode(code: string) {
|
||||||
const target = await AppsumoCodeModel.exists({ code, used_at: { $exists: false } });
|
const target = await AppsumoCodeModel.exists({ code, used_at: { $exists: false } });
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function useAppsumoCode(project_id: string, code: string) {
|
||||||
export async function useAppsumoCode(code: string) {
|
await AppsumoCodeTryModel.updateOne({ project_id }, { $push: { valid_codes: code } }, { upsert: true });
|
||||||
await AppsumoCodeModel.updateOne({ code }, { used_at: Date.now() });
|
await AppsumoCodeModel.updateOne({ code }, { used_at: Date.now() });
|
||||||
}
|
}
|
||||||
@@ -131,7 +131,7 @@ export const PREMIUM_PLAN: Record<PREMIUM_TAG, PREMIUM_DATA> = {
|
|||||||
ID: 6001,
|
ID: 6001,
|
||||||
COUNT_LIMIT: 50_000,
|
COUNT_LIMIT: 50_000,
|
||||||
AI_MESSAGE_LIMIT: 30,
|
AI_MESSAGE_LIMIT: 30,
|
||||||
PRICE: '',
|
PRICE: 'price_1QIXwbB2lPUiVs9VKSsoksaU',
|
||||||
PRICE_TEST: '',
|
PRICE_TEST: '',
|
||||||
COST: 0
|
COST: 0
|
||||||
},
|
},
|
||||||
@@ -139,7 +139,7 @@ export const PREMIUM_PLAN: Record<PREMIUM_TAG, PREMIUM_DATA> = {
|
|||||||
ID: 6002,
|
ID: 6002,
|
||||||
COUNT_LIMIT: 150_000,
|
COUNT_LIMIT: 150_000,
|
||||||
AI_MESSAGE_LIMIT: 100,
|
AI_MESSAGE_LIMIT: 100,
|
||||||
PRICE: '',
|
PRICE: 'price_1QIXxRB2lPUiVs9VrjaVRoOl',
|
||||||
PRICE_TEST: '',
|
PRICE_TEST: '',
|
||||||
COST: 0
|
COST: 0
|
||||||
},
|
},
|
||||||
@@ -147,7 +147,7 @@ export const PREMIUM_PLAN: Record<PREMIUM_TAG, PREMIUM_DATA> = {
|
|||||||
ID: 6003,
|
ID: 6003,
|
||||||
COUNT_LIMIT: 500_000,
|
COUNT_LIMIT: 500_000,
|
||||||
AI_MESSAGE_LIMIT: 3_000,
|
AI_MESSAGE_LIMIT: 3_000,
|
||||||
PRICE: '',
|
PRICE: 'price_1QIXy8B2lPUiVs9VQBOUPAoE',
|
||||||
PRICE_TEST: '',
|
PRICE_TEST: '',
|
||||||
COST: 0
|
COST: 0
|
||||||
},
|
},
|
||||||
|
|||||||
15
shared/schema/AppsumoCodeTrySchema.ts
Normal file
15
shared/schema/AppsumoCodeTrySchema.ts
Normal 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);
|
||||||
Reference in New Issue
Block a user