From a378d764361c8d11f9773fbcbaf5e006dd9edb92 Mon Sep 17 00:00:00 2001 From: Muhammad Sohail Date: Wed, 5 Jun 2024 02:38:51 -0400 Subject: [PATCH] Fix toJSON --- models/account.model.js | 4 ++-- models/accountConfirmationToken.model.js | 12 ++++++------ models/bus.model.js | 4 ++-- models/emailTemplate.model.js | 12 ++++++------ models/hacker.model.js | 4 ++-- models/passwordResetToken.model.js | 12 ++++++------ models/role.model.js | 12 ++++++------ models/roleBinding.model.js | 12 ++++++------ models/settings.model.js | 4 ++-- models/sponsor.model.js | 4 ++-- models/staff.model.js | 4 ++-- models/team.model.js | 4 ++-- models/travel.model.js | 12 ++++++------ models/volunteer.model.js | 4 ++-- tests/hacker.test.js | 18 +++++++++--------- 15 files changed, 61 insertions(+), 61 deletions(-) diff --git a/models/account.model.js b/models/account.model.js index 292cd66e..047c8bc0 100644 --- a/models/account.model.js +++ b/models/account.model.js @@ -55,8 +55,8 @@ const AccountSchema = new mongoose.Schema({ } }); -AccountSchema.methods.toJSON = function() { - const as = this.toObject(); +AccountSchema.methods.toJSON = function(options) { + const as = this.toObject(options); delete as.__v; as.id = as._id; delete as._id; diff --git a/models/accountConfirmationToken.model.js b/models/accountConfirmationToken.model.js index a05eca85..eec6cf06 100644 --- a/models/accountConfirmationToken.model.js +++ b/models/accountConfirmationToken.model.js @@ -26,12 +26,12 @@ const AccountConfirmationSchema = new mongoose.Schema({ } }); -AccountConfirmationSchema.methods.toJSON = function() { - const resetObj = this.toObject(); - delete resetObj.__v; - resetObj.id = resetObj._id; - delete resetObj._id; - return resetObj; +AccountConfirmationSchema.methods.toJSON = function(options) { + const acs = this.toObject(options); + delete acs.__v; + acs.id = acs._id; + delete acs._id; + return acs; }; module.exports = mongoose.model( diff --git a/models/bus.model.js b/models/bus.model.js index 1419e694..218624ee 100644 --- a/models/bus.model.js +++ b/models/bus.model.js @@ -41,8 +41,8 @@ const BusSchema = new mongoose.Schema({ } }); -BusSchema.methods.toJSON = function() { - const bs = this.toObject(); +BusSchema.methods.toJSON = function(options) { + const bs = this.toObject(options); delete bs.__v; bs.id = bs._id; delete bs._id; diff --git a/models/emailTemplate.model.js b/models/emailTemplate.model.js index 11343bc8..2e8c07b9 100644 --- a/models/emailTemplate.model.js +++ b/models/emailTemplate.model.js @@ -13,12 +13,12 @@ const EmailTemplateSchema = new mongoose.Schema({ } }); -EmailTemplateSchema.methods.toJSON = function () { - const emailTemplateObj = this.toObject(); - delete emailTemplateObj.__v; - emailTemplateObj.id = emailTemplateObj._id; - delete emailTemplateObj._id; - return emailTemplateObj; +EmailTemplateSchema.methods.toJSON = function (options) { + const ets = this.toObject(options); + delete ets.__v; + ets.id = ets._id; + delete ets._id; + return ets; }; // export the model diff --git a/models/hacker.model.js b/models/hacker.model.js index f6e8d611..291964e3 100644 --- a/models/hacker.model.js +++ b/models/hacker.model.js @@ -167,8 +167,8 @@ const HackerSchema = new mongoose.Schema({ } }); -HackerSchema.methods.toJSON = function() { - const hs = this.toObject(); +HackerSchema.methods.toJSON = function(options) { + const hs = this.toObject(options); delete hs.__v; hs.id = hs._id; delete hs._id; diff --git a/models/passwordResetToken.model.js b/models/passwordResetToken.model.js index 012cd6f6..fb190ef0 100644 --- a/models/passwordResetToken.model.js +++ b/models/passwordResetToken.model.js @@ -12,12 +12,12 @@ const passwordResetSchema = new mongoose.Schema({ } }); -passwordResetSchema.methods.toJSON = function() { - const resetObj = this.toObject(); - delete resetObj.__v; - resetObj.id = resetObj._id; - delete resetObj._id; - return resetObj; +passwordResetSchema.methods.toJSON = function(options) { + const prs = this.toObject(options); + delete prs.__v; + prs.id = prs._id; + delete prs._id; + return prs; }; module.exports = mongoose.model("PasswordResetToken", passwordResetSchema); diff --git a/models/role.model.js b/models/role.model.js index 810465ea..5fa31127 100644 --- a/models/role.model.js +++ b/models/role.model.js @@ -28,12 +28,12 @@ const RoleSchema = new mongoose.Schema({ ] }); -RoleSchema.methods.toJSON = function() { - const ps = this.toObject(); - delete ps.__v; - ps.id = ps._id; - delete ps._id; - return ps; +RoleSchema.methods.toJSON = function(options) { + const rs = this.toObject(options); + delete rs.__v; + rs.id = rs._id; + delete rs._id; + return rs; }; //export the model module.exports = mongoose.model("Role", RoleSchema); diff --git a/models/roleBinding.model.js b/models/roleBinding.model.js index e00cc035..7d136745 100644 --- a/models/roleBinding.model.js +++ b/models/roleBinding.model.js @@ -19,12 +19,12 @@ const roleBinding = new mongoose.Schema({ } }); -roleBinding.methods.toJSON = function() { - const ps = this.toObject(); - delete ps.__v; - ps.id = ps._id; - delete ps._id; - return ps; +roleBinding.methods.toJSON = function(options) { + const rb = this.toObject(options); + delete rb.__v; + rb.id = rb._id; + delete rb._id; + return rb; }; //export the model module.exports = mongoose.model("RoleBinding", roleBinding); diff --git a/models/settings.model.js b/models/settings.model.js index ab704535..db5f52c4 100644 --- a/models/settings.model.js +++ b/models/settings.model.js @@ -20,8 +20,8 @@ const settings = new mongoose.Schema({ } }); -settings.methods.toJSON = function() { - const ss = this.toObject(); +settings.methods.toJSON = function(options) { + const ss = this.toObject(options); delete ss.__v; ss.id = ss._id; delete ss._id; diff --git a/models/sponsor.model.js b/models/sponsor.model.js index 230ffe82..2bf9f9bb 100644 --- a/models/sponsor.model.js +++ b/models/sponsor.model.js @@ -31,8 +31,8 @@ const SponsorSchema = new mongoose.Schema({ ] }); -SponsorSchema.methods.toJSON = function() { - const ss = this.toObject(); +SponsorSchema.methods.toJSON = function(options) { + const ss = this.toObject(options); delete ss.__v; ss.id = ss._id; delete ss._id; diff --git a/models/staff.model.js b/models/staff.model.js index 9a7d22f8..6ab4f365 100644 --- a/models/staff.model.js +++ b/models/staff.model.js @@ -9,8 +9,8 @@ const StaffSchema = new mongoose.Schema({ } }); -StaffSchema.methods.toJSON = function() { - const ss = this.toObject(); +StaffSchema.methods.toJSON = function(options) { + const ss = this.toObject(options); delete ss.__v; ss.id = ss._id; delete ss._id; diff --git a/models/team.model.js b/models/team.model.js index 7e339f6a..31f55ec2 100644 --- a/models/team.model.js +++ b/models/team.model.js @@ -29,8 +29,8 @@ function validateTeamSize(membersArr) { return membersArr.length <= Constants.MAX_TEAM_SIZE; } -TeamSchema.methods.toJSON = function() { - const ts = this.toObject(); +TeamSchema.methods.toJSON = function(options) { + const ts = this.toObject(options); delete ts.__v; ts.id = ts._id; delete ts._id; diff --git a/models/travel.model.js b/models/travel.model.js index e22f196a..faae04b7 100644 --- a/models/travel.model.js +++ b/models/travel.model.js @@ -41,12 +41,12 @@ const TravelSchema = new mongoose.Schema({ } }); -TravelSchema.methods.toJSON = function() { - const hs = this.toObject(); - delete hs.__v; - hs.id = hs._id; - delete hs._id; - return hs; +TravelSchema.methods.toJSON = function(options) { + const ts = this.toObject(options); + delete ts.__v; + ts.id = ts._id; + delete ts._id; + return ts; }; //export the model diff --git a/models/volunteer.model.js b/models/volunteer.model.js index 6d52d9b9..e68207f0 100644 --- a/models/volunteer.model.js +++ b/models/volunteer.model.js @@ -9,8 +9,8 @@ const VolunteerSchema = new mongoose.Schema({ } }); -VolunteerSchema.methods.toJSON = function() { - const vs = this.toObject(); +VolunteerSchema.methods.toJSON = function(options) { + const vs = this.toObject(options); delete vs.__v; vs.id = vs._id; delete vs._id; diff --git a/tests/hacker.test.js b/tests/hacker.test.js index 6f691a31..6bd7bc69 100644 --- a/tests/hacker.test.js +++ b/tests/hacker.test.js @@ -115,7 +115,7 @@ describe("GET hacker", function() { let hacker = new Hacker(TeamHacker0); chai.assert.deepStrictEqual( res.body.data, - JSON.parse(JSON.stringify(hacker)), + hacker.toJSON({ flattenObjectIds: true}), ); done(); }); @@ -186,7 +186,7 @@ describe("GET hacker", function() { let hacker = new Hacker(TeamHacker0); chai.assert.deepStrictEqual( res.body.data, - JSON.parse(JSON.stringify(hacker)), + hacker.toJSON({ flattenObjectIds: true }), ); done(); @@ -222,7 +222,7 @@ describe("GET hacker", function() { chai.assert.deepStrictEqual( res.body.data, - JSON.parse(JSON.stringify(hacker)), + hacker.toJSON({ flattenObjectIds: true }), ); done(); @@ -312,7 +312,7 @@ describe("GET hacker", function() { let hacker = new Hacker(TeamHacker0); chai.assert.deepStrictEqual( res.body.data, - JSON.parse(JSON.stringify(hacker)) + hacker.toJSON({ flattenObjectIds: true }), ); done(); @@ -348,7 +348,7 @@ describe("GET hacker", function() { chai.assert.deepStrictEqual( res.body.data, - JSON.parse(JSON.stringify(hacker)) + hacker.toJSON({ flattenObjectIds: true }), ); done(); @@ -445,13 +445,13 @@ describe("POST create hacker", function() { // create JSON version of model // delete id as they will be different between model objects // update status to be applied on the comparator hacker object - const hacker = new Hacker(newHacker0).toJSON(); + const hacker = new Hacker(newHacker0).toJSON({ flattenObjectIds: true }); hacker.status = Constants.General.HACKER_STATUS_APPLIED; delete res.body.data.id; delete hacker.id; chai.assert.deepStrictEqual( res.body.data, - JSON.parse(JSON.stringify(hacker)), + hacker, "objects do not match" ); @@ -483,13 +483,13 @@ describe("POST create hacker", function() { // create JSON version of model // delete id as they will be different between model objects // update status to be applied on the comparator hacker object - const hacker = new Hacker(newHacker0).toJSON(); + const hacker = new Hacker(newHacker0).toJSON({ flattenObjectIds: true }); hacker.status = Constants.General.HACKER_STATUS_APPLIED; delete res.body.data.id; delete hacker.id; chai.assert.deepStrictEqual( res.body.data, - JSON.parse(JSON.stringify(hacker)), + hacker, ); done(); });