added locking mechanism
Some checks failed
backend-admin-action / build-image (push) Failing after 38s
backend-action / build-image (push) Successful in 7m35s

This commit is contained in:
2024-06-08 10:04:49 +07:00
parent b28aae201a
commit 3d6de91dc8
3 changed files with 58 additions and 4 deletions

View File

@@ -48,13 +48,11 @@ async function main() {
await create_user(); await create_user();
await create_relation(); await create_relation();
console.log("a");
const allUser = await db.query.user.findMany({ const allUser = await db.query.user.findMany({
with: { with: {
userToSelection: { with: { selection: true } }, userToSelection: { with: { selection: true } },
}, },
}); });
console.log("b");
for (const u of allUser) { for (const u of allUser) {
console.log( console.log(
u.firstName, u.firstName,
@@ -118,6 +116,7 @@ async function create_user() {
job: "", job: "",
education: "", education: "",
zone: thisDistrict.id, zone: thisDistrict.id,
rank: newUser.rank,
}); });
} }
} }

View File

@@ -38,6 +38,7 @@ export const user = sqliteTable(
.notNull() .notNull()
.references(() => zone.id), .references(() => zone.id),
verified: integer("verified", { mode: "boolean" }).notNull().default(false), verified: integer("verified", { mode: "boolean" }).notNull().default(false),
rank: integer("rank").default(9999),
}, },
(t) => ({ (t) => ({
phone_idx: index("phone_idx").on(t.phone), phone_idx: index("phone_idx").on(t.phone),

View File

@@ -86,6 +86,7 @@ export const userRoute = router({
zone: z.number().optional(), zone: z.number().optional(),
opinionCount: z.number().default(3), opinionCount: z.number().default(3),
province: z.number().optional(), province: z.number().optional(),
userId: z.number().optional(),
}), }),
) )
.query( .query(
@@ -97,6 +98,7 @@ export const userRoute = router({
input.group, input.group,
input.zone, input.zone,
input.province, input.province,
input.userId,
), ),
), ),
getAllUserCount: publicProcedure getAllUserCount: publicProcedure
@@ -152,7 +154,43 @@ async function getAllUser(
if (provinceId && zoneIds.length === 0) { if (provinceId && zoneIds.length === 0) {
return []; 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: { with: {
group: true, group: true,
opinions: { opinions: {
@@ -179,8 +217,24 @@ async function getAllUser(
return and(...conditions); 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, ...u,
phone: hidePhone(u.phone), phone: hidePhone(u.phone),
verified: true, verified: true,