@@ -39,7 +39,7 @@ export const buildFormState = async ({
39
39
} : {
40
40
req : PayloadRequest
41
41
} ) : Promise < {
42
- lockedState ?: { isLocked : boolean ; user : ClientUser | number | string }
42
+ lockedState ?: { isLocked : boolean ; lastEditedAt : string ; user : ClientUser | number | string }
43
43
state : FormState
44
44
} > => {
45
45
const reqData : BuildFormStateArgs = ( req . data || { } ) as BuildFormStateArgs
@@ -242,7 +242,7 @@ export const buildFormState = async ({
242
242
}
243
243
} else if ( globalSlug ) {
244
244
lockedDocumentQuery = {
245
- globalSlug : { equals : globalSlug } ,
245
+ and : [ { globalSlug : { equals : globalSlug } } ] ,
246
246
}
247
247
}
248
248
@@ -275,6 +275,7 @@ export const buildFormState = async ({
275
275
if ( lockedDocument . docs && lockedDocument . docs . length > 0 ) {
276
276
const lockedState = {
277
277
isLocked : true ,
278
+ lastEditedAt : lockedDocument . docs [ 0 ] ?. updatedAt ,
278
279
user : lockedDocument . docs [ 0 ] ?. user ?. value ,
279
280
}
280
281
@@ -289,19 +290,32 @@ export const buildFormState = async ({
289
290
290
291
return { lockedState, state : result }
291
292
} else {
292
- // Delete Many Locks that are older than their updatedAt + lockDuration
293
293
// If NO ACTIVE lock document exists, first delete any expired locks and then create a fresh lock
294
294
// Where updatedAt is older than the duration that is specified in the config
295
- const deleteExpiredLocksQuery = {
296
- and : [
297
- { 'document.relationTo' : { equals : collectionSlug } } ,
298
- { 'document.value' : { equals : id } } ,
299
- {
300
- updatedAt : {
301
- less_than : new Date ( now - lockDurationInMilliseconds ) . toISOString ( ) ,
295
+ let deleteExpiredLocksQuery
296
+
297
+ if ( collectionSlug ) {
298
+ deleteExpiredLocksQuery = {
299
+ and : [
300
+ { 'document.relationTo' : { equals : collectionSlug } } ,
301
+ {
302
+ updatedAt : {
303
+ less_than : new Date ( now - lockDurationInMilliseconds ) . toISOString ( ) ,
304
+ } ,
302
305
} ,
303
- } ,
304
- ] ,
306
+ ] ,
307
+ }
308
+ } else if ( globalSlug ) {
309
+ deleteExpiredLocksQuery = {
310
+ and : [
311
+ { globalSlug : { equals : globalSlug } } ,
312
+ {
313
+ updatedAt : {
314
+ less_than : new Date ( now - lockDurationInMilliseconds ) . toISOString ( ) ,
315
+ } ,
316
+ } ,
317
+ ] ,
318
+ }
305
319
}
306
320
307
321
await req . payload . db . deleteMany ( {
@@ -330,6 +344,7 @@ export const buildFormState = async ({
330
344
331
345
const lockedState = {
332
346
isLocked : true ,
347
+ lastEditedAt : new Date ( ) . toISOString ( ) ,
333
348
user : req . user ,
334
349
}
335
350
0 commit comments