create website

main
alex 2024-01-23 22:14:29 +01:00
parent 32b3797ed9
commit cc2bf4cf1e
4 changed files with 65 additions and 10 deletions

View File

@ -6,7 +6,7 @@
"scripts": { "scripts": {
"build": "npx tsc", "build": "npx tsc",
"start": "node build/server.js", "start": "node build/server.js",
"dev": "concurrently \"npx tsc --watch\" \"nodemon --ignore ./tmp/ --ignore ./websites/ -q build/server.js | pino-pretty\"" "dev": "concurrently \"npx tsc --watch\" \"nodemon --ignore ./tmp/ --ignore ./customer-websites/ -q build/server.js | pino-pretty\""
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",

View File

@ -4,6 +4,7 @@ import util from "util";
import { exec } from "child_process"; import { exec } from "child_process";
import fs from "fs-extra"; import fs from "fs-extra";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import Website from "../models/website";
const execPromise = util.promisify(exec); const execPromise = util.promisify(exec);
@ -50,12 +51,43 @@ export async function UpdateTemplateWebhook(req: Request, res: Response) {
export async function CreateWebsite(req: Request, res: Response) { export async function CreateWebsite(req: Request, res: Response) {
try { try {
let tmpId = uuidv4(); let { storeId } = req.body;
// validate request
if (!storeId) {
res.status(400).send({ error: "invalid request" });
return;
}
let storeName = uuidv4();
storeName = storeName.toLowerCase().replace(/ /g, "-");
// check if store exists in database
const websiteExists = await Website.findOne({
where: {
store_id: storeId,
},
});
if (websiteExists) {
res.status(409).send({ error: "website already exists" });
return;
}
// create database record
await Website.create({
website_id: storeName,
store_id: storeId,
});
const tmpDirWebsiteTemplate = process.env const tmpDirWebsiteTemplate = process.env
.TMP_DIR_WEBSITE_TEMPLATE as string; .TMP_DIR_WEBSITE_TEMPLATE as string;
const tmpDirCustomerWebsite = `${process.env.TMP_DIR}/${tmpId}`; const tmpDirCustomerWebsite = `${process.env.TMP_DIR}/${storeName}`;
const customerWebsiteDir = `${process.env.CUSTOMER_WEBSITES_DIR}/${tmpId}`; const customerWebsiteDir = `${process.env.CUSTOMER_WEBSITES_DIR}/${storeName}`;
// check if website template folder exists // check if website template folder exists
@ -68,6 +100,8 @@ export async function CreateWebsite(req: Request, res: Response) {
// copy website-template folder to customer website folder // copy website-template folder to customer website folder
await fs.copy(tmpDirWebsiteTemplate, tmpDirCustomerWebsite); await fs.copy(tmpDirWebsiteTemplate, tmpDirCustomerWebsite);
// TODO: add config file
// run npm build // run npm build
let { stdout: buildOutput } = await execPromise(`npm run build`, { let { stdout: buildOutput } = await execPromise(`npm run build`, {
@ -80,9 +114,35 @@ export async function CreateWebsite(req: Request, res: Response) {
await fs.copy(`${tmpDirCustomerWebsite}/build`, customerWebsiteDir); await fs.copy(`${tmpDirCustomerWebsite}/build`, customerWebsiteDir);
// remove tmp folder
await fs.remove(tmpDirCustomerWebsite);
res.status(200).send({ message: "oks" }); res.status(200).send({ message: "oks" });
} catch (error) { } catch (error) {
console.log("error", error); console.log("error", error);
res.status(500).send({ error: "invalid request" }); res.status(500).send({ error: "invalid request" });
} }
} }
export async function GetWebsite(req: Request, res: Response) {
try {
const { storeId } = req.params;
const website = await Website.findOne({
where: {
store_id: storeId,
},
});
if (!website) {
res.status(404).send({ error: "website not found" });
return;
}
res.status(200).json({ message: "ok" });
} catch (error) {
console.log("error", error);
res.status(500).send({ error: "invalid request" });
}
}

View File

@ -4,13 +4,11 @@ import sequelize from "../database/database";
interface WebsiteAttributes { interface WebsiteAttributes {
website_id: string; website_id: string;
store_id: string; store_id: string;
url: string;
} }
class Website extends Model<WebsiteAttributes> implements WebsiteAttributes { class Website extends Model<WebsiteAttributes> implements WebsiteAttributes {
declare website_id: string; declare website_id: string;
declare store_id: string; declare store_id: string;
declare url: string;
} }
Website.init( Website.init(
@ -23,10 +21,6 @@ Website.init(
type: DataTypes.STRING, type: DataTypes.STRING,
allowNull: false, allowNull: false,
}, },
url: {
type: DataTypes.STRING,
allowNull: false,
},
}, },
{ {
sequelize, sequelize,

View File

@ -8,5 +8,6 @@ router.post(
websiteController.UpdateTemplateWebhook websiteController.UpdateTemplateWebhook
); );
router.post("/", websiteController.CreateWebsite); router.post("/", websiteController.CreateWebsite);
router.get("/:storeId", websiteController.GetWebsite);
export default router; export default router;