refactor service layer
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { ACCOUNT_ACCESS } from '@prisma/client';
|
||||
import Stripe from 'stripe';
|
||||
import UserAccountService, { AccountWithMembers } from '~~/lib/services/user.account.service';
|
||||
import AccountService from '~~/lib/services/account.service';
|
||||
import { AccountWithMembers } from '~~/lib/services/service.types';
|
||||
|
||||
const config = useRuntimeConfig();
|
||||
const stripe = new Stripe(config.stripeSecretKey, { apiVersion: '2022-11-15' });
|
||||
@@ -11,8 +12,8 @@ export default defineEventHandler(async (event) => {
|
||||
account_id = +account_id
|
||||
console.log(`session.post.ts recieved price_id:${price_id}, account_id:${account_id}`);
|
||||
|
||||
const userService = new UserAccountService();
|
||||
const account: AccountWithMembers = await userService.getAccountById(account_id);
|
||||
const accountService = new AccountService();
|
||||
const account: AccountWithMembers = await accountService.getAccountById(account_id);
|
||||
let customer_id: string
|
||||
if(!account.stripe_customer_id){
|
||||
// need to pre-emptively create a Stripe user for this account so we know who they are when the webhook comes back
|
||||
@@ -20,7 +21,7 @@ export default defineEventHandler(async (event) => {
|
||||
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);
|
||||
accountService.updateAccountStipeCustomerId(account_id, customer.id);
|
||||
} else {
|
||||
customer_id = account.stripe_customer_id;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import Stripe from 'stripe';
|
||||
import UserAccountService from '~~/lib/services/user.account.service';
|
||||
import AccountService from '~~/lib/services/account.service';
|
||||
|
||||
const config = useRuntimeConfig();
|
||||
const stripe = new Stripe(config.stripeSecretKey, { apiVersion: '2022-11-15' });
|
||||
@@ -36,13 +36,13 @@ export default defineEventHandler(async (event) => {
|
||||
throw createError({ statusCode: 400, statusMessage: `Error validating Webhook Event` });
|
||||
}
|
||||
|
||||
const userService = new UserAccountService();
|
||||
const accountService = new AccountService();
|
||||
|
||||
let current_period_ends: Date = new Date(subscription.current_period_end * 1000);
|
||||
current_period_ends.setDate(current_period_ends.getDate() + config.subscriptionGraceDays);
|
||||
|
||||
console.log(`updating stripe sub details subscription.current_period_end:${subscription.current_period_end}, subscription.id:${subscription.id}, stripe_product_id:${stripe_product_id}`);
|
||||
userService.updateStripeSubscriptionDetailsForAccount(subscription.customer.toString(), subscription.id, current_period_ends, stripe_product_id);
|
||||
accountService.updateStripeSubscriptionDetailsForAccount(subscription.customer.toString(), subscription.id, current_period_ends, stripe_product_id);
|
||||
}
|
||||
}
|
||||
return `handled ${stripeEvent.type}.`;
|
||||
|
||||
@@ -2,7 +2,8 @@ import { inferAsyncReturnType, TRPCError } from '@trpc/server'
|
||||
import { H3Event } from 'h3';
|
||||
import { serverSupabaseUser } from '#supabase/server'
|
||||
import { User } from '@supabase/supabase-js';
|
||||
import UserAccountService, { FullDBUser } from '~~/lib/services/user.account.service';
|
||||
import { FullDBUser } from '~~/lib/services/service.types';
|
||||
import AuthService from '~~/lib/services/auth.service';
|
||||
|
||||
export async function createContext(event: H3Event){
|
||||
let user: User | null = null;
|
||||
@@ -12,11 +13,11 @@ export async function createContext(event: H3Event){
|
||||
user = await serverSupabaseUser(event);
|
||||
}
|
||||
if (!dbUser && user) {
|
||||
const userService = new UserAccountService();
|
||||
dbUser = await userService.getFullUserBySupabaseId(user.id);
|
||||
const authService = new AuthService();
|
||||
dbUser = await authService.getFullUserBySupabaseId(user.id);
|
||||
|
||||
if (!dbUser && user) {
|
||||
dbUser = await userService.createUser(user.id, user.user_metadata.full_name?user.user_metadata.full_name:"no name supplied", user.email?user.email:"no@email.supplied" );
|
||||
dbUser = await authService.createUser(user.id, user.user_metadata.full_name?user.user_metadata.full_name:"no name supplied", user.email?user.email:"no@email.supplied" );
|
||||
console.log(`\n Created DB User \n ${JSON.stringify(dbUser)}\n`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
import UserAccountService from '~~/lib/services/user.account.service';
|
||||
import { router, adminProcedure } from '../trpc'
|
||||
import { ACCOUNT_ACCESS } from '@prisma/client';
|
||||
import { z } from 'zod';
|
||||
import AccountService from '~~/lib/services/account.service';
|
||||
import { MembershipWithAccount } from '~~/lib/services/service.types';
|
||||
|
||||
export const accountRouter = router({
|
||||
changeAccountName: adminProcedure
|
||||
.input(z.object({ account_id: z.number(), new_name: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const uaService = new UserAccountService();
|
||||
const account = await uaService.changeAccountName(input.account_id, input.new_name);
|
||||
const accountService = new AccountService();
|
||||
const account = await accountService.changeAccountName(input.account_id, input.new_name);
|
||||
|
||||
return {
|
||||
account,
|
||||
@@ -17,8 +18,8 @@ export const accountRouter = router({
|
||||
changeAccountPlan: adminProcedure
|
||||
.input(z.object({ account_id: z.number(), plan_id: z.number() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const uaService = new UserAccountService();
|
||||
const account = await uaService.changeAccountPlan(input.account_id, input.plan_id);
|
||||
const accountService = new AccountService();
|
||||
const account = await accountService.changeAccountPlan(input.account_id, input.plan_id);
|
||||
|
||||
return {
|
||||
account,
|
||||
@@ -27,8 +28,8 @@ export const accountRouter = router({
|
||||
joinUserToAccount: adminProcedure
|
||||
.input(z.object({ account_id: z.number(), user_id: z.number() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const uaService = new UserAccountService();
|
||||
const membership = (ctx.dbUser?.id)?await uaService.joinUserToAccount(input.user_id, input.account_id):null;
|
||||
const accountService = new AccountService();
|
||||
const membership: MembershipWithAccount| null = (ctx.dbUser?.id)?await accountService.joinUserToAccount(input.user_id, input.account_id):null;
|
||||
return {
|
||||
membership,
|
||||
}
|
||||
@@ -36,8 +37,8 @@ export const accountRouter = 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();
|
||||
const membership = await uaService.changeUserAccessWithinAccount(input.user_id, input.account_id, input.access);
|
||||
const accountService = new AccountService();
|
||||
const membership = await accountService.changeUserAccessWithinAccount(input.user_id, input.account_id, input.access);
|
||||
|
||||
return {
|
||||
membership,
|
||||
@@ -46,8 +47,8 @@ export const accountRouter = router({
|
||||
claimOwnershipOfAccount: adminProcedure
|
||||
.input(z.object({ account_id: z.number() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const uaService = new UserAccountService();
|
||||
const membership = await uaService.claimOwnershipOfAccount(ctx.dbUser.id, input.account_id);
|
||||
const accountService = new AccountService();
|
||||
const membership = await accountService.claimOwnershipOfAccount(ctx.dbUser.id, input.account_id);
|
||||
|
||||
return {
|
||||
membership,
|
||||
@@ -56,8 +57,8 @@ export const accountRouter = router({
|
||||
getAccountMembers: adminProcedure
|
||||
.input(z.object({ account_id: z.number() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const uaService = new UserAccountService();
|
||||
const memberships = await uaService.getAccountMembers(input.account_id);
|
||||
const accountService = new AccountService();
|
||||
const memberships = await accountService.getAccountMembers(input.account_id);
|
||||
|
||||
return {
|
||||
memberships,
|
||||
|
||||
@@ -39,7 +39,6 @@ const isAdminForInputAccountId = t.middleware(({ next, rawInput, ctx }) => {
|
||||
if (!result.success) throw new TRPCError({ code: 'BAD_REQUEST' });
|
||||
const { account_id } = result.data;
|
||||
const test_membership = ctx.dbUser.memberships.find(membership => membership.account_id == account_id);
|
||||
console.log(`isAdminForInputAccountId test_membership?.access:${test_membership?.access}`);
|
||||
if(!test_membership || (test_membership?.access !== ACCOUNT_ACCESS.ADMIN && test_membership?.access !== ACCOUNT_ACCESS.OWNER)) {
|
||||
throw new TRPCError({ code: 'UNAUTHORIZED' });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user