Files
yourpart3/backend/models/associations.js
2025-01-28 09:55:36 +01:00

330 lines
18 KiB
JavaScript

import User from './community/user.js';
import UserParam from './community/user_param.js';
import UserParamType from './type/user_param.js';
import UserRightType from './type/user_right.js';
import UserRight from './community/user_right.js';
import SettingsType from './type/settings.js';
import UserParamValue from './type/user_param_value.js';
import InterestType from './type/interest.js';
import InterestTranslationType from './type/interest_translation.js';
import Interest from './community/interest.js';
import UserParamVisibilityType from './type/user_param_visibility.js';
import UserParamVisibility from './community/user_param_visibility.js';
import Folder from './community/folder.js';
import Image from './community/image.js';
import ImageVisibilityType from './type/image_visibility.js';
import ImageVisibilityUser from './community/image_visibility_user.js';
import FolderImageVisibility from './community/folder_image_visibility.js';
import ImageImageVisibility from './community/image_image_visibility.js';
import FolderVisibilityUser from './community/folder_visibility_user.js';
import GuestbookEntry from './community/guestbook.js';
import Forum from './forum/forum.js';
import Title from './forum/title.js';
import Message from './forum/message.js';
import MessageImage from './forum/message_image.js';
import MessageHistory from './forum/message_history.js';
import TitleHistory from './forum/title_history.js';
import ForumPermission from './forum/forum_permission.js';
import ForumUserPermission from './forum/forum_user_permission.js';
import ForumForumPermission from './forum/forum_forum_permission.js';
import Friendship from './community/friendship.js';
import FalukantUser from './falukant/data/user.js';
import RegionType from './falukant/type/region.js';
import RegionData from './falukant/data/region.js';
import FalukantCharacter from './falukant/data/character.js';
import FalukantPredefineFirstname from './falukant/predefine/firstname.js';
import FalukantPredefineLastname from './falukant/predefine/lastname.js';
import FalukantStock from './falukant/data/stock.js';
import FalukantStockType from './falukant/type/stock.js';
import Knowledge from './falukant/data/product_knowledge.js';
import ProductType from './falukant/type/product.js';
import TitleOfNobility from './falukant/type/title_of_nobility.js';
import TitleRequirement from './falukant/type/title_requirement.js';
import Branch from './falukant/data/branch.js';
import BranchType from './falukant/type/branch.js';
import Production from './falukant/data/production.js';
import Inventory from './falukant/data/inventory.js';
import BuyableStock from './falukant/data/buyable_stock.js';
import MoneyFlow from './falukant/log/moneyflow.js';
import Director from './falukant/data/director.js';
import DirectorProposal from './falukant/data/director_proposal.js';
import TownProductWorth from './falukant/data/town_product_worth.js';
import DayProduction from './falukant/log/dayproduction.js';
import DaySell from './falukant/log/daysell.js';
import MarriageProposal from './falukant/data/marriage_proposal.js';
import Notification from './falukant/log/notification';
import CharacterTrait from './falukant/type/character_trait.js';
import FalukantCharacterTrait from './falukant/data/falukant_character_trait.js';
import Mood from './falukant/type/mood.js';
import PromotionalGift from './falukant/type/promotional_gift.js';
import PromotionalGiftCharacterTrait from './falukant/predefine/promotional_gift_character_trait.js';
import PromotionalGiftMood from './falukant/predefine/promotional_gift_mood.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'
});
// 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' });
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' });
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(MessageHistory, { foreignKey: 'messageId' });
MessageHistory.belongsTo(Message, { foreignKey: 'messageId' });
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' });
User.hasMany(ForumUserPermission, { foreignKey: 'userId', as: 'userPermissions' });
ForumUserPermission.belongsTo(User, { foreignKey: 'userId' });
Forum.belongsToMany(ForumPermission, {
through: ForumForumPermission,
foreignKey: 'forumId',
as: 'associatedPermissions'
});
ForumPermission.belongsToMany(Forum, {
through: ForumForumPermission,
foreignKey: 'permissionId',
as: 'forums'
});
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' });
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' });
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' });
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(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(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' });
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(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' });
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(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(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(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' });
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' });
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(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(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(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(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' });
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(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' });
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' });
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'});
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',});
}