user.email account.plan_name - cleanup context and service construction - config for stripe callback - initial plan
This commit is contained in:
@@ -22,5 +22,5 @@ export type AppRouter = typeof appRouter;
|
||||
export default createNuxtApiHandler({
|
||||
router: appRouter,
|
||||
createContext: createContext,
|
||||
onError({ error}) { console.error(error)}, // TODO - logging and reporting
|
||||
onError({ error}) { console.error(error)},
|
||||
})
|
||||
|
||||
@@ -1,26 +1,24 @@
|
||||
import { Account } from '@prisma/client';
|
||||
import { ACCOUNT_ACCESS } from '@prisma/client';
|
||||
import Stripe from 'stripe';
|
||||
import UserAccountService from '~~/lib/services/user.account.service';
|
||||
import prisma_client from '~~/prisma/prisma.client';
|
||||
import UserAccountService, { AccountWithMembers } from '~~/lib/services/user.account.service';
|
||||
|
||||
const config = useRuntimeConfig();
|
||||
const stripe = new Stripe(config.stripeSecretKey, { apiVersion: '2022-11-15' });
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const YOUR_DOMAIN = 'http://localhost:3000'; // TODO - pull from somewhere, this is shit
|
||||
|
||||
const body = await readBody(event)
|
||||
let { price_id, account_id} = body;
|
||||
account_id = +account_id
|
||||
console.log(`session.post.ts recieved price_id:${price_id}, account_id:${account_id}`);
|
||||
|
||||
const userService = new UserAccountService(prisma_client);
|
||||
const account: Account = await userService.getAccountById(account_id);
|
||||
const userService = new UserAccountService();
|
||||
const account: AccountWithMembers = await userService.getAccountById(account_id);
|
||||
let customer_id: string
|
||||
if(!account.stripe_customer_id){
|
||||
// need to pre-emptively create a Stripe user for this account (use name for now, just so is visible on dashboard) TODO - include Email
|
||||
console.log(`Creating account with name ${account.name}`);
|
||||
const customer = await stripe.customers.create({ name: account.name });
|
||||
// need to pre-emptively create a Stripe user for this account so we know who they are when the webhook comes back
|
||||
const owner = account.members.find(member => (member.access == ACCOUNT_ACCESS.OWNER))
|
||||
console.log(`Creating account with name ${account.name} and email ${owner?.user.email}`);
|
||||
const customer = await stripe.customers.create({ name: account.name, email: owner?.user.email });
|
||||
customer_id = customer.id;
|
||||
userService.updateAccountStipeCustomerId(account_id, customer.id);
|
||||
} else {
|
||||
@@ -39,15 +37,15 @@ export default defineEventHandler(async (event) => {
|
||||
// {CHECKOUT_SESSION_ID} is a string literal; do not change it!
|
||||
// the actual Session ID is returned in the query parameter when your customer
|
||||
// is redirected to the success page.
|
||||
success_url: `${YOUR_DOMAIN}/success?session_id={CHECKOUT_SESSION_ID}`,
|
||||
cancel_url: `${YOUR_DOMAIN}/cancel`,
|
||||
success_url: `${config.stripeCallbackUrl}/success?session_id={CHECKOUT_SESSION_ID}`,
|
||||
cancel_url: `${config.stripeCallbackUrl}/cancel`,
|
||||
customer: customer_id
|
||||
});
|
||||
|
||||
if(session?.url){
|
||||
return sendRedirect(event, session.url, 303);
|
||||
} else {
|
||||
return sendRedirect(event, `${YOUR_DOMAIN}/fail`, 303);
|
||||
return sendRedirect(event, `${config.stripeCallbackUrl}/fail`, 303);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import Stripe from 'stripe';
|
||||
import UserAccountService from '~~/lib/services/user.account.service';
|
||||
import prisma_client from '~~/prisma/prisma.client';
|
||||
|
||||
const config = useRuntimeConfig();
|
||||
const stripe = new Stripe(config.stripeSecretKey, { apiVersion: '2022-11-15' });
|
||||
@@ -22,15 +21,15 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
catch (err) {
|
||||
console.log(err);
|
||||
throw createError({ statusCode: 400, statusMessage: `Webhook Error` }); // ${(err as Error).message}
|
||||
throw createError({ statusCode: 400, statusMessage: `Error validating Webhook Event` });
|
||||
}
|
||||
|
||||
console.log(`****** Web Hook Recieved (${stripeEvent.type}) ******`);
|
||||
|
||||
if(stripeEvent.type && stripeEvent.type.startsWith('customer.subscription')){
|
||||
console.log(`****** Web Hook Recieved (${stripeEvent.type}) ******`);
|
||||
|
||||
let subscription = stripeEvent.data.object as Stripe.Subscription;
|
||||
|
||||
const userService = new UserAccountService(prisma_client);
|
||||
const userService = new UserAccountService();
|
||||
|
||||
let current_period_ends: Date = new Date(subscription.current_period_end * 1000);
|
||||
current_period_ends.setDate(current_period_ends.getDate() + config.subscriptionGraceDays);
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { inferAsyncReturnType, TRPCError } from '@trpc/server'
|
||||
import { H3Event } from 'h3';
|
||||
import { serverSupabaseClient } from '#supabase/server';
|
||||
@@ -6,7 +5,6 @@ import SupabaseClient from '@supabase/supabase-js/dist/module/SupabaseClient';
|
||||
import { User } from '@supabase/supabase-js';
|
||||
import UserAccountService, { FullDBUser } from '~~/lib/services/user.account.service';
|
||||
|
||||
let prisma: PrismaClient | undefined
|
||||
let supabase: SupabaseClient | undefined
|
||||
|
||||
export async function createContext(event: H3Event){
|
||||
@@ -19,32 +17,25 @@ export async function createContext(event: H3Event){
|
||||
if (!user) {
|
||||
({data: { user }} = await supabase.auth.getUser());
|
||||
}
|
||||
if (!prisma) {
|
||||
prisma = new PrismaClient()
|
||||
}
|
||||
if (!dbUser && user) {
|
||||
const userService = new UserAccountService(prisma);
|
||||
const userService = new UserAccountService();
|
||||
dbUser = await userService.getFullUserBySupabaseId(user.id);
|
||||
|
||||
if (!dbUser && user) {
|
||||
dbUser = await userService.createUser( user.id, user.user_metadata.full_name );
|
||||
dbUser = await userService.createUser( user.id, user.user_metadata.full_name, user.email?user.email:"no@email.supplied" );
|
||||
console.log(`\n Created user \n ${JSON.stringify(dbUser)}\n`);
|
||||
}
|
||||
}
|
||||
|
||||
if(!supabase || !user || !prisma || !dbUser) {
|
||||
|
||||
if(!user || !dbUser) {
|
||||
throw new TRPCError({
|
||||
code: 'INTERNAL_SERVER_ERROR',
|
||||
message: `Unable to fetch user data, please try again later. Missing ->[supabase:${(!supabase)},user:${(!user)},prisma:${(!prisma)},dbUser:${(!dbUser)}, ]`,
|
||||
message: `Unable to fetch user data, please try again later. Missing ->[user:${(!user)},dbUser:${(!dbUser)}]`,
|
||||
});
|
||||
}
|
||||
|
||||
// TODO - This seems excessive, trim context when I have figured out what I actually need
|
||||
return {
|
||||
supabase,
|
||||
user,
|
||||
prisma,
|
||||
dbUser,
|
||||
}
|
||||
};
|
||||
|
||||
@@ -6,7 +6,7 @@ export const notesRouter = router({
|
||||
getForCurrentUser: protectedProcedure
|
||||
.input(z.object({ account_id: z.number() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const notesService = new NotesService(ctx.prisma);
|
||||
const notesService = new NotesService();
|
||||
const notes = await notesService.getNotesForAccountId(input.account_id);
|
||||
return {
|
||||
notes,
|
||||
@@ -15,7 +15,7 @@ export const notesRouter = router({
|
||||
getById: publicProcedure
|
||||
.input(z.object({ note_id: z.number() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const notesService = new NotesService(ctx.prisma);
|
||||
const notesService = new NotesService();
|
||||
const note = await notesService.getNoteById(input.note_id);
|
||||
return {
|
||||
note,
|
||||
|
||||
@@ -13,7 +13,7 @@ export const userAccountRouter = router({
|
||||
changeAccountPlan: adminProcedure
|
||||
.input(z.object({ account_id: z.number(), plan_id: z.number() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const uaService = new UserAccountService(ctx.prisma);
|
||||
const uaService = new UserAccountService();
|
||||
const account = await uaService.changeAccountPlan(input.account_id, input.plan_id);
|
||||
|
||||
return {
|
||||
@@ -23,7 +23,7 @@ export const userAccountRouter = router({
|
||||
joinUserToAccount: adminProcedure
|
||||
.input(z.object({ account_id: z.number(), user_id: z.number() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const uaService = new UserAccountService(ctx.prisma);
|
||||
const uaService = new UserAccountService();
|
||||
const membership = (ctx.dbUser?.id)?await uaService.joinUserToAccount(input.user_id, input.account_id):null;
|
||||
return {
|
||||
membership,
|
||||
@@ -32,7 +32,7 @@ export const userAccountRouter = router({
|
||||
changeUserAccessWithinAccount: adminProcedure
|
||||
.input(z.object({ user_id: z.number(), account_id: z.number(), access: z.enum([ACCOUNT_ACCESS.ADMIN, ACCOUNT_ACCESS.OWNER, ACCOUNT_ACCESS.READ_ONLY, ACCOUNT_ACCESS.READ_WRITE]) }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const uaService = new UserAccountService(ctx.prisma);
|
||||
const uaService = new UserAccountService();
|
||||
const membership = await uaService.changeUserAccessWithinAccount(input.user_id, input.account_id, input.access);
|
||||
|
||||
return {
|
||||
@@ -42,7 +42,7 @@ export const userAccountRouter = router({
|
||||
claimOwnershipOfAccount: adminProcedure
|
||||
.input(z.object({ account_id: z.number() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const uaService = new UserAccountService(ctx.prisma);
|
||||
const uaService = new UserAccountService();
|
||||
const membership = await uaService.claimOwnershipOfAccount(ctx.dbUser.id, input.account_id);
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user