Skip to content

Instantly share code, notes, and snippets.

@hwclass
Created May 17, 2019 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hwclass/b3ffd1702521a819363dd8ddd0977546 to your computer and use it in GitHub Desktop.
Save hwclass/b3ffd1702521a819363dd8ddd0977546 to your computer and use it in GitHub Desktop.
/* DEBUG=update-city-pages-entries node migration/update-city-pages-entries.ts > out.json 2>&1 */
import * as dotenv from 'dotenv'
const debug = require('debug')('update-city-pages-entries')
const contentful = require('contentful-management')
// const { forcedLocale } = require('../config/export-path-map.js')
// const { benefitsSectionId, contentModels } = require('../config/contenful.js')
// const forcedLocale = 'en-US'
dotenv.config()
interface Reason {
request: {
url: string
}
message: string
statusText: string
status: string
stack: any
}
const client = contentful.createClient({
accessToken: process.env.CONTENTFUL_CONTENT_UPDATE_ACCESS_TOKEN || ''
})
let published = 0
let updated = 0
let id = ''
client.getSpace(process.env.CONTENTFUL_SPACE_ID)
.then(Environment => Environment.getEntries({
content_type: '2od8TlD24wIUuaS02qE6q4'
}))
.then(({ items }) => {
debug('# ITEMS FOUND', items.length)
// loop over entries
items.forEach((entry) => {
debug('# BEFORE UPDATE', entry.fields.sections && entry.fields.sections)
entry.fields.sections = { forcedLocale: [] }
entry.fields.sections['en-US'] = [
{ sys:
{ type: 'Link',
linkType: 'Entry',
id: '59ofMxeqV0NDrZaO9boX2E' //benefits id
}
}
]
debug('# AFTER UPDATE', entry.fields.sections && entry.fields.sections['en-US'])
entry.update()
.then(updatedEntry => {
debug('# UPDATE OK', ++updated)
updatedEntry.publish()
.then(() => {
debug('# PUBLISH OK', ++published)
})
.catch(reason => {
debug('# PUBLISH KO', updatedEntry.fields.urlPath['en-US'], reason)
})
})
.catch((reason: Reason) => {
let url = ''
if (reason && reason.request) {
url = reason.request.url
}
debug('# UPDATE KO', entry.fields.urlPath['en-US'], reason.status, reason.statusText, reason.message, reason.request && url)
})
})
})
.catch((reason: Reason) => {
let url = ''
if (reason && reason.request) {
url = reason.request.url
}
debug('# EXCEPTION', reason.status, reason.statusText, reason.message, reason.stack, url, ' # LAST ID ', id)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment