Skip to content

Commit 4b5de13

Browse files
committed
chore: cleanup user actions
1 parent 9af8d96 commit 4b5de13

35 files changed

+172
-133
lines changed

src/actions/questions/get-previous.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use server';
22
import { prisma } from '@/utils/prisma';
3-
import { getUserFromDb } from '../user/get-user';
3+
import { getUserFromDb } from '../user/authed/get-user';
44
import { Question, QuestionWithoutAnswers } from '@/types/Questions';
55
import { Answer } from '@/types/Answers';
66

@@ -39,40 +39,40 @@ export const getPreviousQuestions = async (opts: {
3939
prisma.questions.count({
4040
where: {
4141
questionDate: {
42-
lt: todayDate,
43-
},
44-
},
42+
lt: todayDate
43+
}
44+
}
4545
}),
4646
prisma.questions.findMany({
4747
where: {
4848
questionDate: {
49-
lt: todayDate,
49+
lt: todayDate
5050
},
5151
AND: {
52-
dailyQuestion: true,
53-
},
52+
dailyQuestion: true
53+
}
5454
},
5555
orderBy: {
56-
questionDate: orderBy,
56+
questionDate: orderBy
5757
},
5858
skip,
5959
take: pageSize, // Calculate the correct number of items to take
6060
include: {
6161
answers: true, // Include the answers in the query
6262
tags: {
6363
include: {
64-
tag: true,
65-
},
66-
},
67-
},
64+
tag: true
65+
}
66+
}
67+
}
6868
}),
6969

7070
// get the user's answers to the questions
7171
prisma.answers.findMany({
7272
where: {
73-
userUid,
74-
},
75-
}),
73+
userUid
74+
}
75+
})
7676
]);
7777

7878
return {
@@ -81,6 +81,6 @@ export const getPreviousQuestions = async (opts: {
8181
answers,
8282
page,
8383
pageSize,
84-
totalPages: Math.ceil(total / pageSize),
84+
totalPages: Math.ceil(total / pageSize)
8585
};
8686
};

src/actions/stripe/stripe-subscription.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use server';
22
import Stripe from 'stripe';
33
import { stripe } from '@/lib/stripe';
4-
import { getUserFromSession } from '../user/get-user';
4+
import { getUserFromSession } from '../user/authed/get-user';
55

66
type SubscriptionResponse = {
77
subscriptionId: string;
@@ -37,8 +37,8 @@ export const createSubscription = async (
3737
const customerParams: Stripe.CustomerCreateParams = {
3838
email,
3939
metadata: {
40-
createdAt: new Date().toISOString(),
41-
},
40+
createdAt: new Date().toISOString()
41+
}
4242
};
4343
customer = await createCustomer(customerParams, stripe);
4444
isNewCustomer = true;
@@ -64,10 +64,10 @@ export const createSubscription = async (
6464
items: [
6565
{
6666
id: existingSubscription.data[0].items.data[0].id,
67-
price: priceId,
68-
},
67+
price: priceId
68+
}
6969
],
70-
proration_behavior: 'create_prorations',
70+
proration_behavior: 'create_prorations'
7171
}
7272
);
7373
} else {
@@ -79,12 +79,12 @@ export const createSubscription = async (
7979
payment_behavior: 'default_incomplete',
8080
payment_settings: {
8181
save_default_payment_method: 'on_subscription',
82-
payment_method_types: ['card'],
82+
payment_method_types: ['card']
8383
},
8484
expand: ['latest_invoice.payment_intent'],
8585
metadata: {
86-
email: customer.email,
87-
},
86+
email: customer.email
87+
}
8888
});
8989
}
9090

@@ -103,7 +103,7 @@ export const createSubscription = async (
103103
clientSecret: paymentIntent.client_secret,
104104
customerId: customer.id,
105105
isNewCustomer,
106-
stripeSubscriptionItemId: subscription.items.data[0].id,
106+
stripeSubscriptionItemId: subscription.items.data[0].id
107107
};
108108
} catch (error) {
109109
console.error('Error in createSubscription:', error);
@@ -120,7 +120,7 @@ export const lookupCustomer = async (
120120
): Promise<Stripe.Customer | null> => {
121121
const existingCustomer = await stripe.customers.list({
122122
email: email,
123-
limit: 1,
123+
limit: 1
124124
});
125125

126126
return existingCustomer.data.length ? existingCustomer.data[0] : null;
@@ -153,7 +153,7 @@ export const hasActiveSubscription = async (
153153
return await stripe.subscriptions.list({
154154
customer: customerId,
155155
status: 'active',
156-
expand: ['data.items.data.price'],
156+
expand: ['data.items.data.price']
157157
});
158158
} catch (error) {
159159
console.error('Error checking active subscription:', error);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/actions/user/update-user-subscription.ts renamed to src/actions/user/authed/update-user-subscription.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use server';
22
import type {
33
CreateSubscriptionInput,
4-
Subscription,
4+
Subscription
55
} from '@/types/Subscription';
66
import { getUserFromSession } from './get-user';
77
import { prisma } from '@/utils/prisma';
@@ -26,27 +26,27 @@ export const updateUserSubscription = async (opts: {
2626
if (!userUid) {
2727
return {
2828
success: false,
29-
error: 'User not found',
29+
error: 'User not found'
3030
};
3131
}
3232

3333
// Check if user already has a subscription
3434
const existingSubscription = await prisma.subscriptions.findUnique({
3535
where: {
36-
userUid,
37-
},
36+
userUid
37+
}
3838
});
3939

4040
if (existingSubscription) {
4141
// get the subscription and customer id from the db
4242
const {
4343
stripeCustomerId: oldStripeCustomerId,
44-
stripeSubscriptionId: oldStripeSubscriptionId,
44+
stripeSubscriptionId: oldStripeSubscriptionId
4545
} = existingSubscription;
4646
if (!oldStripeCustomerId || !oldStripeSubscriptionId) {
4747
return {
4848
success: false,
49-
error: 'Stripe customer or subscription ID not found',
49+
error: 'Stripe customer or subscription ID not found'
5050
};
5151
}
5252

@@ -65,12 +65,12 @@ export const updateUserSubscription = async (opts: {
6565
// Update existing subscription
6666
const updatedSubscription = await prisma.subscriptions.update({
6767
where: {
68-
userUid,
68+
userUid
6969
},
7070
data: {
7171
...subscription,
72-
updatedAt: new Date(),
73-
},
72+
updatedAt: new Date()
73+
}
7474
});
7575

7676
// update the user object based on which plan was chosen
@@ -81,23 +81,23 @@ export const updateUserSubscription = async (opts: {
8181
if (!plan) {
8282
return {
8383
success: false,
84-
error: 'Plan not found',
84+
error: 'Plan not found'
8585
};
8686
}
8787

8888
// update the user level
8989
await prisma.users.update({
9090
where: {
91-
uid: userUid,
91+
uid: userUid
9292
},
9393
data: {
94-
userLevel: plan.value,
95-
},
94+
userLevel: plan.value
95+
}
9696
});
9797

9898
return {
9999
success: true,
100-
subscription: updatedSubscription,
100+
subscription: updatedSubscription
101101
};
102102
} else {
103103
// Create new subscription
@@ -108,8 +108,8 @@ export const updateUserSubscription = async (opts: {
108108
createdAt: new Date(),
109109
updatedAt: new Date(),
110110
stripeCustomerId: subscription.stripeCustomerId,
111-
stripeSubscriptionId: subscription.stripeSubscriptionId,
112-
},
111+
stripeSubscriptionId: subscription.stripeSubscriptionId
112+
}
113113
});
114114

115115
// find the user's plan name
@@ -118,23 +118,23 @@ export const updateUserSubscription = async (opts: {
118118
if (!plan) {
119119
return {
120120
success: false,
121-
error: 'Plan not found',
121+
error: 'Plan not found'
122122
};
123123
}
124124

125125
// update the user level
126126
await prisma.users.update({
127127
where: {
128-
uid: userUid,
128+
uid: userUid
129129
},
130130
data: {
131-
userLevel: plan.value,
132-
},
131+
userLevel: plan.value
132+
}
133133
});
134134

135135
return {
136136
success: true,
137-
subscription: newSubscription,
137+
subscription: newSubscription
138138
};
139139
}
140140
};

src/app/(default_layout)/(questions)/(layout)/questions/all/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '@/components/ui/tooltip';
1010

1111
import { listQuestions } from '@/actions/questions/list';
12-
import { getUserDailyStats } from '@/actions/user/get-daily-streak';
12+
import { getUserDailyStats } from '@/actions/user/authed/get-daily-streak';
1313
import { useUserServer } from '@/hooks/useUserServer';
1414
import { getSuggestions } from '@/actions/questions/get-suggestions';
1515
import { QuestionMarkCircledIcon } from '@radix-ui/react-icons';

src/app/(default_layout)/dashboard/page.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { getUserFromDb, getUserFromSession } from '@/actions/user/get-user';
1+
import {
2+
getUserFromDb,
3+
getUserFromSession
4+
} from '@/actions/user/authed/get-user';
25
import DashboardBentoGrid from '@/components/dashboard/dashboard-bento-grid';
36
import CurrentStreak from '@/components/global/current-streak';
47
import LanguageSwitcher from '@/components/global/language-dropdown';

src/app/(default_layout)/settings/account/page.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { zodResolver } from '@hookform/resolvers/zod';
55
import * as z from 'zod';
66
import { useMutation } from '@tanstack/react-query';
77
import { useUser } from '@/hooks/useUser';
8-
import { updateUserAuth } from '@/actions/user/update-user-auth';
8+
import { updateUserAuth } from '@/actions/user/authed/update-user-auth';
99
import { updateUserSchema } from '@/lib/zod/schemas/update-user';
1010

1111
import { Button } from '@/components/ui/button';
@@ -16,7 +16,7 @@ import {
1616
FormField,
1717
FormItem,
1818
FormLabel,
19-
FormMessage,
19+
FormMessage
2020
} from '@/components/ui/form';
2121
import { Input } from '@/components/ui/input';
2222
import { toast } from 'sonner';
@@ -39,15 +39,15 @@ export default function SettingsProfilePage() {
3939
},
4040
onError: (error) => {
4141
toast.error(`Failed to update user auth: ${error}`);
42-
},
42+
}
4343
});
4444

4545
const form = useForm<SchemaProps>({
4646
resolver: zodResolver(updateUserSchema),
4747
defaultValues: {
4848
email: user?.email || '',
49-
password: '',
50-
},
49+
password: ''
50+
}
5151
});
5252

5353
const onSubmit = async (values: SchemaProps) => {
@@ -81,7 +81,10 @@ export default function SettingsProfilePage() {
8181
<FormItem>
8282
<FormLabel>Email Address</FormLabel>
8383
<FormControl>
84-
<Input placeholder={user?.email} {...field} />
84+
<Input
85+
placeholder={user?.email}
86+
{...field}
87+
/>
8588
</FormControl>
8689
<FormMessage />
8790
</FormItem>
@@ -94,7 +97,11 @@ export default function SettingsProfilePage() {
9497
<FormItem>
9598
<FormLabel>Password</FormLabel>
9699
<FormControl>
97-
<Input type="password" placeholder="********" {...field} />
100+
<Input
101+
type="password"
102+
placeholder="********"
103+
{...field}
104+
/>
98105
</FormControl>
99106
<FormDescription>
100107
Leave blank to keep your current password

0 commit comments

Comments
 (0)