From 3501319738034e69adc0c8d9d14198a4f5379fd7 Mon Sep 17 00:00:00 2001 From: Yashvardhan Arora Date: Tue, 28 May 2024 16:39:21 +0530 Subject: [PATCH] Create Add record endpoint, improve plan creation --- server/src/api/models/record.ts | 12 +-- server/src/api/routes/plan.ts | 80 +++++++++++------ server/src/api/routes/record.ts | 144 +++++++++++++++---------------- server/src/api/routes/types.d.ts | 12 ++- 4 files changed, 141 insertions(+), 107 deletions(-) diff --git a/server/src/api/models/record.ts b/server/src/api/models/record.ts index 9042fdc..3590209 100644 --- a/server/src/api/models/record.ts +++ b/server/src/api/models/record.ts @@ -1,10 +1,10 @@ import mongoose, { Document, Schema } from "mongoose"; interface IRecord extends Document { - cloudflareId: string; - cloudflareZoneId: string; + cloudflareID: string; + cloudflareZoneID: string; domainID: string; - planId: string; + planID: string; ownerID: string; name: string; content: string; @@ -22,10 +22,10 @@ interface IReservedRecord extends Document { const recordSchema: Schema = new mongoose.Schema({ _id: mongoose.Schema.Types.ObjectId, - cloudflareId: { type: String, required: true, default: "x" }, - cloudflareZoneId: { type: String, required: true, default: "x" }, + cloudflareID: { type: String, required: true, default: "x" }, + cloudflareZoneID: { type: String, required: true, default: "x" }, domainID: { type: String, required: true, ref: "Domain" }, - planId: { type: String, required: true, ref: "Plan" }, + planID: { type: String, required: true, ref: "Plan" }, ownerID: { type: String, required: true, ref: "User" }, name: { type: String, required: true, unique: true }, content: { type: String, required: true }, diff --git a/server/src/api/routes/plan.ts b/server/src/api/routes/plan.ts index 8b6a6d8..ba5cfad 100644 --- a/server/src/api/routes/plan.ts +++ b/server/src/api/routes/plan.ts @@ -1,8 +1,10 @@ -import { NextFunction, Response, Router } from "express"; +import { NextFunction, Request, Response, Router } from "express"; import checkAuth from "../middleware/check-auth"; import { CreatePlanRequest, PaymentVerifyRequest } from "./types"; import { Plan } from "../models/plan"; import crypto from "crypto"; +import { AuthenticatedRequest } from "../../utils/types"; +import mongoose from "mongoose"; const router = Router(); @@ -13,16 +15,31 @@ router.post( const ownerID = req.userData?.userID || "x"; const { domainID, planType, planLabel, razorpaySubscriptionID } = req.body; - const plan = new Plan({ - ownerID: ownerID, - domainID: domainID, - planType: planType, - planLabel: planLabel, - status: "processing", - hasTxtRecord: false, - expiry: new Date(), - razorpaySubscriptionID: razorpaySubscriptionID, - }); + try { + const plan = new Plan({ + _id: new mongoose.Types.ObjectId(), + ownerID: ownerID, + domainID: domainID, + planType: planType, + planLabel: planLabel, + status: "processing", + hasTxtRecord: false, + expiry: new Date(), + razorpaySubscriptionID: razorpaySubscriptionID, + }); + + plan.save().then((result) => { + res.status(201).json({ + message: "Plan Created", + data: result, + }); + }); + } catch (err) { + console.log(err); + res.status(500).json({ + message: err, + }); + } } ); @@ -66,21 +83,30 @@ router.post( } ); -// router.get("/", checkAuth, (req, res, next) => { -// const ownerID = req.userData.userID; -// Plan.find({ ownerID: ownerID }) -// .then((result) => { -// res.status(200).json({ -// data: result, -// }); -// }) -// .catch((err) => { -// console.log(err); -// res.status(500).json({ -// error: err, -// }); -// }); -// }); +router.get( + "/", + checkAuth, + (req: AuthenticatedRequest, res: Response, next: NextFunction) => { + const ownerID = req.userData?.userID; + try { + Plan.find({ ownerID: ownerID }).then((result) => { + res.status(200).json({ + data: result, + }); + }); + } catch (err) { + console.log(err); + res.status(500).json({ + message: err, + }); + } + } +); + +router.post("/webhook", (req: Request, res: Response) => { + console.log(req.body); + res.status(200).send("ok"); +}); // router.delete("/incomplete/:recordId", checkAuth, async (req, res, next) => { // // delete record from records and reservedRecord collection @@ -100,6 +126,4 @@ router.post( // } // }); -// module.exports = router; - export default router; diff --git a/server/src/api/routes/record.ts b/server/src/api/routes/record.ts index 90155a0..f3487d8 100644 --- a/server/src/api/routes/record.ts +++ b/server/src/api/routes/record.ts @@ -11,7 +11,10 @@ import { NextFunction, Response, Router } from "express"; import checkAuth from "../middleware/check-auth"; import { AuthenticatedRequest } from "../../utils/types"; -import { Record } from "../models/record"; +import { Record, ReservedRecord } from "../models/record"; +import { CreateRecordRequest } from "./types"; +import { Domain } from "../models/domain"; +import mongoose from "mongoose"; const router = Router(); router.get( @@ -35,81 +38,78 @@ router.get( } ); -// router.post("/", checkAuth, async (req, res, next) => { -// const validTypes = ["A", "CNAME", "TXT"]; -// const plans = ["personal", "vercel", "annual"]; -// const { name, content, type, plan, domainId } = req.body; -// console.log(name, content, type, plan, domainId); -// if ( -// !name || -// !content || -// !validTypes.includes(type) || -// !plans.includes(plan) || -// !domainId -// ) { -// return res.status(400).json({ -// error: "Bad Request", -// }); -// } else { -// try { -// let expiryDate = new Date(); -// // find domain with domainId in db -// const domain = await Domain.findOne({ _id: domainId }).then((res) => res); +router.post( + "/", + checkAuth, + async (req: CreateRecordRequest, res: Response, next: NextFunction) => { + const validTypes = ["A", "CNAME", "TXT"]; + const plans = ["personal", "vercel", "annual"]; + const { name, content, type, planID, domainID } = req.body; + if (!name || !content || !validTypes.includes(type) || !domainID) { + return res.status(400).json({ + error: "Bad Request", + }); + } else { + try { + let expiryDate = new Date(); + // find domain with domainId in db + const domain = await Domain.findOne({ _id: domainID }).then( + (res) => res + ); -// console.log(domain); + console.log(domain); -// if (!domain) { -// return res.status(404).json({ -// message: "Domain not found", -// }); -// } + if (!domain) { + return res.status(404).json({ + message: "Domain not found", + }); + } -// expiryDate.setDate(expiryDate.getDate() + 30); -// const record = new Record({ -// _id: new mongoose.Types.ObjectId(), -// ownerID: req.userData.userID, -// name: name, -// content: content, -// type: type, -// plan: plan, -// domainID: domainId, -// expiry: expiryDate, // calculate expiry date from now -// }); + const record = new Record({ + _id: new mongoose.Types.ObjectId(), + ownerID: req.userData?.userID, + name: name, + content: content, + type: type, + planID: planID, + domainID: domainID, + }); -// record -// .save() -// .then((result) => { -// console.log(result); -// const reservedRecord = new ReservedRecord({ -// _id: new mongoose.Types.ObjectId(), -// recordID: result._id, -// ownerID: req.userData.userID, -// name: name + "." + domain.domainName, -// }); -// reservedRecord -// .save() -// .then((x) => { -// return res.status(201).json({ -// message: "Subdomain Reserved!", -// recordId: result._id, -// }); -// }) -// .catch((err) => { -// throw err; -// }); -// }) -// .catch((err) => { -// throw err; -// }); -// } catch (err) { -// console.log(err); -// return res.status(500).json({ -// message: "Internal Server Error", -// error: err, -// }); -// } -// } -// }); + record + .save() + .then((result) => { + console.log(result); + const reservedRecord = new ReservedRecord({ + _id: new mongoose.Types.ObjectId(), + recordID: result._id, + ownerID: req.userData?.userID, + name: name + "." + domain.domainName, + }); + reservedRecord + .save() + .then((x) => { + return res.status(201).json({ + message: "Subdomain Reserved!", + recordId: result._id, + }); + }) + .catch((err) => { + throw err; + }); + }) + .catch((err) => { + throw err; + }); + } catch (err) { + console.log(err); + return res.status(500).json({ + message: "Internal Server Error", + error: err, + }); + } + } + } +); // router.post( // "/webhook", diff --git a/server/src/api/routes/types.d.ts b/server/src/api/routes/types.d.ts index f3315ab..3410baa 100644 --- a/server/src/api/routes/types.d.ts +++ b/server/src/api/routes/types.d.ts @@ -1,5 +1,5 @@ import { Request } from "express"; -import { AuthenticatedRequest } from "../../utils/types"; +import { AuthenticatedRequest, TRecordType } from "../../utils/types"; import { IUser } from "../models/user"; import { IPlan } from "../models/plan"; @@ -33,6 +33,16 @@ export interface CreatePlanRequest extends AuthenticatedRequest { body: IPlan; } +export interface CreateRecordRequest extends AuthenticatedRequest { + body: { + name: string; + content: string; + type: TRecordType; + planID: string; + domainID: string; + }; +} + export interface PaymentVerifyRequest extends AuthenticatedRequest { body: { razorpay_payment_id: string;