This commit is contained in:
mark H 2024-08-30 16:35:49 +08:00
parent 2bc5041f08
commit c6b13812fe
8 changed files with 228 additions and 190 deletions

View File

@ -12,31 +12,33 @@ const parser = new DatauriParser();
export const register = async (req, res) => {
try {
const {
fName,
lName,
// fName,
// lName,
email,
password,
phone,
address1,
address2,
city,
province,
country,
zip,
username,
type,
status,
transactions,
// phone,
// address1,
// address2,
// city,
// province,
// country,
// zip,
// username,
// type,
// status,
// transactions,
} = req.body;
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
// const result = await User.dropIndex('username_1');
// const result = await User.collection.dropIndex('username_1');
const salt = await bcrypt.genSalt();
const passwordHash = await bcrypt.hash(password, salt);
const emailTaken = await User.findOne({ email: email });
const usernameTaken = await User.findOne({ username: username });
// const usernameTaken = await User.findOne({ username: username });
// const phoneTaken = await User.findOne({ phone: phone });
const pic = null;
@ -49,24 +51,24 @@ export const register = async (req, res) => {
});
}
if (!emailTaken && !usernameTaken) {
if (!emailTaken) {
const newUser = new User({
fName,
lName,
// fName,
// lName,
email,
password: passwordHash,
phone,
address1,
address2,
city,
province,
country,
zip,
username,
type,
status,
transactions,
photo: pic ? pic : "",
// phone,
// address1,
// address2,
// city,
// province,
// country,
// zip,
// username,
// type,
// status,
// transactions,
// photo: pic ? pic : "",
});
const usersaved = await newUser.save();
const user = await User.findOne({ email: email });
@ -113,7 +115,7 @@ export const login = async (req, res) => {
const user = await User.findOne().or([
{ email: users },
{ phone: users },
{ username: users },
// { username: users },
]);
if (!user) {
console.log("does not exist");

View File

@ -13,7 +13,7 @@ export const createCollection = async (req, res, next) => {
console.log(savedCollection);
} catch (err) {
// console.log(req.body);
res.status(400).json({ message: err.message });
next(err);
@ -38,6 +38,27 @@ export const updateCollection = async (req, res, next) => {
}
};
export const updateCollectionByEndpoint = async (req, res, next) => {
try {
const { id, endpoint_id } = req.params;
const updatedCollection = await Collection.findByIdAndUpdate(
req.params.id,
{ $push: { endpoints: req.body.endpoint_id } },
{ new: true }
);
if (updatedCollection) {
console.log("Updated Collection:", endpoint_id);
res.status(200).json(updatedCollection);
} else {
res.status(404).json({ endpoint: "Collection or endpoint not found." });
}
} catch (err) {
next(err);
}
};
export const deleteCollection = async (req, res, next) => {
try {
const getCollection = await Collection.findByIdAndDelete(req.params.id);
@ -54,7 +75,12 @@ export const deleteCollection = async (req, res, next) => {
};
export const getCollections = async (req, res, next) => {
try {
const getCollection = await Collection.find();
const getCollection = await Collection.find()
.populate({
path: "endpoints",
model: "Endpoint",
})
.exec();
// const Collection = getCollection.reverse();
if (getCollection) {
res.status(200).json(getCollection);
@ -71,11 +97,9 @@ export const getCollection = async (req, res, next) => {
try {
const foundCollection = await Collection.findById(req.params.id)
.populate({
path: "userRef",
model: "User",
// select: "fName lName username photo",
path: "endpoints",
model: "Endpoint",
})
.exec();
if (foundCollection) {

View File

@ -66,19 +66,19 @@ export const getUserId = async (req, res) => {
export const updateUser = async (req, res, next) => {
try {
const {
fName,
lName,
// fName,
// lName,
email,
phone,
address1,
address2,
city,
province,
country,
zip,
type,
status,
transactions,
// phone,
// address1,
// address2,
// city,
// province,
// country,
// zip,
// type,
// status,
// transactions,
} = req.body;
const emailTaken = await User.findOne({ email: email });
@ -101,20 +101,20 @@ export const updateUser = async (req, res, next) => {
});
if (!emailTaken && !taken) {
const newUser = {
fName,
lName,
// fName,
// lName,
email,
phone,
address1,
address2,
city,
province,
country,
zip,
type,
status,
transactions,
photo: result.secure_url,
// phone,
// address1,
// address2,
// city,
// province,
// country,
// zip,
// type,
// status,
// transactions,
// photo: result.secure_url,
};
const updatedUser = await User.findByIdAndUpdate(
req.params.id,
@ -130,18 +130,18 @@ export const updateUser = async (req, res, next) => {
} else if (!req.file) {
if (!emailTaken && !taken) {
const newUser = {
fName,
lName,
// fName,
// lName,
email,
phone,
address1,
address2,
city,
province,
country,
zip,
transactions,
status,
// phone,
// address1,
// address2,
// city,
// province,
// country,
// zip,
// transactions,
// status,
};
const updatedUser = await User.findByIdAndUpdate(
req.params.id,
@ -153,18 +153,18 @@ export const updateUser = async (req, res, next) => {
res.status(500).json({ error: "email taken" });
} else if (taken) {
const newUser = {
fName,
lName,
// fName,
// lName,
email,
phone,
address1,
address2,
city,
province,
country,
zip,
transactions,
status,
// phone,
// address1,
// address2,
// city,
// province,
// country,
// zip,
// transactions,
// status,
};
const updatedUser = await User.findByIdAndUpdate(
req.params.id,

View File

@ -4,7 +4,7 @@ const CollectionSchema = mongoose.Schema(
{
name: {
type: String,
// required: true,
// required: true,
},
created_by: {
type: mongoose.Schema.Types.ObjectId,
@ -21,6 +21,12 @@ const CollectionSchema = mongoose.Schema(
//required: true,
},
],
endpoints: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Endpoint",
},
],
},
{ timestamps: true }
);

View File

@ -7,7 +7,7 @@ const EndpointSchema = mongoose.Schema(
// required: true,
},
name: {
type: String,
type: String,
// required: true,
},
method: {
@ -18,12 +18,12 @@ const EndpointSchema = mongoose.Schema(
type: mongoose.Schema.Types.ObjectId,
ref: "Collection",
},
created_by: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
// created_by: {
// type: mongoose.Schema.Types.ObjectId,
// ref: "User",
// },
body_json: {
type: String,
type: Array,
},
form_data: [
{

View File

@ -2,18 +2,18 @@ import mongoose from "mongoose";
const UserSchema = new mongoose.Schema(
{
fName: {
type: String,
// required: true,
min: 2,
max: 50,
},
lName: {
type: String,
// required: true,
min: 2,
max: 50,
},
// fName: {
// type: String,
// // required: true,
// min: 2,
// max: 50,
// },
// lName: {
// type: String,
// // required: true,
// min: 2,
// max: 50,
// },
email: {
type: String,
required: true,
@ -24,93 +24,93 @@ const UserSchema = new mongoose.Schema(
type: String,
// required: true,
max: 30,
unique: true,
unique: false,
},
password: {
type: String,
required: true,
min: 5,
},
phone: {
type: String,
// required: true,
default: "",
},
photo: {
type: String,
},
address1: {
type: String,
default: "",
},
address2: {
type: String,
default: "",
},
city: {
type: String,
default: "",
},
province: {
type: String,
default: "",
},
country: {
type: String,
default: "",
},
zip: {
type: String,
default: "",
},
type: {
type: String,
default: "client",
},
status: {
type: String,
default: "new",
required: true,
},
phoneStatus: {
type: String,
default: "new",
// required: true,
},
kycStatus: {
type: String,
default: "new",
// required: true,
},
kycRef: {
type: mongoose.Schema.Types.ObjectId,
ref: "Kyc",
},
friends: [
{
number: {
type: String,
required: false,
},
favorite: {
type: Boolean,
required: false,
default: false,
},
nickname: {
type: String,
required: false,
},
},
],
otp: {
type: String,
default: null,
},
otpExpiration: {
type: Date,
default: null,
},
// phone: {
// type: String,
// // required: true,
// default: "",
// },
// photo: {
// type: String,
// },
// address1: {
// type: String,
// default: "",
// },
// address2: {
// type: String,
// default: "",
// },
// city: {
// type: String,
// default: "",
// },
// province: {
// type: String,
// default: "",
// },
// country: {
// type: String,
// default: "",
// },
// zip: {
// type: String,
// default: "",
// },
// type: {
// type: String,
// default: "client",
// },
// status: {
// type: String,
// default: "new",
// required: true,
// },
// phoneStatus: {
// type: String,
// default: "new",
// // required: true,
// },
// kycStatus: {
// type: String,
// default: "new",
// // required: true,
// },
// kycRef: {
// type: mongoose.Schema.Types.ObjectId,
// ref: "Kyc",
// },
// friends: [
// {
// number: {
// type: String,
// required: false,
// },
// favorite: {
// type: Boolean,
// required: false,
// default: false,
// },
// nickname: {
// type: String,
// required: false,
// },
// },
// ],
// otp: {
// type: String,
// default: null,
// },
// otpExpiration: {
// type: Date,
// default: null,
// },
// transactions: {
// type: Array,

View File

@ -5,6 +5,7 @@ import {
deleteCollection,
createCollection,
updateCollection,
updateCollectionByEndpoint,
} from "../controllers/collection.js";
import { verifyToken } from "../middleware/auth.js";
import logApiCall from "../middleware/log.js";
@ -26,20 +27,25 @@ router.get(
router.post(
"/create",
// verifyToken,
logApiCall,
// logApiCall,
createCollection
);
router.patch(
"/update/:id",
// verifyToken,
logApiCall,
// logApiCall,
updateCollection
);
router.patch(
"/update/:id/endpoint",
// verifyToken,
// logApiCall,
updateCollectionByEndpoint
);
router.delete(
"/:id/delete",
// verifyToken,
logApiCall,
// logApiCall,
deleteCollection
);

View File

@ -28,20 +28,20 @@ router.get(
router.post(
"/create",
// verifyToken,
logApiCall,
// logApiCall,
createEndpoint
);
router.patch(
"/update/:id",
// verifyToken,
logApiCall,
// logApiCall,
updateEndpoint
);
router.delete(
"/:id/delete",
// verifyToken,
logApiCall,
// logApiCall,
deleteEndpoint
);