Skip to content

Commit

Permalink
Add soft delete to Plan, write record services
Browse files Browse the repository at this point in the history
  • Loading branch information
yash22arora committed Jun 21, 2024
1 parent fad592d commit 152c991
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
4 changes: 4 additions & 0 deletions server/src/api/models/plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ interface IPlan extends Document {
status: TPlanStatus;
expiry: Date;
razorpaySubscriptionID: string;
isDeleted: boolean;
created_at: Date;
updated_at: Date;
}

const planSchema: Schema<IPlan> = new mongoose.Schema({
Expand All @@ -23,7 +25,9 @@ const planSchema: Schema<IPlan> = new mongoose.Schema({
hasTxtRecord: { type: Boolean, required: true, default: false },
expiry: { type: Date, required: true },
razorpaySubscriptionID: { type: String, required: true, default: "x" },
isDeleted: { type: Boolean, required: true, default: false },
created_at: { type: Date, default: Date.now },
updated_at: { type: Date, default: Date.now },
});

const Plan = mongoose.model<IPlan>("Plan", planSchema);
Expand Down
1 change: 0 additions & 1 deletion server/src/api/routes/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ 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({
Expand Down
67 changes: 56 additions & 11 deletions server/src/api/services/record.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
// create a record

import mongoose from "mongoose";
import { Record } from "../models/record";
import { Plan } from "../models/plan";
import { CreateRecordRequest } from "../routes/types";
import { TPlanName } from "../../utils/types";

export const recordService = {
createRecord: async () => {
createRecord: async (
record: CreateRecordRequest["body"],
ownerID: string
) => {
// create a record
try {
const newRecord = new Record({
_id: new mongoose.Types.ObjectId(),
ownerID: ownerID,
...record,
});
const result = await newRecord.save();
return result;
} catch (err) {
console.log(err);
return err;
}
},
getRecord: async () => {
// get a record
Expand All @@ -13,19 +34,43 @@ export const recordService = {
updateRecord: async () => {
// update a record
},
getRecordsByUser: async () => {
getRecordsByUser: async (ownerID: string) => {
// get all records by user
try {
const records = await Record.find({ ownerID });
return records;
} catch (err) {
console.log(err);
return err;
}
},
getRecordsByPlan: async () => {
getRecordsByPlan: async (planID: string) => {
// get all records by plan
try {
const records = await Record.find({ planID });
return records;
} catch (err) {
console.log(err);
return err;
}
},
getReservedRecord: async () => {
// get a reserved record
},
deleteReservedRecord: async () => {
// delete a reserved record
},
getReservedRecords: async () => {
// get all reserved records
getRecordsByUserandPlanType: async (planType: TPlanName, ownerID: string) => {
try {
// find all plans by user and plan type
// for each plan, find all records

const plans = await Plan.find({ ownerID, planType });

const records = await Promise.all(
plans.map(async (plan) => {
return await Record.find({ planID: plan._id });
})
);

return records;
} catch (err) {
console.log(err);
return err;
}
},
};

0 comments on commit 152c991

Please sign in to comment.