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

@@ -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() });
}