From c6b13812fe77bfd06011e98d625a542ca565ad64 Mon Sep 17 00:00:00 2001 From: mark H Date: Fri, 30 Aug 2024 16:35:49 +0800 Subject: [PATCH] up --- controllers/auth.js | 62 +++++++------ controllers/collection.js | 36 ++++++-- controllers/users.js | 94 +++++++++---------- models/Collection.js | 8 +- models/Endpoint.js | 12 +-- models/User.js | 186 +++++++++++++++++++------------------- routes/collection.js | 14 ++- routes/endpoint.js | 6 +- 8 files changed, 228 insertions(+), 190 deletions(-) diff --git a/controllers/auth.js b/controllers/auth.js index 15dd51c..15c1524 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -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"); diff --git a/controllers/collection.js b/controllers/collection.js index 44a8834..b19e0ae 100644 --- a/controllers/collection.js +++ b/controllers/collection.js @@ -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) { diff --git a/controllers/users.js b/controllers/users.js index 87356e8..0158723 100644 --- a/controllers/users.js +++ b/controllers/users.js @@ -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, diff --git a/models/Collection.js b/models/Collection.js index 968e988..e6d2518 100644 --- a/models/Collection.js +++ b/models/Collection.js @@ -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 } ); diff --git a/models/Endpoint.js b/models/Endpoint.js index 782b582..b51e227 100644 --- a/models/Endpoint.js +++ b/models/Endpoint.js @@ -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: [ { diff --git a/models/User.js b/models/User.js index 5619366..46d50e2 100644 --- a/models/User.js +++ b/models/User.js @@ -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, diff --git a/routes/collection.js b/routes/collection.js index bc9ea64..cfad26b 100644 --- a/routes/collection.js +++ b/routes/collection.js @@ -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 ); diff --git a/routes/endpoint.js b/routes/endpoint.js index 4e40a73..cc99a70 100644 --- a/routes/endpoint.js +++ b/routes/endpoint.js @@ -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 );