| Current Path : /proc/thread-self/root/home/deltalab/PMS/partner-manager-backend/services/ |
| Current File : //proc/thread-self/root/home/deltalab/PMS/partner-manager-backend/services/sfg.js |
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const { gql, GraphQLClient } = require('graphql-request');
function storefrontCreateWrapper(resolver) {
return resolver.wrapResolve((next) => async (rp) => {
// Create the mongo document
const payload = await next(rp);
const folder = process.env.STOREFRONT_ROOT_FOLDER;
const webRoot = process.env.STOREFRONT_WEB_ROOT;
const dir = folder + payload.record.name;
if (!fs.existsSync(dir)) { // if directory does not exist, create it and update mongo document
createStorefrontWebSpace(dir, payload.record.name, payload.record.buybutton);
const localClient = new GraphQLClient(process.env.LOCAL_SERV_URL);
const updateStorefrontQuery = gql`
mutation ($id: MongoID!, $storefront: UpdateByIdStorefrontInput!){
storefrontUpdateById(_id: $id, record: $storefront){
recordId
}
}`;
const args = {
id: payload.record._id.toString(),
storefront: {
location: dir,
url: `${webRoot}/${payload.record.name}`,
},
};
const response = await localClient.request(updateStorefrontQuery, args);
}
return payload;
});
}
function storefrontDeleteWrapper(resolver) {
return resolver.wrapResolve((next) => async (rp) => {
// Create the mongo document
const payload = await next(rp);
const folder = process.env.STOREFRONT_ROOT_FOLDER;
const webRoot = process.env.STOREFRONT_WEB_ROOT;
const dir = folder + payload.record.name;
if (fs.existsSync(dir)) {
await deleteDirectoryContent(dir);
}
return payload;
});
}
function storefrontUpdateWrapper(resolver) {
return resolver.wrapResolve((next) => async (rp) => {
const payload = await next(rp);
return payload;
});
}
/**
* Creates an example web space that can be overwritten with storefront generator button
*/
function createStorefrontWebSpace(directory, name, buybutton) {
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, true);
}
let html = `<html>
<head>
<title>
Welcome to {name} storefront
</title>
</head>
<body>
Welcome to {name} storefront
If you see this, it works!
<indaco-buybutton-code-to-replace />
</body>
</html>`;
html = html.replace('<indaco-buybutton-code-to-replace />', buybutton);
fs.writeFile(`${directory}/index.html`,
html,
(err) => {
if (err) throw err;
console.log('File is created successfully.');
});
}
/**
* Deletes all files in directory, then removes it
*/
async function deleteDirectoryContent(directory) {
const names = fs.readdir(directory, async (err, files) => {
if (err) throw err;
for (const file of files) {
await fs.unlink(path.join(directory, file), (err) => {
if (err) throw err;
});
}
await fs.rmdir(directory, (err) => {
if (err) throw err;
console.log('File is deleted successfully.');
});
});
}
// EXPORTS =========================================
module.exports = {
storefrontCreateWrapper,
storefrontUpdateWrapper,
storefrontDeleteWrapper,
createStorefrontWebSpace,
deleteDirectoryContent,
};