added locking mechanism
This commit is contained in:
@@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user