Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pluginfields): additional flag for translation sync checkboxes #188

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions dev/src/lib/tests/helpers/nock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import nock from "nock";
import { mockCrowdinClient } from "plugin/src/lib/api/mock/crowdin-api-responses";
import { pluginConfig } from "../helpers/plugin-config"

const pluginOptions = pluginConfig()
const mockClient = mockCrowdinClient(pluginOptions)

export function nockCreateSourceTranslationFiles({
createProjectDirectory = false,
fileCount = 1,
}) {
const directoryCount = createProjectDirectory ? 2 : 1

return nock('https://api.crowdin.com')
.post(
`/api/v2/projects/${pluginOptions.projectId}/directories`
)
.times(directoryCount)
.reply(200, mockClient.createDirectory({}))
.post(
`/api/v2/storages`
)
.times(fileCount)
.reply(200, mockClient.addStorage())
.post(
`/api/v2/projects/${pluginOptions.projectId}/files`
)
.times(fileCount)
.reply(200, mockClient.createFile({}))
}
33 changes: 6 additions & 27 deletions dev/src/lib/tests/translations/receive/virtual-fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import nock from "nock";
import { mockCrowdinClient } from "plugin/src/lib/api/mock/crowdin-api-responses";
import { pluginConfig } from "../../helpers/plugin-config"

import { nockCreateSourceTranslationFiles } from "../../helpers/nock"

/**
* Test virtual fields
*
Expand Down Expand Up @@ -44,20 +46,9 @@ describe("Virtual fields", () => {

describe("No database storage", () => {
it("does not store syncTranslations", async () => {
nock('https://api.crowdin.com')
.post(
`/api/v2/projects/${pluginOptions.projectId}/directories`
)
.twice()
.reply(200, mockClient.createDirectory({}))
.post(
`/api/v2/storages`
)
.reply(200, mockClient.addStorage())
.post(
`/api/v2/projects/${pluginOptions.projectId}/files`
)
.reply(200, mockClient.createFile({}))
nockCreateSourceTranslationFiles({
createProjectDirectory: true,
})

const post = await payload.create({
collection: "localized-posts",
Expand All @@ -75,19 +66,7 @@ describe("Virtual fields", () => {
});

it("does not store syncAllTranslations", async () => {
nock('https://api.crowdin.com')
.post(
`/api/v2/projects/${pluginOptions.projectId}/directories`
)
.reply(200, mockClient.createDirectory({}))
.post(
`/api/v2/storages`
)
.reply(200, mockClient.addStorage())
.post(
`/api/v2/projects/${pluginOptions.projectId}/files`
)
.reply(200, mockClient.createFile({}))
nockCreateSourceTranslationFiles({})

const post = await payload.create({
collection: "localized-posts",
Expand Down
7 changes: 4 additions & 3 deletions plugin/src/lib/fields/pluginFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ export const pluginCollectionOrGlobalFields = ({
context['articleDirectoryId'] = typeof siblingData["crowdinArticleDirectory"] === 'string' ? siblingData["crowdinArticleDirectory"] : siblingData["crowdinArticleDirectory"].id
context['draft'] = draft
context['excludeLocales'] = excludeLocales

context["syncTranslations"] = true
}
// Mutate the sibling data to prevent DB storage
// eslint-disable-next-line no-param-reassign
siblingData["syncTranslations"] = undefined;
}],
afterChange: [async ({ context, req }) => {
// type check context, if valid we can safely assume translation updates are desired
if (typeof context['articleDirectoryId'] === 'string' && typeof context['draft'] === 'boolean' && Array.isArray(context['excludeLocales']))
if (typeof context['articleDirectoryId'] === 'string' && typeof context['draft'] === 'boolean' && Array.isArray(context['excludeLocales']) && typeof context["syncTranslations"] === 'boolean')
await updatePayloadTranslation({
articleDirectoryId: context['articleDirectoryId'],
pluginOptions,
Expand Down Expand Up @@ -100,14 +100,15 @@ export const pluginCollectionOrGlobalFields = ({

context['articleDirectoryId'] = typeof siblingData["crowdinArticleDirectory"] === 'string' ? siblingData["crowdinArticleDirectory"] : siblingData["crowdinArticleDirectory"].id
context['draft'] = draft
context["syncAllTranslations"] = true
}
// Mutate the sibling data to prevent DB storage
// eslint-disable-next-line no-param-reassign
siblingData["syncAllTranslations"] = undefined;
}],
afterChange: [async ({ context, req }) => {
// type check context, if valid we can safely assume translation updates are desired
if (typeof context['articleDirectoryId'] === 'string' && typeof context['draft'] === 'boolean')
if (typeof context['articleDirectoryId'] === 'string' && typeof context['draft'] === 'boolean' && typeof context["syncAllTranslations"] === 'boolean')
await updatePayloadTranslation({
articleDirectoryId: context['articleDirectoryId'],
pluginOptions,
Expand Down
Loading