added remaining user api
This commit is contained in:
@@ -5,7 +5,7 @@ import {
|
||||
protectedProcedure,
|
||||
} from "./trpc";
|
||||
import { db } from "./db";
|
||||
import { phoneToken, user } from "./schema";
|
||||
import { phoneToken, user, userOpinion } from "./schema";
|
||||
import { createInsertSchema } from "drizzle-zod";
|
||||
import { z } from "zod";
|
||||
import { SQL, eq } from "drizzle-orm";
|
||||
@@ -13,6 +13,19 @@ import { Config } from "./config";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
const userInsertSchema = createInsertSchema(user);
|
||||
const opinionInsertSchema = createInsertSchema(userOpinion)
|
||||
.omit({
|
||||
userId: true,
|
||||
})
|
||||
.array()
|
||||
.default([]);
|
||||
|
||||
const opinionUpdateSchema = createInsertSchema(userOpinion)
|
||||
.omit({
|
||||
userId: true,
|
||||
})
|
||||
.required({ opinionId: true });
|
||||
type OpinionInsertSchema = z.infer<typeof opinionInsertSchema>;
|
||||
type UserInsertSchema = z.infer<typeof userInsertSchema>;
|
||||
|
||||
export const userRoute = router({
|
||||
@@ -30,9 +43,14 @@ export const userRoute = router({
|
||||
await getAllUser(input.offset, input.limit, input.group, input.zone)
|
||||
),
|
||||
createUser: verifiedPhone
|
||||
.input(userInsertSchema.omit({ id: true, phone: true }))
|
||||
.input(
|
||||
userInsertSchema.omit({ id: true, phone: true }).extend({
|
||||
opinions: opinionInsertSchema,
|
||||
})
|
||||
)
|
||||
.mutation(
|
||||
async ({ input, ctx }) => await createUser({ ...input, phone: ctx.phone })
|
||||
async ({ input, ctx }) =>
|
||||
await createUser({ ...input, phone: ctx.phone }, input.opinions)
|
||||
),
|
||||
requestOtp: publicProcedure
|
||||
.input(z.object({ phone: z.string().trim().min(5) }))
|
||||
@@ -45,6 +63,12 @@ export const userRoute = router({
|
||||
})
|
||||
)
|
||||
.mutation(async ({ input }) => await verifyOtp(input.token, input.pin)),
|
||||
changeOpinion: protectedProcedure
|
||||
.input(opinionUpdateSchema)
|
||||
.mutation(
|
||||
async ({ input, ctx }) =>
|
||||
await changeOpinion(input.opinionId, ctx.user.id, input.choice)
|
||||
),
|
||||
});
|
||||
|
||||
async function getAllUser(
|
||||
@@ -76,10 +100,18 @@ async function getAllUser(
|
||||
});
|
||||
return users;
|
||||
}
|
||||
async function createUser(newUser: UserInsertSchema) {
|
||||
async function createUser(
|
||||
newUser: UserInsertSchema,
|
||||
opinions: OpinionInsertSchema
|
||||
) {
|
||||
try {
|
||||
let result = await db.insert(user).values(newUser);
|
||||
return result.lastInsertRowid;
|
||||
let result = (
|
||||
await db.insert(user).values(newUser).returning({ id: user.id })
|
||||
)[0];
|
||||
for (let op of opinions) {
|
||||
await db.insert(userOpinion).values({ ...op, userId: result.id });
|
||||
}
|
||||
return { status: "OK" };
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
throw new Error(`Unable to create new user:\n${e}`);
|
||||
@@ -168,3 +200,28 @@ async function verifyOtp(token: string, pin: string) {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function changeOpinion(
|
||||
opinionId: number,
|
||||
userId: number,
|
||||
opinion: OpinionInsertSchema[0]["choice"]
|
||||
) {
|
||||
try {
|
||||
db.insert(userOpinion)
|
||||
.values({
|
||||
opinionId,
|
||||
userId,
|
||||
choice: opinion,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [userOpinion.userId, userOpinion.opinionId],
|
||||
set: { choice: opinion },
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
throw new TRPCError({
|
||||
message: "Error updating schema",
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user