added validation to opinion
This commit is contained in:
@@ -16,3 +16,8 @@ tasks:
|
||||
studio:
|
||||
cmds:
|
||||
- pnpm drizzle-kit studio
|
||||
start:
|
||||
cmds:
|
||||
- node -r @swc-node/register src/app.ts
|
||||
env:
|
||||
NODE_ENV: production
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
import {
|
||||
router,
|
||||
verifiedPhone,
|
||||
publicProcedure,
|
||||
protectedProcedure,
|
||||
} from "./trpc";
|
||||
import { router, publicProcedure, protectedProcedure } from "./trpc";
|
||||
import { db } from "./db";
|
||||
import { phoneToken, user, userOpinion } from "./schema";
|
||||
import { opinion, user, userOpinion } from "./schema";
|
||||
import { createInsertSchema } from "drizzle-zod";
|
||||
import { z } from "zod";
|
||||
import { SQL, eq } from "drizzle-orm";
|
||||
@@ -45,6 +40,7 @@ export const userRoute = router({
|
||||
limit: z.number().max(50).default(10),
|
||||
group: z.number().optional(),
|
||||
zone: z.number().optional(),
|
||||
opinionCount: z.number().default(3),
|
||||
})
|
||||
)
|
||||
.query(
|
||||
@@ -154,19 +150,38 @@ function createJWT(phone: string) {
|
||||
async function changeOpinion(
|
||||
opinionId: number,
|
||||
userId: number,
|
||||
opinion: OpinionInsertSchema[0]["choice"]
|
||||
opinionChoice: OpinionInsertSchema[0]["choice"]
|
||||
) {
|
||||
try {
|
||||
db.insert(userOpinion)
|
||||
let thisOpinion = await db
|
||||
.select()
|
||||
.from(opinion)
|
||||
.where(eq(opinion.id, opinionId))
|
||||
.then((opinions) => opinions.at(0));
|
||||
|
||||
if (thisOpinion === undefined) {
|
||||
throw new TRPCError({
|
||||
message: "Invalid Opinion ID",
|
||||
code: "BAD_REQUEST",
|
||||
});
|
||||
} else if (thisOpinion.type === "3Choice" && opinionChoice === "ignore") {
|
||||
throw new TRPCError({
|
||||
message: "Invalid Opinion Choice",
|
||||
code: "BAD_REQUEST",
|
||||
});
|
||||
}
|
||||
await db
|
||||
.insert(userOpinion)
|
||||
.values({
|
||||
opinionId,
|
||||
userId,
|
||||
choice: opinion,
|
||||
choice: opinionChoice,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [userOpinion.userId, userOpinion.opinionId],
|
||||
set: { choice: opinion },
|
||||
set: { choice: opinionChoice },
|
||||
});
|
||||
return { status: "success" };
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
throw new TRPCError({
|
||||
|
||||
Reference in New Issue
Block a user