-
-
Save bastien/f3a87baf18fcb53dcbebcd1804779288 to your computer and use it in GitHub Desktop.
Test script that creates a help-center article with an attachment and updates its content
This file contains hidden or 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
EMAIL={admin-email} | |
PASSWORD={admin-password} | |
SUBDOMAIN={your-subdomain} | |
SECTIONID={some-id} | |
PERMISSIONGROUPID={permssion-group-id} | |
FILENAME={relative-path-to-file-to-upload} | |
LOCALE={locale} | |
ZENDESKDOMAIN=zendesk.com |
This file contains hidden or 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
#!/usr/bin/node | |
// Dependencies: | |
// node v20.11.1 | |
// npm install --save form-data | |
// | |
// .env file format: | |
// | |
// EMAIL={admin-email} | |
// PASSWORD={admin-password} | |
// SUBDOMAIN={your-subdomain} | |
// SECTIONID={some-id} | |
// PERMISSIONGROUPID={permssion-group-id} | |
// FILENAME={relative-path-to-file-to-upload} | |
// LOCALE={locale} | |
// ZENDESKDOMAIN=zendesk.com | |
// | |
// node --env-file=.env article_attachment_update.js | |
const https = require('https') | |
const formData = require('form-data'); | |
const fs = require('fs') | |
const email = process.env.EMAIL | |
const password = process.env.PASSWORD | |
const subdomain = process.env.SUBDOMAIN | |
const zendeskDomain = process.env.ZENDESKDOMAIN | |
const sectionId = process.env.SECTIONID | |
const permissionGroupId = process.env.PERMISSIONGROUPID | |
const filename = process.env.FILENAME | |
const locale = process.env.LOCALE | |
const headers = { | |
'Authorization': 'Basic ' + Buffer.from(email + ':' + password).toString('base64'), | |
'content-type': 'application/json' | |
} | |
function createAttachment(attachmentLocale, articleId=null) { | |
return new Promise((resolve, reject) => { | |
let form = new formData() | |
const file = fs.readFileSync(__dirname + '/' + filename) | |
form.append('file', file, filename) | |
form.append('inline', 'true') | |
form.append('locale', attachmentLocale) | |
const attachmentHeaders = { ...headers, ...form.getHeaders()} | |
let path | |
if (!!articleId) { | |
console.log("Article attachment for article") | |
console.log(articleId) | |
path = '/api/v2/help_center/articles/'+articleId+'/attachments.json' | |
} else { | |
path = '/api/v2/help_center/articles/attachments.json' | |
} | |
const attachmentCreateOptions = { | |
hostname: subdomain + '.' + zendeskDomain, | |
method: 'POST', | |
path: path, | |
headers: attachmentHeaders | |
} | |
const attachmentCreateRequest = https.request(attachmentCreateOptions, (res) => { | |
let data = [] | |
console.log('statusCode:', res.statusCode) | |
res.on('data', (chunk) => { | |
data.push(chunk); | |
}); | |
res.on('end', () => { | |
const response = JSON.parse(Buffer.concat(data).toString()) | |
resolve(response) | |
}) | |
res.on('error', (error) => { | |
reject(error); | |
}) | |
}) | |
form.pipe(attachmentCreateRequest) | |
attachmentCreateRequest.end() | |
}) | |
} | |
function createArticle(attachmentId) { | |
return new Promise((resolve, reject) => { | |
const articleCreateOptions = { | |
hostname: subdomain + '.' + zendeskDomain, | |
method: 'POST', | |
path: '/api/v2/help_center/sections/' + sectionId + '/articles', | |
headers: headers | |
} | |
const articleCreateRequest = https.request(articleCreateOptions, (res) => { | |
let data = [] | |
console.log('statusCode:', res.statusCode) | |
res.on('data', (chunk) => { | |
data.push(chunk); | |
}); | |
res.on('end', () => { | |
const response = JSON.parse(Buffer.concat(data).toString()) | |
resolve(response) | |
}) | |
res.on('error', (error) => { | |
reject(error); | |
}) | |
}) | |
const body = '<p><img src="/hc/article_attachments/'+attachmentId+'"></p>' | |
const data = { | |
article: { | |
body: body, | |
title: 'Attachment Update Test', | |
locale: locale, | |
user_segment_id: null, | |
permission_group_id: permissionGroupId | |
} | |
} | |
articleCreateRequest.write(JSON.stringify(data)) | |
articleCreateRequest.end() | |
}) | |
} | |
function deleteAttachment(id) { | |
return new Promise((resolve, reject) => { | |
const attachmentDeleteOptions = { | |
hostname: subdomain + '.' + zendeskDomain, | |
method: 'DELETE', | |
path: '/api/v2/help_center/articles/attachments/' + id, | |
headers: headers | |
} | |
const attachmentDeleteRequest = https.request(attachmentDeleteOptions, (res) => { | |
let data = [] | |
console.log('statusCode:', res.statusCode) | |
res.on('data', (chunk) => { | |
data.push(chunk); | |
}); | |
res.on('end', () => { | |
const response = Buffer.concat(data).toString() | |
resolve(response) | |
}) | |
res.on('error', (error) => { | |
reject(error); | |
}) | |
}) | |
attachmentDeleteRequest.end() | |
}) | |
} | |
function listAttachments(articleId) { | |
return new Promise((resolve, reject) => { | |
const attachmentListOptions = { | |
hostname: subdomain + '.' + zendeskDomain, | |
method: 'GET', | |
path: '/api/v2/help_center/articles/' + articleId + '/attachments', | |
headers: headers | |
} | |
const attachmentListRequest = https.request(attachmentListOptions, (res) => { | |
let data = [] | |
console.log('statusCode:', res.statusCode) | |
res.on('data', (chunk) => { | |
data.push(chunk); | |
}); | |
res.on('end', () => { | |
const response = JSON.parse(Buffer.concat(data).toString()) | |
resolve(response) | |
}) | |
res.on('error', (error) => { | |
reject(error); | |
}) | |
}) | |
attachmentListRequest.end() | |
}) | |
} | |
function updateTranslation(article) { | |
return new Promise((resolve, reject) => { | |
const translationUpdateOptions = { | |
hostname: subdomain + '.' + zendeskDomain, | |
method: 'PUT', | |
path: '/api/v2/help_center/articles/' + article.id + '/translations/' + locale, | |
headers: headers | |
} | |
const translationUpdateRequest = https.request(translationUpdateOptions, (res) => { | |
let data = [] | |
console.log('statusCode:', res.statusCode) | |
res.on('data', (chunk) => { | |
data.push(chunk); | |
}); | |
res.on('end', () => { | |
const response = JSON.parse(Buffer.concat(data).toString()) | |
resolve(response) | |
}) | |
res.on('error', (error) => { | |
reject(error); | |
}) | |
}) | |
const body = article.body + '<p>UPDATED</p>' | |
const data = { | |
translation: { | |
body: body | |
} | |
} | |
translationUpdateRequest.write(JSON.stringify(data)) | |
translationUpdateRequest.end() | |
}) | |
} | |
async function testAttachmentUpdate () { | |
let attachment = await createAttachment(locale) | |
let article = await createArticle(attachment.article_attachment.id) | |
let esAttachment = await createAttachment('es', article.article.id) | |
let attachments = await listAttachments(article.article.id) | |
let attachmentIds = attachments.article_attachments.map((attachment) => attachment.id) | |
console.log(attachment) | |
console.log(esAttachment) | |
console.log(article) | |
console.log(attachments) | |
/* for the sake of the test we delete the attachment, then update the | |
* translation still with the same attachment reference inside | |
* and check that the article again is associated to a new attachment | |
*/ | |
await deleteAttachment(attachment.article_attachment.id) | |
let translation = await updateTranslation(article.article) | |
attachments = await listAttachments(article.article.id) | |
let postDeleteAttachmentIds = attachments.article_attachments.map((attachment) => attachment.id) | |
console.log(translation) | |
console.log(attachments) | |
let result = postDeleteAttachmentIds.length > 0 && | |
postDeleteAttachmentIds.length == attachmentIds.length | |
console.log(postDeleteAttachmentIds) | |
console.log(attachmentIds) | |
console.log('All attachment present? - ' + result) | |
} | |
testAttachmentUpdate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment