Skip to content

Commit

Permalink
inventory view cache: refresh when creating new item
Browse files Browse the repository at this point in the history
  • Loading branch information
jum-s committed Aug 31, 2020
1 parent 73f258d commit 9391b02
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions server/controllers/items/lib/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const getByAuthorizationLevel = require('./get_by_authorization_level')
const user_ = __.require('controllers', 'user/lib/user')
const db = __.require('couch', 'base')('items')
const validateEntityType = require('./validate_entity_type')
const { refreshInventoryViews } = require('./view/inventory_view')

const items_ = module.exports = {
byId: db.get,
Expand Down Expand Up @@ -75,6 +76,7 @@ const items_ = module.exports = {
const res = await db.bulk(items)
const itemsIds = _.map(res, 'id')
const { docs } = await db.fetch(itemsIds)
await refreshInventoryViews({ usersIds: [ userId ], items })
emit('user:inventory:update', userId)
return docs
},
Expand Down
12 changes: 12 additions & 0 deletions server/controllers/items/lib/view/inventory_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ const replaceEditionsByTheirWork = require('./replace_editions_by_their_work')
const getEntitiesByUris = __.require('controllers', 'entities/lib/get_entities_by_uris')

const inventoryView_ = module.exports = {
refreshInventoryViews: ({ usersIds, items }) => {
// refresh caches assuming usersIds are items owner, beware if not
const listings = _.map(items, _.property('listing'))
const authorizationLevels = _.uniq(listings)
return Promise.all(authorizationLevels.map(authorizationLevel => {
return inventoryView_.getInventoryViews({
usersIds,
authorizationLevel,
refresh: true
})
}))
},
getInventoryViews: ({ usersIds, authorizationLevel, refresh }) => {
return Promise.all(usersIds.map(userId => {
return getInventoryView({ userId, authorizationLevel, refresh })
Expand Down
28 changes: 26 additions & 2 deletions tests/api/items/inventory_view.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const CONFIG = require('config')
const __ = CONFIG.universalPath
const should = require('should')
const { nonAuthReq, undesiredRes, getReservedUser } = __.require('apiTests', 'utils/utils')
const { customAuthReq, nonAuthReq, undesiredRes, getReservedUser } = __.require('apiTests', 'utils/utils')
const endpoint = '/api/items?action=inventory-view'
const { groupPromise, createGroup, addMember } = require('../fixtures/groups')
const { createItem } = require('../fixtures/items')
const { createEdition } = require('../fixtures/entities')
const { createUserWithItems } = require('../fixtures/populate')
const { createItem } = require('../fixtures/items')
const editionUriPromise = createEdition().then(({ uri }) => uri)
const userPromise = createUserWithItems()

describe('items:inventory-view', () => {
it('should reject requests without a user or a group', done => {
Expand All @@ -20,6 +22,28 @@ describe('items:inventory-view', () => {
.catch(done)
})

describe('cache update', () => {
it('should refresh when a public new item is created', async () => {
const { _id: userId } = await userPromise
const { itemsByDate: oldItemsByDate } = await nonAuthReq('get', `${endpoint}&user=${userId}`)
const editionUri = await editionUriPromise

await customAuthReq(userPromise, 'post', '/api/items', { entity: editionUri, listing: 'public' })
const { itemsByDate: refreshedItemsByDate } = await nonAuthReq('get', `${endpoint}&user=${userId}`)
refreshedItemsByDate.length.should.equal(oldItemsByDate.length + 1)
})

it('should refresh when a private new item is created', async () => {
const { _id: userId } = await userPromise
const { itemsByDate: oldItemsByDate } = await nonAuthReq('get', `${endpoint}&user=${userId}`)
const editionUri = await editionUriPromise

await customAuthReq(userPromise, 'post', '/api/items', { entity: editionUri, listing: 'private' })
const { itemsByDate: ownerViewFreshItems } = await customAuthReq(userPromise, 'get', `${endpoint}&user=${userId}`)
ownerViewFreshItems.length.should.equal(oldItemsByDate.length + 1)
})
})

describe('user', () => {
it('should return a user inventory-view', async () => {
const { _id: userId } = await createUserWithItems()
Expand Down

0 comments on commit 9391b02

Please sign in to comment.