Skip to content

Commit

Permalink
fix: Model associations and importing
Browse files Browse the repository at this point in the history
  • Loading branch information
tristenwallace committed Apr 30, 2024
1 parent 0838e1e commit d9a7960
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
10 changes: 6 additions & 4 deletions server/src/controllers/pollController.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Request, Response } from 'express';
import { Poll } from '../database/models/poll';
import { Vote } from '../database/models/vote';
import models from '../database/models';

const { Poll, Vote } = models;

export const getPolls = async (req: Request, res: Response): Promise<void> => {
try {
const polls = await Poll.findAll({
include: [
{
model: Vote,
as: 'voters',
as: 'votes',
attributes: ['userId', 'chosenOption'], // Include relevant vote details
},
],
Expand Down Expand Up @@ -50,7 +51,8 @@ export const voteOnPoll = async (
req: Request,
res: Response,
): Promise<void> => {
const { pollId, userId, chosenOption } = req.body;
const { userId, chosenOption } = req.body;
const pollId = req.params.id;

if (!pollId || !chosenOption || !userId) {
res.status(400).json({ error: 'Missing information' });
Expand Down
2 changes: 0 additions & 2 deletions server/src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ function createToken(
username: user.username,
name: user.name,
avatar_url: user.avatar_url,
},
stats: {
pollsCreated: pollsCreated,
pollsVotedOn: pollsVotedOn,
},
Expand Down
33 changes: 19 additions & 14 deletions server/src/database/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,36 @@ import User from './user';
import Poll from './poll';
import Vote from './vote';

// Set up relationships for polls created by a user
User.hasMany(Poll, {
foreignKey: 'userId',
as: 'authoredPolls',
});
// Poll relationships

console.log('Defining association between Poll and Users');
Poll.belongsTo(User, {
foreignKey: 'userId',
as: 'author', // Clearly indicates the creator of the poll
as: 'author',
});
Poll.belongsToMany(User, {
through: Vote,
as: 'voters',
foreignKey: 'pollId',
otherKey: 'userId',
});
console.log('Defining association between Poll and Vote');
Poll.hasMany(Vote, { foreignKey: 'pollId', as: 'votes' });
console.log('Association defined:', Poll.associations);

// Set up relationships for voting
// User Relationships
User.belongsToMany(Poll, {
through: Vote,
as: 'votedPolls', // Indicates polls on which the user has voted
as: 'votedPolls',
foreignKey: 'userId',
otherKey: 'pollId',
});
Poll.belongsToMany(User, {
through: Vote,
as: 'voters', // Indicates users who have voted on this poll
foreignKey: 'pollId',
otherKey: 'userId',
User.hasMany(Poll, {
foreignKey: 'userId',
as: 'authoredPolls',
});

// Relationships in Vote model
// Vote Relationships
Vote.belongsTo(User, { foreignKey: 'userId', as: 'voter' });
Vote.belongsTo(Poll, { foreignKey: 'pollId', as: 'votedPoll' });

Expand Down
2 changes: 1 addition & 1 deletion server/src/database/models/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Poll.init(
userId: {
type: DataTypes.UUID,
references: {
model: 'Users',
model: 'User',
key: 'id',
},
},
Expand Down
4 changes: 2 additions & 2 deletions server/src/database/models/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ Vote.init(
userId: {
type: DataTypes.UUID,
references: {
model: 'Users',
model: 'User',
key: 'id',
},
primaryKey: true,
},
pollId: {
type: DataTypes.UUID,
references: {
model: 'Polls',
model: 'Poll',
key: 'id',
},
primaryKey: true,
Expand Down

0 comments on commit d9a7960

Please sign in to comment.