fix(next, payload): enable relationship & upload version tracking when localization enabled (#7508)

This commit is contained in:
Patrik
2024-08-06 12:28:06 -04:00
committed by GitHub
parent e8bed7b315
commit 62744e79ac
9 changed files with 237 additions and 43 deletions

View File

@@ -12,3 +12,4 @@ export const collection2Slug = 'collection-2'
export const videoCollectionSlug = 'videos'
export const podcastCollectionSlug = 'podcasts'
export const mixedMediaCollectionSlug = 'mixed-media'
export const versionedRelationshipFieldSlug = 'versioned-relationship-field'

View File

@@ -0,0 +1,21 @@
import type { CollectionConfig } from 'payload'
import { collection1Slug, versionedRelationshipFieldSlug } from '../../collectionSlugs.js'
export const VersionedRelationshipFieldCollection: CollectionConfig = {
slug: versionedRelationshipFieldSlug,
fields: [
{
name: 'title',
type: 'text',
required: true,
},
{
name: 'relationshipField',
type: 'relationship',
relationTo: [collection1Slug],
hasMany: true,
},
],
versions: true,
}

View File

@@ -24,6 +24,7 @@ import {
slug,
videoCollectionSlug,
} from './collectionSlugs.js'
import { VersionedRelationshipFieldCollection } from './collections/VersionedRelationshipField/index.js'
export interface FieldsRelationship {
createdAt: Date
@@ -321,6 +322,9 @@ export default buildConfigWithDefaults({
},
],
slug: collection1Slug,
admin: {
useAsTitle: 'name',
},
},
{
fields: [
@@ -376,7 +380,13 @@ export default buildConfigWithDefaults({
},
],
},
VersionedRelationshipFieldCollection,
],
localization: {
locales: ['en'],
defaultLocale: 'en',
fallback: true,
},
onInit: async (payload) => {
await payload.create({
collection: 'users',

View File

@@ -0,0 +1,101 @@
import type { Payload } from 'payload'
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
import type { Collection1 } from './payload-types.js'
import { devUser } from '../credentials.js'
import { initPayloadInt } from '../helpers/initPayloadInt.js'
import { collection1Slug, versionedRelationshipFieldSlug } from './collectionSlugs.js'
import configPromise from './config.js'
let payload: Payload
let restClient: NextRESTClient
const { email, password } = devUser
describe('Relationship Fields', () => {
beforeAll(async () => {
const initialized = await initPayloadInt(configPromise)
;({ payload, restClient } = initialized)
await restClient.login({
slug: 'users',
credentials: {
email,
password,
},
})
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('Versioned Relationship Field', () => {
let version2ID: string
const relatedDocName = 'Related Doc'
beforeAll(async () => {
const relatedDoc = await payload.create({
collection: collection1Slug,
data: {
name: relatedDocName,
},
})
const version1 = await payload.create({
collection: versionedRelationshipFieldSlug,
data: {
title: 'Version 1 Title',
relationshipField: {
value: relatedDoc.id,
relationTo: collection1Slug,
},
},
})
const version2 = await payload.update({
collection: versionedRelationshipFieldSlug,
id: version1.id,
data: {
title: 'Version 2 Title',
},
})
const versions = await payload.findVersions({
collection: versionedRelationshipFieldSlug,
where: {
parent: {
equals: version2.id,
},
},
sort: '-updatedAt',
limit: 1,
})
version2ID = versions.docs[0].id
})
it('should return the correct versioned relationship field via REST', async () => {
const version2Data = await restClient
.GET(`/${versionedRelationshipFieldSlug}/versions/${version2ID}?locale=all`)
.then((res) => res.json())
expect(version2Data.version.title).toEqual('Version 2 Title')
expect(version2Data.version.relationshipField[0].value.name).toEqual(relatedDocName)
})
it('should return the correct versioned relationship field via LocalAPI', async () => {
const version2Data = await payload.findVersionByID({
collection: versionedRelationshipFieldSlug,
id: version2ID,
locale: 'all',
})
expect(version2Data.version.title).toEqual('Version 2 Title')
expect((version2Data.version.relationshipField[0].value as Collection1).name).toEqual(
relatedDocName,
)
})
})
})

View File

@@ -24,12 +24,16 @@ export interface Config {
videos: Video;
podcasts: Podcast;
'mixed-media': MixedMedia;
'versioned-relationship-field': VersionedRelationshipField;
users: User;
'payload-preferences': PayloadPreference;
'payload-migrations': PayloadMigration;
};
db: {
defaultIDType: string;
};
globals: {};
locale: null;
locale: 'en';
user: User & {
collection: 'users';
};
@@ -37,15 +41,20 @@ export interface Config {
export interface UserAuthOperations {
forgotPassword: {
email: string;
password: string;
};
login: {
password: string;
email: string;
password: string;
};
registerFirstUser: {
email: string;
password: string;
};
unlock: {
email: string;
password: string;
};
}
/**
* This interface was referenced by `Config`'s JSON-Schema
@@ -252,6 +261,22 @@ export interface MixedMedia {
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "versioned-relationship-field".
*/
export interface VersionedRelationshipField {
id: string;
title: string;
relationshipField?:
| {
relationTo: 'collection-1';
value: string | Collection1;
}[]
| null;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "users".