Compare commits

...

3 Commits

Author SHA1 Message Date
cba647ca27 randomize result
All checks were successful
backend-action / build-image (push) Successful in 11m53s
2024-06-24 15:16:47 +07:00
f6b18d3f82 apply limit to resulting array
All checks were successful
backend-action / build-image (push) Successful in 6m21s
2024-06-24 00:23:42 +07:00
d660fd12fa update province user code 2024-06-24 00:23:16 +07:00
3 changed files with 52 additions and 45 deletions

2
.gitignore vendored
View File

@@ -12,3 +12,5 @@ caddy/logs
sqlite.db-shm sqlite.db-shm
sqlite.db-wal sqlite.db-wal
user.json user.json
user-p.json
user-c.json

View File

@@ -10,9 +10,20 @@ import {
import { Groups, Opinions, Provinces, Districts } from "./initialData"; import { Groups, Opinions, Provinces, Districts } from "./initialData";
import { createBucket, createClient } from "./src/minio"; import { createBucket, createClient } from "./src/minio";
import { Config } from "./src/config"; import { Config } from "./src/config";
import ud from "./user.json"; import ud from "./user-c.json";
const user_data: UserData[] = ud; const user_data: UserData[] = ud;
console.log(ud.length);
for (const user of user_data) {
let thisName = `${user.first_name} ${user.last_name}`;
if (
user_data.filter((u) => `${u.first_name} ${u.last_name}` == thisName)
.length != 1
) {
console.log(`duplicate name ${user}`);
}
}
async function main() { async function main() {
try { try {
@@ -53,15 +64,15 @@ async function main() {
userToSelection: { with: { selection: true } }, userToSelection: { with: { selection: true } },
}, },
}); });
for (const u of allUser) { // for (const u of allUser) {
console.log( // console.log(
u.firstName, // u.firstName,
u.lastName, // u.lastName,
u.userToSelection.map( // u.userToSelection.map(
(t) => `${t.selection?.firstName} ${t.selection?.lastName}`, // (t) => `${t.selection?.firstName} ${t.selection?.lastName}`,
), // ),
); // );
} // }
console.log("Done"); console.log("Done");
} }
@@ -71,7 +82,6 @@ type UserData = {
job_code: number; job_code: number;
selection: string[]; selection: string[];
province: string; province: string;
district: string;
rank: number; rank: number;
}; };
@@ -79,31 +89,17 @@ async function create_user() {
const provinces = await db.query.province.findMany({}); const provinces = await db.query.province.findMany({});
const district = await db.query.zone.findMany({}); const district = await db.query.zone.findMany({});
for (const newUser of user_data) { for (const newUser of user_data) {
const thisProvince = provinces.find((p) => p.name == newUser.province);
const thisDistrict = district.find((p) => p.name == newUser.district);
let isSelectionFound = true; let isSelectionFound = true;
for (const selection of newUser.selection) { for (const selection of newUser.selection) {
const isFound = user_data.findIndex( const isFound = user_data.findIndex(
(p) => (p) => `${p.first_name} ${p.last_name}` == selection,
`${p.first_name} ${p.last_name}` == selection &&
p.district == newUser.district,
); );
if (isFound == -1) { if (isFound == -1) {
isSelectionFound = false; isSelectionFound = false;
} }
} }
if ( if (!isSelectionFound) {
thisProvince === undefined || console.log(newUser.province, newUser, isSelectionFound);
thisDistrict === undefined ||
!isSelectionFound
) {
console.log(
newUser.province,
newUser.district,
thisDistrict,
thisProvince,
isSelectionFound,
);
} else { } else {
await db.insert(user).values({ await db.insert(user).values({
group: newUser.job_code, group: newUser.job_code,
@@ -115,7 +111,7 @@ async function create_user() {
age: 0, age: 0,
job: "", job: "",
education: "", education: "",
zone: thisDistrict.id, zone: 1001,
rank: newUser.rank, rank: newUser.rank,
}); });
} }
@@ -130,11 +126,7 @@ async function create_relation() {
}); });
for (const u of allUser) { for (const u of allUser) {
let thisUsers = user_data.filter( let thisUsers = user_data.filter(
(raw) => (raw) => raw.first_name == u.firstName && raw.last_name == u.lastName,
raw.first_name == u.firstName &&
raw.last_name == u.lastName &&
raw.district == u.zone.name &&
raw.province == u.zone.province.name,
); );
if (thisUsers.length !== 1) { if (thisUsers.length !== 1) {
console.log("duplicated users", thisUsers); console.log("duplicated users", thisUsers);
@@ -144,7 +136,7 @@ async function create_relation() {
const selections = allUser.filter( const selections = allUser.filter(
(target) => (target) =>
rawUser.selection.includes(`${target.firstName} ${target.lastName}`) && rawUser.selection.includes(`${target.firstName} ${target.lastName}`) &&
target.zone.id == u.zone.id, target.zone.province.id == u.zone.province.id,
); );
if (selections.length == 0) { if (selections.length == 0) {
console.log("selection not found", selections); console.log("selection not found", selections);

View File

@@ -177,7 +177,7 @@ async function getAllUser(
}, },
}, },
}); });
const topThree = await db.query.user.findMany({ const topTen = await db.query.user.findMany({
with: { with: {
group: true, group: true,
opinions: { opinions: {
@@ -187,7 +187,7 @@ async function getAllUser(
with: { province: true }, with: { province: true },
}, },
}, },
limit: 3, limit: 10,
orderBy: user.rank, orderBy: user.rank,
where: (user, { eq, and }) => { where: (user, { eq, and }) => {
const conditions: SQL[] = []; const conditions: SQL[] = [];
@@ -245,19 +245,24 @@ async function getAllUser(
(u) => validSelection.filter((v) => v.id == u.id).length == 0, (u) => validSelection.filter((v) => v.id == u.id).length == 0,
), ),
]; ];
resultUser = randomArray(1, 10, resultUser);
} else { } else {
resultUser = [ resultUser = [
...topThree, ...topTen,
...users.filter((u) => topThree.filter((v) => v.id == u.id).length == 0), ...users.filter((u) => topTen.filter((v) => v.id == u.id).length == 0),
]; ];
resultUser = randomArray(0, 5, resultUser);
resultUser = randomArray(5, 10, resultUser);
} }
return resultUser.map((u) => ({ return resultUser
.map((u) => ({
...u, ...u,
phone: hidePhone(u.phone), phone: hidePhone(u.phone),
verified: true, verified: true,
image: u.image ? `${Config.minioPublicBucketEndpoint}${u.image}` : null, image: u.image ? `${Config.minioPublicBucketEndpoint}${u.image}` : null,
})); }))
.slice(0, limit);
} }
async function getUser(userId: number, showPhone: boolean) { async function getUser(userId: number, showPhone: boolean) {
@@ -503,3 +508,11 @@ async function getZone(province?: number) {
.then((queryResult) => queryResult.map((v) => v.id)); .then((queryResult) => queryResult.map((v) => v.id));
return zoneIds; return zoneIds;
} }
function randomArray<T>(from: number, to: number, arr: T[]): T[] {
for (let i = Math.min(arr.length - 1, from); i < to; i++) {
const j = Math.floor(Math.random() * (i - from)) + from;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}