Skip to content

Commit 852f9fc

Browse files
fix!: multiple preferences for the same user and entry (#8100)
fixes #7762 This change mitigates having multiple preferences for one user but not awaiting the change to a preference and reduces querying by skipping the access control. In the event that a user has multiple preferences with the same key, only the one with the latest updatedAt will be returned. BREAKING CHANGES: - payload/preferences/operations are no longer default exports ## Description <!-- Please include a summary of the pull request and any related issues it fixes. Please also include relevant motivation and context. --> - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change <!-- Please delete options that are not relevant. --> - [ ] Chore (non-breaking change which does not add functionality) - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Change to the [templates](https://github.com/payloadcms/payload/tree/main/templates) directory (does not affect core functionality) - [ ] Change to the [examples](https://github.com/payloadcms/payload/tree/main/examples) directory (does not affect core functionality) - [ ] This change requires a documentation update ## Checklist: - [ ] I have added tests that prove my fix is effective or that my feature works - [x] Existing test suite passes locally with my changes - [ ] I have made corresponding changes to the documentation --------- Co-authored-by: Paul Popus <paul@nouance.io>
1 parent e2d8038 commit 852f9fc

File tree

6 files changed

+24
-34
lines changed

6 files changed

+24
-34
lines changed

packages/payload/src/preferences/operations/delete.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import type { Document, Where } from '../../types/index.js'
22
import type { PreferenceRequest } from '../types.js'
33

4-
import defaultAccess from '../../auth/defaultAccess.js'
5-
import executeAccess from '../../auth/executeAccess.js'
64
import { NotFound } from '../../errors/NotFound.js'
75
import { UnauthorizedError } from '../../errors/UnathorizedError.js'
86

9-
async function deleteOperation(args: PreferenceRequest): Promise<Document> {
7+
export async function deleteOperation(args: PreferenceRequest): Promise<Document> {
108
const {
119
key,
12-
overrideAccess,
1310
req: { payload },
1411
req,
1512
user,
@@ -19,10 +16,6 @@ async function deleteOperation(args: PreferenceRequest): Promise<Document> {
1916
throw new UnauthorizedError(req.t)
2017
}
2118

22-
if (!overrideAccess) {
23-
await executeAccess({ req }, defaultAccess)
24-
}
25-
2619
const where: Where = {
2720
and: [
2821
{ key: { equals: key } },
@@ -42,5 +35,3 @@ async function deleteOperation(args: PreferenceRequest): Promise<Document> {
4235
}
4336
throw new NotFound(req.t)
4437
}
45-
46-
export default deleteOperation

packages/payload/src/preferences/operations/findOne.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { TypedCollection } from '../../index.js'
22
import type { Where } from '../../types/index.js'
33
import type { PreferenceRequest } from '../types.js'
44

5-
async function findOne(args: PreferenceRequest): Promise<TypedCollection['_preference']> {
5+
export async function findOne(args: PreferenceRequest): Promise<TypedCollection['_preference']> {
66
const {
77
key,
88
req: { payload },
@@ -22,11 +22,14 @@ async function findOne(args: PreferenceRequest): Promise<TypedCollection['_prefe
2222
],
2323
}
2424

25-
return await payload.db.findOne({
25+
const { docs } = await payload.db.find({
2626
collection: 'payload-preferences',
27+
limit: 1,
28+
pagination: false,
2729
req,
30+
sort: '-updatedAt',
2831
where,
2932
})
30-
}
3133

32-
export default findOne
34+
return docs?.[0] || null
35+
}
Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1+
import type { Where } from '../../types/index.js'
12
import type { PreferenceUpdateRequest } from '../types.js'
23

3-
import defaultAccess from '../../auth/defaultAccess.js'
4-
import executeAccess from '../../auth/executeAccess.js'
54
import { UnauthorizedError } from '../../errors/UnathorizedError.js'
65

7-
async function update(args: PreferenceUpdateRequest) {
6+
export async function update(args: PreferenceUpdateRequest) {
87
const {
98
key,
10-
overrideAccess,
119
req: { payload },
1210
req,
1311
user,
@@ -20,10 +18,12 @@ async function update(args: PreferenceUpdateRequest) {
2018

2119
const collection = 'payload-preferences'
2220

23-
const filter = {
24-
key: { equals: key },
25-
'user.relationTo': { equals: user.collection },
26-
'user.value': { equals: user.id },
21+
const where: Where = {
22+
and: [
23+
{ key: { equals: key } },
24+
{ 'user.value': { equals: user.id } },
25+
{ 'user.relationTo': { equals: user.collection } },
26+
],
2727
}
2828

2929
const preference = {
@@ -35,27 +35,23 @@ async function update(args: PreferenceUpdateRequest) {
3535
value,
3636
}
3737

38-
if (!overrideAccess) {
39-
await executeAccess({ req }, defaultAccess)
40-
}
38+
let result
4139

4240
try {
4341
// try/catch because we attempt to update without first reading to check if it exists first to save on db calls
44-
await payload.db.updateOne({
42+
result = await payload.db.updateOne({
4543
collection,
4644
data: preference,
4745
req,
48-
where: filter,
46+
where,
4947
})
5048
} catch (err: unknown) {
51-
await payload.db.create({
49+
result = await payload.db.create({
5250
collection,
5351
data: preference,
5452
req,
5553
})
5654
}
5755

58-
return preference
56+
return result
5957
}
60-
61-
export default update

packages/payload/src/preferences/requestHandlers/delete.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import httpStatus from 'http-status'
33
import type { PayloadHandler } from '../../config/types.js'
44
import type { PayloadRequest } from '../../types/index.js'
55

6-
import deleteOperation from '../operations/delete.js'
6+
import { deleteOperation } from '../operations/delete.js'
77

88
export const deleteHandler: PayloadHandler = async (incomingReq): Promise<Response> => {
99
// We cannot import the addDataAndFileToRequest utility here from the 'next' package because of dependency issues

packages/payload/src/preferences/requestHandlers/findOne.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import httpStatus from 'http-status'
33
import type { PayloadHandler } from '../../config/types.js'
44
import type { PayloadRequest } from '../../types/index.js'
55

6-
import findOne from '../operations/findOne.js'
6+
import { findOne } from '../operations/findOne.js'
77

88
export const findByIDHandler: PayloadHandler = async (incomingReq): Promise<Response> => {
99
// We cannot import the addDataAndFileToRequest utility here from the 'next' package because of dependency issues

packages/payload/src/preferences/requestHandlers/update.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import httpStatus from 'http-status'
33
import type { PayloadHandler } from '../../config/types.js'
44
import type { PayloadRequest } from '../../types/index.js'
55

6-
import update from '../operations/update.js'
6+
import { update } from '../operations/update.js'
77

88
export const updateHandler: PayloadHandler = async (incomingReq) => {
99
// We cannot import the addDataAndFileToRequest utility here from the 'next' package because of dependency issues

0 commit comments

Comments
 (0)