181 lines
4.5 KiB
TypeScript
181 lines
4.5 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.json";
|
|
|
|
const user_data: UserData[] = ud;
|
|
|
|
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();
|
|
|
|
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,
|
|
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;
|
|
district: 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) {
|
|
const thisProvince = provinces.find((p) => p.name == newUser.province);
|
|
const thisDistrict = district.find((p) => p.name == newUser.district);
|
|
let isSelectionFound = true;
|
|
for (const selection of newUser.selection) {
|
|
const isFound = user_data.findIndex(
|
|
(p) =>
|
|
`${p.first_name} ${p.last_name}` == selection &&
|
|
p.district == newUser.district,
|
|
);
|
|
if (isFound == -1) {
|
|
isSelectionFound = false;
|
|
}
|
|
}
|
|
if (
|
|
thisProvince === undefined ||
|
|
thisDistrict === undefined ||
|
|
!isSelectionFound
|
|
) {
|
|
console.log(
|
|
newUser.province,
|
|
newUser.district,
|
|
thisDistrict,
|
|
thisProvince,
|
|
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: thisDistrict.id,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
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 &&
|
|
raw.district == u.zone.name &&
|
|
raw.province == u.zone.province.name,
|
|
);
|
|
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.id == u.zone.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();
|