Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b9a969a
chore: added model folder for better stucture
tran2106 Jul 12, 2025
49d5bfb
feat: user model addeed adn export to db
tran2106 Jul 13, 2025
82e325b
feat: added auth0Id to support 2 methods of login/Hailia's feedback
tran2106 Jul 13, 2025
8711ff0
fix: changed passwordHash allowNull to true
tran2106 Jul 13, 2025
c84765a
Merge pull request #13 from TPP-Team1/models-user
haicomet Jul 13, 2025
ca01929
feat-define Poll model
Rend1027 Jul 14, 2025
22b77d4
feat-defined Vote model
Rend1027 Jul 14, 2025
4d92ca7
fix-fixed model attributes
Rend1027 Jul 14, 2025
25a36ac
chore-exported poll model from index
Rend1027 Jul 14, 2025
7e534ad
feat-Poll seed data
Rend1027 Jul 14, 2025
8257073
refactor-added console.log
Rend1027 Jul 14, 2025
e568559
fix-User model import
Rend1027 Jul 14, 2025
da88cc7
refactor-cleaned poll data
Rend1027 Jul 14, 2025
32b177c
Merge pull request #14 from TPP-Team1/poll_model
Rend1027 Jul 14, 2025
3972969
feat- defined pollOption Model
Rend1027 Jul 14, 2025
9b44ade
feat- created poll_option data
Rend1027 Jul 14, 2025
432728a
refactor- modified poll data to be more dinamic
Rend1027 Jul 14, 2025
15b7bf8
feat-created poll_option data
Rend1027 Jul 14, 2025
9f55ed8
refactor- made poll_option data more dinamic
Rend1027 Jul 14, 2025
e94a08c
optimized user_model
Rend1027 Jul 14, 2025
9b3c5a9
feat- associations
Rend1027 Jul 15, 2025
e1bcb9c
refactor- change DB name
Rend1027 Jul 15, 2025
2a3b0e8
fixed-bugs with model attributes
Rend1027 Jul 15, 2025
697ceaa
fixed-Poll FK
Rend1027 Jul 15, 2025
adfb645
feat-votingRank model
Rend1027 Jul 15, 2025
a1ba0b4
defined VotingRank model
Rend1027 Jul 15, 2025
7c4cc31
feat- association
Rend1027 Jul 15, 2025
85544fb
feat- created Vote data
Rend1027 Jul 15, 2025
f252e90
feat- created voteRank data
Rend1027 Jul 15, 2025
bdc266c
fixed- bug in rank data
Rend1027 Jul 15, 2025
eca4f0d
feat- added attributes to VotingRank
Rend1027 Jul 15, 2025
b5b23bd
feat- added attr to Vote model
Rend1027 Jul 15, 2025
4e9d505
fixed- association bugs
Rend1027 Jul 15, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion database/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { Sequelize } = require("sequelize");
const pg = require("pg");

// Feel free to rename the database to whatever you want!
const dbName = "capstone-1";
const dbName = "capstone_1";

