Merge branch 'main' into feat/profile-page
This commit is contained in:
@ -245,114 +245,6 @@ export default {
|
||||
|
||||
return success
|
||||
} else return methodNotAllowed
|
||||
} else if (path === "/api/sandbox/generate" && method === "POST") {
|
||||
const generateSchema = z.object({
|
||||
userId: z.string(),
|
||||
})
|
||||
const body = await request.json()
|
||||
const { userId } = generateSchema.parse(body)
|
||||
|
||||
const dbUser = await db.query.user.findFirst({
|
||||
where: (user, { eq }) => eq(user.id, userId),
|
||||
})
|
||||
if (!dbUser) {
|
||||
return new Response("User not found.", { status: 400 })
|
||||
}
|
||||
if (dbUser.generations !== null && dbUser.generations >= 10) {
|
||||
return new Response("You reached the maximum # of generations.", {
|
||||
status: 400,
|
||||
})
|
||||
}
|
||||
|
||||
await db
|
||||
.update(user)
|
||||
.set({ generations: sql`${user.generations} + 1` })
|
||||
.where(eq(user.id, userId))
|
||||
.get()
|
||||
|
||||
return success
|
||||
} else if (path === "/api/sandbox/like") {
|
||||
if (method === "POST") {
|
||||
const likeSchema = z.object({
|
||||
sandboxId: z.string(),
|
||||
userId: z.string(),
|
||||
})
|
||||
|
||||
try {
|
||||
const body = await request.json()
|
||||
const { sandboxId, userId } = likeSchema.parse(body)
|
||||
|
||||
// Check if user has already liked
|
||||
const existingLike = await db.query.sandboxLikes.findFirst({
|
||||
where: (likes, { and, eq }) =>
|
||||
and(eq(likes.sandboxId, sandboxId), eq(likes.userId, userId)),
|
||||
})
|
||||
|
||||
if (existingLike) {
|
||||
// Unlike
|
||||
await db
|
||||
.delete(sandboxLikes)
|
||||
.where(
|
||||
and(
|
||||
eq(sandboxLikes.sandboxId, sandboxId),
|
||||
eq(sandboxLikes.userId, userId)
|
||||
)
|
||||
)
|
||||
|
||||
await db
|
||||
.update(sandbox)
|
||||
.set({
|
||||
likeCount: sql`${sandbox.likeCount} - 1`,
|
||||
})
|
||||
.where(eq(sandbox.id, sandboxId))
|
||||
|
||||
return json({
|
||||
message: "Unlike successful",
|
||||
liked: false,
|
||||
})
|
||||
} else {
|
||||
// Like
|
||||
await db.insert(sandboxLikes).values({
|
||||
sandboxId,
|
||||
userId,
|
||||
createdAt: new Date(),
|
||||
})
|
||||
|
||||
await db
|
||||
.update(sandbox)
|
||||
.set({
|
||||
likeCount: sql`${sandbox.likeCount} + 1`,
|
||||
})
|
||||
.where(eq(sandbox.id, sandboxId))
|
||||
|
||||
return json({
|
||||
message: "Like successful",
|
||||
liked: true,
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
return new Response("Invalid request format", { status: 400 })
|
||||
}
|
||||
} else if (method === "GET") {
|
||||
const params = url.searchParams
|
||||
const sandboxId = params.get("sandboxId")
|
||||
const userId = params.get("userId")
|
||||
|
||||
if (!sandboxId || !userId) {
|
||||
return invalidRequest
|
||||
}
|
||||
|
||||
const like = await db.query.sandboxLikes.findFirst({
|
||||
where: (likes, { and, eq }) =>
|
||||
and(eq(likes.sandboxId, sandboxId), eq(likes.userId, userId)),
|
||||
})
|
||||
|
||||
return json({
|
||||
liked: !!like,
|
||||
})
|
||||
} else {
|
||||
return methodNotAllowed
|
||||
}
|
||||
} else if (path === "/api/user") {
|
||||
if (method === "GET") {
|
||||
const params = url.searchParams
|
||||
@ -426,12 +318,14 @@ export default {
|
||||
avatarUrl: z.string().optional(),
|
||||
createdAt: z.string().optional(),
|
||||
generations: z.number().optional(),
|
||||
tier: z.enum(["FREE", "PRO", "ENTERPRISE"]).optional(),
|
||||
tierExpiresAt: z.number().optional(),
|
||||
lastResetDate: z.number().optional(),
|
||||
})
|
||||
|
||||
const body = await request.json()
|
||||
const { id, name, email, username, avatarUrl, createdAt, generations } =
|
||||
userSchema.parse(body)
|
||||
|
||||
const { id, name, email, username, avatarUrl, createdAt, generations, tier, tierExpiresAt, lastResetDate } = userSchema.parse(body)
|
||||
const res = await db
|
||||
.insert(user)
|
||||
.values({
|
||||
@ -442,6 +336,9 @@ export default {
|
||||
avatarUrl,
|
||||
createdAt: createdAt ? new Date(createdAt) : new Date(),
|
||||
generations,
|
||||
tier,
|
||||
tierExpiresAt,
|
||||
lastResetDate,
|
||||
})
|
||||
.returning()
|
||||
.get()
|
||||
@ -521,6 +418,76 @@ export default {
|
||||
return json({ exists: !!exists })
|
||||
}
|
||||
return methodNotAllowed
|
||||
} else if (path === "/api/user/increment-generations" && method === "POST") {
|
||||
const schema = z.object({
|
||||
userId: z.string(),
|
||||
})
|
||||
|
||||
const body = await request.json()
|
||||
const { userId } = schema.parse(body)
|
||||
|
||||
await db
|
||||
.update(user)
|
||||
.set({ generations: sql`${user.generations} + 1` })
|
||||
.where(eq(user.id, userId))
|
||||
.get()
|
||||
|
||||
return success
|
||||
} else if (path === "/api/user/update-tier" && method === "POST") {
|
||||
const schema = z.object({
|
||||
userId: z.string(),
|
||||
tier: z.enum(["FREE", "PRO", "ENTERPRISE"]),
|
||||
tierExpiresAt: z.date(),
|
||||
})
|
||||
|
||||
const body = await request.json()
|
||||
const { userId, tier, tierExpiresAt } = schema.parse(body)
|
||||
|
||||
await db
|
||||
.update(user)
|
||||
.set({
|
||||
tier,
|
||||
tierExpiresAt: tierExpiresAt.getTime(),
|
||||
// Reset generations when upgrading tier
|
||||
generations: 0
|
||||
})
|
||||
.where(eq(user.id, userId))
|
||||
.get()
|
||||
|
||||
return success
|
||||
} else if (path === "/api/user/check-reset" && method === "POST") {
|
||||
const schema = z.object({
|
||||
userId: z.string(),
|
||||
})
|
||||
|
||||
const body = await request.json()
|
||||
const { userId } = schema.parse(body)
|
||||
|
||||
const dbUser = await db.query.user.findFirst({
|
||||
where: (user, { eq }) => eq(user.id, userId),
|
||||
})
|
||||
|
||||
if (!dbUser) {
|
||||
return new Response("User not found", { status: 404 })
|
||||
}
|
||||
|
||||
const now = new Date()
|
||||
const lastReset = dbUser.lastResetDate ? new Date(dbUser.lastResetDate) : new Date(0)
|
||||
|
||||
if (now.getMonth() !== lastReset.getMonth() || now.getFullYear() !== lastReset.getFullYear()) {
|
||||
await db
|
||||
.update(user)
|
||||
.set({
|
||||
generations: 0,
|
||||
lastResetDate: now.getTime()
|
||||
})
|
||||
.where(eq(user.id, userId))
|
||||
.get()
|
||||
|
||||
return new Response("Reset successful", { status: 200 })
|
||||
}
|
||||
|
||||
return new Response("No reset needed", { status: 200 })
|
||||
} else return notFound
|
||||
},
|
||||
}
|
||||
|
@ -16,6 +16,9 @@ export const user = sqliteTable("user", {
|
||||
sql`CURRENT_TIMESTAMP`
|
||||
),
|
||||
generations: integer("generations").default(0),
|
||||
tier: text("tier", { enum: ["FREE", "PRO", "ENTERPRISE"] }).default("FREE"),
|
||||
tierExpiresAt: integer("tierExpiresAt"),
|
||||
lastResetDate: integer("lastResetDate"),
|
||||
})
|
||||
|
||||
export type User = typeof user.$inferSelect
|
||||
|
Reference in New Issue
Block a user