-
Notifications
You must be signed in to change notification settings - Fork 3
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
add import languages #8
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 143 additions & 4 deletions
147
src/commands/importExportEntities/entities/languages.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,150 @@ | ||
import { LanguageContracts } from "@kontent-ai/management-sdk"; | ||
import { LanguageContracts, LanguageModels, ManagementClient } from "@kontent-ai/management-sdk"; | ||
|
||
import { serially } from "../../../utils/requests.js"; | ||
import { notNull } from "../../../utils/typeguards.js"; | ||
import { EntityDefinition } from "../entityDefinition.js"; | ||
|
||
export const languagesExportEntity: EntityDefinition<ReadonlyArray<LanguageContracts.ILanguageModelContract>> = { | ||
const defaultLanguageId = '00000000-0000-0000-0000-000000000000'; | ||
|
||
export const languagesEntity: EntityDefinition<ReadonlyArray<LanguageContracts.ILanguageModelContract>> = { | ||
name: "languages", | ||
fetchEntities: client => client.listLanguages().toAllPromise().then(res => res.data.items.map(l => l._raw)), | ||
serializeEntities: collections => JSON.stringify(collections), | ||
importEntities: () => { throw new Error("Not supported yet.")}, | ||
deserializeEntities: () => { throw new Error("Not supported yet.")}, | ||
deserializeEntities: serialized => JSON.parse(serialized), | ||
importEntities: async (client, entities, context) => { | ||
const importDefaultLanguage = entities.find(l => l.id === defaultLanguageId) as LanguageContracts.ILanguageModelContract; | ||
const projectDefaultLangauge = await client.viewLanguage().byLanguageId(defaultLanguageId).toPromise().then(res => res.data); | ||
|
||
// Order is important | ||
await updateProjectLanguage(client, projectDefaultLangauge, importDefaultLanguage); | ||
await importLanguagesToProject(client, entities); | ||
|
||
const projectLanguages = await client.listLanguages().toAllPromise().then(res => res.data.items); | ||
|
||
await updateFallbackLanguages(client, projectLanguages, entities); | ||
|
||
return { | ||
...context, | ||
languageIdsByOldIds: new Map(entities | ||
.map(importLanguage => { | ||
const projectLanguage = projectLanguages.find(lang => lang.codename === importLanguage.codename); | ||
|
||
return projectLanguage ? [importLanguage.id, projectLanguage.id] as const : null; | ||
}) | ||
.filter(notNull) | ||
) | ||
} | ||
}, | ||
}; | ||
|
||
const createReplaceCodenameOperation = (codename: string): LanguageModels.IModifyLanguageData => ({ | ||
'op': 'replace', | ||
'property_name': 'codename', | ||
'value': codename | ||
}) | ||
|
||
const createReplaceNameOperation = (name: string): LanguageModels.IModifyLanguageData => ({ | ||
'op': 'replace', | ||
'property_name': 'name', | ||
'value': name | ||
}) | ||
|
||
const createReplaceFallbackLanguageOperation = (codename: string): LanguageModels.IModifyLanguageData => ({ | ||
'op': 'replace', | ||
'property_name': 'fallback_language', | ||
'value': { codename } | ||
}) | ||
|
||
const createReplaceIsActiveOperation = (isActive: boolean): LanguageModels.IModifyLanguageData => ({ | ||
'op': 'replace', | ||
'property_name': 'is_active', | ||
'value': isActive | ||
}) | ||
|
||
|
||
const updateProjectLanguage = async (client: ManagementClient, projectLanguage: LanguageModels.LanguageModel, importLanguage: LanguageContracts.ILanguageModelContract) => { | ||
const operations: LanguageModels.IModifyLanguageData[] = []; | ||
|
||
if (projectLanguage.codename !== importLanguage.codename) { | ||
operations.push(createReplaceCodenameOperation(importLanguage.codename)); | ||
} | ||
|
||
if (projectLanguage.name !== importLanguage.name) { | ||
operations.push(createReplaceNameOperation(importLanguage.name)); | ||
} | ||
|
||
if (projectLanguage.id !== defaultLanguageId && !projectLanguage.isActive) { | ||
operations.push(createReplaceIsActiveOperation(true)); | ||
} | ||
|
||
if (operations.length > 0) { | ||
return client | ||
.modifyLanguage() | ||
.byLanguageCodename(projectLanguage.codename) | ||
.withData(operations) | ||
.toPromise(); | ||
} | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
const importLanguagesToProject = async (client: ManagementClient, importLanguages: ReadonlyArray<LanguageContracts.ILanguageModelContract>) => { | ||
const projectLanguages = await client.listLanguages().toAllPromise().then(res => res.data.items); | ||
|
||
await serially(importLanguages | ||
.filter(l => l.id !== defaultLanguageId) | ||
.map(importLanguage => () => { | ||
const projectLanguage = projectLanguages.find(l => l.codename === importLanguage.codename); | ||
|
||
const otherProjectLanguageMatchOnName = projectLanguages.find(l => l.name === importLanguage.name && l.codename !== importLanguage.codename); | ||
|
||
if (otherProjectLanguageMatchOnName) { | ||
throw new Error(`Could not update name of the language with codename ${importLanguage.codename}. The other language with codename ${otherProjectLanguageMatchOnName.codename} has the same name. Fix your languages names to continue`); | ||
} | ||
|
||
if (projectLanguage) { | ||
return updateProjectLanguage(client, projectLanguage, importLanguage) | ||
} | ||
|
||
return client | ||
.addLanguage() | ||
.withData({ | ||
name: importLanguage.name, | ||
codename: importLanguage.codename, | ||
is_active: true, | ||
external_id: importLanguage.external_id ?? `${importLanguage.codename}`.slice(0, 50), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you put codename into the template string, codename is a string on its own. 😄 |
||
}) | ||
.toPromise(); | ||
})) | ||
} | ||
|
||
|
||
const updateFallbackLanguages = async (client: ManagementClient, projectLanguages: LanguageModels.LanguageModel[], importLanguages: ReadonlyArray<LanguageContracts.ILanguageModelContract>) => { | ||
await serially(importLanguages | ||
.filter(lang => lang.id !== defaultLanguageId) | ||
.map(importLanguage => { | ||
const projectLanguage = projectLanguages.find(l => l.codename === importLanguage.codename); | ||
if (!projectLanguage) { | ||
throw new Error(`Could not find the language with codename ${importLanguage.codename} in the project.`); | ||
} | ||
return [importLanguage, projectLanguage] as const; | ||
}) | ||
.map(([importLanguage, projectLanguage]) => () => { | ||
const projectFallbackLanguage = projectLanguages.find(l => l.id === projectLanguage.fallbackLanguage?.id); | ||
const importFallbackLanguage = importLanguages.find(l => l.id === importLanguage.fallback_language?.id); | ||
|
||
if (projectFallbackLanguage && importFallbackLanguage && projectFallbackLanguage.codename !== importFallbackLanguage.codename) { | ||
return client | ||
.modifyLanguage() | ||
.byLanguageCodename(importLanguage.codename) | ||
.withData([ | ||
createReplaceFallbackLanguageOperation(importFallbackLanguage.codename) | ||
]) | ||
.toPromise(); | ||
} | ||
|
||
return Promise.resolve(); | ||
}) | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need the
async
keyword here.