Skip to content

Commit

Permalink
replace isSupporter with plan, add stripe table
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 16, 2024
1 parent 231e45a commit 2192431
Show file tree
Hide file tree
Showing 22 changed files with 70 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/scripts/seed/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function main() {
bio: "test bio",
role: "creator",
isContributor: true,
isSupporter: true,
plan: "supporter",
},
{
username: "testuser2",
Expand All @@ -73,7 +73,7 @@ async function main() {
bio: "test bio 3",
role: "uploader",
isContributor: false,
isSupporter: true,
plan: "supporter",
},
])
.returning()
Expand Down
1 change: 1 addition & 0 deletions src/v2/db/drizzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const tableNames = {
userCollectionCollaborators: "userCollectionCollaborators",
game: "game",
gameLikes: "gameLikes",
stripeUser: "stripeUser",
// atlas: "atlas",
// atlasToAsset: "atlasToAsset",
assetExternalFile: "assetExternalFile",
Expand Down
50 changes: 47 additions & 3 deletions src/v2/db/schema/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export type UserRoles =
| "uploader"
| "user"

export type UserPlan = "free" | "supporter"

export const authUser = sqliteTable(
tableNames.authUser,
{
Expand All @@ -63,9 +65,7 @@ export const authUser = sqliteTable(
.$defaultFn(() => {
return new Date().toISOString()
}),
isSupporter: integer("is_supporter", { mode: "boolean" })
.default(false)
.notNull(),
plan: text("plan").default("free").notNull().$type<UserPlan>(),
isBanned: integer("is_banned", { mode: "boolean" })
.default(false)
.notNull(),
Expand All @@ -91,6 +91,41 @@ export type NewUser = typeof authUser.$inferInsert
export const insertUserSchema = createInsertSchema(authUser)
export const selectUserSchema = createSelectSchema(authUser)

export const stripeUser = sqliteTable(
tableNames.stripeUser,
{
id: text("id")
.primaryKey()
.notNull()
.$defaultFn(() => {
return generateID()
}),
userId: text("user_id")
.notNull()
.references(() => authUser.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
customerId: text("stripe_customer_id"),
subscriptionId: text("stripe_subscription_id"),
endsAt: text("ends_at"),
paidUntil: text("paid_until"),
},
(stripeUser) => {
return {
userIdx: index("stripe_user_user_id_idx").on(stripeUser.userId),
stripeCustomerIdIdx: index("stripe_user_stripe_customer_id_idx").on(
stripeUser.customerId
),
}
}
)

export type StripeUser = typeof stripeUser.$inferSelect
export type NewStripeUser = typeof stripeUser.$inferInsert
export const insertStripeUserSchema = createInsertSchema(stripeUser)
export const selectStripeUserSchema = createSelectSchema(stripeUser)

export const authCredentials = sqliteTable(
tableNames.authCredentials,
{
Expand Down Expand Up @@ -176,6 +211,7 @@ export const usersRelations = relations(authUser, ({ one, many }) => ({
assetLikes: many(assetLikes),
socialsConnection: one(socialsConnection),
userCollection: many(userCollection),
stripeUser: one(stripeUser),
passwordResetToken: one(passwordResetToken),
emailVerificationToken: one(emailVerificationToken),
gameLikes: many(gameLikes),
Expand All @@ -186,6 +222,14 @@ export const usersRelations = relations(authUser, ({ one, many }) => ({
requestFormUpvotes: many(requestFormUpvotes),
}))

export const stripeUserRelations = relations(stripeUser, ({ one }) => ({
user: one(authUser, {
fields: [stripeUser.userId],
references: [authUser.id],
relationName: "stripe_user",
}),
}))

export const authCredentialsRelations = relations(
authCredentials,
({ one }) => ({
Expand Down
3 changes: 2 additions & 1 deletion src/v2/lib/auth/definitions/auth-definitions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { UserRoles } from "@/v2/db/schema"
import type { LuciaAuth } from "../lucia"
import type { UserPlan } from "@/v2/db/schema"

declare module "lucia" {
interface Register {
Expand All @@ -21,7 +22,7 @@ declare module "lucia" {
verified: boolean
bio: string | null
date_joined: string
is_supporter: boolean
plan: UserPlan
is_banned: boolean
role: UserRoles
is_contributor: boolean
Expand Down
2 changes: 1 addition & 1 deletion src/v2/lib/auth/lucia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function luciaAuth(env: Bindings) {
verified: user.verified,
bio: user.bio,
dateJoined: user.date_joined,
isSupporter: Boolean(user.is_supporter),
plan: user.plan,
isBanned: Boolean(user.is_banned),
isContributor: user.is_contributor,
role: user.role as UserRoles,
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/asset/get/id/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ handler.openapi(getAssetByIdRoute, async (ctx) => {
verified: true,
bio: true,
dateJoined: true,
isSupporter: true,
plan: true,
role: true,
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/asset/get/id/[id]/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const getAssetByIdResponseSchema = z.object({
verified: true,
bio: true,
dateJoined: true,
isSupporter: true,
plan: true,
role: true,
}),
game: selectGameSchema,
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/contributors/get/all/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const contributorListSchema = z.object({
id: true,
username: true,
avatarUrl: true,
isSupporter: true,
plan: true,
role: true,
})
.array(),
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/contributors/get/all/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ handler.openapi(contributorsRoute, async (ctx) => {
id: authUser.id,
username: authUser.username,
avatarUrl: authUser.avatarUrl,
isSupporter: authUser.isSupporter,
plan: authUser.plan,
role: authUser.role,
})
.from(authUser)
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/requests/form/create/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ handler.openapi(createRequestFormEntryRoute, async (ctx) => {

const { user } = await authSessionManager.validateSession()

if (!user || user.role != "creator" || !user.isSupporter) {
if (!user || user.role != "creator" || user.plan == "supporter") {
return ctx.json(
{
success: false,
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/requests/form/delete/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ handler.openapi(deleteRequestByIdRoute, async (ctx) => {

const { user } = await authSessionManager.validateSession()

if (!user || user.role != "creator" || !user.isSupporter) {
if (!user || user.role != "creator" || user.plan == "supporter") {
return ctx.json(
{
success: false,
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/user/follows/followers/id/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ handler.openapi(viewUserFollowsByIdRoute, async (ctx) => {
id: true,
avatarUrl: true,
username: true,
isSupporter: true,
plan: true,
verified: true,
displayName: true,
},
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/user/follows/followers/id/[id]/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const viewUserFollowsbyIdResponseSchema = z.object({
id: true,
avatarUrl: true,
username: true,
isSupporter: true,
plan: true,
verified: true,
displayName: true,
}),
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/user/follows/following/id/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ handler.openapi(viewUserfollowingbyIdRoute, async (ctx) => {
id: true,
avatarUrl: true,
username: true,
isSupporter: true,
plan: true,
verified: true,
displayName: true,
},
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/user/follows/following/id/[id]/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const viewUserfollowingbyIdResponseSchema = z.object({
id: true,
avatarUrl: true,
username: true,
isSupporter: true,
plan: true,
verified: true,
displayName: true,
}),
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/user/get/id/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ handler.openapi(getUserByIdRoute, async (ctx) => {
verified: authUser.verified,
bio: authUser.bio,
dateJoined: authUser.dateJoined,
isSupporter: authUser.isSupporter,
plan: authUser.plan,
role: authUser.role,
})
.from(authUser)
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/user/get/id/[id]/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const getUserByIdResponseSchema = z.object({
verified: true,
bio: true,
dateJoined: true,
isSupporter: true,
plan: true,
role: true,
}),
})
2 changes: 1 addition & 1 deletion src/v2/routes/user/get/username/[username]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ handler.openapi(getUserByNameRoute, async (ctx) => {
verified: authUser.verified,
bio: authUser.bio,
dateJoined: authUser.dateJoined,
isSupporter: authUser.isSupporter,
plan: authUser.plan,
role: authUser.role,
})
.from(authUser)
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/user/get/username/[username]/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const getUserByNameResponseSchema = z.object({
verified: true,
bio: true,
dateJoined: true,
isSupporter: true,
plan: true,
role: true,
}),
})
2 changes: 1 addition & 1 deletion src/v2/routes/user/search/username/[username]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ handler.openapi(searchUsersByUsernameRoute, async (ctx) => {
verified: authUser.verified,
bio: authUser.bio,
dateJoined: authUser.dateJoined,
isSupporter: authUser.isSupporter,
plan: authUser.plan,
role: authUser.role,
})
.from(authUser)
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/user/search/username/[username]/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const searchUsersByUsernameSchema = z.object({
verified: true,
bio: true,
dateJoined: true,
isSupporter: true,
plan: true,
role: true,
})
.array(),
Expand Down
1 change: 1 addition & 0 deletions src/worker-configuration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ declare global {
DISCORD_REDIRECT_URI: string
RESEND_API_KEY: string
RATE_LIMITER: DurableObjectNamespace
REKOGNITION_LABEL_API_KEY: string
}

type Variables = {
Expand Down

0 comments on commit 2192431

Please sign in to comment.