diff --git a/addMetadata.ts b/addMetadata.ts index a4a97f3..e12a634 100644 --- a/addMetadata.ts +++ b/addMetadata.ts @@ -48,13 +48,11 @@ async function main() { await create_user(); await create_relation(); - console.log("a"); const allUser = await db.query.user.findMany({ with: { userToSelection: { with: { selection: true } }, }, }); - console.log("b"); for (const u of allUser) { console.log( u.firstName, @@ -118,6 +116,7 @@ async function create_user() { job: "", education: "", zone: thisDistrict.id, + rank: newUser.rank, }); } } diff --git a/src/schema.ts b/src/schema.ts index 48a763b..1d75502 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -38,6 +38,7 @@ export const user = sqliteTable( .notNull() .references(() => zone.id), verified: integer("verified", { mode: "boolean" }).notNull().default(false), + rank: integer("rank").default(9999), }, (t) => ({ phone_idx: index("phone_idx").on(t.phone), diff --git a/src/userRoute.ts b/src/userRoute.ts index 248d375..8914ec2 100644 --- a/src/userRoute.ts +++ b/src/userRoute.ts @@ -86,6 +86,7 @@ export const userRoute = router({ zone: z.number().optional(), opinionCount: z.number().default(3), province: z.number().optional(), + userId: z.number().optional(), }), ) .query( @@ -97,6 +98,7 @@ export const userRoute = router({ input.group, input.zone, input.province, + input.userId, ), ), getAllUserCount: publicProcedure @@ -152,7 +154,43 @@ async function getAllUser( if (provinceId && zoneIds.length === 0) { return []; } - const users = await db.query.user.findMany({ + let thisUser = + userId == undefined + ? undefined + : await db.query.user.findFirst({ + where: eq(user.id, userId), + with: { + userToSelection: { + with: { + selection: { + with: { + group: true, + opinions: { + limit: opinionLimit, + }, + zone: { + with: { province: true }, + }, + }, + }, + }, + }, + }, + }); + const topThree = await db.query.user.findMany({ + with: { + group: true, + opinions: { + limit: opinionLimit, + }, + zone: { + with: { province: true }, + }, + }, + limit: 3, + orderBy: user.rank, + }); + let users = await db.query.user.findMany({ with: { group: true, opinions: { @@ -179,8 +217,24 @@ async function getAllUser( return and(...conditions); }, }); + let resultUser: typeof users; + if (thisUser && thisUser.group == group) { + const selections = thisUser.userToSelection.map((v) => v.selection); + let validSelection: typeof users = []; + for (const sl of selections) { + if (sl !== null) { + validSelection.push(sl); + } + } + resultUser = [ + ...validSelection, + ...users.filter((u) => !validSelection.includes(u)), + ]; + } else { + resultUser = [...topThree, ...users.filter((u) => !topThree.includes(u))]; + } - return users.map((u) => ({ + return resultUser.map((u) => ({ ...u, phone: hidePhone(u.phone), verified: true,