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