Skip to content

Commit

Permalink
Create Add record endpoint, improve plan creation
Browse files Browse the repository at this point in the history
  • Loading branch information
yash22arora committed May 28, 2024
1 parent 03a078e commit 3501319
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 107 deletions.
12 changes: 6 additions & 6 deletions server/src/api/models/record.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,10 +22,10 @@ interface IReservedRecord extends Document {

const recordSchema: Schema<IRecord> = 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 },
Expand Down
80 changes: 52 additions & 28 deletions server/src/api/routes/plan.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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,
});
}
}
);

Expand Down Expand Up @@ -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
Expand All @@ -100,6 +126,4 @@ router.post(
// }
// });

// module.exports = router;

export default router;
144 changes: 72 additions & 72 deletions server/src/api/routes/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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",
Expand Down
12 changes: 11 additions & 1 deletion server/src/api/routes/types.d.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3501319

Please sign in to comment.