Merge pull request #3259 from overleaf/jpa-hotfix-object-id-detection

[misc] fix detection of native and mongoose ObjectIds

GitOrigin-RevId: 98e2f3f009061e7cce9948341ebd5dc42d613448
This commit is contained in:
Jakob Ackermann 2020-10-06 12:31:11 +02:00 committed by Copybot
parent db422ecafa
commit 9f68193876
4 changed files with 16 additions and 1 deletions

View file

@ -14,6 +14,7 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const { db, ObjectId } = require('../../infrastructure/mongodb')
const { ObjectId: MongooseObjectId } = require('mongoose').mongo
const OError = require('@overleaf/o-error')
const metrics = require('metrics-sharelatex')
const async = require('async')
@ -106,6 +107,8 @@ const ProjectGetter = {
query = { _id: ObjectId(project_id) }
} else if (project_id instanceof ObjectId) {
query = { _id: project_id }
} else if (project_id instanceof MongooseObjectId) {
query = { _id: ObjectId(project_id.toString()) }
} else if (
(project_id != null ? project_id.toString().length : undefined) === 24
) {

View file

@ -1,4 +1,5 @@
const { db, ObjectId } = require('../../infrastructure/mongodb')
const { ObjectId: MongooseObjectId } = require('mongoose').mongo
const metrics = require('metrics-sharelatex')
const logger = require('logger-sharelatex')
const { promisifyAll } = require('../../util/promises')
@ -154,6 +155,8 @@ function normalizeQuery(query) {
}
if (typeof query === 'string') {
return { _id: ObjectId(query) }
} else if (query instanceof MongooseObjectId) {
return { _id: ObjectId(query.toString()) }
} else if (query instanceof ObjectId) {
return { _id: query }
} else if (Array.isArray(query)) {

View file

@ -1,6 +1,7 @@
const logger = require('logger-sharelatex')
const OError = require('@overleaf/o-error')
const { db, ObjectId } = require('../../infrastructure/mongodb')
const { ObjectId: MongooseObjectId } = require('mongoose').mongo
const metrics = require('metrics-sharelatex')
const async = require('async')
const { callbackify, promisify } = require('util')
@ -196,6 +197,8 @@ const UserUpdater = {
query = { _id: ObjectId(query) }
} else if (query instanceof ObjectId) {
query = { _id: query }
} else if (query instanceof MongooseObjectId) {
query = { _id: ObjectId(query.toString()) }
} else if (typeof query._id === 'string') {
query._id = ObjectId(query._id)
}

View file

@ -12,6 +12,7 @@
*/
let UserMembershipViewModel
const { ObjectId } = require('mongodb')
const { ObjectId: MongooseObjectId } = require('mongoose').mongo
const UserGetter = require('../User/UserGetter')
module.exports = UserMembershipViewModel = {
@ -27,7 +28,12 @@ module.exports = UserMembershipViewModel = {
if (callback == null) {
callback = function(error, viewModel) {}
}
if (!(userOrIdOrEmail instanceof ObjectId)) {
if (
!(
userOrIdOrEmail instanceof ObjectId ||
userOrIdOrEmail instanceof MongooseObjectId
)
) {
// userOrIdOrEmail is a user or an email and can be parsed by #build
return callback(null, UserMembershipViewModel.build(userOrIdOrEmail))
}