Skip to content

Commit

Permalink
fix(api): #2163
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin.apolinario committed Dec 7, 2023
1 parent f363b1b commit 7c54552
Showing 1 changed file with 63 additions and 8 deletions.
71 changes: 63 additions & 8 deletions src/pages/api/hello.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,77 @@
// pages/api/fetchData.js

import axios from 'axios';
import MarkdownIt from 'markdown-it';

const INSTANCE_ZUID = `8-aaeffee09b-7w6v22`;
const MODEL_ZUID = `6-e092db91a5-rgfcx2`;
const ENV = 'dev';

// edit the content data
async function makePutRequestWithToken(token, putData, apiUrl) {
try {
const response = await axios.put(apiUrl, putData, {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
});

return response.data;
} catch (error) {
return error;
}
}

// Publish all item
async function PublishAllItem(auth) {
const url = `https://us-central1-zesty-${ENV}.cloudfunctions.net/publishAllModelContent?instanceZUID=${INSTANCE_ZUID}&modelZUID=${MODEL_ZUID}`;
return await axios.get(url, { headers: { Authorization: auth } });
}

// main function
export default async function handler(req, res) {
const token = req.query['token'];
const url =
'https://8-aaeffee09b-7w6v22.api.zesty.io/v1/content/models/6-e092db91a5-rgfcx2/items?limit=5000&page=1&lang=en-US';

const url = `https://${INSTANCE_ZUID}.api.${ENV}.zesty.io/v1/content/models/${MODEL_ZUID}/items?page=3&lang=en-US`;
const auth = 'Bearer ' + token;

// get all the data
const response = await axios.get(url, { headers: { Authorization: auth } });

const md = new MarkdownIt();

// convert the body from md to html
const data = response.data.data.map((e) => {
const content = md.render(e.data.body).replaceAll('\n', '');
return { item: e.meta.ZUID, content };
const content = md.render(e?.data?.body || '').replaceAll('\n', '');
return {
item: e.meta.ZUID,
content,
web: e.web,
meta: e.meta,
data: e.data,
};
});

// update the `content` of the item
const promises = data.map(async (e) => {
const putData = {
web: e.web,
meta: e.meta,
data: { ...e['data'], content: e.content },
};

const url1 = `https://${INSTANCE_ZUID}.api.${ENV}.zesty.io/v1/content/models/${MODEL_ZUID}/items/${e.item}`;

await makePutRequestWithToken(token, putData, url1);
});

await Promise.all(promises);

// get all the updated data
await axios.get(url, { headers: { Authorization: auth } });

// publish all items
const finalRes = PublishAllItem(auth);
res.status(200).json({
data: finalRes,
msg: `Successfully updated and publish the data in Instance: ${INSTANCE_ZUID} Model: ${MODEL_ZUID}`,
});
res.status(200).json({ data });
}

0 comments on commit 7c54552

Please sign in to comment.