websockets implemented
This commit is contained in:
81
backend/utils/falukant/initializeFalukantTypes.js
Normal file
81
backend/utils/falukant/initializeFalukantTypes.js
Normal file
@@ -0,0 +1,81 @@
|
||||
import RegionData from "../../models/falukant/data/region.js";
|
||||
import RegionType from "../../models/falukant/type/region.js";
|
||||
|
||||
const regionTypes = [];
|
||||
const regionTypeTrs = [
|
||||
"country",
|
||||
"duchy",
|
||||
"markgravate",
|
||||
"shire",
|
||||
"county",
|
||||
"city"
|
||||
];
|
||||
const regions = [
|
||||
{ labelTr: "falukant", regionType: "country", parentTr: null },
|
||||
{ labelTr: "duchy1", regionType: "duchy", parentTr: "falukant" },
|
||||
{ labelTr: "markgravate", regionType: "markgravate", parentTr: "duchy1" },
|
||||
{ labelTr: "shire1", regionType: "shire", parentTr: "markgravate" },
|
||||
{ labelTr: "county1", regionType: "county", parentTr: "shire1" },
|
||||
{ labelTr: "town1", regionType: "city", parentTr: "county1" },
|
||||
{ labelTr: "town2", regionType: "city", parentTr: "county1" },
|
||||
{ labelTr: "town3", regionType: "city", parentTr: "county1" },
|
||||
{ labelTr: "town4", regionType: "city", parentTr: "county1" },
|
||||
];
|
||||
|
||||
// Step 1: Initialize region types
|
||||
export const initializeFalukantTypes = async () => {
|
||||
await initializeFalukantTypeRegions();
|
||||
}
|
||||
|
||||
const initializeFalukantTypeRegions = async () => {
|
||||
for (const regionType of regionTypeTrs) {
|
||||
const [regionTypeRecord] = await RegionType.findOrCreate({
|
||||
where: { labelTr: regionType },
|
||||
defaults: {
|
||||
parentId: regionTypes[regionTypes.length - 1]?.id
|
||||
}
|
||||
});
|
||||
|
||||
regionTypes.push(regionTypeRecord);
|
||||
}
|
||||
};
|
||||
|
||||
// Utility: Find region type object by region type
|
||||
const findRegionTypeObjectByRegionType = async (regionType) => {
|
||||
return regionTypes.find(region => region.labelTr === regionType) || null;
|
||||
};
|
||||
|
||||
// Utility: Find region object by label
|
||||
const findRegionObjectByLabelTr = async (labelTr) => {
|
||||
return await RegionData.findOne({ where: { name: labelTr } });
|
||||
};
|
||||
|
||||
// Step 2: Initialize regions
|
||||
export const initializeFalukantRegions = async () => {
|
||||
for (const region of regions) {
|
||||
const regionType = await findRegionTypeObjectByRegionType(region.regionType);
|
||||
if (!regionType) {
|
||||
console.error(`Region type not found for: ${region.regionType}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const parentRegion = region.parentTr
|
||||
? await findRegionObjectByLabelTr(region.parentTr)
|
||||
: null;
|
||||
|
||||
if (region.parentTr && !parentRegion) {
|
||||
console.error(`Parent region not found for: ${region.parentTr}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log('Creating/Fetching Region:', region.labelTr, 'Type:', regionType.labelTr, 'Parent:', parentRegion?.name);
|
||||
|
||||
await RegionData.findOrCreate({
|
||||
where: { name: region.labelTr },
|
||||
defaults: {
|
||||
regionTypeId: regionType.id,
|
||||
parentId: parentRegion?.id || null
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user