Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ For the given (source) content type, transforms all its entries according to the
- **`sourceContentType : string`** _(required)_ – Content type ID of source entries
- **`targetContentType : string`** _(required)_ – Targeted Content type ID
- **`from : array`** _(optional)_ – Array of the source field IDs, returns complete list of fields if not configured
- **`entryIds : array`** _(optional)_ – Array of entry IDs to limit transformation to specific entries. If not provided, all entries of the source content type will be transformed
- **`identityKey: function (fields): string`** _(required)_ - Function to create a new entry ID for the target entry
- **`shouldPublish : bool | 'preserve'`** _(optional)_ – Flag that specifies publishing of target entries, `preserve` will keep current states of the source entries (default `false`)
- **`updateReferences : bool`** _(optional)_ – Flag that specifies if linking entries should be updated with target entries (default `false`). Note that this flag does not support Rich Text Fields references.
Expand Down
7 changes: 6 additions & 1 deletion src/lib/action/entry-transform-to-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class EntryTransformToTypeAction extends APIAction {
private fromFields?: string[]
private sourceContentTypeId: string
private targetContentTypeId: string
private entryIds?: string[]
private transformEntryForLocale: (
inputFields: any,
locale: string,
Expand All @@ -25,6 +26,7 @@ class EntryTransformToTypeAction extends APIAction {
this.fromFields = entryTransformation.from
this.sourceContentTypeId = entryTransformation.sourceContentType
this.targetContentTypeId = entryTransformation.targetContentType
this.entryIds = entryTransformation.entryIds
this.identityKey = entryTransformation.identityKey
this.shouldPublish = entryTransformation.shouldPublish || false
this.removeOldEntries = entryTransformation.removeOldEntries || false
Expand All @@ -34,7 +36,10 @@ class EntryTransformToTypeAction extends APIAction {
}

async applyTo(api: OfflineAPI) {
const entries: Entry[] = await api.getEntriesForContentType(this.sourceContentTypeId)
const allEntries: Entry[] = await api.getEntriesForContentType(this.sourceContentTypeId)
const entries: Entry[] = this.entryIds
? allEntries.filter((entry) => this.entryIds!.includes(entry.id))
: allEntries
const locales: string[] = await api.getLocalesForSpace()

for (const entry of entries) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/interfaces/entry-transform-to-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export default interface TransformEntryToType {
sourceContentType: string
targetContentType: string
from?: string[]
entryIds?: string[]
identityKey: (fromFields: any) => Promise<string>
shouldPublish?: boolean | 'preserve'
useLocaleBasedPublishing?: boolean
Expand Down
52 changes: 52 additions & 0 deletions test/unit/lib/actions/entry-transform-to-type.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,4 +652,56 @@ describe('Transform Entry to Type Action', function () {
await api.stopRecordingRequests()
expect(ids).to.eql(['246'])
})

it('transforms only specified entries when entryIds is provided', async function () {
const transformation: TransformEntryToType = {
sourceContentType: 'dog',
targetContentType: 'copycat',
from: ['name'],
entryIds: ['246'],
identityKey: async (fields) => fields.name['en-US'].toString(),
transformEntryForLocale: async (fields, locale) => {
return { name: 'x' + fields['name'][locale] }
}
}

const action = new EntryTransformToTypeAction(transformation)
const entries = [
new Entry(
makeApiEntry({
id: '246',
contentTypeId: 'dog',
version: 1,
fields: {
name: {
'en-US': 'bob'
}
}
})
),
new Entry(
makeApiEntry({
id: '247',
contentTypeId: 'dog',
version: 1,
fields: {
name: {
'en-US': 'alice'
}
}
})
)
]
const api = new OfflineApi({ contentTypes: new Map(), entries, locales: ['en-US'] })
await api.startRecordingRequests(null)

await action.applyTo(api)
await api.stopRecordingRequests()
const batches = await api.getRequestBatches()

expect(batches[0].requests.length).to.eq(1)
const targetData = batches[0].requests[0].data as APIEntry
expect(targetData.fields['name']['en-US']).to.eql('xbob')
expect(targetData.sys.id).to.eql('bob')
})
})