Some fixes and additions

This commit is contained in:
Torsten Schulz
2025-07-09 14:28:35 +02:00
parent 5029be81e9
commit fceea5b7fb
32 changed files with 4373 additions and 1294 deletions

View File

@@ -85,434 +85,437 @@ import PoliticalOffice from './falukant/data/political_office.js';
import PoliticalOfficeBenefit from './falukant/predefine/political_office_benefit.js';
import PoliticalOfficeBenefitType from './falukant/type/political_office_benefit_type.js';
import PoliticalOfficeRequirement from './falukant/predefine/political_office_prerequisite.js';
import PoliticalOfficePrerequisite from './falukant/predefine/political_office_prerequisite.js';
import PoliticalOfficeHistory from './falukant/log/political_office_history.js';
import ElectionHistory from './falukant/log/election_history.js';
export default function setupAssociations() {
// UserParam related associations
SettingsType.hasMany(UserParamType, { foreignKey: 'settingsId', as: 'user_param_types' });
UserParamType.belongsTo(SettingsType, { foreignKey: 'settingsId', as: 'settings_type' });
UserParamType.hasMany(UserParam, { foreignKey: 'paramTypeId', as: 'user_params' });
UserParam.belongsTo(UserParamType, { foreignKey: 'paramTypeId', as: 'paramType' });
User.hasMany(UserParam, { foreignKey: 'userId', as: 'user_params' });
UserParam.belongsTo(User, { foreignKey: 'userId', as: 'user' });
UserParamValue.belongsTo(UserParamType, { foreignKey: 'userParamTypeId', as: 'user_param_value_type' });
UserParamType.hasMany(UserParamValue, { foreignKey: 'userParamTypeId', as: 'user_param_type_value' });
UserRight.belongsTo(User, { foreignKey: 'userId', as: 'user_with_rights' });
UserRight.belongsTo(UserRightType, { foreignKey: 'rightTypeId', as: 'rightType' });
UserRightType.hasMany(UserRight, { foreignKey: 'rightTypeId', as: 'user_rights' });
UserParam.hasMany(UserParamVisibility, { foreignKey: 'param_id', as: 'param_visibilities' });
UserParamVisibility.belongsTo(UserParam, { foreignKey: 'param_id', as: 'param' });
UserParamVisibility.belongsTo(UserParamVisibilityType, { foreignKey: 'visibility', as: 'visibility_type' });
UserParamVisibilityType.hasMany(UserParamVisibility, { foreignKey: 'visibility', as: 'user_param_visibilities' });
// Interest related associations
InterestType.hasMany(InterestTranslationType, { foreignKey: 'interestsId', as: 'interest_translations' });
InterestTranslationType.belongsTo(InterestType, { foreignKey: 'interestsId', as: 'interest_translations' });
InterestType.hasMany(Interest, { foreignKey: 'userinterestId', as: 'user_interest_type' });
User.hasMany(Interest, { foreignKey: 'userId', as: 'user_interests' });
Interest.belongsTo(InterestType, { foreignKey: 'userinterestId', as: 'interest_type' });
Interest.belongsTo(User, { foreignKey: 'userId', as: 'interest_owner' });
// Folder and Image related associations
Folder.belongsTo(User, { foreignKey: 'userId' });
User.hasMany(Folder, { foreignKey: 'userId' });
Folder.belongsTo(Folder, { foreignKey: 'parentId', as: 'parent' });
Folder.hasMany(Folder, { foreignKey: 'parentId', as: 'children' });
Image.belongsTo(Folder, { foreignKey: 'folderId' });
Folder.hasMany(Image, { foreignKey: 'folderId' });
Image.belongsTo(User, { foreignKey: 'userId' });
User.hasMany(Image, { foreignKey: 'userId' });
// Image visibility associations
Folder.belongsToMany(ImageVisibilityType, {
through: FolderImageVisibility,
foreignKey: 'folderId',
otherKey: 'visibilityTypeId'
});
ImageVisibilityType.belongsToMany(Folder, {
through: FolderImageVisibility,
foreignKey: 'visibilityTypeId',
otherKey: 'folderId'
});
Image.belongsToMany(ImageVisibilityType, {
through: ImageImageVisibility,
foreignKey: 'imageId',
otherKey: 'visibilityTypeId'
});
ImageVisibilityType.belongsToMany(Image, {
through: ImageImageVisibility,
foreignKey: 'visibilityTypeId',
otherKey: 'imageId'
});
Folder.belongsToMany(ImageVisibilityUser, {
through: FolderVisibilityUser,
foreignKey: 'folderId',
otherKey: 'visibilityUserId'
});
ImageVisibilityUser.belongsToMany(Folder, {
through: FolderVisibilityUser,
foreignKey: 'visibilityUserId',
otherKey: 'folderId'
});
// UserParam related associations
SettingsType.hasMany(UserParamType, { foreignKey: 'settingsId', as: 'user_param_types' });
UserParamType.belongsTo(SettingsType, { foreignKey: 'settingsId', as: 'settings_type' });
UserParamType.hasMany(UserParam, { foreignKey: 'paramTypeId', as: 'user_params' });
UserParam.belongsTo(UserParamType, { foreignKey: 'paramTypeId', as: 'paramType' });
User.hasMany(UserParam, { foreignKey: 'userId', as: 'user_params' });
UserParam.belongsTo(User, { foreignKey: 'userId', as: 'user' });
UserParamValue.belongsTo(UserParamType, { foreignKey: 'userParamTypeId', as: 'user_param_value_type' });
UserParamType.hasMany(UserParamValue, { foreignKey: 'userParamTypeId', as: 'user_param_type_value' });
UserRight.belongsTo(User, { foreignKey: 'userId', as: 'user_with_rights' });
UserRight.belongsTo(UserRightType, { foreignKey: 'rightTypeId', as: 'rightType' });
UserRightType.hasMany(UserRight, { foreignKey: 'rightTypeId', as: 'user_rights' });
UserParam.hasMany(UserParamVisibility, { foreignKey: 'param_id', as: 'param_visibilities' });
UserParamVisibility.belongsTo(UserParam, { foreignKey: 'param_id', as: 'param' });
UserParamVisibility.belongsTo(UserParamVisibilityType, { foreignKey: 'visibility', as: 'visibility_type' });
UserParamVisibilityType.hasMany(UserParamVisibility, { foreignKey: 'visibility', as: 'user_param_visibilities' });
// Interest related associations
InterestType.hasMany(InterestTranslationType, { foreignKey: 'interestsId', as: 'interest_translations' });
InterestTranslationType.belongsTo(InterestType, { foreignKey: 'interestsId', as: 'interest_translations' });
InterestType.hasMany(Interest, { foreignKey: 'userinterestId', as: 'user_interest_type' });
User.hasMany(Interest, { foreignKey: 'userId', as: 'user_interests' });
Interest.belongsTo(InterestType, { foreignKey: 'userinterestId', as: 'interest_type' });
Interest.belongsTo(User, { foreignKey: 'userId', as: 'interest_owner' });
// Folder and Image related associations
Folder.belongsTo(User, { foreignKey: 'userId' });
User.hasMany(Folder, { foreignKey: 'userId' });
Folder.belongsTo(Folder, { foreignKey: 'parentId', as: 'parent' });
Folder.hasMany(Folder, { foreignKey: 'parentId', as: 'children' });
Image.belongsTo(Folder, { foreignKey: 'folderId' });
Folder.hasMany(Image, { foreignKey: 'folderId' });
Image.belongsTo(User, { foreignKey: 'userId' });
User.hasMany(Image, { foreignKey: 'userId' });
// Image visibility associations
Folder.belongsToMany(ImageVisibilityType, {
through: FolderImageVisibility,
foreignKey: 'folderId',
otherKey: 'visibilityTypeId'
});
ImageVisibilityType.belongsToMany(Folder, {
through: FolderImageVisibility,
foreignKey: 'visibilityTypeId',
otherKey: 'folderId'
});
Image.belongsToMany(ImageVisibilityType, {
through: ImageImageVisibility,
foreignKey: 'imageId',
otherKey: 'visibilityTypeId'
});
ImageVisibilityType.belongsToMany(Image, {
through: ImageImageVisibility,
foreignKey: 'visibilityTypeId',
otherKey: 'imageId'
});
Folder.belongsToMany(ImageVisibilityUser, {
through: FolderVisibilityUser,
foreignKey: 'folderId',
otherKey: 'visibilityUserId'
});
ImageVisibilityUser.belongsToMany(Folder, {
through: FolderVisibilityUser,
foreignKey: 'visibilityUserId',
otherKey: 'folderId'
});
// Guestbook related associations
User.hasMany(GuestbookEntry, { foreignKey: 'recipientId', as: 'receivedEntries' });
User.hasMany(GuestbookEntry, { foreignKey: 'senderId', as: 'sentEntries' });
GuestbookEntry.belongsTo(User, { foreignKey: 'recipientId', as: 'recipient' });
GuestbookEntry.belongsTo(User, { foreignKey: 'senderId', as: 'sender' });
// Guestbook related associations
User.hasMany(GuestbookEntry, { foreignKey: 'recipientId', as: 'receivedEntries' });
User.hasMany(GuestbookEntry, { foreignKey: 'senderId', as: 'sentEntries' });
GuestbookEntry.belongsTo(User, { foreignKey: 'recipientId', as: 'recipient' });
GuestbookEntry.belongsTo(User, { foreignKey: 'senderId', as: 'sender' });
// Forum related associations
Forum.hasMany(Title, { foreignKey: 'forumId' });
Title.belongsTo(Forum, { foreignKey: 'forumId' });
// Forum related associations
Forum.hasMany(Title, { foreignKey: 'forumId' });
Title.belongsTo(Forum, { foreignKey: 'forumId' });
Title.belongsTo(User, { foreignKey: 'createdBy', as: 'createdByUser' });
User.hasMany(Title, { foreignKey: 'createdBy', as: 'titles' });
Title.belongsTo(User, { foreignKey: 'createdBy', as: 'createdByUser' });
User.hasMany(Title, { foreignKey: 'createdBy', as: 'titles' });
Title.hasMany(Message, { foreignKey: 'titleId', as: 'messages' });
Message.belongsTo(Title, { foreignKey: 'titleId', as: 'title' });
Title.hasMany(Message, { foreignKey: 'titleId', as: 'messages' });
Message.belongsTo(Title, { foreignKey: 'titleId', as: 'title' });
Message.belongsTo(User, { foreignKey: 'createdBy', as: 'lastMessageUser' });
User.hasMany(Message, { foreignKey: 'createdBy', as: 'userMessages' });
Message.belongsTo(User, { foreignKey: 'createdBy', as: 'lastMessageUser' });
User.hasMany(Message, { foreignKey: 'createdBy', as: 'userMessages' });
Message.hasMany(MessageImage, { foreignKey: 'messageId' });
MessageImage.belongsTo(Message, { foreignKey: 'messageId' });
Message.hasMany(MessageImage, { foreignKey: 'messageId' });
MessageImage.belongsTo(Message, { foreignKey: 'messageId' });
Message.hasMany(MessageHistory, { foreignKey: 'messageId' });
MessageHistory.belongsTo(Message, { foreignKey: 'messageId' });
Message.hasMany(MessageHistory, { foreignKey: 'messageId' });
MessageHistory.belongsTo(Message, { foreignKey: 'messageId' });
Title.hasMany(TitleHistory, { foreignKey: 'titleId' });
TitleHistory.belongsTo(Title, { foreignKey: 'titleId' });
Title.hasMany(TitleHistory, { foreignKey: 'titleId' });
TitleHistory.belongsTo(Title, { foreignKey: 'titleId' });
// Forum permissions associations
Forum.hasMany(ForumUserPermission, { foreignKey: 'forumId', as: 'userPermissions' });
ForumUserPermission.belongsTo(Forum, { foreignKey: 'forumId' });
// Forum permissions associations
Forum.hasMany(ForumUserPermission, { foreignKey: 'forumId', as: 'userPermissions' });
ForumUserPermission.belongsTo(Forum, { foreignKey: 'forumId' });
User.hasMany(ForumUserPermission, { foreignKey: 'userId', as: 'userPermissions' });
ForumUserPermission.belongsTo(User, { foreignKey: 'userId' });
User.hasMany(ForumUserPermission, { foreignKey: 'userId', as: 'userPermissions' });
ForumUserPermission.belongsTo(User, { foreignKey: 'userId' });
Forum.belongsToMany(ForumPermission, {
through: ForumForumPermission,
foreignKey: 'forumId',
as: 'associatedPermissions'
});
Forum.belongsToMany(ForumPermission, {
through: ForumForumPermission,
foreignKey: 'forumId',
as: 'associatedPermissions'
});
ForumPermission.belongsToMany(Forum, {
through: ForumForumPermission,
foreignKey: 'permissionId',
as: 'forums'
});
ForumPermission.belongsToMany(Forum, {
through: ForumForumPermission,
foreignKey: 'permissionId',
as: 'forums'
});
ForumPermission.hasMany(ForumUserPermission, { foreignKey: 'permissionId' });
ForumUserPermission.belongsTo(ForumPermission, { foreignKey: 'permissionId' });
ForumPermission.hasMany(ForumUserPermission, { foreignKey: 'permissionId' });
ForumUserPermission.belongsTo(ForumPermission, { foreignKey: 'permissionId' });
Friendship.belongsTo(User, { foreignKey: 'user1Id', as: 'friendSender' });
Friendship.belongsTo(User, { foreignKey: 'user2Id', as: 'friendReceiver' });
User.hasMany(Friendship, { foreignKey: 'user1Id', as: 'friendSender' });
User.hasMany(Friendship, { foreignKey: 'user2Id', as: 'friendReceiver' });
Friendship.belongsTo(User, { foreignKey: 'user1Id', as: 'friendSender' });
Friendship.belongsTo(User, { foreignKey: 'user2Id', as: 'friendReceiver' });
User.hasMany(Friendship, { foreignKey: 'user1Id', as: 'friendSender' });
User.hasMany(Friendship, { foreignKey: 'user2Id', as: 'friendReceiver' });
User.hasMany(FalukantUser, { foreignKey: 'userId', as: 'falukantData' });
FalukantUser.belongsTo(User, { foreignKey: 'userId', as: 'user' });
User.hasMany(FalukantUser, { foreignKey: 'userId', as: 'falukantData' });
FalukantUser.belongsTo(User, { foreignKey: 'userId', as: 'user' });
RegionType.hasMany(RegionType, { foreignKey: 'parentId', as: 'children' });
RegionType.belongsTo(RegionType, { foreignKey: 'parentId', as: 'parent' });
RegionType.hasMany(RegionType, { foreignKey: 'parentId', as: 'children' });
RegionType.belongsTo(RegionType, { foreignKey: 'parentId', as: 'parent' });
RegionData.hasMany(RegionData, { foreignKey: 'parentId', as: 'children' });
RegionData.belongsTo(RegionData, { foreignKey: 'parentId', as: 'parent' });
RegionData.hasMany(RegionData, { foreignKey: 'parentId', as: 'children' });
RegionData.belongsTo(RegionData, { foreignKey: 'parentId', as: 'parent' });
RegionData.belongsTo(RegionType, { foreignKey: 'regionTypeId', as: 'regionType' });
RegionType.hasMany(RegionData, { foreignKey: 'regionTypeId', as: 'regions' });
RegionData.belongsTo(RegionType, { foreignKey: 'regionTypeId', as: 'regionType' });
RegionType.hasMany(RegionData, { foreignKey: 'regionTypeId', as: 'regions' });
FalukantUser.belongsTo(RegionData, { foreignKey: 'mainBranchRegionId', as: 'mainBranchRegion' });
RegionData.hasMany(FalukantUser, { foreignKey: 'mainBranchRegionId', as: 'users' });
FalukantUser.belongsTo(RegionData, { foreignKey: 'mainBranchRegionId', as: 'mainBranchRegion' });
RegionData.hasMany(FalukantUser, { foreignKey: 'mainBranchRegionId', as: 'users' });
FalukantCharacter.belongsTo(FalukantUser, { foreignKey: 'userId', as: 'user' });
FalukantUser.hasOne(FalukantCharacter, { foreignKey: 'userId', as: 'character' });
FalukantCharacter.belongsTo(FalukantUser, { foreignKey: 'userId', as: 'user' });
FalukantUser.hasOne(FalukantCharacter, { foreignKey: 'userId', as: 'character' });
FalukantCharacter.belongsTo(FalukantPredefineFirstname, { foreignKey: 'firstName', as: 'definedFirstName' });
FalukantPredefineFirstname.hasMany(FalukantCharacter, { foreignKey: 'firstName', as: 'charactersWithFirstName' });
FalukantCharacter.belongsTo(FalukantPredefineFirstname, { foreignKey: 'firstName', as: 'definedFirstName' });
FalukantPredefineFirstname.hasMany(FalukantCharacter, { foreignKey: 'firstName', as: 'charactersWithFirstName' });
FalukantCharacter.belongsTo(FalukantPredefineLastname, { foreignKey: 'lastName', as: 'definedLastName' });
FalukantPredefineLastname.hasMany(FalukantCharacter, { foreignKey: 'lastName', as: 'charactersWithLastName' });
FalukantCharacter.belongsTo(FalukantPredefineLastname, { foreignKey: 'lastName', as: 'definedLastName' });
FalukantPredefineLastname.hasMany(FalukantCharacter, { foreignKey: 'lastName', as: 'charactersWithLastName' });
FalukantCharacter.belongsTo(TitleOfNobility, { foreignKey: 'titleOfNobility', as: 'nobleTitle' });
TitleOfNobility.hasMany(FalukantCharacter, { foreignKey: 'titleOfNobility', as: 'charactersWithNobleTitle' });
FalukantCharacter.belongsTo(TitleOfNobility, { foreignKey: 'titleOfNobility', as: 'nobleTitle' });
TitleOfNobility.hasMany(FalukantCharacter, { foreignKey: 'titleOfNobility', as: 'charactersWithNobleTitle' });
FalukantCharacter.belongsTo(RegionData, { foreignKey: 'regionId', as: 'region' });
RegionData.hasMany(FalukantCharacter, { foreignKey: 'regionId', as: 'charactersInRegion' });
FalukantCharacter.belongsTo(RegionData, { foreignKey: 'regionId', as: 'region' });
RegionData.hasMany(FalukantCharacter, { foreignKey: 'regionId', as: 'charactersInRegion' });
FalukantStock.belongsTo(FalukantStockType, { foreignKey: 'stockTypeId', as: 'stockType' });
FalukantStockType.hasMany(FalukantStock, { foreignKey: 'stockTypeId', as: 'stocks' });
FalukantStock.belongsTo(FalukantStockType, { foreignKey: 'stockTypeId', as: 'stockType' });
FalukantStockType.hasMany(FalukantStock, { foreignKey: 'stockTypeId', as: 'stocks' });
Knowledge.belongsTo(ProductType, { foreignKey: 'productId', as: 'productType' });
ProductType.hasMany(Knowledge, { foreignKey: 'productId', as: 'knowledges' });
Knowledge.belongsTo(ProductType, { foreignKey: 'productId', as: 'productType' });
ProductType.hasMany(Knowledge, { foreignKey: 'productId', as: 'knowledges' });
Knowledge.belongsTo(FalukantCharacter, { foreignKey: 'characterId', as: 'character' });
FalukantCharacter.hasMany(Knowledge, { foreignKey: 'characterId', as: 'knowledges' });
Knowledge.belongsTo(FalukantCharacter, { foreignKey: 'characterId', as: 'character' });
FalukantCharacter.hasMany(Knowledge, { foreignKey: 'characterId', as: 'knowledges' });
TitleRequirement.belongsTo(TitleOfNobility, { foreignKey: 'titleId', as: 'title' });
TitleOfNobility.hasMany(TitleRequirement, { foreignKey: 'titleId', as: 'requirements' });
TitleRequirement.belongsTo(TitleOfNobility, { foreignKey: 'titleId', as: 'title' });
TitleOfNobility.hasMany(TitleRequirement, { foreignKey: 'titleId', as: 'requirements' });
Branch.belongsTo(RegionData, { foreignKey: 'regionId', as: 'region' });
RegionData.hasMany(Branch, { foreignKey: 'regionId', as: 'branches' });
Branch.belongsTo(RegionData, { foreignKey: 'regionId', as: 'region' });
RegionData.hasMany(Branch, { foreignKey: 'regionId', as: 'branches' });
Branch.belongsTo(FalukantUser, { foreignKey: 'falukantUserId', as: 'user' });
FalukantUser.hasMany(Branch, { foreignKey: 'falukantUserId', as: 'branches' });
Branch.belongsTo(FalukantUser, { foreignKey: 'falukantUserId', as: 'user' });
FalukantUser.hasMany(Branch, { foreignKey: 'falukantUserId', as: 'branches' });
Branch.belongsTo(BranchType, { foreignKey: 'branchTypeId', as: 'branchType' });
BranchType.hasMany(Branch, { foreignKey: 'branchTypeId', as: 'branches' });
Branch.belongsTo(BranchType, { foreignKey: 'branchTypeId', as: 'branchType' });
BranchType.hasMany(Branch, { foreignKey: 'branchTypeId', as: 'branches' });
Production.belongsTo(Branch, { foreignKey: 'branchId', as: 'branch' });
Branch.hasMany(Production, { foreignKey: 'branchId', as: 'productions' });
Production.belongsTo(Branch, { foreignKey: 'branchId', as: 'branch' });
Branch.hasMany(Production, { foreignKey: 'branchId', as: 'productions' });
Production.belongsTo(ProductType, { foreignKey: 'productId', as: 'productType' });
ProductType.hasMany(Production, { foreignKey: 'productId', as: 'productions' });
Production.belongsTo(ProductType, { foreignKey: 'productId', as: 'productType' });
ProductType.hasMany(Production, { foreignKey: 'productId', as: 'productions' });
Inventory.belongsTo(FalukantStock, { foreignKey: 'stockId', as: 'stock' });
FalukantStock.hasMany(Inventory, { foreignKey: 'stockId', as: 'inventories' });
Inventory.belongsTo(FalukantStock, { foreignKey: 'stockId', as: 'stock' });
FalukantStock.hasMany(Inventory, { foreignKey: 'stockId', as: 'inventories' });
Inventory.belongsTo(ProductType, { foreignKey: 'productId', as: 'productType' });
ProductType.hasMany(Inventory, { foreignKey: 'productId', as: 'inventories' });
Inventory.belongsTo(ProductType, { foreignKey: 'productId', as: 'productType' });
ProductType.hasMany(Inventory, { foreignKey: 'productId', as: 'inventories' });
BuyableStock.belongsTo(RegionData, { foreignKey: 'regionId', as: 'region' });
RegionData.hasMany(BuyableStock, { foreignKey: 'regionId', as: 'buyableStocks' });
BuyableStock.belongsTo(RegionData, { foreignKey: 'regionId', as: 'region' });
RegionData.hasMany(BuyableStock, { foreignKey: 'regionId', as: 'buyableStocks' });
Branch.hasMany(FalukantStock, { foreignKey: 'branchId', as: 'stocks' });
FalukantStock.belongsTo(Branch, { foreignKey: 'branchId', as: 'branch' });
Branch.hasMany(FalukantStock, { foreignKey: 'branchId', as: 'stocks' });
FalukantStock.belongsTo(Branch, { foreignKey: 'branchId', as: 'branch' });
MoneyFlow.belongsTo(FalukantUser, { foreignKey: 'falukantUserId', as: 'user' });
FalukantUser.hasMany(MoneyFlow, { foreignKey: 'falukantUserId', as: 'flows' });
MoneyFlow.belongsTo(FalukantUser, { foreignKey: 'falukantUserId', as: 'user' });
FalukantUser.hasMany(MoneyFlow, { foreignKey: 'falukantUserId', as: 'flows' });
BuyableStock.belongsTo(FalukantStockType, { foreignKey: 'stockTypeId', as: 'stockType' });
FalukantStockType.hasMany(BuyableStock, { foreignKey: 'stockTypeId', as: 'buyableStocks' });
BuyableStock.belongsTo(FalukantStockType, { foreignKey: 'stockTypeId', as: 'stockType' });
FalukantStockType.hasMany(BuyableStock, { foreignKey: 'stockTypeId', as: 'buyableStocks' });
Director.belongsTo(FalukantUser, { foreignKey: 'employerUserId', as: 'user' });
FalukantUser.hasMany(Director, { foreignKey: 'employerUserId', as: 'directors' });
Director.belongsTo(FalukantUser, { foreignKey: 'employerUserId', as: 'user' });
FalukantUser.hasMany(Director, { foreignKey: 'employerUserId', as: 'directors' });
Director.belongsTo(FalukantCharacter, { foreignKey: 'directorCharacterId', as: 'character' });
FalukantCharacter.hasMany(Director, { foreignKey: 'directorCharacterId', as: 'directors' });
Director.belongsTo(FalukantCharacter, { foreignKey: 'directorCharacterId', as: 'character' });
FalukantCharacter.hasMany(Director, { foreignKey: 'directorCharacterId', as: 'directors' });
DirectorProposal.belongsTo(FalukantUser, { foreignKey: 'employerUserId', as: 'user' });
FalukantUser.hasMany(DirectorProposal, { foreignKey: 'employerUserId', as: 'directorProposals' });
DirectorProposal.belongsTo(FalukantUser, { foreignKey: 'employerUserId', as: 'user' });
FalukantUser.hasMany(DirectorProposal, { foreignKey: 'employerUserId', as: 'directorProposals' });
DirectorProposal.belongsTo(FalukantCharacter, { foreignKey: 'directorCharacterId', as: 'character' });
FalukantCharacter.hasMany(DirectorProposal, { foreignKey: 'directorCharacterId', as: 'directorProposals' });
DirectorProposal.belongsTo(FalukantCharacter, { foreignKey: 'directorCharacterId', as: 'character' });
FalukantCharacter.hasMany(DirectorProposal, { foreignKey: 'directorCharacterId', as: 'directorProposals' });
TownProductWorth.belongsTo(ProductType, { foreignKey: 'productId', as: 'productType' });
ProductType.hasMany(TownProductWorth, { foreignKey: 'productId', as: 'townProductWorths' });
TownProductWorth.belongsTo(ProductType, { foreignKey: 'productId', as: 'productType' });
ProductType.hasMany(TownProductWorth, { foreignKey: 'productId', as: 'townProductWorths' });
TownProductWorth.belongsTo(RegionData, { foreignKey: 'regionId', as: 'region' });
RegionData.hasMany(TownProductWorth, { foreignKey: 'regionId', as: 'townProductWorths' });
TownProductWorth.belongsTo(RegionData, { foreignKey: 'regionId', as: 'region' });
RegionData.hasMany(TownProductWorth, { foreignKey: 'regionId', as: 'townProductWorths' });
DayProduction.belongsTo(ProductType, { foreignKey: 'productId', as: 'productType' });
ProductType.hasMany(DayProduction, { foreignKey: 'productId', as: 'dayProductions' });
DayProduction.belongsTo(ProductType, { foreignKey: 'productId', as: 'productType' });
ProductType.hasMany(DayProduction, { foreignKey: 'productId', as: 'dayProductions' });
DayProduction.belongsTo(RegionData, { foreignKey: 'regionId', as: 'region' });
RegionData.hasMany(DayProduction, { foreignKey: 'regionId', as: 'dayProductions' });
DayProduction.belongsTo(RegionData, { foreignKey: 'regionId', as: 'region' });
RegionData.hasMany(DayProduction, { foreignKey: 'regionId', as: 'dayProductions' });
DayProduction.belongsTo(FalukantUser, { foreignKey: 'producerId', as: 'user' });
FalukantUser.hasMany(DayProduction, { foreignKey: 'producerId', as: 'dayProductions' });
DayProduction.belongsTo(FalukantUser, { foreignKey: 'producerId', as: 'user' });
FalukantUser.hasMany(DayProduction, { foreignKey: 'producerId', as: 'dayProductions' });
DaySell.belongsTo(ProductType, { foreignKey: 'productId', as: 'productType' });
ProductType.hasMany(DaySell, { foreignKey: 'productId', as: 'daySells' });
DaySell.belongsTo(ProductType, { foreignKey: 'productId', as: 'productType' });
ProductType.hasMany(DaySell, { foreignKey: 'productId', as: 'daySells' });
DaySell.belongsTo(RegionData, { foreignKey: 'regionId', as: 'region' });
RegionData.hasMany(DaySell, { foreignKey: 'regionId', as: 'daySells' });
DaySell.belongsTo(RegionData, { foreignKey: 'regionId', as: 'region' });
RegionData.hasMany(DaySell, { foreignKey: 'regionId', as: 'daySells' });
DaySell.belongsTo(FalukantUser, { foreignKey: 'sellerId', as: 'user' });
FalukantUser.hasMany(DaySell, { foreignKey: 'sellerId', as: 'daySells' });
DaySell.belongsTo(FalukantUser, { foreignKey: 'sellerId', as: 'user' });
FalukantUser.hasMany(DaySell, { foreignKey: 'sellerId', as: 'daySells' });
Notification.belongsTo(FalukantUser, { foreignKey: 'userId', as: 'user' });
FalukantUser.hasMany(Notification, { foreignKey: 'userId', as: 'notifications' });
Notification.belongsTo(FalukantUser, { foreignKey: 'userId', as: 'user' });
FalukantUser.hasMany(Notification, { foreignKey: 'userId', as: 'notifications' });
MarriageProposal.belongsTo(FalukantCharacter, { foreignKey: 'requesterCharacterId', as: 'requesterCharacter', });
FalukantCharacter.hasMany(MarriageProposal, { foreignKey: 'requesterCharacterId', as: 'initiatedProposals' });
MarriageProposal.belongsTo(FalukantCharacter, { foreignKey: 'requesterCharacterId', as: 'requesterCharacter', });
FalukantCharacter.hasMany(MarriageProposal, { foreignKey: 'requesterCharacterId', as: 'initiatedProposals' });
MarriageProposal.belongsTo(FalukantCharacter, { foreignKey: 'proposedCharacterId', as: 'proposedCharacter', });
FalukantCharacter.hasMany(MarriageProposal, { foreignKey: 'proposedCharacterId', as: 'receivedProposals' });
MarriageProposal.belongsTo(FalukantCharacter, { foreignKey: 'proposedCharacterId', as: 'proposedCharacter', });
FalukantCharacter.hasMany(MarriageProposal, { foreignKey: 'proposedCharacterId', as: 'receivedProposals' });
FalukantCharacter.belongsToMany(CharacterTrait, { through: FalukantCharacterTrait, foreignKey: 'character_id', as: 'traits', });
CharacterTrait.belongsToMany(FalukantCharacter, { through: FalukantCharacterTrait, foreignKey: 'trait_id', as: 'characters', });
FalukantCharacter.belongsToMany(CharacterTrait, { through: FalukantCharacterTrait, foreignKey: 'character_id', as: 'traits', });
CharacterTrait.belongsToMany(FalukantCharacter, { through: FalukantCharacterTrait, foreignKey: 'trait_id', as: 'characters', });
Mood.hasMany(FalukantCharacter, { foreignKey: 'mood_id', as: 'moods' });
FalukantCharacter.belongsTo(Mood, { foreignKey: 'mood_id', as: 'mood' });
Mood.hasMany(FalukantCharacter, { foreignKey: 'mood_id', as: 'moods' });
FalukantCharacter.belongsTo(Mood, { foreignKey: 'mood_id', as: 'mood' });
PromotionalGift.belongsToMany(CharacterTrait, { through: PromotionalGiftCharacterTrait, foreignKey: 'gift_id', as: 'traits', });
CharacterTrait.belongsToMany(PromotionalGift, { through: PromotionalGiftCharacterTrait, foreignKey: 'trait_id', as: 'gifts', });
PromotionalGift.belongsToMany(CharacterTrait, { through: PromotionalGiftCharacterTrait, foreignKey: 'gift_id', as: 'traits', });
CharacterTrait.belongsToMany(PromotionalGift, { through: PromotionalGiftCharacterTrait, foreignKey: 'trait_id', as: 'gifts', });
PromotionalGift.belongsToMany(Mood, { through: PromotionalGiftMood, foreignKey: 'gift_id', as: 'moods', });
Mood.belongsToMany(PromotionalGift, { through: PromotionalGiftMood, foreignKey: 'mood_id', as: 'gifts', });
PromotionalGift.belongsToMany(Mood, { through: PromotionalGiftMood, foreignKey: 'gift_id', as: 'moods', });
Mood.belongsToMany(PromotionalGift, { through: PromotionalGiftMood, foreignKey: 'mood_id', as: 'gifts', });
Relationship.belongsTo(RelationshipType, { foreignKey: 'relationshipTypeId', as: 'relationshipType' });
RelationshipType.hasMany(Relationship, { foreignKey: 'relationshipTypeId', as: 'relationships' });
Relationship.belongsTo(RelationshipType, { foreignKey: 'relationshipTypeId', as: 'relationshipType' });
RelationshipType.hasMany(Relationship, { foreignKey: 'relationshipTypeId', as: 'relationships' });
Relationship.belongsTo(FalukantCharacter, { foreignKey: 'character1Id', as: 'character1', });
Relationship.belongsTo(FalukantCharacter, { foreignKey: 'character2Id', as: 'character2', });
FalukantCharacter.hasMany(Relationship, { foreignKey: 'character1Id', as: 'relationshipsAsCharacter1', });
FalukantCharacter.hasMany(Relationship, { foreignKey: 'character2Id', as: 'relationshipsAsCharacter2', });
Relationship.belongsTo(FalukantCharacter, { foreignKey: 'character1Id', as: 'character1', });
Relationship.belongsTo(FalukantCharacter, { foreignKey: 'character2Id', as: 'character2', });
FalukantCharacter.hasMany(Relationship, { foreignKey: 'character1Id', as: 'relationshipsAsCharacter1', });
FalukantCharacter.hasMany(Relationship, { foreignKey: 'character2Id', as: 'relationshipsAsCharacter2', });
PromotionalGiftLog.belongsTo(PromotionalGift, { foreignKey: 'giftId', as: 'gift' });
PromotionalGift.hasMany(PromotionalGiftLog, { foreignKey: 'giftId', as: 'logs' });
PromotionalGiftLog.belongsTo(PromotionalGift, { foreignKey: 'giftId', as: 'gift' });
PromotionalGift.hasMany(PromotionalGiftLog, { foreignKey: 'giftId', as: 'logs' });
PromotionalGiftLog.belongsTo(FalukantCharacter, { foreignKey: 'senderCharacterId', as: 'character' });
FalukantCharacter.hasMany(PromotionalGiftLog, { foreignKey: 'senderCharacterId', as: 'logs' });
PromotionalGiftLog.belongsTo(FalukantCharacter, { foreignKey: 'senderCharacterId', as: 'character' });
FalukantCharacter.hasMany(PromotionalGiftLog, { foreignKey: 'senderCharacterId', as: 'logs' });
PromotionalGiftLog.belongsTo(FalukantCharacter, { foreignKey: 'recipientCharacterId', as: 'recipient' });
FalukantCharacter.hasMany(PromotionalGiftLog, { foreignKey: 'recipientCharacterId', as: 'giftlogs' });
PromotionalGiftLog.belongsTo(FalukantCharacter, { foreignKey: 'recipientCharacterId', as: 'recipient' });
FalukantCharacter.hasMany(PromotionalGiftLog, { foreignKey: 'recipientCharacterId', as: 'giftlogs' });
PromotionalGift.hasMany(PromotionalGiftCharacterTrait, { foreignKey: 'gift_id', as: 'characterTraits' });
PromotionalGift.hasMany(PromotionalGiftMood, { foreignKey: 'gift_id', as: 'promotionalgiftmoods' });
PromotionalGift.hasMany(PromotionalGiftCharacterTrait, { foreignKey: 'gift_id', as: 'characterTraits' });
PromotionalGift.hasMany(PromotionalGiftMood, { foreignKey: 'gift_id', as: 'promotionalgiftmoods' });
PromotionalGiftCharacterTrait.belongsTo(PromotionalGift, { foreignKey: 'gift_id', as: 'promotionalgiftcharactertrait' });
PromotionalGiftMood.belongsTo(PromotionalGift, { foreignKey: 'gift_id', as: 'promotionalgiftcharactermood' });
PromotionalGiftCharacterTrait.belongsTo(PromotionalGift, { foreignKey: 'gift_id', as: 'promotionalgiftcharactertrait' });
PromotionalGiftMood.belongsTo(PromotionalGift, { foreignKey: 'gift_id', as: 'promotionalgiftcharactermood' });
HouseType.hasMany(BuyableHouse, { foreignKey: 'houseTypeId', as: 'buyableHouses' });
BuyableHouse.belongsTo(HouseType, { foreignKey: 'houseTypeId', as: 'houseType' });
HouseType.hasMany(BuyableHouse, { foreignKey: 'houseTypeId', as: 'buyableHouses' });
BuyableHouse.belongsTo(HouseType, { foreignKey: 'houseTypeId', as: 'houseType' });
HouseType.hasMany(UserHouse, { foreignKey: 'houseTypeId', as: 'userHouses' });
UserHouse.belongsTo(HouseType, { foreignKey: 'houseTypeId', as: 'houseType' });
HouseType.hasMany(UserHouse, { foreignKey: 'houseTypeId', as: 'userHouses' });
UserHouse.belongsTo(HouseType, { foreignKey: 'houseTypeId', as: 'houseType' });
FalukantUser.hasOne(UserHouse, { foreignKey: 'userId', as: 'userHouse' });
UserHouse.belongsTo(FalukantUser, { foreignKey: 'userId', as: 'houseUser' });
FalukantUser.hasOne(UserHouse, { foreignKey: 'userId', as: 'userHouse' });
UserHouse.belongsTo(FalukantUser, { foreignKey: 'userId', as: 'houseUser' });
TitleOfNobility.hasMany(HouseType, { foreignKey: 'minimumNobleTitle', as: 'houseTypes' });
HouseType.belongsTo(TitleOfNobility, { foreignKey: 'minimumNobleTitle', as: 'titleOfNobility' });
PartyType.hasMany(Party, { foreignKey: 'partyTypeId', as: 'parties' });
Party.belongsTo(PartyType, { foreignKey: 'partyTypeId', as: 'partyType' });
MusicType.hasMany(Party, { foreignKey: 'musicTypeId', as: 'parties' });
Party.belongsTo(MusicType, { foreignKey: 'musicTypeId', as: 'musicType' });
BanquetteType.hasMany(Party, { foreignKey: 'banquetteTypeId', as: 'parties' });
Party.belongsTo(BanquetteType, { foreignKey: 'banquetteTypeId', as: 'banquetteType' });
FalukantUser.hasMany(Party, { foreignKey: 'falukantUserId', as: 'parties' });
Party.belongsTo(FalukantUser, { foreignKey: 'falukantUserId', as: 'partyUser' });
Party.belongsToMany(TitleOfNobility, {
through: PartyInvitedNobility,
foreignKey: 'party_id',
otherKey: 'title_of_nobility_id',
as: 'invitedNobilities',
});
TitleOfNobility.belongsToMany(Party, {
through: PartyInvitedNobility,
foreignKey: 'title_of_nobility_id',
otherKey: 'party_id',
as: 'partiesInvitedTo',
});
ChildRelation.belongsTo(FalukantCharacter, {
foreignKey: 'fatherCharacterId',
as: 'father'
});
FalukantCharacter.hasMany(ChildRelation, {
foreignKey: 'fatherCharacterId',
as: 'childrenFather'
});
ChildRelation.belongsTo(FalukantCharacter, {
foreignKey: 'motherCharacterId',
as: 'mother'
});
FalukantCharacter.hasMany(ChildRelation, {
foreignKey: 'motherCharacterId',
as: 'childrenMother'
});
ChildRelation.belongsTo(FalukantCharacter, {
foreignKey: 'childCharacterId',
as: 'child'
});
FalukantCharacter.hasMany(ChildRelation, {
foreignKey: 'childCharacterId',
as: 'parentRelations'
});
Learning.belongsTo(LearnRecipient, {
foreignKey: 'learningRecipientId',
as: 'recipient'
}
);
LearnRecipient.hasMany(Learning, {
foreignKey: 'learningRecipientId',
as: 'learnings'
});
Learning.belongsTo(FalukantUser, {
foreignKey: 'associatedFalukantUserId',
as: 'learner'
}
);
FalukantUser.hasMany(Learning, {
foreignKey: 'associatedFalukantUserId',
as: 'learnings'
});
Learning.belongsTo(ProductType, {
foreignKey: 'productId',
as: 'productType'
});
ProductType.hasMany(Learning, {
foreignKey: 'productId',
as: 'learnings'
});
Learning.belongsTo(FalukantCharacter, {
foreignKey: 'associatedLearningCharacterId',
as: 'learningCharacter'
});
FalukantCharacter.hasMany(Learning, {
foreignKey: 'associatedLearningCharacterId',
as: 'learningsCharacter'
});
FalukantUser.hasMany(Credit, {
foreignKey: 'falukantUserId',
as: 'credits'
});
Credit.belongsTo(FalukantUser, {
foreignKey: 'falukantUserId',
as: 'user'
});
FalukantCharacter.hasMany(DebtorsPrism, {
foreignKey: 'character_id',
as: 'debtorsPrisms'
});
DebtorsPrism.belongsTo(FalukantCharacter, {
foreignKey: 'character_id',
as: 'character'
});
HealthActivity.belongsTo(FalukantCharacter, {
foreignKey: 'character_id',
as: 'character'
});
FalukantCharacter.hasMany(HealthActivity, {
foreignKey: 'character_id',
as: 'healthActivities'
});
// — Political Offices —
// predefine requirements for office
PoliticalOfficeRequirement.belongsTo(PoliticalOfficeType, {
TitleOfNobility.hasMany(HouseType, { foreignKey: 'minimumNobleTitle', as: 'houseTypes' });
HouseType.belongsTo(TitleOfNobility, { foreignKey: 'minimumNobleTitle', as: 'titleOfNobility' });
PartyType.hasMany(Party, { foreignKey: 'partyTypeId', as: 'parties' });
Party.belongsTo(PartyType, { foreignKey: 'partyTypeId', as: 'partyType' });
MusicType.hasMany(Party, { foreignKey: 'musicTypeId', as: 'parties' });
Party.belongsTo(MusicType, { foreignKey: 'musicTypeId', as: 'musicType' });
BanquetteType.hasMany(Party, { foreignKey: 'banquetteTypeId', as: 'parties' });
Party.belongsTo(BanquetteType, { foreignKey: 'banquetteTypeId', as: 'banquetteType' });
FalukantUser.hasMany(Party, { foreignKey: 'falukantUserId', as: 'parties' });
Party.belongsTo(FalukantUser, { foreignKey: 'falukantUserId', as: 'partyUser' });
Party.belongsToMany(TitleOfNobility, {
through: PartyInvitedNobility,
foreignKey: 'party_id',
otherKey: 'title_of_nobility_id',
as: 'invitedNobilities',
});
TitleOfNobility.belongsToMany(Party, {
through: PartyInvitedNobility,
foreignKey: 'title_of_nobility_id',
otherKey: 'party_id',
as: 'partiesInvitedTo',
});
ChildRelation.belongsTo(FalukantCharacter, {
foreignKey: 'fatherCharacterId',
as: 'father'
});
FalukantCharacter.hasMany(ChildRelation, {
foreignKey: 'fatherCharacterId',
as: 'childrenFather'
});
ChildRelation.belongsTo(FalukantCharacter, {
foreignKey: 'motherCharacterId',
as: 'mother'
});
FalukantCharacter.hasMany(ChildRelation, {
foreignKey: 'motherCharacterId',
as: 'childrenMother'
});
ChildRelation.belongsTo(FalukantCharacter, {
foreignKey: 'childCharacterId',
as: 'child'
});
FalukantCharacter.hasMany(ChildRelation, {
foreignKey: 'childCharacterId',
as: 'parentRelations'
});
Learning.belongsTo(LearnRecipient, {
foreignKey: 'learningRecipientId',
as: 'recipient'
}
);
LearnRecipient.hasMany(Learning, {
foreignKey: 'learningRecipientId',
as: 'learnings'
});
Learning.belongsTo(FalukantUser, {
foreignKey: 'associatedFalukantUserId',
as: 'learner'
}
);
FalukantUser.hasMany(Learning, {
foreignKey: 'associatedFalukantUserId',
as: 'learnings'
});
Learning.belongsTo(ProductType, {
foreignKey: 'productId',
as: 'productType'
});
ProductType.hasMany(Learning, {
foreignKey: 'productId',
as: 'learnings'
});
Learning.belongsTo(FalukantCharacter, {
foreignKey: 'associatedLearningCharacterId',
as: 'learningCharacter'
});
FalukantCharacter.hasMany(Learning, {
foreignKey: 'associatedLearningCharacterId',
as: 'learningsCharacter'
});
FalukantUser.hasMany(Credit, {
foreignKey: 'falukantUserId',
as: 'credits'
});
Credit.belongsTo(FalukantUser, {
foreignKey: 'falukantUserId',
as: 'user'
});
FalukantCharacter.hasMany(DebtorsPrism, {
foreignKey: 'character_id',
as: 'debtorsPrisms'
});
DebtorsPrism.belongsTo(FalukantCharacter, {
foreignKey: 'character_id',
as: 'character'
});
HealthActivity.belongsTo(FalukantCharacter, {
foreignKey: 'character_id',
as: 'character'
});
FalukantCharacter.hasMany(HealthActivity, {
foreignKey: 'character_id',
as: 'healthActivities'
});
// — Political Offices —
// predefine requirements for office
PoliticalOfficeRequirement.belongsTo(PoliticalOfficeType, {
foreignKey: 'officeTypeId',
as: 'officeType'
});
@@ -520,9 +523,9 @@ PoliticalOfficeRequirement.belongsTo(PoliticalOfficeType, {
foreignKey: 'officeTypeId',
as: 'requirements'
});
// predefine benefits for office
PoliticalOfficeBenefit.belongsTo(
PoliticalOfficeBenefit.belongsTo(
PoliticalOfficeBenefitType,
{ foreignKey: 'benefitTypeId', as: 'benefitDefinition' }
);
@@ -530,7 +533,7 @@ PoliticalOfficeRequirement.belongsTo(PoliticalOfficeType, {
PoliticalOfficeBenefit,
{ foreignKey: 'benefitTypeId', as: 'benefitDefinitions' }
);
// tie benefits back to office type
PoliticalOfficeBenefit.belongsTo(PoliticalOfficeType, {
foreignKey: 'officeTypeId',
@@ -540,7 +543,7 @@ PoliticalOfficeRequirement.belongsTo(PoliticalOfficeType, {
foreignKey: 'officeTypeId',
as: 'benefits'
});
// actual office holdings
PoliticalOffice.belongsTo(PoliticalOfficeType, {
foreignKey: 'officeTypeId',
@@ -550,16 +553,16 @@ PoliticalOfficeRequirement.belongsTo(PoliticalOfficeType, {
foreignKey: 'officeTypeId',
as: 'offices'
});
PoliticalOffice.belongsTo(FalukantCharacter, {
foreignKey: 'characterId',
as: 'holder'
});
FalukantCharacter.hasMany(PoliticalOffice, {
FalukantCharacter.hasOne(PoliticalOffice, {
foreignKey: 'characterId',
as: 'heldOffices'
as: 'heldOffice'
});
// elections
Election.belongsTo(PoliticalOfficeType, {
foreignKey: 'officeTypeId',
@@ -569,7 +572,17 @@ PoliticalOfficeRequirement.belongsTo(PoliticalOfficeType, {
foreignKey: 'officeTypeId',
as: 'elections'
});
Election.belongsTo(RegionData, {
foreignKey: 'regionId',
as: 'region'
});
RegionData.hasMany(Election, {
foreignKey: 'regionId',
as: 'elections'
});
// candidates in an election
Candidate.belongsTo(Election, {
foreignKey: 'electionId',
@@ -579,7 +592,7 @@ PoliticalOfficeRequirement.belongsTo(PoliticalOfficeType, {
foreignKey: 'electionId',
as: 'candidates'
});
Candidate.belongsTo(FalukantCharacter, {
foreignKey: 'characterId',
as: 'character'
@@ -588,7 +601,7 @@ PoliticalOfficeRequirement.belongsTo(PoliticalOfficeType, {
foreignKey: 'characterId',
as: 'candidacies'
});
// votes cast
Vote.belongsTo(Election, {
foreignKey: 'electionId',
@@ -598,7 +611,7 @@ PoliticalOfficeRequirement.belongsTo(PoliticalOfficeType, {
foreignKey: 'electionId',
as: 'votes'
});
Vote.belongsTo(Candidate, {
foreignKey: 'candidateId',
as: 'candidate'
@@ -607,7 +620,7 @@ PoliticalOfficeRequirement.belongsTo(PoliticalOfficeType, {
foreignKey: 'candidateId',
as: 'votes'
});
Vote.belongsTo(FalukantCharacter, {
foreignKey: 'voterCharacterId',
as: 'voter'
@@ -616,7 +629,7 @@ PoliticalOfficeRequirement.belongsTo(PoliticalOfficeType, {
foreignKey: 'voterCharacterId',
as: 'votesCast'
});
// election results
ElectionResult.belongsTo(Election, {
foreignKey: 'electionId',
@@ -626,7 +639,7 @@ PoliticalOfficeRequirement.belongsTo(PoliticalOfficeType, {
foreignKey: 'electionId',
as: 'results'
});
ElectionResult.belongsTo(Candidate, {
foreignKey: 'candidateId',
as: 'candidate'
@@ -645,4 +658,43 @@ PoliticalOfficeRequirement.belongsTo(PoliticalOfficeType, {
as: 'offices'
});
PoliticalOfficePrerequisite.belongsTo(PoliticalOfficeType, {
foreignKey: 'office_type_id',
as: 'officeTypePrerequisite',
});
PoliticalOfficeType.hasMany(PoliticalOfficePrerequisite, {
foreignKey: 'office_type_id',
as: 'prerequisites',
});
PoliticalOfficeHistory.belongsTo(PoliticalOfficeType, {
foreignKey: 'officeTypeId',
as: 'officeTypeHistory',
});
PoliticalOfficeType.hasMany(PoliticalOfficeHistory, {
foreignKey: 'officeTypeId',
as: 'history',
});
FalukantCharacter.hasMany(PoliticalOfficeHistory, {
foreignKey: 'characterId',
as: 'officeHistories',
});
PoliticalOfficeHistory.belongsTo(FalukantCharacter, {
foreignKey: 'characterId',
as: 'character',
});
ElectionHistory.belongsTo(PoliticalOfficeType, {
foreignKey: 'officeTypeId',
as: 'officeTypeHistory',
});
PoliticalOfficeType.hasMany(ElectionHistory, {
foreignKey: 'officeTypeId',
as: 'electionHistory',
}
)
}

View File

@@ -30,6 +30,11 @@ ChildRelation.init(
allowNull: false,
default: false,
},
isHeir: {
type: DataTypes.BOOLEAN,
allowNull: true,
default: false,
}
},
{
sequelize,

View File

@@ -10,15 +10,19 @@ Election.init({
primaryKey: true,
autoIncrement: true,
},
political_office_id: {
officeTypeId: {
type: DataTypes.INTEGER,
allowNull: false,
allowNull: true,
},
date: {
regionId: {
type: DataTypes.INTEGER,
allowNull: false
},
date: {
type: DataTypes.DATE,
allowNull: false,
},
posts_to_fill: {
postsToFill: {
type: DataTypes.INTEGER,
allowNull: false,
},

View File

@@ -26,6 +26,11 @@ RegionData.init({
key: 'id',
schema: 'falukant_data',
}
},
map: {
type: DataTypes.JSONB,
allowNull: true,
defaultValue: {}
}
}, {
sequelize,

View File

@@ -4,36 +4,45 @@ import { sequelize } from '../../../utils/sequelize.js';
class Vote extends Model {}
Vote.init({
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
Vote.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
electionId: {
type: DataTypes.INTEGER,
allowNull: false,
},
candidateId: {
type: DataTypes.INTEGER,
allowNull: false,
},
timestamp: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
falukantUserId: {
type: DataTypes.INTEGER,
allowNull: false,
},
},
election_id: {
type: DataTypes.INTEGER,
allowNull: false,
},
voter_character_id: {
type: DataTypes.INTEGER,
allowNull: false,
},
candidate_id: {
type: DataTypes.INTEGER,
allowNull: false,
},
timestamp: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
}, {
sequelize,
modelName: 'Vote',
tableName: 'vote',
schema: 'falukant_data',
timestamps: false,
underscored: true,
});
{
sequelize,
modelName: 'Vote',
tableName: 'vote',
schema: 'falukant_data',
timestamps: true,
underscored: true,
indexes: [
{
unique: true,
fields: ['election_id', 'candidate_id'],
},
],
}
);
export default Vote;

View File

@@ -0,0 +1,32 @@
import { Model, DataTypes } from 'sequelize';
import { sequelize } from '../../../utils/sequelize.js';
class ElectionHistory extends Model { }
ElectionHistory.init({
electionId: {
type: DataTypes.INTEGER,
allowNull: false,
},
politicalOfficeTypeId: {
type: DataTypes.INTEGER,
allowNull: false,
},
electionDate: {
type: DataTypes.DATE,
allowNull: false,
},
electionResult: {
type: DataTypes.JSON,
allowNull: false,
}
}, {
sequelize,
modelName: 'ElectionHistory',
tableName: 'election_history',
schema: 'falukant_log',
timestamps: true,
underscored: true,
});
export default ElectionHistory;

View File

@@ -0,0 +1,35 @@
import { Model, DataTypes } from 'sequelize';
import { sequelize } from '../../../utils/sequelize.js';
class PoliticalOfficeHistory extends Model { }
PoliticalOfficeHistory.init(
{
characterId: {
type: DataTypes.INTEGER,
allowNull: false,
},
officeTypeId: {
type: DataTypes.INTEGER,
allowNull: false,
},
startDate: {
type: DataTypes.DATE,
allowNull: false,
},
endDate: {
type: DataTypes.DATE,
allowNull: false,
}
},
{
sequelize,
modelName: 'PoliticalOfficeHistory',
tableName: 'political_office_history',
schema: 'falukant_log',
timestamps: true,
underscored: true,
}
);
export default PoliticalOfficeHistory;

View File

@@ -1,4 +1,5 @@
// falukant/predefine/political_office_prerequisite.js
// models/falukant/predefine/political_office_prerequisite.js
import { Model, DataTypes } from 'sequelize';
import { sequelize } from '../../../utils/sequelize.js';
@@ -10,10 +11,13 @@ PoliticalOfficePrerequisite.init({
primaryKey: true,
autoIncrement: true,
},
political_office_id: {
// Neu: Feld heißt jetzt eindeutig "office_type_id"
office_type_id: {
type: DataTypes.INTEGER,
allowNull: false,
},
prerequisite: {
type: DataTypes.JSONB,
allowNull: false,

View File

@@ -94,6 +94,8 @@ import Election from './falukant/data/election.js';
import Candidate from './falukant/data/candidate.js';
import Vote from './falukant/data/vote.js';
import ElectionResult from './falukant/data/election_result.js';
import PoliticalOfficeHistory from './falukant/log/political_office_history.js';
import ElectionHistory from './falukant/log/election_history.js';
const models = {
SettingsType,
@@ -191,6 +193,8 @@ const models = {
Candidate,
Vote,
ElectionResult,
PoliticalOfficeHistory,
ElectionHistory,
};
export default models;

View File

@@ -241,6 +241,180 @@ export async function createTriggers() {
$$ LANGUAGE plpgsql VOLATILE;
`;
// process_electionsStored-Procedure anlegen
const createProcessElectionsFunction = `
CREATE OR REPLACE FUNCTION falukant_data.process_elections()
RETURNS TABLE (
office_id INTEGER,
office_type_id INTEGER,
character_id INTEGER,
region_id INTEGER
)
AS $$
BEGIN
RETURN QUERY
WITH
-- 1) Alle Wahlen, die vor mindestens 3 Tagen erstellt wurden
to_process AS (
SELECT
e.id AS election_id,
e.office_type_id AS tp_office_type_id,
e.region_id AS tp_region_id,
e.posts_to_fill AS tp_posts_to_fill,
e.date AS tp_election_date
FROM falukant_data.election e
WHERE (e.created_at::date + INTERVAL '3 days') <= NOW()::date
),
-- 2) Stimmen pro Kandidat zählen
votes AS (
SELECT
tp.election_id,
tp.tp_posts_to_fill AS posts_to_fill,
c.character_id,
COUNT(v.*) AS votes_received
FROM to_process tp
JOIN falukant_data.candidate c
ON c.election_id = tp.election_id
LEFT JOIN falukant_data.vote v
ON v.election_id = c.election_id
AND v.candidate_id = c.id
GROUP BY tp.election_id, tp.tp_posts_to_fill, c.character_id
),
-- 3) Ranking nach Stimmen
ranked AS (
SELECT
v.election_id,
v.character_id,
v.votes_received,
ROW_NUMBER() OVER (
PARTITION BY v.election_id
ORDER BY v.votes_received DESC, RANDOM()
) AS rn
FROM votes v
),
-- 4) Top-N (posts_to_fill) sind Gewinner
winners AS (
SELECT
r.election_id,
r.character_id
FROM ranked r
JOIN to_process tp
ON tp.election_id = r.election_id
WHERE r.rn <= tp.tp_posts_to_fill
),
-- 5) Verbleibende Kandidaten ohne Gewinner
remaining AS (
SELECT
tp.election_id,
c.character_id
FROM to_process tp
JOIN falukant_data.candidate c
ON c.election_id = tp.election_id
WHERE c.character_id NOT IN (
SELECT w.character_id
FROM winners w
WHERE w.election_id = tp.election_id
)
),
-- 6) Zufalls-Nachrücker bis alle Plätze gefüllt sind
random_fill AS (
SELECT
rp.election_id,
rp.character_id
FROM remaining rp
JOIN to_process tp
ON tp.election_id = rp.election_id
JOIN LATERAL (
SELECT r2.character_id
FROM remaining r2
WHERE r2.election_id = rp.election_id
ORDER BY RANDOM()
LIMIT GREATEST(
0,
tp.tp_posts_to_fill
- (SELECT COUNT(*) FROM winners w2 WHERE w2.election_id = tp.election_id)
)
) sub
ON sub.character_id = rp.character_id
),
-- 7) Finale Gewinner (Winners random_fill)
final_winners AS (
SELECT * FROM winners
UNION ALL
SELECT * FROM random_fill
),
-- 8) Neue Ämter anlegen und sofort zurückliefern
created_offices AS (
INSERT INTO falukant_data.political_office
(office_type_id, character_id, created_at, updated_at, region_id)
SELECT
tp.tp_office_type_id,
fw.character_id,
NOW() AS created_at,
NOW() AS updated_at,
tp.tp_region_id
FROM final_winners fw
JOIN to_process tp
ON tp.election_id = fw.election_id
RETURNING
id AS co_office_id,
falukant_data.political_office.office_type_id AS co_office_type_id,
falukant_data.political_office.character_id AS co_character_id,
falukant_data.political_office.region_id AS co_region_id
),
-- 9) election_history befüllen
_hist AS (
INSERT INTO falukant_log.election_history
(election_id, political_office_type_id, election_date, election_result, created_at, updated_at)
SELECT
tp.election_id,
tp.tp_office_type_id,
tp.tp_election_date,
(
SELECT json_agg(vr)
FROM votes vr
WHERE vr.election_id = tp.election_id
),
NOW() AS created_at,
NOW() AS updated_at
FROM to_process tp
),
-- 10) Cleanup: Stimmen, Kandidaten und Wahlen löschen
_del_votes AS (
DELETE FROM falukant_data.vote
WHERE election_id IN (SELECT election_id FROM to_process)
),
_del_candidates AS (
DELETE FROM falukant_data.candidate
WHERE election_id IN (SELECT election_id FROM to_process)
),
_del_elections AS (
DELETE FROM falukant_data.election
WHERE id IN (SELECT election_id FROM to_process)
)
-- 11) Ergebnis wirklich zurückliefern
SELECT
co.co_office_id AS office_id,
co.co_office_type_id,
co.co_character_id,
co.co_region_id
FROM created_offices co
ORDER BY co.co_region_id, co.co_office_id;
END;
$$ LANGUAGE plpgsql;
`;
try {
await sequelize.query(createTriggerFunction);
await sequelize.query(createInsertTrigger);
@@ -257,6 +431,7 @@ export async function createTriggers() {
await sequelize.query(createChildRelationNameFunction);
await sequelize.query(createChildRelationNameTrigger);
await sequelize.query(createRandomMoodUpdateMethod);
await sequelize.query(createProcessElectionsFunction);
await initializeCharacterTraitTrigger();
console.log('Triggers created successfully');