const db = new Sequelize(
process.env.DATABASE_URL || `postgres://localhost:5432/${dbName}`,
Expand Down
72 changes: 71 additions & 1 deletion database/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,77 @@
const db = require("./db");
const User = require("./user");
const User = require("./models/user");
const Poll = require("./models/poll");
const PollOption = require("./models/pollOption");
const VotingRank = require("./models/votingRank");
const Vote = require("./models/vote");

//One to many - user has many polls
User.hasMany(Poll, {
foreignKey: 'userId',
// onDelete: 'CASCADE' deletes poll is user is deleted
});


// many to one - Each Poll belongs to one user
Poll.belongsTo(User, {
foreignKey: 'userId',
});


// One to many - one Poll has many options
Poll.hasMany(PollOption, {
foreignKey: 'pollId',
onDelete: "CASCADE", // delete poll_options if poll is deleted
});


// many to one - Each pollOption belongs to one Poll
PollOption.belongsTo(Poll, {
foreignKey: "pollId"
});


// one to many- each user can submit many votes
User.hasMany(Vote, {
foreignKey: "userId",
})


// one to one- Each vote(ballot) belongs to a user
Vote.belongsTo(User, {
foreignKey: "userId",
})


// many to one - Each vote(ballot) belongs to a poll
Vote.belongsTo(Poll, {
foreignKey: "pollId"
})


// one to many- each vote(ballot) can have many ranked options
Vote.hasMany(VotingRank, {
foreignKey: "voteId",
})


// many to one - each rank entry belongs to one vote(ballot)
VotingRank.belongsTo(Vote, {
foreignKey: "voteId",
})


// many to one- Each voteRank belongs to one Polloption
VotingRank.belongsTo(PollOption, {
foreignKey: 'pollOptionId',
})


module.exports = {
db,
User,
Poll,
PollOption,
Vote,
VotingRank,
};
45 changes: 45 additions & 0 deletions database/models/poll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { DataTypes } = require('sequelize');
const db = require('../db');

// define the Poll model

const Poll = db.define("poll", {
title: {
type: DataTypes.STRING,
allowNull: false,
},
description: {
type: DataTypes.TEXT,
// allowNull: true,
},
participants: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
status: {
type: DataTypes.ENUM("draft", "published", "ended"),
allowNull: false,
},
deadline: {
type: DataTypes.DATE,
allowNull: false,
},
authRequired: {
type: DataTypes.BOOLEAN, // allow only user votes if true
default: false,
},
isDisabled: {
type: DataTypes.BOOLEAN, // if true poll is disabled by admin
default: false,
},
restricted: {
type: DataTypes.BOOLEAN, // only specic users can parcipate if true
default: false,
},
},
{
timestamps: true,
}
);

module.exports = Poll;
26 changes: 26 additions & 0 deletions database/models/pollOption.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { DataTypes } = require('sequelize');
const db = require('../db');

// Table Poll_Option {
// id PK
// option_text string
// position integer ("?")
// poll_id FK
// created_at timestamp
// }
const pollOption = db.define("pollOption", {
optionText: {
type: DataTypes.STRING,
allowNull: false,
},
position: {
type: DataTypes.INTEGER,
allowNull: true,
},
},
{
timestamps: true,
}
)

module.exports = pollOption;
56 changes: 49 additions & 7 deletions database/user.js → database/models/user.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,76 @@
const { DataTypes } = require("sequelize");
const db = require("./db");
const db = require("../db");
const bcrypt = require("bcrypt");

const User = db.define("user", {
username: {
userName: {
type: DataTypes.STRING,
allowNull: false,
allowNull: true,
unique: true,
validate: {
len: [3, 20],
len: [1, 50]
}
},
firstName: {
type: DataTypes.STRING,
// allowNull: false,
validate: {
notEmpty: true,
len: [1, 50],
},
},
lastName: {
type: DataTypes.STRING,
// allowNull: false,
validate: {
notEmpty: true,
len: [1, 50],
},
},
email: {
type: DataTypes.STRING,
allowNull: true,
// allowNull: false,
unique: true,
validate: {
isEmail: true,
notEmpty: true,
},
},
passwordHash: {
type: DataTypes.STRING,
allowNull: true,
validate: {
notEmpty: true,
},
},
auth0Id: {
type: DataTypes.STRING,
allowNull: true,
unique: true,
},
passwordHash: {
img: {
type: DataTypes.STRING,
allowNull: true,
validate: {
isUrl: true,
},
},
isAdmin: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
});
isDisable: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
},
{
timestamps: true,
createdAt: 'created_at',
}
);

// Instance method to check password
User.prototype.checkPassword = function (password) {
Expand Down
31 changes: 31 additions & 0 deletions database/models/vote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { DataTypes } = require('sequelize');
const db = require('../db');

// define the Vote model

const Vote = db.define("vote", {
submitted: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
voterToken: {
type: DataTypes.STRING,
allowNull: true,
},
ipAddress: {
type: DataTypes.STRING,
allowNull: true,
},
userId: {
type: DataTypes.INTEGER,
allowNull: false,
},
pollId: {
type: DataTypes.INTEGER,
allowNull: false,
},
}, {
timestamps: true,
});

module.exports = Vote;
21 changes: 21 additions & 0 deletions database/models/votingRank.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { DataTypes } = require("sequelize");
const db = require("../db");

// define the votingRank model

const VotingRank = db.define("votingRank", {
voteId: {
type: DataTypes.INTEGER,
allowNull: false,
},
pollOptionId: {
type: DataTypes.INTEGER,
allowNull: false,
},
rank: {
type: DataTypes.INTEGER,
allowNull: false,
}
})

module.exports = VotingRank;
Loading