mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-07-07 09:51:37 +00:00
fix postgres legacy user id lookup
This commit is contained in:
parent
cb31fd34d5
commit
6227a60fed
2 changed files with 45 additions and 1 deletions
|
|
@ -422,9 +422,11 @@ class User extends Model {
|
|||
const cachedUser = userCache.getById(userId) || userCache.getByOldId(userId)
|
||||
if (cachedUser) return cachedUser
|
||||
|
||||
const idMatcher = this.sequelize.getDialect() === 'postgres' ? sequelize.where(sequelize.cast(sequelize.col('user.id'), 'text'), userId) : { id: userId }
|
||||
|
||||
const user = await this.findOne({
|
||||
where: {
|
||||
[sequelize.Op.or]: [{ id: userId }, { 'extraData.oldUserId': userId }]
|
||||
[sequelize.Op.or]: [idMatcher, { 'extraData.oldUserId': userId }]
|
||||
},
|
||||
include: this.sequelize.models.mediaProgress
|
||||
})
|
||||
|
|
|
|||
42
test/server/models/User.test.js
Normal file
42
test/server/models/User.test.js
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
const { expect } = require('chai')
|
||||
const sinon = require('sinon')
|
||||
const sequelize = require('sequelize')
|
||||
|
||||
const User = require('../../../server/models/User')
|
||||
|
||||
describe('User model', () => {
|
||||
describe('getUserByIdOrOldId', () => {
|
||||
let originalSequelize
|
||||
|
||||
beforeEach(() => {
|
||||
originalSequelize = User.sequelize
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
User.sequelize = originalSequelize
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('should cast id to text on postgres when checking legacy token ids', async () => {
|
||||
User.sequelize = {
|
||||
getDialect: () => 'postgres',
|
||||
models: {
|
||||
mediaProgress: {}
|
||||
}
|
||||
}
|
||||
|
||||
const findOneStub = sinon.stub(User, 'findOne').resolves(null)
|
||||
|
||||
await User.getUserByIdOrOldId('root')
|
||||
|
||||
expect(findOneStub.calledOnce).to.equal(true)
|
||||
|
||||
const options = findOneStub.firstCall.args[0]
|
||||
const whereOr = options.where[sequelize.Op.or]
|
||||
|
||||
expect(whereOr).to.have.length(2)
|
||||
expect(whereOr[0].attribute.type).to.equal('text')
|
||||
expect(whereOr[1]).to.deep.equal({ 'extraData.oldUserId': 'root' })
|
||||
})
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue