From b9a969aae094dcc8d4856a3f155f0ec9015b1316 Mon Sep 17 00:00:00 2001 From: Tran Vo Date: Sat, 12 Jul 2025 10:53:30 -0400 Subject: [PATCH 01/31] chore: added model folder for better stucture --- database/{ => models}/user.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename database/{ => models}/user.js (96%) diff --git a/database/user.js b/database/models/user.js similarity index 96% rename from database/user.js rename to database/models/user.js index 755c757..4fe3d9a 100644 --- a/database/user.js +++ b/database/models/user.js @@ -1,5 +1,5 @@ const { DataTypes } = require("sequelize"); -const db = require("./db"); +const db = require("../db"); const bcrypt = require("bcrypt"); const User = db.define("user", { From 49d5bfb93ce0a6f11a105378085d6bc93a034a98 Mon Sep 17 00:00:00 2001 From: Tran Vo Date: Sun, 13 Jul 2025 14:05:48 -0400 Subject: [PATCH 02/31] feat: user model addeed adn export to db --- database/index.js | 2 +- database/models/user.js | 44 +++++++++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/database/index.js b/database/index.js index e498df6..34e8404 100644 --- a/database/index.js +++ b/database/index.js @@ -1,5 +1,5 @@ const db = require("./db"); -const User = require("./user"); +const User = require("./models/User"); module.exports = { db, diff --git a/database/models/user.js b/database/models/user.js index 4fe3d9a..2ab73f9 100644 --- a/database/models/user.js +++ b/database/models/user.js @@ -3,31 +3,59 @@ const db = require("../db"); const bcrypt = require("bcrypt"); const User = db.define("user", { - username: { + firstName: { + type: DataTypes.STRING, + allowNull: false, + validate: { + notEmpty: true, + len: [1, 50], + }, + }, + lastName: { type: DataTypes.STRING, allowNull: false, - unique: true, validate: { - len: [3, 20], + notEmpty: true, + len: [1, 50], }, }, email: { type: DataTypes.STRING, - allowNull: true, + allowNull: false, unique: true, validate: { isEmail: true, + notEmpty: true, }, }, - auth0Id: { + passwordHash: { type: DataTypes.STRING, - allowNull: true, - unique: true, + allowNull: false, + validate: { + notEmpty: 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', + updatedAt: false, }); // Instance method to check password From 82e325b70aab1e8f55ed7178f99833f9f4c3ac5f Mon Sep 17 00:00:00 2001 From: Tran Vo Date: Sun, 13 Jul 2025 19:22:24 -0400 Subject: [PATCH 03/31] feat: added auth0Id to support 2 methods of login/Hailia's feedback --- database/models/user.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/database/models/user.js b/database/models/user.js index 2ab73f9..f6b1f46 100644 --- a/database/models/user.js +++ b/database/models/user.js @@ -35,6 +35,11 @@ const User = db.define("user", { notEmpty: true, }, }, + auth0Id: { + type: DataTypes.STRING, + allowNull: true, + unique: true, + }, img: { type: DataTypes.STRING, allowNull: true, From 8711ff07339138e78d95576262ffc047359a5501 Mon Sep 17 00:00:00 2001 From: Tran Vo Date: Sun, 13 Jul 2025 19:26:04 -0400 Subject: [PATCH 04/31] fix: changed passwordHash allowNull to true --- database/models/user.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/models/user.js b/database/models/user.js index f6b1f46..eba5ff0 100644 --- a/database/models/user.js +++ b/database/models/user.js @@ -30,7 +30,7 @@ const User = db.define("user", { }, passwordHash: { type: DataTypes.STRING, - allowNull: false, + allowNull: true, validate: { notEmpty: true, }, From ca01929446f9002d408692828d692688d0663d5c Mon Sep 17 00:00:00 2001 From: rend1027 Date: Sun, 13 Jul 2025 21:46:10 -0400 Subject: [PATCH 05/31] feat-define Poll model --- database/models/poll.js | 36 ++++++++++++++++++++++++++++++++++++ package-lock.json | 4 ++-- 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 database/models/poll.js diff --git a/database/models/poll.js b/database/models/poll.js new file mode 100644 index 0000000..6e89be6 --- /dev/null +++ b/database/models/poll.js @@ -0,0 +1,36 @@ +const {DataTypes} = require('sequelize'); +const db = require('./db'); + +// define the Poll model + +const Poll = db.define("poll", { + title: { + type: DataTypes.STRING + }, + description: { + type: DataTypes.TEXT + }, + participants: { + type: DataTypes.INTEGER + }, + status: { + type: DataTypes.STRING // draft, published , ended + }, + deadline: { + type: DataTypes.TIME + }, + 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 + } +}); + +module.exports = Poll; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index af0cf82..1b30289 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "capstone-i-backend", + "name": "capstone-1-backend", "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "capstone-i-backend", + "name": "capstone-1-backend", "version": "1.0.0", "license": "ISC", "dependencies": { From 22b77d4564e138754499b0c7b139739a62503c2f Mon Sep 17 00:00:00 2001 From: rend1027 Date: Sun, 13 Jul 2025 23:17:14 -0400 Subject: [PATCH 06/31] feat-defined Vote model --- database/models/poll.js | 2 +- database/models/vote.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 database/models/vote.js diff --git a/database/models/poll.js b/database/models/poll.js index 6e89be6..731d6c4 100644 --- a/database/models/poll.js +++ b/database/models/poll.js @@ -1,5 +1,5 @@ const {DataTypes} = require('sequelize'); -const db = require('./db'); +const db = require('../db'); // define the Poll model diff --git a/database/models/vote.js b/database/models/vote.js new file mode 100644 index 0000000..644f195 --- /dev/null +++ b/database/models/vote.js @@ -0,0 +1,22 @@ +const {DataTypes} = require('sequelize'); +const db = require('../db'); + +// define the Vote model + +const Vote = db.define("vote", { + submitted: { + type: DataTypes.BOOLEAN, + defaultValue: false // if false; vote has not yet been submited; ballot can still be edited + }, + voterToken: { + type: DataTypes.STRING, + allowNull: true, // this allows us to uniquely identify a guest . we can track if they voted. + }, + ipAddress: { + type: DataTypes.STRING, + allowNull: true, // same as voter token we can use either + }, + timestamps: true +}) + +module.export = Vote; \ No newline at end of file From 4d92ca70ee8e11d25561342ca2aad07b5c437f08 Mon Sep 17 00:00:00 2001 From: rend1027 Date: Sun, 13 Jul 2025 23:23:09 -0400 Subject: [PATCH 07/31] fix-fixed model attributes --- database/models/poll.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/database/models/poll.js b/database/models/poll.js index 731d6c4..b0f102d 100644 --- a/database/models/poll.js +++ b/database/models/poll.js @@ -5,32 +5,41 @@ const db = require('../db'); const Poll = db.define("poll", { title: { - type: DataTypes.STRING + type: DataTypes.STRING, + allowNull: false, }, description: { - type: DataTypes.TEXT + type: DataTypes.TEXT, + allowNull: true, }, participants: { - type: DataTypes.INTEGER + type: DataTypes.INTEGER, + defaultValue: 0, }, status: { - type: DataTypes.STRING // draft, published , ended + type: DataTypes.ENUM, // draft, published , ended + allowNull: false, }, deadline: { - type: DataTypes.TIME + type: DataTypes.TIMESTAMP, + allowNull: false, }, authRequired: { type: DataTypes.BOOLEAN, // allow only user votes if true - default: false + default: false, + allowNull: false, }, isDisabled: { type: DataTypes.BOOLEAN, // if true poll is disabled by admin - default: false + default: false, + allowNull: false, }, restricted: { type: DataTypes.BOOLEAN, // only specic users can parcipate if true - default: false - } + default: false, + allowNull: false, + }, + timestamps: true }); module.exports = Poll; \ No newline at end of file From 25a36ace51f39969f17508fa418e307dc2d3c7bb Mon Sep 17 00:00:00 2001 From: rend1027 Date: Sun, 13 Jul 2025 23:48:27 -0400 Subject: [PATCH 08/31] chore-exported poll model from index --- database/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/database/index.js b/database/index.js index 34e8404..d6873b0 100644 --- a/database/index.js +++ b/database/index.js @@ -1,7 +1,9 @@ const db = require("./db"); const User = require("./models/User"); +const Poll = require("./models/poll"); module.exports = { db, User, + Poll }; From 7e534ad958aca36c69f6ff105611e46346fb08c8 Mon Sep 17 00:00:00 2001 From: rend1027 Date: Mon, 14 Jul 2025 00:06:43 -0400 Subject: [PATCH 09/31] feat-Poll seed data --- database/seed.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/database/seed.js b/database/seed.js index e58b595..1fe1699 100644 --- a/database/seed.js +++ b/database/seed.js @@ -1,5 +1,5 @@ const db = require("./db"); -const { User } = require("./index"); +const { User, Poll } = require("./index"); const seed = async () => { try { @@ -12,6 +12,59 @@ const seed = async () => { { username: "user2", passwordHash: User.hashPassword("user222") }, ]); + const poll = await Poll.bulkCreate([ + { + title: "Best Anime?", + description: "Rank your favorite animes!", + participants: 0, + status: "published", + deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), // 3 days from now + authRequired: false, + isDisabled: false, + restricted: false, + }, + { + title: "Best Movie?", + description: "Rank your favorite movies!", + participants: 0, + status: "published", + deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), // 3 days from now + authRequired: false, + isDisabled: false, + restricted: false, + }, + { + title: "Best BBQ Item?", + description: "Rank your favorite BBQ food!", + participants: 0, + status: "published", + deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), // 3 days from now + authRequired: false, + isDisabled: false, + restricted: false, + }, + { + title: "authRequired true", + description: "?", + participants: 0, + status: "published", + deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), // 3 days from now + authRequired: true, + isDisabled: false, + restricted: false, + }, + { + title: "restricted true", + description: "Rank your favorite anime of all time!", + participants: 0, + status: "published", + deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), // 3 days from now + authRequired: false, + isDisabled: false, + restricted: true, + }, + ]); + console.log(`๐Ÿ‘ค Created ${users.length} users`); // Create more seed data here once you've created your models From 8257073cb787cf3da6ab676b3332b8b80d81bcfd Mon Sep 17 00:00:00 2001 From: rend1027 Date: Mon, 14 Jul 2025 00:09:11 -0400 Subject: [PATCH 10/31] refactor-added console.log --- database/seed.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/database/seed.js b/database/seed.js index 1fe1699..ce3fc60 100644 --- a/database/seed.js +++ b/database/seed.js @@ -12,7 +12,7 @@ const seed = async () => { { username: "user2", passwordHash: User.hashPassword("user222") }, ]); - const poll = await Poll.bulkCreate([ + const polls = await Poll.bulkCreate([ { title: "Best Anime?", description: "Rank your favorite animes!", @@ -66,6 +66,7 @@ const seed = async () => { ]); console.log(`๐Ÿ‘ค Created ${users.length} users`); + console.log(`Created ${polls.length} polls`) // Create more seed data here once you've created your models // Seed files are a great way to test your database schema! From e56855918cab3c5ee4187046f23285aee578ae0c Mon Sep 17 00:00:00 2001 From: rend1027 Date: Mon, 14 Jul 2025 00:10:52 -0400 Subject: [PATCH 11/31] fix-User model import --- database/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/index.js b/database/index.js index d6873b0..d2c3118 100644 --- a/database/index.js +++ b/database/index.js @@ -1,5 +1,5 @@ const db = require("./db"); -const User = require("./models/User"); +const User = require("./models/user"); const Poll = require("./models/poll"); module.exports = { From da88cc7b501430c518aeae839eb364c7a6ebdafe Mon Sep 17 00:00:00 2001 From: rend1027 Date: Mon, 14 Jul 2025 00:20:20 -0400 Subject: [PATCH 12/31] refactor-cleaned poll data --- database/seed.js | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/database/seed.js b/database/seed.js index ce3fc60..f79ea62 100644 --- a/database/seed.js +++ b/database/seed.js @@ -18,49 +18,35 @@ const seed = async () => { description: "Rank your favorite animes!", participants: 0, status: "published", - deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), // 3 days from now - authRequired: false, - isDisabled: false, - restricted: false, + deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), }, { title: "Best Movie?", description: "Rank your favorite movies!", participants: 0, status: "published", - deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), // 3 days from now - authRequired: false, - isDisabled: false, - restricted: false, + deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), }, { title: "Best BBQ Item?", description: "Rank your favorite BBQ food!", - participants: 0, status: "published", - deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), // 3 days from now - authRequired: false, - isDisabled: false, - restricted: false, + deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), }, { title: "authRequired true", description: "?", participants: 0, status: "published", - deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), // 3 days from now + deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), authRequired: true, - isDisabled: false, - restricted: false, }, { title: "restricted true", description: "Rank your favorite anime of all time!", participants: 0, status: "published", - deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), // 3 days from now - authRequired: false, - isDisabled: false, + deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), restricted: true, }, ]); From 397296958e6439696b91222bb54748b6f5dcc4e3 Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Mon, 14 Jul 2025 11:02:47 -0400 Subject: [PATCH 13/31] feat- defined pollOption Model --- database/models/pollOption.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 database/models/pollOption.js diff --git a/database/models/pollOption.js b/database/models/pollOption.js new file mode 100644 index 0000000..a78d4c6 --- /dev/null +++ b/database/models/pollOption.js @@ -0,0 +1,24 @@ +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("poll", { + optionText: { + type: DataTypes.STRING, + allowNull: false, + }, + position: { + type: DataTypes.INTEGER, + allowNull: true, + }, + timestamps: true, + +}) + +module.exports = pollOption; \ No newline at end of file From 9b44adea8682f1abe7a3786a8c92c49eabc8d3d2 Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Mon, 14 Jul 2025 11:56:22 -0400 Subject: [PATCH 14/31] feat- created poll_option data --- database/index.js | 4 +- database/models/pollOption.js | 9 +++- database/seed.js | 94 +++++++++++++++++++++++++++++++++-- 3 files changed, 100 insertions(+), 7 deletions(-) diff --git a/database/index.js b/database/index.js index d2c3118..639205b 100644 --- a/database/index.js +++ b/database/index.js @@ -1,9 +1,11 @@ const db = require("./db"); const User = require("./models/user"); const Poll = require("./models/poll"); +const PollOption = require("./models/pollOption") module.exports = { db, User, - Poll + Poll, + PollOption, }; diff --git a/database/models/pollOption.js b/database/models/pollOption.js index a78d4c6..ce8743f 100644 --- a/database/models/pollOption.js +++ b/database/models/pollOption.js @@ -18,7 +18,14 @@ const pollOption = db.define("poll", { allowNull: true, }, timestamps: true, - + poll_id: { + type: DataTypes.INTEGER, + allowNull: false, + references: { + model: 'poll', // refers to poll table + key: 'id' + } + } }) module.exports = pollOption; \ No newline at end of file diff --git a/database/seed.js b/database/seed.js index f79ea62..5d7a002 100644 --- a/database/seed.js +++ b/database/seed.js @@ -1,5 +1,5 @@ const db = require("./db"); -const { User, Poll } = require("./index"); +const { User, Poll, PollOption } = require("./index"); const seed = async () => { try { @@ -12,6 +12,8 @@ const seed = async () => { { username: "user2", passwordHash: User.hashPassword("user222") }, ]); + + const polls = await Poll.bulkCreate([ { title: "Best Anime?", @@ -25,20 +27,20 @@ const seed = async () => { description: "Rank your favorite movies!", participants: 0, status: "published", - deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), + deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), }, { title: "Best BBQ Item?", description: "Rank your favorite BBQ food!", status: "published", - deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), + deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), }, { title: "authRequired true", description: "?", participants: 0, status: "published", - deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), + deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), authRequired: true, }, { @@ -47,10 +49,92 @@ const seed = async () => { participants: 0, status: "published", deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), - restricted: true, + restricted: true, }, ]); + + + const PollOptions = await PollOption.bulkCreate([ + { + optionText: "Demon Slayer", + position: 1, + poll_id: + }, + { + optionText: "One Piece", + position: 2, + }, + { + optionText: "AOT", + position: 3, + }, + { + optionText: "Naruto", + position: 4, + }, + { + optionText: "Devil May Cry", + position: 5, + }, + { + optionText: "Castlevania", + position: 6, + }, + { + optionText: "" + }, + { + optionText: "One Piece" + }, + { + optionText: "One Piece" + }, + { + optionText: "One Piece" + }, + { + optionText: "One Piece" + }, + { + optionText: "One Piece" + }, + { + optionText: "One Piece" + }, + { + optionText: "One Piece" + }, + { + optionText: "One Piece" + }, + { + optionText: "One Piece" + }, + { + optionText: "One Piece" + }, + { + optionText: "One Piece" + }, + { + optionText: "One Piece" + }, + { + optionText: "One Piece" + }, + { + optionText: "One Piece" + }, + { + optionText: "One Piece" + }, + { + optionText: "One Piece" + }, + + ]) + console.log(`๐Ÿ‘ค Created ${users.length} users`); console.log(`Created ${polls.length} polls`) From 432728a7ed1ea37d83a734c8f435fd753c933450 Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Mon, 14 Jul 2025 13:13:49 -0400 Subject: [PATCH 15/31] refactor- modified poll data to be more dinamic --- database/seed.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/database/seed.js b/database/seed.js index 5d7a002..b93f045 100644 --- a/database/seed.js +++ b/database/seed.js @@ -1,3 +1,4 @@ +const { Pool } = require("pg"); const db = require("./db"); const { User, Poll, PollOption } = require("./index"); @@ -12,46 +13,59 @@ const seed = async () => { { username: "user2", passwordHash: User.hashPassword("user222") }, ]); + // deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), - - const polls = await Poll.bulkCreate([ + const pollData = [ { + key: "anime", title: "Best Anime?", description: "Rank your favorite animes!", participants: 0, status: "published", - deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), + }, { + key: "movie", title: "Best Movie?", description: "Rank your favorite movies!", participants: 0, status: "published", - deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), }, { + key: "bbq", title: "Best BBQ Item?", description: "Rank your favorite BBQ food!", status: "published", - deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), }, { + key: "authRequired", title: "authRequired true", description: "?", participants: 0, status: "published", - deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), authRequired: true, }, { + key: "restricited", title: "restricted true", description: "Rank your favorite anime of all time!", participants: 0, status: "published", - deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), restricted: true, }, - ]); + ]; + + const createdPoll = {}; + const deadline = new Date(Date.now() + 3 * 24 * 60 * 60 * 1000); + + for (const poll of pollData) { + const created = await Poll.create({ + ...poll, + deadline, + user_id: someUser_id, + }) + createdPoll[poll.key] = created; + } @@ -59,7 +73,6 @@ const seed = async () => { { optionText: "Demon Slayer", position: 1, - poll_id: }, { optionText: "One Piece", From 15b7bf8d9f88c357a372463411a1811651edd74a Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Mon, 14 Jul 2025 13:59:13 -0400 Subject: [PATCH 16/31] feat-created poll_option data --- database/seed.js | 66 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/database/seed.js b/database/seed.js index b93f045..137b1de 100644 --- a/database/seed.js +++ b/database/seed.js @@ -55,7 +55,7 @@ const seed = async () => { }, ]; - const createdPoll = {}; + const createdPolls = {}; const deadline = new Date(Date.now() + 3 * 24 * 60 * 60 * 1000); for (const poll of pollData) { @@ -64,8 +64,9 @@ const seed = async () => { deadline, user_id: someUser_id, }) - createdPoll[poll.key] = created; - } + createdPolls[poll.key] = created; + console.log(createdPolls.anime.id) + }; @@ -73,77 +74,98 @@ const seed = async () => { { optionText: "Demon Slayer", position: 1, + poll_id: createdPolls.anime.id, }, { optionText: "One Piece", position: 2, + poll_id: createdPolls.anime.id, }, { optionText: "AOT", position: 3, + poll_id: createdPolls.anime.id, }, { optionText: "Naruto", position: 4, + poll_id: createdPolls.anime.id, }, { optionText: "Devil May Cry", position: 5, + poll_id: createdPolls.anime.id, }, { optionText: "Castlevania", position: 6, + poll_id: createdPolls.anime.id, }, { - optionText: "" - }, - { - optionText: "One Piece" + optionText: "Die Hard", + poll_id: createdPolls.movie.id }, { - optionText: "One Piece" + optionText: "Die Hard 2", + poll_id: createdPolls.movie.id, }, { - optionText: "One Piece" + optionText: "Twilight", + poll_id: createdPolls.movie.id, }, { - optionText: "One Piece" + optionText: "Spiderverse", + poll_id: createdPolls.movie.id, }, { - optionText: "One Piece" + optionText: "Pork Ribs", + poll_id: createdPolls.bbq.id, }, { - optionText: "One Piece" + optionText: "Hot Dog", + poll_id: createdPolls.bbq.id, }, { - optionText: "One Piece" + optionText: "Cheeseburger", + poll_id: createdPolls.bbq.id, }, { - optionText: "One Piece" + optionText: "Suasage", + poll_Id: createdPolls.bbq.id, }, { - optionText: "One Piece" + optionText: "a", + poll_id: createdPolls.authRequired.id, }, { - optionText: "One Piece" + optionText: "b", + poll_id: createdPolls.authRequired.id, }, { - optionText: "One Piece" + optionText: "c", + poll_id: createdPolls.authRequired.id, }, { - optionText: "One Piece" + optionText: "d", + poll_id: createdPolls.authRequired.id, }, { - optionText: "One Piece" + optionText: "1", + poll_id: createdPolls.restricited.id, }, { - optionText: "One Piece" + optionText: "2", + poll_id: createdPolls.restricited.id, }, { - optionText: "One Piece" + optionText: "3", + poll_id: createdPolls.restricited.id, + }, { - optionText: "One Piece" + optionText: "4", + poll_id: createdPolls.restricited.id, + }, ]) From 9f55ed87133a95f24de75dc841f77e8540dbe965 Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Mon, 14 Jul 2025 14:35:03 -0400 Subject: [PATCH 17/31] refactor- made poll_option data more dinamic --- database/seed.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/database/seed.js b/database/seed.js index 137b1de..56cd4e8 100644 --- a/database/seed.js +++ b/database/seed.js @@ -22,6 +22,7 @@ const seed = async () => { description: "Rank your favorite animes!", participants: 0, status: "published", + userkey: "user1", }, { @@ -30,12 +31,14 @@ const seed = async () => { description: "Rank your favorite movies!", participants: 0, status: "published", + userKey: "user2", }, { key: "bbq", title: "Best BBQ Item?", description: "Rank your favorite BBQ food!", status: "published", + userKey: "user1" }, { key: "authRequired", @@ -44,6 +47,8 @@ const seed = async () => { participants: 0, status: "published", authRequired: true, + userKey: "user2" + }, { key: "restricited", @@ -52,17 +57,25 @@ const seed = async () => { participants: 0, status: "published", restricted: true, + userKey: "user1" + }, ]; const createdPolls = {}; const deadline = new Date(Date.now() + 3 * 24 * 60 * 60 * 1000); + const userMap = { + admin: users[0], + user1: users[1], + user2: users[2], + }; + for (const poll of pollData) { const created = await Poll.create({ ...poll, deadline, - user_id: someUser_id, + user_id: userMap[poll.userKey].id, }) createdPolls[poll.key] = created; console.log(createdPolls.anime.id) @@ -131,7 +144,7 @@ const seed = async () => { }, { optionText: "Suasage", - poll_Id: createdPolls.bbq.id, + poll_id: createdPolls.bbq.id, }, { optionText: "a", @@ -171,7 +184,7 @@ const seed = async () => { ]) console.log(`๐Ÿ‘ค Created ${users.length} users`); - console.log(`Created ${polls.length} polls`) + console.log(`Created ${createdPolls.length} polls`) // Create more seed data here once you've created your models // Seed files are a great way to test your database schema! From e94a08ca49f3e706e6eb88c34e6dc6f936ab5c33 Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Mon, 14 Jul 2025 15:28:49 -0400 Subject: [PATCH 18/31] optimized user_model --- database/models/user.js | 16 ++++++++++++---- database/seed.js | 19 ++++++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/database/models/user.js b/database/models/user.js index eba5ff0..906f89b 100644 --- a/database/models/user.js +++ b/database/models/user.js @@ -3,9 +3,17 @@ const db = require("../db"); const bcrypt = require("bcrypt"); const User = db.define("user", { + userName: { + type: DataTypes.STRING, + allowNull: true, + unique: true, + validate: { + len: [1, 50] + } + }, firstName: { type: DataTypes.STRING, - allowNull: false, + // allowNull: false, validate: { notEmpty: true, len: [1, 50], @@ -13,7 +21,7 @@ const User = db.define("user", { }, lastName: { type: DataTypes.STRING, - allowNull: false, + // allowNull: false, validate: { notEmpty: true, len: [1, 50], @@ -21,7 +29,7 @@ const User = db.define("user", { }, email: { type: DataTypes.STRING, - allowNull: false, + // allowNull: false, unique: true, validate: { isEmail: true, @@ -60,7 +68,7 @@ const User = db.define("user", { }, { timestamps: true, createdAt: 'created_at', - updatedAt: false, + updatedAt: false, }); // Instance method to check password diff --git a/database/seed.js b/database/seed.js index 56cd4e8..e84c7f7 100644 --- a/database/seed.js +++ b/database/seed.js @@ -8,9 +8,18 @@ const seed = async () => { await db.sync({ force: true }); // Drop and recreate tables const users = await User.bulkCreate([ - { username: "admin", passwordHash: User.hashPassword("admin123") }, - { username: "user1", passwordHash: User.hashPassword("user111") }, - { username: "user2", passwordHash: User.hashPassword("user222") }, + { + username: "admin", + passwordHash: User.hashPassword("admin123"), + }, + { + username: "user1", + passwordHash: User.hashPassword("user111") + }, + { + username: "user2", + passwordHash: User.hashPassword("user222") + }, ]); // deadline: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), @@ -22,7 +31,7 @@ const seed = async () => { description: "Rank your favorite animes!", participants: 0, status: "published", - userkey: "user1", + userKey: "user1", }, { @@ -184,7 +193,7 @@ const seed = async () => { ]) console.log(`๐Ÿ‘ค Created ${users.length} users`); - console.log(`Created ${createdPolls.length} polls`) + console.log(`Created ${Object.keys(createdPolls).length} polls`); // Create more seed data here once you've created your models // Seed files are a great way to test your database schema! From 9b3c5a9ff69ad61eef125d45c2c339b2b3d018cc Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Mon, 14 Jul 2025 20:30:24 -0400 Subject: [PATCH 19/31] feat- associations --- database/index.js | 25 ++++++++++++++++++++++++- database/models/poll.js | 12 ++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/database/index.js b/database/index.js index 639205b..eb37a68 100644 --- a/database/index.js +++ b/database/index.js @@ -1,8 +1,31 @@ const db = require("./db"); const User = require("./models/user"); const Poll = require("./models/poll"); -const PollOption = require("./models/pollOption") +const PollOption = require("./models/pollOption"); +const pollOption = require("./models/pollOption"); + +//One to many - user has many polls +User.hasMany(Poll, { + foreignKey: 'userId', + // onDelete: 'CASCADE' deletes poll is user is deleted +}); + +// One 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: "CASCASDE", // delete poll_options if poll is deleted +}); + +// one to one - Each pollOption belongs to one Poll +PollOption.belongsTo(Poll, { + foreignKey: "pollId" +}) module.exports = { db, User, diff --git a/database/models/poll.js b/database/models/poll.js index b0f102d..f8bbbf0 100644 --- a/database/models/poll.js +++ b/database/models/poll.js @@ -1,4 +1,4 @@ -const {DataTypes} = require('sequelize'); +const { DataTypes } = require('sequelize'); const db = require('../db'); // define the Poll model @@ -21,7 +21,7 @@ const Poll = db.define("poll", { allowNull: false, }, deadline: { - type: DataTypes.TIMESTAMP, + type: DataTypes.DATE, allowNull: false, }, authRequired: { @@ -39,7 +39,11 @@ const Poll = db.define("poll", { default: false, allowNull: false, }, - timestamps: true -}); +}, + { + timestamps: true, + createdAt: "created at", + + }); module.exports = Poll; \ No newline at end of file From e1bcb9c545bdac569fc8b9c5380c115978f0b307 Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Mon, 14 Jul 2025 20:35:09 -0400 Subject: [PATCH 20/31] refactor- change DB name --- database/db.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/db.js b/database/db.js index b251a9d..bba1d5f 100644 --- a/database/db.js +++ b/database/db.js @@ -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}`, From 2a3b0e8cf3a945e839043ec5e8f0dddbcd5de402 Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Mon, 14 Jul 2025 21:34:42 -0400 Subject: [PATCH 21/31] fixed-bugs with model attributes --- database/index.js | 4 +-- database/models/poll.js | 12 +++----- database/models/pollOption.js | 15 ++++------ database/models/user.js | 11 +++---- database/models/vote.js | 11 ++++--- database/seed.js | 54 +++++++++++++++++------------------ 6 files changed, 50 insertions(+), 57 deletions(-) diff --git a/database/index.js b/database/index.js index eb37a68..10624b5 100644 --- a/database/index.js +++ b/database/index.js @@ -2,8 +2,6 @@ const db = require("./db"); const User = require("./models/user"); const Poll = require("./models/poll"); const PollOption = require("./models/pollOption"); -const pollOption = require("./models/pollOption"); - //One to many - user has many polls User.hasMany(Poll, { @@ -19,7 +17,7 @@ Poll.belongsTo(User, { // One to many - one Poll has many options Poll.hasMany(PollOption, { foreignKey: 'pollId', - onDelete: "CASCASDE", // delete poll_options if poll is deleted + onDelete: "CASCADE", // delete poll_options if poll is deleted }); // one to one - Each pollOption belongs to one Poll diff --git a/database/models/poll.js b/database/models/poll.js index f8bbbf0..5031a1f 100644 --- a/database/models/poll.js +++ b/database/models/poll.js @@ -10,14 +10,14 @@ const Poll = db.define("poll", { }, description: { type: DataTypes.TEXT, - allowNull: true, + // allowNull: true, }, participants: { type: DataTypes.INTEGER, defaultValue: 0, }, status: { - type: DataTypes.ENUM, // draft, published , ended + type: DataTypes.ENUM("draft", "published", "ended"), allowNull: false, }, deadline: { @@ -27,23 +27,19 @@ const Poll = db.define("poll", { authRequired: { type: DataTypes.BOOLEAN, // allow only user votes if true default: false, - allowNull: false, }, isDisabled: { type: DataTypes.BOOLEAN, // if true poll is disabled by admin default: false, - allowNull: false, }, restricted: { type: DataTypes.BOOLEAN, // only specic users can parcipate if true default: false, - allowNull: false, }, }, { timestamps: true, - createdAt: "created at", - - }); + } +); module.exports = Poll; \ No newline at end of file diff --git a/database/models/pollOption.js b/database/models/pollOption.js index ce8743f..0e56250 100644 --- a/database/models/pollOption.js +++ b/database/models/pollOption.js @@ -8,7 +8,7 @@ const db = require('../db'); // poll_id FK // created_at timestamp // } -const pollOption = db.define("poll", { +const pollOption = db.define("pollOption", { optionText: { type: DataTypes.STRING, allowNull: false, @@ -17,15 +17,10 @@ const pollOption = db.define("poll", { type: DataTypes.INTEGER, allowNull: true, }, - timestamps: true, - poll_id: { - type: DataTypes.INTEGER, - allowNull: false, - references: { - model: 'poll', // refers to poll table - key: 'id' - } +}, + { + timestamps: true, } -}) +) module.exports = pollOption; \ No newline at end of file diff --git a/database/models/user.js b/database/models/user.js index 906f89b..6e82a40 100644 --- a/database/models/user.js +++ b/database/models/user.js @@ -65,11 +65,12 @@ const User = db.define("user", { allowNull: false, defaultValue: false, }, -}, { - timestamps: true, - createdAt: 'created_at', - updatedAt: false, -}); +}, + { + timestamps: true, + createdAt: 'created_at', + } +); // Instance method to check password User.prototype.checkPassword = function (password) { diff --git a/database/models/vote.js b/database/models/vote.js index 644f195..a0f1695 100644 --- a/database/models/vote.js +++ b/database/models/vote.js @@ -1,4 +1,4 @@ -const {DataTypes} = require('sequelize'); +const { DataTypes } = require('sequelize'); const db = require('../db'); // define the Vote model @@ -16,7 +16,10 @@ const Vote = db.define("vote", { type: DataTypes.STRING, allowNull: true, // same as voter token we can use either }, - timestamps: true -}) +}, + { + timestamps: true, + } +); -module.export = Vote; \ No newline at end of file +module.exports = Vote; \ No newline at end of file diff --git a/database/seed.js b/database/seed.js index e84c7f7..e6ec5f1 100644 --- a/database/seed.js +++ b/database/seed.js @@ -9,15 +9,15 @@ const seed = async () => { const users = await User.bulkCreate([ { - username: "admin", + userName: "admin", passwordHash: User.hashPassword("admin123"), }, { - username: "user1", + userName: "user1", passwordHash: User.hashPassword("user111") }, { - username: "user2", + userName: "user2", passwordHash: User.hashPassword("user222") }, ]); @@ -72,7 +72,7 @@ const seed = async () => { ]; const createdPolls = {}; - const deadline = new Date(Date.now() + 3 * 24 * 60 * 60 * 1000); + const deadline = new Date(Date.now() + 3 * 24 * 60 * 60 * 1000); // 3days const userMap = { admin: users[0], @@ -87,7 +87,7 @@ const seed = async () => { user_id: userMap[poll.userKey].id, }) createdPolls[poll.key] = created; - console.log(createdPolls.anime.id) + // console.log(createdPolls.anime.id) }; @@ -96,97 +96,97 @@ const seed = async () => { { optionText: "Demon Slayer", position: 1, - poll_id: createdPolls.anime.id, + pollId: createdPolls.anime.id, }, { optionText: "One Piece", position: 2, - poll_id: createdPolls.anime.id, + pollId: createdPolls.anime.id, }, { optionText: "AOT", position: 3, - poll_id: createdPolls.anime.id, + pollId: createdPolls.anime.id, }, { optionText: "Naruto", position: 4, - poll_id: createdPolls.anime.id, + pollId: createdPolls.anime.id, }, { optionText: "Devil May Cry", position: 5, - poll_id: createdPolls.anime.id, + poll_Id: createdPolls.anime.id, }, { optionText: "Castlevania", position: 6, - poll_id: createdPolls.anime.id, + pollId: createdPolls.anime.id, }, { optionText: "Die Hard", - poll_id: createdPolls.movie.id + pollId: createdPolls.movie.id }, { optionText: "Die Hard 2", - poll_id: createdPolls.movie.id, + pollId: createdPolls.movie.id, }, { optionText: "Twilight", - poll_id: createdPolls.movie.id, + pollId: createdPolls.movie.id, }, { optionText: "Spiderverse", - poll_id: createdPolls.movie.id, + pollId: createdPolls.movie.id, }, { optionText: "Pork Ribs", - poll_id: createdPolls.bbq.id, + pollId: createdPolls.bbq.id, }, { optionText: "Hot Dog", - poll_id: createdPolls.bbq.id, + pollId: createdPolls.bbq.id, }, { optionText: "Cheeseburger", - poll_id: createdPolls.bbq.id, + pollId: createdPolls.bbq.id, }, { optionText: "Suasage", - poll_id: createdPolls.bbq.id, + pollId: createdPolls.bbq.id, }, { optionText: "a", - poll_id: createdPolls.authRequired.id, + pollId: createdPolls.authRequired.id, }, { optionText: "b", - poll_id: createdPolls.authRequired.id, + pollId: createdPolls.authRequired.id, }, { optionText: "c", - poll_id: createdPolls.authRequired.id, + pollId: createdPolls.authRequired.id, }, { optionText: "d", - poll_id: createdPolls.authRequired.id, + pollId: createdPolls.authRequired.id, }, { optionText: "1", - poll_id: createdPolls.restricited.id, + pollId: createdPolls.restricited.id, }, { optionText: "2", - poll_id: createdPolls.restricited.id, + pollId: createdPolls.restricited.id, }, { optionText: "3", - poll_id: createdPolls.restricited.id, + pollId: createdPolls.restricited.id, }, { optionText: "4", - poll_id: createdPolls.restricited.id, + pollId: createdPolls.restricited.id, }, From 697ceaa6854a9205b50695d8e29fc6a00dba066a Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Mon, 14 Jul 2025 21:58:07 -0400 Subject: [PATCH 22/31] fixed-Poll FK --- database/seed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/seed.js b/database/seed.js index e6ec5f1..2110406 100644 --- a/database/seed.js +++ b/database/seed.js @@ -84,7 +84,7 @@ const seed = async () => { const created = await Poll.create({ ...poll, deadline, - user_id: userMap[poll.userKey].id, + userId: userMap[poll.userKey].id, }) createdPolls[poll.key] = created; // console.log(createdPolls.anime.id) From adfb64596b2ecfd1e61df597b02bf2c88f378f0c Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Mon, 14 Jul 2025 22:09:30 -0400 Subject: [PATCH 23/31] feat-votingRank model --- database/models/votingRank.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 database/models/votingRank.js diff --git a/database/models/votingRank.js b/database/models/votingRank.js new file mode 100644 index 0000000..e69de29 From a1ba0b463917682c6dec27230273926326272d73 Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Mon, 14 Jul 2025 23:23:59 -0400 Subject: [PATCH 24/31] defined VotingRank model --- database/index.js | 3 ++- database/models/votingRank.js | 13 +++++++++++++ database/seed.js | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/database/index.js b/database/index.js index 10624b5..9c1b570 100644 --- a/database/index.js +++ b/database/index.js @@ -2,7 +2,7 @@ const db = require("./db"); const User = require("./models/user"); const Poll = require("./models/poll"); const PollOption = require("./models/pollOption"); - +const VotingRank = require("./models/votingRank"); //One to many - user has many polls User.hasMany(Poll, { foreignKey: 'userId', @@ -29,4 +29,5 @@ module.exports = { User, Poll, PollOption, + VotingRank, }; diff --git a/database/models/votingRank.js b/database/models/votingRank.js index e69de29..05aa1b2 100644 --- a/database/models/votingRank.js +++ b/database/models/votingRank.js @@ -0,0 +1,13 @@ +const { DataTypes } = require("sequelize"); +const db = require("../db"); + +// define the votingRank model + +const VotingRank = db.define("votingRank", { + rank: { + type: DataTypes.INTEGER, + allowNull: false, + } +}) + +module.exports = VotingRank; \ No newline at end of file diff --git a/database/seed.js b/database/seed.js index 2110406..c789900 100644 --- a/database/seed.js +++ b/database/seed.js @@ -1,6 +1,6 @@ const { Pool } = require("pg"); const db = require("./db"); -const { User, Poll, PollOption } = require("./index"); +const { User, Poll, PollOption, VotingRank } = require("./index"); const seed = async () => { try { From 7c4cc319cfe76847e313467e363f1194705afbca Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Tue, 15 Jul 2025 00:56:46 -0400 Subject: [PATCH 25/31] feat- association --- database/index.js | 54 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/database/index.js b/database/index.js index 9c1b570..c91f4ef 100644 --- a/database/index.js +++ b/database/index.js @@ -3,31 +3,81 @@ 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 }); -// One to one - Each Poll belongs to one user + +// 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 }); -// one to one - Each pollOption belongs to one Poll + +// 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: "voteRankId", +}) + + +//one to many- A vote(ballot) can have many ranked options +VotingRank.belongsTo(Vote, { + 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, }; From 85544fb80e614291b11a4300c74d2b98f535efbe Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Tue, 15 Jul 2025 11:09:46 -0400 Subject: [PATCH 26/31] feat- created Vote data --- database/seed.js | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/database/seed.js b/database/seed.js index c789900..1517bd3 100644 --- a/database/seed.js +++ b/database/seed.js @@ -1,6 +1,7 @@ const { Pool } = require("pg"); const db = require("./db"); -const { User, Poll, PollOption, VotingRank } = require("./index"); +const { User, Poll, PollOption, Vote, VotingRank } = require("./index"); +const pollOption = require("./models/pollOption"); const seed = async () => { try { @@ -190,11 +191,38 @@ const seed = async () => { }, + ]); + + + + const votes = await Vote.bulkCreate([ + { + userId: users[1].id, + pollId: createdPolls.anime.id + }, + { + userId: users[2].id, + pollId: createdPolls.movie.id + }, + { + userId: users[1].id, + pollId: createdPolls.bbq.id + }, + { + userId: users[2].id, + pollId: createdPolls.authRequired.id + }, + { + userId: users[1].id, + pollId: createdPolls.restricited.id + }, ]) + + console.log(`๐Ÿ‘ค Created ${users.length} users`); console.log(`Created ${Object.keys(createdPolls).length} polls`); - + console.log(`Created ${pollOption.length} poll options`); // Create more seed data here once you've created your models // Seed files are a great way to test your database schema! From f252e90ead17a78be3d51af88e7fbda8f587c7e3 Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Tue, 15 Jul 2025 11:38:55 -0400 Subject: [PATCH 27/31] feat- created voteRank data --- database/seed.js | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/database/seed.js b/database/seed.js index 1517bd3..e8a2c06 100644 --- a/database/seed.js +++ b/database/seed.js @@ -194,7 +194,7 @@ const seed = async () => { ]); - + // vote ---> envelope const votes = await Vote.bulkCreate([ { userId: users[1].id, @@ -218,6 +218,44 @@ const seed = async () => { }, ]) + const optionMap = {}; + PollOptions.forEach((option) => { + optionMap[option.optionText] = option; + }); + + const ranks = await VotingRank.bulkCreate([ + { + voteId: votes[0].id, + pollOptionId: optionMap["Demon Slayer"].id, + rank: 1, + }, + { + voteId: votes[0].id, + pollOptionId: optionMap["OnePiece"].id, + rank: 3, + }, + { + voteId: votes[0].id, + pollOptionId: optionMap["AOT"].id, + rank: 4, + }, + { + voteId: votes[0].id, + pollOptionId: optionMap["Devil May Cry"].id, + rank: 6 + }, + { + voteId: votes[0].id, + pollOptionId: optionMap["Castlevania"].id, + rank: 5 + }, + { + voteId: votes[0].id, + pollOptionId: optionMap["Naruto"].id, + rank: 2 + }, + ]) + console.log(`๐Ÿ‘ค Created ${users.length} users`); From bdc266cfe0c4167fa35392ec850edf3a0ad31355 Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Tue, 15 Jul 2025 11:52:22 -0400 Subject: [PATCH 28/31] fixed- bug in rank data --- database/seed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/seed.js b/database/seed.js index e8a2c06..6a5a272 100644 --- a/database/seed.js +++ b/database/seed.js @@ -231,7 +231,7 @@ const seed = async () => { }, { voteId: votes[0].id, - pollOptionId: optionMap["OnePiece"].id, + pollOptionId: optionMap["One Piece"].id, rank: 3, }, { From eca4f0db85f51b8603879eea112140091fc90a1b Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Tue, 15 Jul 2025 12:28:09 -0400 Subject: [PATCH 29/31] feat- added attributes to VotingRank --- database/models/votingRank.js | 8 ++++++++ database/seed.js | 7 ++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/database/models/votingRank.js b/database/models/votingRank.js index 05aa1b2..d5a03c2 100644 --- a/database/models/votingRank.js +++ b/database/models/votingRank.js @@ -4,6 +4,14 @@ 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, diff --git a/database/seed.js b/database/seed.js index 6a5a272..160746a 100644 --- a/database/seed.js +++ b/database/seed.js @@ -117,7 +117,7 @@ const seed = async () => { { optionText: "Devil May Cry", position: 5, - poll_Id: createdPolls.anime.id, + pollId: createdPolls.anime.id, }, { optionText: "Castlevania", @@ -218,6 +218,7 @@ const seed = async () => { }, ]) + const optionMap = {}; PollOptions.forEach((option) => { optionMap[option.optionText] = option; @@ -254,13 +255,13 @@ const seed = async () => { pollOptionId: optionMap["Naruto"].id, rank: 2 }, - ]) + ]); console.log(`๐Ÿ‘ค Created ${users.length} users`); console.log(`Created ${Object.keys(createdPolls).length} polls`); - console.log(`Created ${pollOption.length} poll options`); + console.log(`๐Ÿงพ Created ${PollOptions.length} poll options`); // Create more seed data here once you've created your models // Seed files are a great way to test your database schema! From b5b23bd567a589b5fa6abea59c4a1ccbbc89a887 Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Tue, 15 Jul 2025 12:34:57 -0400 Subject: [PATCH 30/31] feat- added attr to Vote model --- database/models/vote.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/database/models/vote.js b/database/models/vote.js index a0f1695..e078ef9 100644 --- a/database/models/vote.js +++ b/database/models/vote.js @@ -6,20 +6,26 @@ const db = require('../db'); const Vote = db.define("vote", { submitted: { type: DataTypes.BOOLEAN, - defaultValue: false // if false; vote has not yet been submited; ballot can still be edited + defaultValue: false, }, voterToken: { type: DataTypes.STRING, - allowNull: true, // this allows us to uniquely identify a guest . we can track if they voted. + allowNull: true, }, ipAddress: { type: DataTypes.STRING, - allowNull: true, // same as voter token we can use either + allowNull: true, }, -}, - { - timestamps: true, - } -); + userId: { + type: DataTypes.INTEGER, + allowNull: false, + }, + pollId: { + type: DataTypes.INTEGER, + allowNull: false, + }, +}, { + timestamps: true, +}); module.exports = Vote; \ No newline at end of file From 4e9d505719630b1a9c8bc116fd0956b6d5c868de Mon Sep 17 00:00:00 2001 From: Florencio Rendon Date: Tue, 15 Jul 2025 13:15:57 -0400 Subject: [PATCH 31/31] fixed- association bugs --- database/index.js | 8 +------- database/seed.js | 1 - 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/database/index.js b/database/index.js index c91f4ef..20775f3 100644 --- a/database/index.js +++ b/database/index.js @@ -51,16 +51,10 @@ Vote.belongsTo(Poll, { // one to many- each vote(ballot) can have many ranked options Vote.hasMany(VotingRank, { - foreignKey: "voteRankId", + foreignKey: "voteId", }) -//one to many- A vote(ballot) can have many ranked options -VotingRank.belongsTo(Vote, { - foreignKey: 'voteId', -}); - - // many to one - each rank entry belongs to one vote(ballot) VotingRank.belongsTo(Vote, { foreignKey: "voteId", diff --git a/database/seed.js b/database/seed.js index 160746a..661f859 100644 --- a/database/seed.js +++ b/database/seed.js @@ -1,7 +1,6 @@ const { Pool } = require("pg"); const db = require("./db"); const { User, Poll, PollOption, Vote, VotingRank } = require("./index"); -const pollOption = require("./models/pollOption"); const seed = async () => { try {