From 78eed77ec7a3fbd0d13a6abbfca1059f5843abfe Mon Sep 17 00:00:00 2001 From: Steven Thompson <44806974+thompsonsj@users.noreply.github.com> Date: Mon, 3 Jul 2023 11:23:06 +0100 Subject: [PATCH] fix(files): correct parameter for file update/delete (#57) --- dev/src/tests/collections.test.ts | 17 -- dev/src/tests/files.test.ts | 261 ++++++++++++++------------ src/api/payload-crowdin-sync/files.ts | 6 +- 3 files changed, 143 insertions(+), 141 deletions(-) diff --git a/dev/src/tests/collections.test.ts b/dev/src/tests/collections.test.ts index 8e183f8..b1805b0 100644 --- a/dev/src/tests/collections.test.ts +++ b/dev/src/tests/collections.test.ts @@ -194,21 +194,4 @@ describe('Collections', () => { expect(postOneRefreshed.crowdinArticleDirectory.crowdinCollectionDirectory).toEqual(postTwoRefreshed.crowdinArticleDirectory.crowdinCollectionDirectory) }) }) - - describe('crowdin-files: on update', () => { - it('updates the `fields` file if a text field has changed', async () => { - const post = await payload.create({ - collection: collections.localized, - data: { title: 'Test post' }, - }); - const file = await getFileByDocumentID('fields', post.id, payload) - const updatedPost = await payload.update({ - id: post.id, - collection: collections.localized, - data: { title: 'Test post updated' }, - }); - const updatedFile = await getFileByDocumentID('fields', post.id, payload) - expect(file.updatedAt).not.toEqual(updatedFile.updatedAt) - }) - }) }); diff --git a/dev/src/tests/files.test.ts b/dev/src/tests/files.test.ts index 8e94db3..6bac3b0 100644 --- a/dev/src/tests/files.test.ts +++ b/dev/src/tests/files.test.ts @@ -1,6 +1,6 @@ import payload from 'payload'; import { initPayloadTest } from './helpers/config'; -import { getFilesByDocumentID } from '../../../dist/api/helpers'; +import { getFileByDocumentID, getFilesByDocumentID } from '../../../dist/api/helpers'; /** * Test files @@ -18,133 +18,152 @@ const collections = { nestedFields: 'nested-field-collection' } -describe(`Collection: ${collections.nestedFields}`, () => { +describe(`CrowdIn file create, update and delete`, () => { beforeEach(async () => { await initPayloadTest({ __dirname }); }); - it('does not create files for empty localized fields', async () => { - const article = await payload.create({ - collection: collections.nestedFields, - data: {}, - }); - - const crowdInFiles = await getFilesByDocumentID(article.id, payload) - expect(crowdInFiles.length).toEqual(0) + describe(`Collection: ${collections.localized}`, () => { + it('updates the `fields` file if a text field has changed', async () => { + const post = await payload.create({ + collection: collections.localized, + data: { title: 'Test post' }, + }); + const file = await getFileByDocumentID('fields', post.id, payload) + const updatedPost = await payload.update({ + id: post.id, + collection: collections.localized, + data: { title: 'Test post updated' }, + }); + const updatedFile = await getFileByDocumentID('fields', post.id, payload) + expect(file.updatedAt).not.toEqual(updatedFile.updatedAt) + }) }) - /** - * TODO: Fix this test - * - * ValidationError: nested-field-collection validation failed: title.en: Cast to string failed for value "{ en: 'Test title' }" (type Object) at path "en" - * model.Object..Document.invalidate (dev/node_modules/mongoose/lib/document.js:3050:32) - * / - it('creates files containing fieldType content', async () => { - const article = await payload.create({ - collection: collections.nestedFields, - locale: "en", - data: { - title: 'Test title', - content: [ - { - children: [ - { - text: "Test content" - } - ] - } - ], - metaDescription: 'Test meta description', - }, - }); - const crowdInFiles = await getFilesByDocumentID(article.id, payload) - expect(crowdInFiles.length).toEqual(2) - expect(crowdInFiles.find((file) => file.name === 'content.html')).toBeDefined() - expect(crowdInFiles.find((file) => file.name === 'fields.json')).toBeDefined() - }) - */ + describe(`Collection: ${collections.nestedFields}`, () => { + it('does not create files for empty localized fields', async () => { + const article = await payload.create({ + collection: collections.nestedFields, + data: {}, + }); - it('creates files containing `array` fieldType content', async () => { - const article = await payload.create({ - collection: collections.nestedFields, - data: { - arrayField: [ - { - title: 'Test title 1', - content: [ - { - children: [ - { - text: "Test content 1" - } - ] - } - ], - metaDescription: 'Test meta description 1', - }, - { - title: 'Test title 2', - content: [ - { - children: [ - { - text: "Test content 2" - } - ] - } - ], - metaDescription: 'Test meta description 2', - }, - ], - }, - }); - const crowdInFiles = await getFilesByDocumentID(article.id, payload) - expect(crowdInFiles.length).toEqual(3) - expect(crowdInFiles.find((file) => file.name === 'arrayField[0].content.html')).toBeDefined() - expect(crowdInFiles.find((file) => file.name === 'arrayField[1].content.html')).toBeDefined() - expect(crowdInFiles.find((file) => file.name === 'fields.json')).toBeDefined() - }) + const crowdInFiles = await getFilesByDocumentID(article.id, payload) + expect(crowdInFiles.length).toEqual(0) + }) + + /** + * TODO: Fix this test + * + * ValidationError: nested-field-collection validation failed: title.en: Cast to string failed for value "{ en: 'Test title' }" (type Object) at path "en" + * model.Object..Document.invalidate (dev/node_modules/mongoose/lib/document.js:3050:32) + * / + it('creates files containing fieldType content', async () => { + const article = await payload.create({ + collection: collections.nestedFields, + locale: "en", + data: { + title: 'Test title', + content: [ + { + children: [ + { + text: "Test content" + } + ] + } + ], + metaDescription: 'Test meta description', + }, + }); + const crowdInFiles = await getFilesByDocumentID(article.id, payload) + expect(crowdInFiles.length).toEqual(2) + expect(crowdInFiles.find((file) => file.name === 'content.html')).toBeDefined() + expect(crowdInFiles.find((file) => file.name === 'fields.json')).toBeDefined() + }) + */ + + it('creates files containing `array` fieldType content', async () => { + const article = await payload.create({ + collection: collections.nestedFields, + data: { + arrayField: [ + { + title: 'Test title 1', + content: [ + { + children: [ + { + text: "Test content 1" + } + ] + } + ], + metaDescription: 'Test meta description 1', + }, + { + title: 'Test title 2', + content: [ + { + children: [ + { + text: "Test content 2" + } + ] + } + ], + metaDescription: 'Test meta description 2', + }, + ], + }, + }); + const crowdInFiles = await getFilesByDocumentID(article.id, payload) + expect(crowdInFiles.length).toEqual(3) + expect(crowdInFiles.find((file) => file.name === 'arrayField[0].content.html')).toBeDefined() + expect(crowdInFiles.find((file) => file.name === 'arrayField[1].content.html')).toBeDefined() + expect(crowdInFiles.find((file) => file.name === 'fields.json')).toBeDefined() + }) - it('creates files containing `blocks` fieldType content', async () => { - const article = await payload.create({ - collection: collections.nestedFields, - data: { - layout: [ - { - title: 'Test title 1', - content: [ - { - children: [ - { - text: "Test content 1" - } - ] - } - ], - metaDescription: 'Test meta description 1', - blockType: 'basicBlock' - }, - { - title: 'Test title 2', - content: [ - { - children: [ - { - text: "Test content 2" - } - ] - } - ], - metaDescription: 'Test meta description 2', - blockType: 'basicBlock' - }, - ], - }, - }); - const crowdInFiles = await getFilesByDocumentID(article.id, payload) - expect(crowdInFiles.length).toEqual(3) - expect(crowdInFiles.find((file) => file.name === 'layout[0].content.html')).toBeDefined() - expect(crowdInFiles.find((file) => file.name === 'layout[1].content.html')).toBeDefined() - expect(crowdInFiles.find((file) => file.name === 'fields.json')).toBeDefined() + it('creates files containing `blocks` fieldType content', async () => { + const article = await payload.create({ + collection: collections.nestedFields, + data: { + layout: [ + { + title: 'Test title 1', + content: [ + { + children: [ + { + text: "Test content 1" + } + ] + } + ], + metaDescription: 'Test meta description 1', + blockType: 'basicBlock' + }, + { + title: 'Test title 2', + content: [ + { + children: [ + { + text: "Test content 2" + } + ] + } + ], + metaDescription: 'Test meta description 2', + blockType: 'basicBlock' + }, + ], + }, + }); + const crowdInFiles = await getFilesByDocumentID(article.id, payload) + expect(crowdInFiles.length).toEqual(3) + expect(crowdInFiles.find((file) => file.name === 'layout[0].content.html')).toBeDefined() + expect(crowdInFiles.find((file) => file.name === 'layout[1].content.html')).toBeDefined() + expect(crowdInFiles.find((file) => file.name === 'fields.json')).toBeDefined() + }) }) }); diff --git a/src/api/payload-crowdin-sync/files.ts b/src/api/payload-crowdin-sync/files.ts index 0215493..48f7698 100644 --- a/src/api/payload-crowdin-sync/files.ts +++ b/src/api/payload-crowdin-sync/files.ts @@ -8,7 +8,7 @@ import { isEmpty } from 'lodash'; interface IcrowdinFile { id: string - fileId: number + originalId: number } interface IfindOrCreateCollectionDirectory { @@ -242,7 +242,7 @@ export class payloadCrowdInSyncFilesApi { }: IupdateFile) { // Update file on CrowdIn const updatedCrowdInFile = await this.crowdInUpdateFile({ - fileId: crowdInFile.fileId, + fileId: crowdInFile.originalId, name, fileData, fileType, @@ -320,7 +320,7 @@ export class payloadCrowdInSyncFilesApi { } private async deleteFile(crowdInFile: IcrowdinFile) { - const file = await this.sourceFilesApi.deleteFile(this.projectId, crowdInFile.fileId) + const file = await this.sourceFilesApi.deleteFile(this.projectId, crowdInFile.originalId) const payloadFile = await this.payload.delete({ collection: 'crowdin-files', // required id: crowdInFile.id, // required