added remaining user api
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { createContext, router } from "./trpc";
|
||||
import { createHTTPServer } from "@trpc/server/adapters/standalone";
|
||||
import { db } from "./db";
|
||||
import { userRoute } from "./userRoute";
|
||||
import { runPlayground } from "./playgroud";
|
||||
import cors from "cors";
|
||||
|
||||
@@ -41,7 +41,7 @@ export const userRelation = relations(user, ({ many, one }) => ({
|
||||
//----------------Group
|
||||
export const group = sqliteTable("groups", {
|
||||
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
|
||||
name: text("name"),
|
||||
name: text("name").notNull(),
|
||||
});
|
||||
|
||||
export const groupRelation = relations(group, ({ many }) => ({
|
||||
@@ -51,19 +51,25 @@ export const groupRelation = relations(group, ({ many }) => ({
|
||||
//----------------Opinion
|
||||
export const opinion = sqliteTable("opinions", {
|
||||
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
|
||||
name: text("name"),
|
||||
type: text("type", { enum: ["3Choice", "4Choice"] }),
|
||||
name: text("name").notNull(),
|
||||
type: text("type", { enum: ["3Choice", "4Choice"] })
|
||||
.default("3Choice")
|
||||
.notNull(),
|
||||
});
|
||||
|
||||
//----------------UserOpinion
|
||||
export const userOpinion = sqliteTable(
|
||||
"user_opinions",
|
||||
{
|
||||
userId: integer("user_id").references(() => user.id),
|
||||
opinionId: integer("opinion_id").references(() => opinion.id),
|
||||
userId: integer("user_id")
|
||||
.notNull()
|
||||
.references(() => user.id),
|
||||
opinionId: integer("opinion_id")
|
||||
.notNull()
|
||||
.references(() => opinion.id),
|
||||
choice: text("choice", {
|
||||
enum: ["agree", "disagree", "deciding", "ignore"],
|
||||
}).default("ignore"),
|
||||
}).default("deciding"),
|
||||
},
|
||||
(t) => ({
|
||||
pk: primaryKey({ columns: [t.userId, t.opinionId] }),
|
||||
|
||||
@@ -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