Files
sorvor-back/addMetadata.ts
Thanu Poptiphueng cba647ca27
All checks were successful
backend-action / build-image (push) Successful in 11m53s
randomize result
2024-06-24 15:16:47 +07:00

172 lines
4.3 KiB
TypeScript

import { db } from "./src/db";
import {
group,
opinion,
zone,
province,
user,
userToSelection,
} from "./src/schema";
import { Groups, Opinions, Provinces, Districts } from "./initialData";
import { createBucket, createClient } from "./src/minio";
import { Config } from "./src/config";
import ud from "./user-c.json";
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() {
try {
await setupBucket();
} catch {
console.error("Setting up bucket failed");
}
const isInitialized = await db.query.group
.findMany()
.then((groups) => groups.length > 0);
if (isInitialized) {
console.log("Already initialized");
return;
}
let groupValues = Groups.map((group) => ({ name: group }));
await db.insert(group).values(groupValues);
let opinionValues = Opinions.map((opinion) => ({
name: opinion.name,
type: opinion.choicemode,
}));
await db.insert(opinion).values(opinionValues);
let provinceValues = Provinces.map((province) => ({
id: province.code,
name: province.name_th,
}));
await db.insert(province).values(provinceValues);
const zoneValues = Districts.map((district) => ({
id: district.code,
name: district.name_th,
province: district.province_code,
}));
await db.insert(zone).values(zoneValues);
await create_user();
await create_relation();
const allUser = await db.query.user.findMany({
with: {
userToSelection: { with: { selection: true } },
},
});
// for (const u of allUser) {
// console.log(
// u.firstName,
// u.lastName,
// u.userToSelection.map(
// (t) => `${t.selection?.firstName} ${t.selection?.lastName}`,
// ),
// );
// }
console.log("Done");
}
type UserData = {
first_name: string;
last_name: string;
job_code: number;
selection: string[];
province: string;
rank: number;
};
async function create_user() {
const provinces = await db.query.province.findMany({});
const district = await db.query.zone.findMany({});
for (const newUser of user_data) {
let isSelectionFound = true;
for (const selection of newUser.selection) {
const isFound = user_data.findIndex(
(p) => `${p.first_name} ${p.last_name}` == selection,
);
if (isFound == -1) {
isSelectionFound = false;
}
}
if (!isSelectionFound) {
console.log(newUser.province, newUser, isSelectionFound);
} else {
await db.insert(user).values({
group: newUser.job_code,
firstName: newUser.first_name,
lastName: newUser.last_name,
title: "",
cid: "0000000000000",
phone: "0000000000",
age: 0,
job: "",
education: "",
zone: 1001,
rank: newUser.rank,
});
}
}
}
async function create_relation() {
const allUser = await db.query.user.findMany({
with: {
zone: { with: { province: true } },
},
});
for (const u of allUser) {
let thisUsers = user_data.filter(
(raw) => raw.first_name == u.firstName && raw.last_name == u.lastName,
);
if (thisUsers.length !== 1) {
console.log("duplicated users", thisUsers);
return;
}
const rawUser = thisUsers[0];
const selections = allUser.filter(
(target) =>
rawUser.selection.includes(`${target.firstName} ${target.lastName}`) &&
target.zone.province.id == u.zone.province.id,
);
if (selections.length == 0) {
console.log("selection not found", selections);
return;
}
for (const selection of selections) {
await db
.insert(userToSelection)
.values({ userId: u.id, targetId: selection.id });
}
}
}
async function setupBucket() {
const BucketPolicy = {
Version: "2012-10-17",
Statement: [
{
Action: ["s3:GetObject"],
Effect: "Allow",
Principal: "*",
Resource: [`arn:aws:s3:::${Config.bucketName}/*`],
Sid: "AllowAnonymousAccess",
},
],
};
let mc = createClient();
await createBucket(mc);
await mc.setBucketPolicy(Config.bucketName, JSON.stringify(BucketPolicy));
}
main();